cuda – 在CMake中确定我需要的nvcc所需的gencode(compute_,arch_)值
时间 2019-08-11
标签 cuda cmake build-automation detection 栏目 Linux
我正在使用CMake作为我的代码的构建系统,它涉及CUDA.我正在考虑自动执行决定哪个compute_XX和arch_XX需要传递给我的nvcc以便为我当前机器上的GPU编译的任务.
>有没有办法做到这一点:
>使用NVIDIA GPU部署套件?
>没有NVIDIA GPU部署套件?
> CMake的FindCUDA可以帮助您确定这些开关的值吗?
我的策略是编译并运行一个bash脚本来探测卡并返回cmake的gencode.灵感来自 University of Chicago’s SLURM.要处理错误或多个gpus或其他情况,请根据需要进行修改.
在项目文件夹中创建一个文件cudaComputeVersion.bash并确保它可以从shell执行.进入这个文件放:
#!/bin/bash # create a 'here document' that is code we compile and use to probe the card cat << EOF > /tmp/cudaComputeVersion.cu #include <stdio.h> int main() { cudaDeviceProp prop; cudaGetDeviceProperties(&prop,0); int v = prop.major * 10 + prop.minor; printf("-gencode arch=compute_%d,code=sm_%d\n",v,v); } EOF # probe the card and cleanup /usr/local/cuda/bin/nvcc /tmp/cudaComputeVersion.cu -o /tmp/cudaComputeVersion /tmp/cudaComputeVersion rm /tmp/cudaComputeVersion.cu rm /tmp/cudaComputeVersion
并在您的CMakeLists.txt中放置:
# at cmake-build-time, probe the card and set a cmake variable execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/cudaComputeVersion.bash OUTPUT_VARIABLE GENCODE) # at project-compile-time, include the gencode into the compile options set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; "${GENCODE}") # this makes CMake all chatty and allows you to see that GENCODE was set correctly set(CMAKE_VERBOSE_MAKEFILE TRUE)