如何从cmake(3.10版本)将CUDA架构设置为compute_50和sm_50?

时间:2018-01-16 04:22:11

标签: c++ cmake cuda cmake-gui

我的项目使用CMake-GUI和visual studio。我的系统上没有安装gpu卡。生成的visual studio解决方案将nvcc标志设置为compute_30和sm_30,但我需要将其设置为compute_50和sm_50。

我使用CMake 3.10.1和Visual Studio 14 2015进行64位编译。

我希望取代CMake的默认设置。我没有使用Find CUDA方法来搜索和添加CUDA。我在CMAKE中添加了CUDA作为语言支持,而VS则基于此实现了CUDA Build定制。

4 个答案:

答案 0 :(得分:4)

有了CMake 3.18,就有了新的目标属性CUDA_ARCHITECTURES。从文档here

set_property(TARGET myTarget PROPERTY CUDA_ARCHITECTURES 35 50 72)

为真实和虚拟体系结构30、50和72生成代码。

set_property(TARGET myTarget PROPERTY CUDA_ARCHITECTURES 70-real 72-virtual)

生成用于实际架构70和虚拟架构72的代码。

答案 1 :(得分:3)

正确的方法是:

target_compile_options(myTarget PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-gencode arch=compute_50,code=sm_50>)

根据需要选择“ PRIVATE / PUBLIC”。这是设置每个目标标志的正确方法。

答案 2 :(得分:2)

所以我能够自己解决这个问题。我们可以通过以下方式设置它 -

string(APPEND CMAKE_CUDA_FLAGS“-gencode arch = compute_50,code = sm_50”)

答案 3 :(得分:0)

webpack.config.js默认获取(和使用)第一个已安装的物理设备的计算机功能设置,但可以通过设置CUDA_SM环境变量来覆盖。这是模块(将内容放在.cmake文件中并将其放在包含的文件夹中):

# This module determines which compute capability / SM version
# we should be compiling our CUDA code for, and adds the appropriate
# switch to the NVCC compiler flags - so that you don't have to worry
# about it.
#
# TODO: Be willing to take CUDA_CC, CUDA_TARGET_COMPUTE_CAPABILITY, 
# CUDA_TARGET_COMPUTE or CUDA_TARGET_COMPUTE_CAP and maybe even 
# those without the CUDA_ prefix

if (NOT CUDA_TARGET_COMPUTE_CAPABILITY)
        if("$ENV{CUDA_SM}" STREQUAL "")
                set(ENV{CUDA_INCLUDE_DIRS} "${CUDA_INCLUDE_DIRS}")
                set(ENV{CUDA_CUDART_LIBRARY} "${CUDA_CUDART_LIBRARY}")
                set(ENV{CMAKE_CXX_COMPILER} "${CMAKE_CXX_COMPILER}")
                execute_process(COMMAND bash -c "${CMAKE_CURRENT_SOURCE_DIR}/scripts/get_cuda_sm.sh"  OUTPUT_VARIABLE CUDA_TARGET_COMPUTE_CAPABILITY_) 
        else()
                set(CUDA_TARGET_COMPUTE_CAPABILITY_ $ENV{CUDA_SM})
        endif()

        set(CUDA_TARGET_COMPUTE_CAPABILITY "${CUDA_TARGET_COMPUTE_CAPABILITY_}" CACHE STRING "CUDA compute capability of the (first) CUDA device on the system, in XY format (like the X.Y format but no dot); see table of features and capabilities by capability X.Y value at https://en.wikipedia.org/wiki/CUDA#Version_features_and_specifications")

        execute_process(COMMAND bash -c "echo -n $(echo ${CUDA_TARGET_COMPUTE_CAPABILITY})" OUTPUT_VARIABLE CUDA_TARGET_COMPUTE_CAPABILITY) 
        execute_process(COMMAND bash -c "echo ${CUDA_TARGET_COMPUTE_CAPABILITY} | sed 's/^\\([0-9]\\)\\([0-9]\\)/\\1.\\2/;' | xargs echo -n" OUTPUT_VARIABLE FORMATTED_COMPUTE_CAPABILITY) 

        message(STATUS "CUDA device-side code will assume compute capability ${FORMATTED_COMPUTE_CAPABILITY}")
endif()

set(CUDA_GENCODE "arch=compute_${CUDA_TARGET_COMPUTE_CAPABILITY},code=compu