1. 程式人生 > >CUDA的獲取裝置詳細資訊

CUDA的獲取裝置詳細資訊

首先介紹一下開發環境,Visual Studio 2008 + CUDA Wizard for Visual Studio. 確保顯示卡支援CUDA(GeForce 8系列之後,否則只能用模擬模式)並安裝CUDA DDK及CUDA Toolkit。
    安裝完CUDA Wizard for Visual Studio之後,如果編譯執行時出現"無法找到cutil32D.dll"的錯誤,則需要將安裝的CUDA的sdk的路徑加到系統環境變數中:

例如C:\Program Files\NVIDIA Corporation\NVIDIA CUDA SDK\bin\win32\
下的
├─Debug
├─EmuDebug
├─EmuRelease
└─Release

幾個目錄都加入到系統環境變數Path中,這樣才能在執行程式的時候找到相應的dll庫。
     
     然後就可以在VS下直接編譯CUDA程式了(*.cu)。以下是一個CUDA初始化的程式。

  1. #include <stdio.h>
  2. #include <cuda_runtime.h>
  3. bool InitCUDA()  
  4. {  
  5.     int count;  
  6.     cudaGetDeviceCount(&count);  
  7.     if(count==0)  
  8.     {  
  9.         fprintf(stderr,"There is no device.\n"
    );  
  10.         returnfalse;  
  11.     }  
  12.     int i;  
  13.     for(i=0;i<count;i++)  
  14.     {  
  15.         cudaDeviceProp prop;  
  16.         if(cudaGetDeviceProperties(&prop,i) == cudaSuccess)  
  17.         {  
  18.             if(prop.major>=1)  
  19.             {  
  20.                 //列舉詳細資訊
  21.                 printf("Identify: %s\n"
    ,prop.name);  
  22.                 printf("Host Memory: %d\n",prop.canMapHostMemory);                  
  23.                 printf("Clock Rate: %d khz\n",prop.clockRate);                  
  24.                 printf("Compute Mode: %d\n",prop.computeMode);                  
  25.                 printf("Device Overlap: %d\n",prop.deviceOverlap);                  
  26.                 printf("Integrated: %d\n",prop.integrated);                  
  27.                 printf("Kernel Exec Timeout Enabled: %d\n",prop.kernelExecTimeoutEnabled);                  
  28.                 printf("Max Grid Size: %d * %d * %d\n",prop.maxGridSize[0],prop.maxGridSize[1],prop.maxGridSize[2]);  
  29.                 printf("Max Threads Dim: %d * %d * %d\n",prop.maxThreadsDim[0],prop.maxThreadsDim[1],prop.maxThreadsDim[2]);  
  30.                 printf("Max Threads per Block: %d\n",prop.maxThreadsPerBlock);  
  31.                 printf("Maximum Pitch: %d bytes\n",prop.memPitch);  
  32.                 printf("Minor Compute Capability: %d\n",prop.minor);  
  33.                 printf("Number of Multiprocessors: %d\n",prop.multiProcessorCount);                  
  34.                 printf("32bit Registers Availble per Block: %d\n",prop.regsPerBlock);  
  35.                 printf("Shared Memory Available per Block: %d bytes\n",prop.sharedMemPerBlock);  
  36.                 printf("Alignment Requirement for Textures: %d\n",prop.textureAlignment);  
  37.                 printf("Constant Memory Available: %d bytes\n",prop.totalConstMem);  
  38.                 printf("Global Memory Available: %d bytes\n",prop.totalGlobalMem);  
  39.                 printf("Warp Size: %d threads\n",prop.warpSize);  
  40.                 break;  
  41.             }  
  42.         }  
  43.     }  
  44.     if(i==count)  
  45.     {  
  46.         fprintf(stderr,"There is no device supporting CUDA.\n");  
  47.         returnfalse;  
  48.     }  
  49.     cudaSetDevice(i);  
  50.     returntrue;  
  51. }  
  52. void main()  
  53. {  
  54.     if(!InitCUDA())  
  55.         {  
  56.             getchar();  
  57.             return;  
  58.         }  
  59.         printf("CUDA initialized.\n");  
  60.         getchar();          
  61. }  

執行結果如下: