CUDA中自動初始化顯卡設備宏

每次爲了減小初始化的工做量,能夠寫入下面的宏。ui

#define CUT_DEVICE_INIT(ARGC,ARGV){    \
    int deviceCount; \ CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount)); \ if(deviceCount == 0){ \ fprintf(stderr,"cutil error:no devices supporting CUDA.\n") \ exit(EXIT_FAILURE); \ } \ int dev=0; \ cutGetCmdLineArgumenti(ARGC,(const char **) ARGV,"device",&dev); \ if(dev < 0) dev=0; \ if(dev > deviceCount - 1) dev=deviceCount - 1; \ cudaDeviceProp deviceProp; \ CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp,dev)); \ if(deviceProp.major < 1){ \ fprintf(stderr,"cutil error: device does not support CUDA.\n"); \ exit(EXIT_FAILURE); \ } \ if(cutCheckCmdLineFlag(ARGC,    (const char **) ARGV,"quiet") == CUTFalse) \ fprintf(stderr,"Using device %d:%s\n",dev,deviceProp.name); \ CUDA_SAFE_CALL(cudaSetDevice(dev)); \ } #define CUT_EXIT(argc,argv)    \
    if(!cutCheckCmdLineFlag(argc, ( const char **)argv, "noprompt")){ \ printf("\n Press ENTER to exit...\n"); \ fflush(stdout); \ fflush(stderr); \ getchar(); \ } \ exit(EXIT_SUCCESS);

在主程序中:spa

int main(int argc,char** argv){ CUT_DEVICE_INIT(argc,argv); ...主程序內容 CUT_EXIT(argc,argv); }
相關文章
相關標籤/搜索