How to Start a New Cuda Project in Visual Studio 2008

How do I start a new CUDA project in Visual Studio 2008?

NOTE With the release of version 3.2 of the CUDA Toolkit, NVIDIA now includes the rules file with the Toolkit as opposed to the SDK. Therefore I've split this answer into two halves, use the correct instructions for your version of the Toolkit.

NOTE These instructions are valid for Visual Studio 2005 and 2008. For Visual Studio 2010 see this answer.


CUDA TOOLKIT 3.2 and later

I recommend using the NvCudaRuntimeApi.rules file (or NvCudaDriverApi.rules if using the driver API) provided by NVIDIA, this is released with the toolkit and supports the latest compiler flags in a friendly manner. Personally I would advise against using the VS wizard, but only because I really don't think you need it.

The rules file (installed into the Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults directory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.

  • Create a new project using the standard MS wizards (e.g. an empty console project)
  • Implement your host (serial) code in .c or .cpp files
  • Implement your wrappers and kernels in .cu files
  • Add the NvCudaRuntimeApi.rules (right click on the project, Custom Build Rules, tick the relevant box), see note 1
  • Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add $(CUDA_PATH)\lib\$(PlatformName) to the Additional Library Directories and in Linker -> Input add cudart.lib to the Additional Dependencies), see notes [2] and [3]
  • Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then in C/C++ -> General add $(CUDA_PATH)\include to the Additional Include Directories), see note [3]
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically

Some other tips:

  • Change the code generation to use statically loaded C runtime to match the CUDA runtime; right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT (or /MTd for debug, in which case you will need to mirror this in Runtime API -> Host -> Runtime Library), see note [4]
  • Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in <sdk_install_dir>\C\doc\syntax_highlighting\visual_studio_8

I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"

Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall wrapper calls exit() if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!


CUDA TOOLKIT 3.1 and earlier

I would use the Cuda.rules file provided by NVIDIA with the SDK, this is released alongside the toolkit and supports the latest compiler flags in a friendly manner. Personally I would advise against using the VS wizard, but only because I really don't think you need it.

The rules file (in the C\common directory of the SDK) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.

  • Create a new project using the standard MS wizards (e.g. an empty console project)
  • Implement your host (serial) code in .c or .cpp files
  • Implement your wrappers and kernels in .cu files
  • Add the Cuda.rules (right click on the project, Custom Build Rules, browse for the rules file and ensure it is ticked)
  • Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add $(CUDA_LIB_PATH) to the Additional Library Directories and in Linker -> Input add cudart.lib to the Additional Dependencies), see note [2] below
  • Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then in C/C++ -> General add $(CUDA_INC_PATH) to the Additional Include Directories)
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically

Some other tips:

  • Change the code generation to use statically loaded C runtime to match the CUDA runtime, right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT (or /MTd for debug, in which case you will need to mirror this in CUDA Build Rule -> Hybrid CUDA/C++ Options), see note [4]
  • Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in <sdk_install_dir>\C\doc\syntax_highlighting\visual_studio_8

I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"

Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall wrapper calls exit() if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!


NOTE

  1. You can also use a Toolkit-version-specific rules fule e.g. NvCudaRuntimeApi.v3.2.rules. This means that instead of looking for the CUDA Toolkit in %CUDA_PATH% it will look in %CUDA_PATH_V3_2%, which in turn means that you can have multiple versions of the CUDA Toolkit installed on your system and different projects can target different versions. See also note [3].
  2. The rules file cannot modify the C/C++ compilation and linker settings, since it is simply adding compilation settings for the CUDA code. Therefore you need to do this step manually. Remember to do it for all configurations!
  3. If you want to stabilise on a specific CUDA Toolkit version then you should replace CUDA_PATH with CUDA_PATH_V3_2. See also note 1.
  4. Having mismatched version of the C runtime can cause a variety of problems; in particular if you have any errors regarding LIBCMT (e.g. LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs) or multiply defined symbols for standard library functions, then this should be your first suspect.

CUDA & Visual Studio 2008: Problems when trying to link different projects

I checked your code and this is what I did to succesfully compile it and run it:

  • Used CUDA Runtime API (it is OK to use it even if you have multiple projects)

  • MainProject -> Configuration Properties -> Linker -> General -> Additional Library Directories : added path to CUDA libs (it's one of subfolders of your CUDA installation path)

  • MainProject -> Configuration Properties -> Linker -> Input -> Additional Dependencies : added "cudart.lib"

  • MainProject -> Configuration Properties -> Linker -> Input -> Ignore Specific Library : added "LIBCMT" to remove that warning about conflicts.

  • Make sure that you use the same "Runtime Library" for normal .cpp and .cu. In your case it didn't match and it seems it was the reason for the warning abount library incompatibility. Compare the values of :

    • C/C++ -> Code Generation -> Runtime Library

    • CUDA Runtime API -> Host -> Runtime Library

Also, you are generating "testCppProject.lib" and "CUDAProject.lib", but you are including "test.lib" & "CUDAProject.lib", where the first one is some older version of the lib.

How do I start a CUDA app in Visual Studio 2010?

CUDA TOOLKIT 4.0 and later

The build customisations file (installed into the Program Files\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations directory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application. If you chose to skip installing the customisations, or if you installed VS2010 after CUDA, you can add them later by following the instructions in Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\extras\visual_studio_integration.

  • Create a new project using the standard MS wizards (e.g. an empty console project)
  • Implement your host (serial) code in .c or .cpp files
  • Add the NVIDIA build customisation (right click on the project, Build customizations, tick the relevant CUDA box)
  • See note 1 if using CUDA 4.0
  • Implement your wrappers and kernels in .cu files
  • If you added .cu files before the build customisations, then you'll need to set the type of the .cu files to CUDA C/C++ (right-click on the file, Properties, set Item Type)
  • Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> Input add cudart.lib to the Additional Dependencies)
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically

Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall wrapper calls exit() if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!


NOTE

  1. For CUDA 4.0 only you may need to apply this fix to the build customisations. This patch fixes the following message:

The result "" of evaluating the value "$(CudaBuildTasksPath)" of the "AssemblyFile" attribute in the element is not valid

How to integrate CUDA .cu code with C++ app

It looks like you have a conflict between two different versions of the C runtime. The CUDA runtime uses the statically linked C runtime, and your application uses the dynamically loaded C runtime (typically the default in Visual Studio).

Change the code generation to use statically loaded C runtime to match the CUDA runtime:

  • Right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT

You could use /MTd for debug, in which case you will need to mirror this in CUDA Build Rule -> Hybrid CUDA/C++ Options.

See this earlier post for a step-by-step with more tips.

VS2008 not compiling cuda

You are compiling with nvcc for 32bit (--machine 32) but your linked cuda libs are 64bit ([..]\lib\x64).

How to Compile CUDA App is Visual Studio 2010?

Yes i did, and it IS working.

  1. Create C++ project

  2. Project(right click)->build customisation Check "Cuda 3.2 compiler"*

  3. Add cudart.lib to properties->linker->input->additional dependencies

  4. Add main.cu -> properties Item type = CUDA C/C++*

  5. Project -> properties ->configuration Properties -> general -> v90 toolset**

*) will appear after you install Nvidia Parallel Nsight. Be careful, you need special drivers for that, more on NSight homepage)

**) you need to instal visual c++ 2008 express.

Anyway, example project still should be available at my nvidia forum post. The problem i'm complaining there about is just out-of-date drivers.



Related Topics



Leave a reply



Submit