CUDA and cmake

Hello everybody !

I’m working on a project which uses CUDA for some functions. But I also want the program to be run on a computer without cuda installed using equivalent (but of course slower) versions of the functions.

To compile my project, I’m currently using cmake together with ccmake to produce the Makefiles. I’ve been trying to write some CMakelists file for ages but I’ve not been able to achieve what I am looking for.
I’d like nvcc to compile the .cu files, gcc to compile the standard c++ files and the usual linker to link everything if needed. At the same time, I’d like to be able to switch off the cuda-compilation so that gcc only compiles the whole project.

Do you have any advice on how I could manage that ?

I’ve tried with the findCUDA script, but as far as I understood, I cannot use it on a computer without CUDA and hence switch off the GPU part.

Thanks in advance !

Create an option (i.e. ENABLE_CUDA) and only find_package(FindCUDA.cmake) if ENABLE_CUDA is true. You will probably need to pass the ENABLE_CUDA as a -D define and use ifdef to conditionally include cuda related stuff in your .cc files.

This is what I do. FinCUDA.cmake is pretty advanced and extremely well tested, I wouldn’t try to roll your own :)

Thanks for you quick answer

I already have an ENABLE_CUDA pre-processor option in my files. I don’t know how to launch FindCUDA if the option is switched on in the ccmake interface.

What you want is a CMake variable.

option(ENABLE_CUDA "Enable the cuda build" ON)

if(ENABLE_CUDA)

  find_package(CUDA REQUIRED)

endif()

You could also check to see if cuda is found and enable it that way. FindCUDA defines a CUDA_FOUND variable if it found CUDA properly.

find_package(CUDA)

if(CUDA_FOUND)

  ....

endif()

You should also make sure you are using the latest version found here:

svn co https://code.sci.utah.edu/svn/findcuda/trunk FindCUDA

Thanks. I’ll try it out.

Are there any issues with the linker ? For example if some non-GPU files have been compiled with gcc ?

FindCUDA.cmake + CMake takes care of all the linking for you. All .cc / .cpp files are compiled by gcc/visual studio and only the .cu files are compiled by nvcc.

Yup! True! It just works…

When I switched from Windows to Linux, I did not have to change anything… It just works…

Thanks for your help.

It worked perfectly