clang 4.0.0 cuda和cmake最小警告的最佳实践

时间:2017-06-07 01:50:09

标签: c++ c++11 cmake clang clang++

我无法弄清楚如何只为CUDA文件获取clang CUDA特定标志:

# building all except 20
set(BUILD_CUDA_ARCH "30;32;35;50;52;53")

# According to the docs, we need static cudart:
# http://llvm.org/docs/CompileCudaWithLLVM.html#compiling-cuda-code
list(APPEND EXTRA_LIBS "cudart_static")

# Let clang know exactly where cuda came from
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")

# Add the compilation trajectories for different architectures
# go to the docs linked above
foreach(arch ${BUILD_CUDA_ARCH})
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --cuda-gpu-arch=sm_${arch}")
endforeach()

# Add the executable like normal, now that CMAKE_CXX_FLAGS has been adjusted.
# clang will compile the .cu files separately
add_library(${LIB_NAME} ${LIBRARY_TYPE} ${CXX_INC} ${CXX_SRC} ${CUDA_INC} ${CUDA_SRC})

定义文件中的其他位置

  • LIB_NAME:正在构建的库的名称
  • LIBRARY_TYPESHAREDSTATIC
  • CXX_INCCXX_SRC:纯C ++标头/源代码
  • CUDA_INCCUDA_SRC:直接CUDA代码
  • EXTRA_LIBS:仅在target_link_libraries中使用其他内容

问题是这种方法导致:

clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_30' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_32' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_35' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_50' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_52' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_53' [-Wunused-command-line-argument]
每个非cuda编译的警告。我将其分成两个独立的库,还是有某种方式告诉clanglinked docs in the comments似乎表明它应该仅使用.cu文件来处理cuda,但我似乎无法仅为该模式找到特定的标志。将它拆分为两个库并将逻辑分离的方式感觉很尴尬--CPU版本包含不同的文件,可以有效地执行与gpu相同的操作。

2 个答案:

答案 0 :(得分:2)

您应该仅为CUDA源文件添加CUDA编译标志作为cmake编译属性,而不是单方面添加它们,然后尝试有选择地抑制它们。

答案 1 :(得分:0)

有一种方法可以忽略它,如this answer

所示
# Squelch warnings about cuda flags for C++ files
foreach(cxx ${CXX_SRC} ${CXX_INC})
  set_source_files_properties(${cxx} PROPERTIES COMPILE_FLAGS -Wno-unused-command-line-argument)
endforeach()

我认为提供一揽子-Wno-unused-command-line-argument是不恰当的,但似乎单独处理的标志不在llvm