conditional nvcc-compiling


I have a somehow complicated question but will try to explan it:

Imagine you have a program using cuda, if devices are present, else using ipp or other stuff like that.

Now I would like to program such a thing but want to have the ability to choose if cuda is used or not already at compile time. One easy thing would be a preprocessor swicht which wraps and uncomments any cuda-capable code if I don’t want to use it. This shows a strange linker error if nvcc finds actually nothing to compile. (I will try to reproduce it, but have already splitted my program for further tests)

Other try: I construct a main-wrapper which conditionally includes the cuda-files. But then I will still need the nvcc, because it wants to compile the cuda files, if needed or not. Or I exclude the cuda file from biuld, but then it never is built, even if I want to use cuda.

I’m using Visual Studio 2005 but maybe this problem can be solved platform idependent. How can I tell my compiler only to use the nvcc to compile the cu files when I want it?

Thanks in advance,


Why do you need to check for CUDA at compile time? AFAIK, it should be relatively simple to check for CUDA at runtime and then run the CUDA or non-CUDA version of your code based on that check.

The thing I’m working on will be usable as a main part in lots of implementations of our company. But not all of those who will use it, will have cuda capable devices and therefore not have CUDA installed. I try to avoid having two parallel versions lying around for those who use cuda should still be able to compile for non cuda capable machines, which includes exactly the procedures I want to use for the programs completely not using cuda.

ATM I’m thinking about using a library of some sort as an alternative solution. But that would require to have the library as single project which will result in always havin to compile twice while still implementing the lib. Because then I would need an independend wrapper, and every little change in the lib results in recompiling the lib and the wrapper. So maybe there is an easier way to implement these things.

Thanks for any suggestions,


I do exactly what you are describing with CMake. If USE_CUDA is enabled in CMake, all the .cu files are added to the project and compiled with nvcc. Furthermore, USE_CUDA is defined in the preprocessor, enabling all CUDA code in .cc files to be conditinally compiled by the host compiler (whatever it may be).

If USE_CUDA is off, the .cu files are never added to the project and this nvcc doesn’t even need to be installed.

I don’t have any suggestions of how to implement this with just VS2005 projects, though… They don’t lend themselves well to the autoconf/CMake concept of “configuring” and then producing the appropriate make files / IDE project files.

But you seem to be saying that anyone who compiles this will have nvcc anyways? In that case, there is no harm in compiling all the CUDA stuff into your code. Just choose which path (CPU or GPU) to run at runtime based on the result of calling getGetDeviceCount().

CMake works just fine for Visual Studio 2005. It should be no problem to have a USE_CUDA flag that conditionally adds files to the project.

Thank you guys for the quick response. I will take a look at CMake.

To the last part, sorry if it was written misunderstandable, I do have the problem because NOT everyone compiling the project will have nvcc installed. Otherwise there would be no problem at all. The getDeviceCount is already implemented for those who have nvcc at compile time but are using their programs on machines which do not necessarily have a cuda capable device.

Thanks again for the hint. I will try CMake.