Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake + Cuda: compile cpp files in Cuda-mode (--x=cu)

Tags:

c++

cuda

cmake

Suppose I have a file, let's call it foo.cpp, my goal is to compile this file with nvcc in cuda mode. From the command-line this can be easily accomplished by invoking:

nvcc --x=cu foo.cpp

What I'm struggling with, is to get CMake to do this exact same thing. It turns out that the CMake command cuda_add_executable(foo foo.cpp) will filter the *.cpp files and use the c++-compiler (instead of nvcc).

Note that renaming all the files to *.cu is not an option as the codebase has to also support non-cuda builds.

like image 670
thfabian Avatar asked Mar 15 '16 20:03

thfabian


1 Answers

In the FindCUDA source code I found an option to activate CUDA compilation for specific non-.cu files. Though it seems documentation is lacking (outside of the source code).

You can set CUDA compilation on a per file level with

set_source_files_properties( foo.cpp PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ )

in CMAKE 3.3 or later.

The rest works as usual:

cuda_add_executable(foo foo.cpp)

Actually, I expected there should be an easy solution like using CUDA_NVCC_FLAGS or cuda_add_executable( ... OPTIONS --x=cu ) to pass the --x=cu flag in. Unfortunately, it does not work. The reason is probably that the scope for a setting at that level would not be useful, since it would affect all files.

like image 115
havogt Avatar answered Oct 24 '22 07:10

havogt