• 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);
    }
  • 相关阅读:
    Swoole 协程使用示例及协程优先级
    Swoole 协程简介
    Laravel Redis分布式锁的使用
    Laravel Redis分布式锁实现源码分析
    Swoole 中使用异步任务
    runtime相关面试
    oc笔试题
    属性关键字面试题
    KVC面试题
    KVO面试题
  • 原文地址:https://www.cnblogs.com/xing901022/p/3369643.html
Copyright © 2020-2023  润新知