How to add a cuda-file to VS 2015 c++ solution

I am having a similar problem in adding a CUDA application to an existing 64-bit C++ project. In my existing project, there is a computation I want to offload to the GPU device.

I have current Visual Studio 2017 (15.3.1) and CUDA 9.0 RC. I have successfully built and run the deviceQuery and bandwidthTest applications, 64-bit, under Windows 7 64-bit.

However, if I try to add a .cu file, created outside Visual Studio, to my 64-bit C++ project, its properties show that it is not a ‘CUDA C/C++’ file, as determined by right-clicking on the file>Properties>Configuration Properties>General>Item Type>. And ‘CUDA C/C++’ is not one of the possibilities in the list of item types.

So there must be some setting for my C++ project to tell it to use the CUDA nvcc compiler for .cu files.

If I create a new project, ‘CUDA 9.0 runtime’ is a choice, but I need to add the CUDA files to my existing project. How can I do this?

If you create a new CUDA 9.0 runtime project, that is a good starting point because it will get you the build customizations you need.

If you start a project that way, thereafter you can add existing files to the project by right-clicking in the project window, selecting Add…Existing Item from the pop up menu.

If you select a .cu file, and add it to your project, then when you go to build the project, the build customizations in the project will cause that file to be compiled by nvcc

@txbob: When I compare the project properties for my C++ project vs a new CUDA 9.0 runtime project, the latter has ‘CUDA C/C++’ under Configuration Properties, while the C++ project has ‘C/C++’.

It is not feasible to create a CUDA 9.0 runtime project and then add all 200 of my source files to it, and, in any case, it is not a console project.

So, I am looking for a way to add CUDA files to an existing project. Is that possible? If not, how would you ever do any realistic-sized HPC?

Yes, it’s possible. Then you have to add the CUDA build customizations to your C/C++ project.

http://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html#build-customizations-for-existing-projects

OK, I found at least part of the answer in the CUDA Installation Guide.

3.4. Build Customizations for Existing Projects

When adding CUDA acceleration to existing applications, the relevant Visual Studio project files must be updated to include CUDA build customizations. This can be done using one of the following two methods:

  1. Open the Visual Studio project, right click on the project name, and select Build Customizations… , then select the CUDA Toolkit version you would like to target.

After doing the above method 1, a file in the existing C++ project can be made to be of item type CUDA C/C++.

NOTE: The documentation above is not quite correct: It should be “right click on the project name, and select Build Dependencies, then Build Customizations…”

I have now successfully compiled my .cu file as part of the C++ project, but there are two other problems. The first is, when I rebuild the entire project, the compilation of PosX_CUDA.cu (my only CUDA file) fails, with error

1>------ Rebuild All started: Project: MyApp, Configuration: Debug x64 ------
1>
1>C:\MyApp>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin\nvcc.exe" -ccbin "C:\Program Files (x86)\Microsoft Visual Studio017\Community\VC\Tools\MSVC4.11.25503\bin\HostX86\x64" -x cu  -IC:\MyApp\ -IC:\MyLibraries\Utility -IC:\Miscellaneous_Libraries\Utilities -IC:\Miscellaneous_Libraries\Controls -I"C:\Miscellaneous_Libraries\zstd-dev\lib" -IC:\MyLibraries\Tables -IC:\MyLibraries\Move -IC:\MyLibraries\Position -IC:\MyLibraries\Include -IC:\MyLibraries\Band -IC:\Miscellaneous_Libraries\UtilitiesGeneral -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile   -g   -DWIN64 -D_WINDOWS -D_DEBUG -DMyAppBuild -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MTd " -o x64\Debug\PosX_CUDA.cu.obj "C:\MyApp\PosX_CUDA.cu" -clean

1>CUDACOMPILE : nvcc error : 'C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/bin/HostX86/x64/cl.exe' died with status 0xC0000135

But, if I compile PosX_CUDA.cu separately (Ctrl-F7), the compile is successful. The command line is significantly different, so I suspect that for a ‘Rebuild’, the .cu file is getting fed to the Visual Studio C++ compiler, rather than nvcc.

My second problem will appear in a separate thread.

It seems there is a missing DLL when nvcc invokes cl.exe

Beyond that I’m not sure. May need a complete test case.

@txbob: Although nvcc was invoked, when you specify ‘rebuild’, the command line is much different. In particular, after “\nvcc.exe”, the sections that begin “-gencode=arch” is missing, and the Windows C++ compiler settings appear instead.

To me this indicates that NVidia made an error in the VS2017 integrations, failing to formulate the command line correctly in case of rebuilding.

about vs2015+cuda8 problems - unfortunately, cuda8 supports only vs2015.update1, so later vs2015 versions shouldn’t work with cuda8. i haven’t tried IDE, but it definitely failed for cmdline compilation