CUDA的獲取裝置詳細資訊
阿新 • • 發佈:2019-02-20
首先介紹一下開發環境,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初始化的程式。
- #include <stdio.h>
- #include <cuda_runtime.h>
- bool InitCUDA()
- {
- int count;
- cudaGetDeviceCount(&count);
- if(count==0)
- {
-
fprintf(stderr,"There is no device.\n"
- returnfalse;
- }
- int i;
- for(i=0;i<count;i++)
- {
- cudaDeviceProp prop;
- if(cudaGetDeviceProperties(&prop,i) == cudaSuccess)
- {
- if(prop.major>=1)
- {
- //列舉詳細資訊
-
printf("Identify: %s\n"
- printf("Host Memory: %d\n",prop.canMapHostMemory);
- printf("Clock Rate: %d khz\n",prop.clockRate);
- printf("Compute Mode: %d\n",prop.computeMode);
- printf("Device Overlap: %d\n",prop.deviceOverlap);
- printf("Integrated: %d\n",prop.integrated);
- printf("Kernel Exec Timeout Enabled: %d\n",prop.kernelExecTimeoutEnabled);
- printf("Max Grid Size: %d * %d * %d\n",prop.maxGridSize[0],prop.maxGridSize[1],prop.maxGridSize[2]);
- printf("Max Threads Dim: %d * %d * %d\n",prop.maxThreadsDim[0],prop.maxThreadsDim[1],prop.maxThreadsDim[2]);
- printf("Max Threads per Block: %d\n",prop.maxThreadsPerBlock);
- printf("Maximum Pitch: %d bytes\n",prop.memPitch);
- printf("Minor Compute Capability: %d\n",prop.minor);
- printf("Number of Multiprocessors: %d\n",prop.multiProcessorCount);
- printf("32bit Registers Availble per Block: %d\n",prop.regsPerBlock);
- printf("Shared Memory Available per Block: %d bytes\n",prop.sharedMemPerBlock);
- printf("Alignment Requirement for Textures: %d\n",prop.textureAlignment);
- printf("Constant Memory Available: %d bytes\n",prop.totalConstMem);
- printf("Global Memory Available: %d bytes\n",prop.totalGlobalMem);
- printf("Warp Size: %d threads\n",prop.warpSize);
- break;
- }
- }
- }
- if(i==count)
- {
- fprintf(stderr,"There is no device supporting CUDA.\n");
- returnfalse;
- }
- cudaSetDevice(i);
- returntrue;
- }
- void main()
- {
- if(!InitCUDA())
- {
- getchar();
- return;
- }
- printf("CUDA initialized.\n");
- getchar();
- }
執行結果如下: