CUDA查询和选取设备信息

CUDA查询设备信息


CUDA C中的cudaGetDeviceProperties函数可以很方便的获取到设备的信息,函数原型是:

cudaError_t CUDARTAPI cudaGetDeviceProperties(struct cudaDeviceProp *prop, int device);

第二个参数device是从0开始的设备的编号。

第一个参数prop指向的是一个cudaDeviceProp类型的结构。cudaDeviceProp结构中包含了设备的相关属性,下图是 其中的几个属性信息:



部分属性信息的相关说明如下:


(注:1MB=1024KB=1024*1024字节)


#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>

using namespace std;

int main()
{
	cudaDeviceProp deviceProp;
	int deviceCount;
	cudaError_t cudaError;
	cudaError = cudaGetDeviceCount(&deviceCount);
	for (int i = 0; i < deviceCount; i++)
	{
		cudaError = cudaGetDeviceProperties(&deviceProp, i);

		cout << "设备 " << i + 1 << " 的主要属性: " << endl;
		cout << "设备显卡型号: " << deviceProp.name << endl;
		cout << "设备全局内存总量(以MB为单位): " << deviceProp.totalGlobalMem / 1024 / 1024 << endl;
		cout << "设备上一个线程块(Block)中可用的最大共享内存(以KB为单位): " << deviceProp.sharedMemPerBlock / 1024 << endl;
		cout << "设备上一个线程块(Block)种可用的32位寄存器数量: " << deviceProp.regsPerBlock << endl;
		cout << "设备上一个线程块(Block)可包含的最大线程数量: " << deviceProp.maxThreadsPerBlock << endl;
		cout << "设备的计算功能集(Compute Capability)的版本号: " << deviceProp.major << "." << deviceProp.minor << endl;
		cout << "设备上多处理器的数量: " << deviceProp.multiProcessorCount << endl;
	}
	getchar();
	return 0;
}

运行输出的部分设备属性信息:




CUDA选取设备信息


系统中可能包含多个GPU,这种情况下,最好依据对GPU某些特定属性的需求,让程序自动选择执行速度最快的GUP。例如对于核函数与CUP之间需要进行密集交互的情况,要尽可能考虑在集成的GPU上运行。集成的GPU与CPU共享内存,数据交互效率更高。还有一些功能如双精度浮点运算只有在计算功能集的版本为1.3或者以上才具有,这种情况下就必须要选择版本号大于等于1.3的GPU。


CUDA提供了一个简便的方法来自动选择合适的GPU。以下以“要求设备的计算功能集的版本号为5.2”这个条件为例说明选择GPU的方法。

  • 1、设置需要的设备属性

定义一个cudaDeviceProp结构对象,设置需要的设备属性:

//定义需要的设备属性
	cudaDeviceProp devicePropDefined;
	memset(&devicePropDefined, 0, sizeof(cudaDeviceProp));  //设置devicepropDefined的值
	devicePropDefined.major = 5;
	devicePropDefined.minor = 2;


  • 2、调用cudaChooseDevice函数返回满足要求的设备ID

设置完所要求的的设备属性之后,把cudaDeviceProp结构的对象传递给cudaChooseDevice函数,cudaChooseDevice函数会查找机器上是否有满足所设置属性的设备,并返回设备编号:

cudaChooseDevice(&devicedChoosed, &devicePropDefined);  //查找符合要求的设备ID
	cout << "满足指定属性要求的设备的编号: " << devicedChoosed << endl;

  • 3、调用cudaSetDevice函数设置下文使用的设备编号

cudaSetDevice函数的作用是使输入ID代表的设备生效,之后所进行的CUDA操作就会在该设备上执行:

cudaError = cudaSetDevice(devicedChoosed); //设置选中的设备为下文的运行设备


以下是依据设定的属性选取设备的流程示例:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>

using namespace std;

int main()
{
	//定义需要的设备属性
	cudaDeviceProp devicePropDefined;
	memset(&devicePropDefined, 0, sizeof(cudaDeviceProp));  //设置devicepropDefined的值
	devicePropDefined.major = 5;
	devicePropDefined.minor = 2;

	int devicedChoosed;  //选中的设备ID
	cudaError_t cudaError;
	cudaGetDevice(&devicedChoosed);  //获取当前设备ID
	cout << "当前使用设备的编号: " << devicedChoosed << endl;

	cudaChooseDevice(&devicedChoosed, &devicePropDefined);  //查找符合要求的设备ID
	cout << "满足指定属性要求的设备的编号: " << devicedChoosed << endl;

	cudaError = cudaSetDevice(devicedChoosed); //设置选中的设备为下文的运行设备

	if (cudaError == cudaSuccess)
		cout << "设备选取成功!" << endl;
	else
		cout << "设备选取失败!" << endl;
	getchar();
	return 0;
}


原文地址:https://www.cnblogs.com/mtcnn/p/9411876.html