在CMake中确定nvcc需要哪个gencode(compute_,arch_)值

ein*_*ica 5 build-automation cuda cmake detection

我正在使用CMake作为我的代码的构建系统,它涉及CUDA.我正在考虑自动执行决定哪个compute_XXarch_XX我需要传递给我的nvcc以便在我当前的机器上编译GPU的任务.

  • 有没有办法做到这一点:

    1. 使用NVIDIA GPU部署套件?
    2. 没有NVIDIA GPU部署套件?
  • CMake是否FindCUDA帮助您确定这些开关的值?

小智 7

我的策略是编译并运行一个bash脚本来探测卡并返回cmake的gencode.灵感来自芝加哥大学的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
Run Code Online (Sandbox Code Playgroud)

并在您的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)
Run Code Online (Sandbox Code Playgroud)

干杯


Mar*_*gur 5

使用 CMake 3.7 或更高版本时,您可以使用FindCUDAcuda_select_nvcc_arch_flags()模块中的宏来实现此目的,无需任何其他脚本。

include(FindCUDA)
set(CUDA_ARCH_LIST Auto CACHE STRING
    "List of CUDA architectures (e.g. Pascal, Volta, etc) or \
compute capability versions (6.1, 7.0, etc) to generate code for. \
Set to Auto for automatic detection (default)."
)
cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS ${CUDA_ARCH_LIST})
list(APPEND CUDA_NVCC_FLAGS ${CUDA_ARCH_FLAGS})
Run Code Online (Sandbox Code Playgroud)

以上设置CUDA_ARCH_FLAGS-gencode arch=compute_61,code=sm_61我的机器为例。CUDA_ARCH_LIST用户可以配置缓存变量来生成特定计算功能的代码,而不是自动检测。

注意:自 CMake 3.10 起,FindCUDA 模块已被弃用。cuda_select_nvcc_arch_flags()但是,最新的 CMake 版本 (v3.14) 中似乎尚未提供该宏的等效替代方案。有关更多详细信息,请参阅CMake 问题跟踪器中的相关问题。