• CUDA中自动初始化显卡设备宏


    每次为了减少初始化的工作量,可以写入下面的宏。

    #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.
    ")    
            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.
    ");    
            exit(EXIT_FAILURE);    
        }    
        if(cutCheckCmdLineFlag(ARGC,    (const char **) ARGV,"quiet") == CUTFalse)    
            fprintf(stderr,"Using device %d:%s
    ",dev,deviceProp.name);    
        CUDA_SAFE_CALL(cudaSetDevice(dev));    
    }
    
    #define CUT_EXIT(argc,argv)    
        if(!cutCheckCmdLineFlag(argc, ( const char **)argv, "noprompt")){    
            printf("
     Press ENTER to exit...
    ");    
            fflush(stdout);    
            fflush(stderr);    
            
            getchar();    
        }    
    exit(EXIT_SUCCESS);

    在主程序中:

    int main(int argc,char** argv){
        CUT_DEVICE_INIT(argc,argv);
        ...主程序内容
        CUT_EXIT(argc,argv);
    }
  • 相关阅读:
    canvas---HTML5新特性
    flex布局之兼容
    前端Blob对二进制流数据的处理方式
    execCommand的复制
    express快速入门
    react+redux+webpack+git技术栈
    react开发
    gulp工程化工具
    python---django安装
    vue+webpack+element-ui+git
  • 原文地址:https://www.cnblogs.com/xing901022/p/3369643.html
Copyright © 2020-2023  润新知