CUDA编程主要做的就是和GPU打交道,在和这样的一个陌生的家伙交流之前,我们需要做的就是先得认识和熟悉这个家伙。

在深入研究如何编写设备代码之前,我们需要通过某种机制来判断计算机中当前有哪些设备,以及每个设备都支持哪些功能。幸运的是,可以通过一个非常简单的接口来获得这种信息。首先,我们希望知道在系统中有多少个设备是支持CUDA架构的,并且这些设备能够运行基于CUDA C编写的核函数。要获得CUDA设备的数量.可以调用cudaGetDeviceCount()。这个函数的作用从色的名字就可以看出来。在调用cudaGetDeviceCount()后,可以对每个设备进行迭代、井查询各个设备的相关信息。CUDA运行时将返回一个cudaDevice Prop类型的结构,其中包含了设备的相关属性。我们可以获得哪些属性?从CUDA 3.0开始,在cudaDeviceProp结构中包含了以下信息:

 

[cpp] view plaincopy
 
  1. struct cudaDeviceProp  

  2. {  

  3.    char name[256];         //标识设备的ASCII字符串  

  4.    size_t totalGlobalMem;          //设备上全局内存的总量,单位为字节  

  5.    size_t sharedMemPerBlock;   //在一个线程块(Block)中可使用的共享内存总量,单位为字节  

  6.    int regsPerBlock;       //每个线程块中可用的32位寄存器数量  

  7.    int warpSize;           //在一个线程束(warp)中包含的线程数量  

  8.    size_t memPitch;        //在内存复制中最大的修正量(Pitch),单位为字节  

  9.    int maxThreadsPerBlock;     //在一个线程块中包含的最大线程数目  

  10.    int maxThreadsDim[3];       //在多维线程块数组中,每一维包含的最大线程数量  

  11.    int maxGridSize [3];        //在一个线程格(Grid)中,每一维可以包含的线程块的数量  

  12.    size_t totalConstMem;       //常量内存的总量  

  13.    int major;          //设备计算功能集的主版本号  

  14.    int minor;          //设备计算功能集的次版本号  

  15.    int clockRate;          //  

  16.    size_t textureAlignment;    //设备的纹理对齐要求  

  17.    int deviceoverlap;      //一个布尔类型值,表示设备是否可以同时执行一个cudaMemory()调用和一个核函数调用  

  18.    int multiProcessorCount;    //设备上多处理器的数量  

  19.    int kernelExecTimeoutEnabled;   //一个布尔值,表示该设备上执行的核函数是否存在运行时限制  

  20.    int integrated;         //一个布尔值,表示设备是否是一个集成的GPU  

  21.    int canMapHostMemory;       //一个布尔值,表示设备是否将主机内存映射到cuda设备地址空间  

  22.    int computeMode;        //表示设备的计算模式:默认,独占或禁止  

  23.    int maxTexture1D;       //一维纹理的最大大小  

  24.    int maxTexture2D[2];        //二维纹理的最大维数  

  25.    int maxTexture3D[3];        //三维纹理的最大维数  

  26.    int maxTexture2DArray[3];   //二维纹理数组的最大维数  

  27.    int concurrentKernels ;     //一个布尔值,表示设备是否支持在同一个上下文中同时执行多个核函数  

  28. };  

设备属性的使用

 

通过上面的结构体,我们大致了解了设备的属性,然后我们就可以通过这个结构体来查询设备属性了。可能会有人问,到底我们需要这些设备属性来干嘛,别着急,以后在编写相关性能优化的代码的时候,就知道了解这些属性的好处了。现在我们只需要知道方法就可以了。

首先我们可以通过两个函数,第一个就是上面的cudaGetDeviceCount()来选择设备,然后循环地通过getDeviceProperties()来获得设备的属性,之后我们就可以通过这样的一个结构体变量将设备的属性值获取出来。

 

[cpp] view plaincopy
 
  1. #include <cuda_runtime.h>  

  2. #include <iostream>  

  3. using namespace std;  

  4.  

  5. int main()  

  6. {  

  7.    cudaDeviceProp prop;  

  8.  

  9.    int count;  

  10.    cudaGetDeviceCount(&count);  

  11.  

  12.    for(int i = 0 ; i < count ; i++)  

  13.    {  

  14.        cudaGetDeviceProperties(&prop,i);  

  15.        cout<<"the information for the device : "<<i<<endl;  

  16.        cout<<"name:"<<prop.name<<endl;  

  17.        cout<<"the memory information for the device : "<<i<<endl;  

  18.        cout<<"total global memory:"<<prop.totalGlobalMem<<endl;  

  19.        cout<<"total constant memory:"<<prop.totalConstMem<<endl;  

  20.        cout<<"threads in warps:"<<prop.warpSize<<endl;  

  21.        cout<<"max threads per block:"<<prop.maxThreadsPerBlock<<endl;  

  22.        cout<<"max threads dims:"<<prop.maxThreadsDim[0]<<"  "<<prop.maxThreadsDim[1]<<  

  23.            "  "<<prop.maxThreadsDim[2]<<endl;  

  24.        cout<<"max grid dims:"<<prop.maxGridSize[0]<<"  "<<  

  25.            prop.maxGridSize[1]<<"  "<<prop.maxGridSize[2]<<endl;  

  26.  

  27.    }  

  28.    return 0;  

  29. }  

我这边只是获取一部分的属性值,只是和大家介绍一下,具体的属性值可以按照这样的方法来获取。

原文地址:http://blog.csdn.net/timmawang/article/details/10362701



http://blog.sciencenet.cn/blog-935970-861092.html  此文来自科学网崔英博博客,转载请注明出处。 

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()