zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    DPDK ring简单说明
    DPDK初始化流程
    从《雪白血红》说起(2)
    从《雪白血红》说起(1)
    苏联印象(1)-过往与想象
    DPDK ip分片与重组的设计实现
    linux协议栈分析-序
    DPDK与QoS(服务质量)
    DPDK LPM路由存储与查找
    《教父》曾说
  • 原文地址:https://www.cnblogs.com/xing901022/p/3369643.html
Copyright © 2011-2022 走看看