加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

cuda – 在CMake中确定我需要的nvcc所需的gencode(compute_,arch

发布时间:2020-12-13 23:52:17 所属栏目:Linux 来源:网络整理
导读:我正在使用CMake作为我的代码的构建系统,它涉及CUDA.我正在考虑自动执行决定哪个compute_XX和arch_XX需要传递给我的nvcc以便为我当前机器上的GPU编译的任务. 有没有办法做到这一点: 使用NVIDIA GPU部署套件? 没有NVIDIA GPU部署套件? CMake的FindCUDA可以
我正在使用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_%dn",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)

干杯

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读