I would like to add CUDA to an existing Visual Studio project. However, I am having trouble finding any guidance on a working method for Visual Studio 2022.
RESEARCH
The best advice I have found on this is here, but it seems outdated now in the details if not the general principle:
There seem to be two ways of using CUDA in a Visual Studio project:
- Based on the “template” project from the CUDA SDK: for each .cu file, set a custom build step, and call nvcc from the Command Line.
- Based on other projects from the CUDA SDK: Import the Cuda.rules file into the project and the project’s .cu files will be built based on these rules.
We have successfully created projects from scratch using either method. An advantage to method 1 seems that one could have greater control over nvcc and the environment in general. An advantage to method 2 seems that settings won’t have to be done on a per .cu file basis, simplifying the project setup.
I also see the second option being suggested here:
https://users.cs.utah.edu/~mhall/cs4961f10/CUDA-VS08.pdf
1) Create new Empty Win32 Console C project
2) Project > Custom Build Rules > Find Existing Project > Custom Build Rules > Find Existing
3) Browse to C:\Program Files\NVIDIA Corporation\NVIDIA GPU Computing SDK\C
4) Choose Cuda.rules
5) Project > Custom Build Rules > Enable Cuda rule
6) Add the .cu file to your project
I would like to follow this approach ideally (adding a rules file to compile the .cu
files specifically first). JUCE (the framework I am using) automatically generates the Visual Studio project, but if I can modify them if I know what to do.
In the above linked thread they also suggest:
Integrating CUDA in a project is easy enough, the only real crux is having CUDA C compiled by nvcc and not by whichever compiler you have in the framework. That usually requires a custom build step.
In the NVIDIA forum link, one user suggests moving all CUDA related functions and includes to .cu
files. Ie. Put basic function declarations with benign C++ type arguments in your regular files, and then put any function definitions with their CUDA object types and functions in the .cu
files.
Then the only thing you need to do (I think) is instruct Visual Studio to compile all .cu
files with nvcc as a first step. Either by rules or something else.
Is that correct? If so, how can one do this?
There was also a CUDA Wizard linked over there but hasn’t been updated in 5 years, so is again dead in the details:
Download and install CUDA VS Wizard, it will add a CUDA build rule and integrate with Visual Studio. Now you can Add a new Item and name a file any way you want with a .cu extension (like cudaFunctions.cu), the extension will be automatically recognized and the CUDA build rule will be applied. Check this item’s properties, it will be compiled with nvcc.
So presumably I just need to manually add a build rule for the .cu
files manually (and not mention anything CUDA related in my code outside the .cu
files) and that should do it, right?
PROBLEMS
- There are no
cuda.rules
files inside the SDK or any installation directories I can find anymore. - “Custom Build Rules” does not exist in in Visual Studio 2022 where it is claimed to have previously.
- I do not know how to make a “custom build step” or “custom rules” or what that involves.
- The only reference I can find on custom build steps is this one but I am not sure how to specifically use that here: https://learn.microsoft.com/en-us/cpp/build/how-to-add-a-custom-build-step-to-msbuild-projects?view=msvc-170
STEPS SO FAR
The only step I have so far completed was adding the CUDA headers search path to my JUCE project so now my auto generated Visual Studio project can find all the CUDA functions. It is obviously no problem to move all my CUDA related functions and #include
lines into purely .cu
files.
The question is how do I then add a special build direction or rules file to Visual Studio 2022 to run nvcc
on these .cu
files as part of building? (Or whatever it is that then needs to be done at this stage?)
Thanks for any help or ideas.