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

55,351

Solution 1

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

Solution 2

Another Good tutorial here:

http://www.stevenmarkford.com/installing-nvidia-cuda-with-visual-studio-2010/

if you get an error about '<' note this step (from a previous answer):

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)

But if you follow their steps, it should work!

Solution 3

This answer applies to CUDA 3.2, from 4.0 onwards CUDA supports the VC 10 compiler directly, see other answers for more information

You need either VS 2008 or the 6.1 Windows SDK installed. That's because NSight 1.5 RC or the CUDA 3.2 SDK use the VC 9 compiler under the hood. I've got this working successfully with 2008 installed and am told it should work with the SDK but haven't tried.

With NSight 1.5 and/or the CUDA 3.2 SDK you shouldn't need to muck with any custom build rules. I've been there and it's painful. With the latest builds all that goes away:

  • Create your VC++ project.
  • Add a .CU file to it.
  • Select the project file in the Solution Explorer.
  • Open Project | Build Customizations...
  • Check the "CUDA 3.2 (.targets, .props)" customization.
  • Select a .CU file in your project and hit Alt-Enter to show it's properties.
  • Make sure it's Item Type is set to "CUDA C/C++"

It should just build. Let me know if this helps and if you run into problems as this is from memory.

The good news it getting CUDA working with VS 2010 just got much easier.

Ade

BTW: I'll update my blog post.

Share:
55,351

Related videos on Youtube

M. Tibbits
Author by

M. Tibbits

PSU Alumni!! Woot.

Updated on July 09, 2022

Comments

  • M. Tibbits
    M. Tibbits almost 2 years

    Direct Question: How do I create a simple hello world CUDA project within visual studio 2010?

    Background: I've written CUDA kernels. I'm intimately familiar with the .vcproj files from Visual Studio 2005 -- tweaked several by hand. In VS 2005, if I want to build a CUDA kernel, I add a custom build rule and then explicitly define the nvcc call to build the files.

    I have migrated to Win 7, and VS 2010 because I really want to try out nSight. I have nSight 1.5 installed. But this is where I'm totally lost. If I proceed as before, nvcc reports that it only supports msvc 8.0 & 9.0. But the website clearly states that it supports VS 2010.

    I read somewhere else that I need to have VS 2008 (msvc 9.0) also installed -- my word. Doing so now.

    But I'm guessing that at least part of my problems stem from the homegrown custom build tool specifications. Several websites talk about adding a *.rules file to the build, but I've gathered that this is only applicable to VS 2008. Under "Build Customizations" I see CUDA 3.1 and 3.2, but when I add kernels to the project they aren't built. Another website proclaims that the key is three files: Cuda.props Cuda.xml Cuda.targets, but it doesn't say how or where to add these files -- or rather I'll gamble that I just don't understand the notes referenced in the website.

    So does anyone know how to create a simple project in VS 2010 which builds a CUDA kernel -- using either the nSight 1.5 setup or the NvCudaRuntimeApi.v3.2.rules file which ships with the CUDA 3.2 RC?

    Thanks in advance! I'd offer a bounty, but I only have 65 points total.

    • John Dibling
      John Dibling over 13 years
      Comment rather than response since I don't know if this will help. You may find this link informative: forums.nvidia.com/index.php?showtopic=161096
    • M. Tibbits
      M. Tibbits over 13 years
      I had seen this, but I was hesitant to apply the patch to nSight 1.5 as this patch was last updated in May 2010. Has anyone used it on the newest RC?
  • M. Tibbits
    M. Tibbits over 13 years
    I'm accepting your answer, but as I've temporarily regressed to VS 2008 I haven't had a chance to confirm your answer. I'll do so over the weekend! Thanks so much!
  • Ade Miller
    Ade Miller over 13 years
    I did the same thing and only moved back to VS 2010 a few weeks back (for GTC). It should work let me know if you have issues.
  • Tom
    Tom over 12 years
    This information is no longer applicable, CUDA 4.0 and later support VS2010 natively (i.e. you no longer need VS2008).
  • Sumi Straessle
    Sumi Straessle almost 7 years
    Link is broken. Here's an archive from WayBackMachine