确定 nvcc 需要哪些 gencode(compute_、arch_)值 - 在 CMake 中

2023-11-23

我使用 CMake 作为我的代码的构建系统,其中涉及 CUDA。我正在考虑将决定哪个任务自动化compute_XX and arch_XX我需要传递给我的 nvcc 以便为我当前计算机上的 GPU 进行编译。

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

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


我的策略是编译并运行一个 bash 脚本来探测卡并返回 cmake 的 gencode。灵感来自于芝加哥大学 SLURM。要处理错误或多个 GPU 或其他情况,请根据需要进行修改。

在项目文件夹中创建一个文件 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)

cheers

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

确定 nvcc 需要哪些 gencode(compute_、arch_)值 - 在 CMake 中 的相关文章

随机推荐