How to create or import a "CUDA.rules" into another Visual Studio 2022 project to build the .cu files with nvcc as part of the build process?

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:

https://forums.developer.nvidia.com/t/how-to-integrate-cuda-into-existing-visual-studio-project-the-right-way/10401/1

There seem to be two ways of using CUDA in a Visual Studio project:

  1. 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.
  2. 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

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.

I’m currently running Visual Studio Pro 2022 v.17.12.0 (preview 2). My CUDA version is 12.6. I had a C++ project that was originally built without CUDA (no GPU acceleration). I added CUDA functions to that project, so I think what I did is similar to what you’re asking about.

As was already suggested, all of your CUDA-related functions should be in .cu/.cuh files, as they need to be compiled by the CUDA C compiler independent of the Visual C++ compiler. I prefer to keep my source and header files separated, and my CUDA and C++ files separated (i.e. Headers, Headers/CUDA, and Source, Source/CUDA) to avoid inadvertent mixing within the same project.

I assume you have already downloaded and installed CUDA 12.6, yes? For VS 2022 you will need at least (I think) CUDA 12.5 (most recent is 12.6.1 as I write this). I suggest you have your VS up-to-date too, and your NVIDIA drivers. And assuming you installed using the Express option, your CUDA install should be here: "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6"

So now, assuming your project is open in VS and your .cu/.cuh files have been added into your project, click ‘Project’ (top menu bar), ‘Build Customizations…’ to open the ‘Visual C++ Build Customization Files’ dialog. You should see an option that says ‘CUDA 12.6 (.targets, .props)’ and an associated path. And, you should be able to check the checkbox on the left edge. That will link the CUDA C compiler into your project.

Once that’s done, highlight and right-click your project name to open the ‘Property Pages’ dialog. On the left panel (Configuration Properties) you should see ‘CUDA C/C++’ and ‘CUDA Linker’ under the ‘C/C++’ property list. If so, you have the CUDA compiler enabled.

Now, highlight all of the .cu and .cuh files in your project (which is easy if you keep them grouped under a common filter) and right-click to open the ‘Property Pages’ dialog again. You will see ‘Item Type’ which you need to change to ‘CUDA C/C++’ (which will cause VS to pass those files to the CUDA C compiler for processing).

Compilation and linking will take longer, of course, because you’re calling an external compiler. But the process should be seamless and all output messages (warnings and errors) will show up in your Output window in VS.

I hope that helps. Good luck!