Getting CUDA 3.1 Working with Visual Studio 2010

Monday, August 30, 2010 – 7:54 PM

With the release of CUDA 3.2 RC and Nsight 1.5 this blog post is out of date. Do yourself a favor and get the latest bits and save a lot of hassle.

I spent a few hours today working out how to fix it so that you can compile a CUDA 3.1 project from within VS 2010 using the VS 2008 compiler. There are a couple of blog posts and forum entries they give you most of the detail (see The Details section below if you’re really interested).

cuda_proprtiesThe end result is that the following three files will fix these issues:

Replace your existing files with these and you should be able to build your CUDA project using the custom build rule from VS 2010 provided you have an earlier version of the compiler installed on the machine. This could either be from VS 2005 or 2008 or the appropriate Windows SDK.

The “Path “ field specifies the VcCompilerPath property. This should point to the root directory of the C++ install. It expects to find $(VcCompilerPath)\bin\cl.exe and $(VcCompilerPath)\vcvarsall.bat.

Currently the original dropdown menu items have been replaced with numerical indexes. This isn’t ideal but I’m hoping for a release from Nvidia soon.

The Details

In case you really care… There are essentially a couple of hurdles here. Firstly the upgrade tool for VS 2010 breaks the CUDA custom rules file. You can read more about that here and here. Basically “MSBuild does not support expanding environment variables if they are part of the switches when generating tasks”. Which means that EnumValue Switch attributes defined in the Cuda.xml file and that contain properties do not get expanded in the Cuda.targets file. You can fix it by replacing the EnumProperty with a StringProperty and adding a new ItemGroup to the targets file. This applies to the NvccCompilation, CompilerPath and compileout properties.

The next problem is that the CUDA compiler, NVCC, takes a dependency the Visual C++ compiler, CL.EXE, and expects to find it at $(VCINSTALLDIR)\bin. If you are running in VS 2010 this will point to the newer 10.0 compiler and that’s not supported. The simplest solution to this is to add another custom build property which allows the user to specify the path to the version of CL.EXE

See here for a nasty bug which crashes the build unless the Switch="[value]" attributes contain a trailing whitespace.

Cuda.props – Adds a VcCompilerPath property, modified CommandLineTemplate property.

Cuda.xml – Replaces the NvccCompilation, CompilerPath and compileout EnumProperties with StringProperties.

Cuda.targets – Contains ItemGroups to set the correct values for NvccCompilation, compilerPath, compileout and Outputs with property expansion. Adds the VcCompilerPath attribute to the CUDA_Build_Rule.

Each of the changes in clearly commented in the code. Each comment starts “VS2010: “.

  1. 19 Responses to “Getting CUDA 3.1 Working with Visual Studio 2010”

  2. Thanks ,

    the nvcc of cuda 3.1 are only supported 8.0 or 9.0 ;

    By Bing on Sep 12, 2010

  3. Thanks!

    I have created the project with number of warnings like
    ——————————-
    Warning 1 The element ‘ItemGroup’ in namespace ‘http://schemas.microsoft.com/developer/msbuild/2003’ has invalid child element ‘PropertyPageSchema’ in namespace ‘http://schemas.microsoft.com/developer/msbuild/2003’. List of possible elements expected: ‘Item, Reference, COMReference, COMFileReference, Xdcmake, Bscmake, ClCompile, ClInclude, Midl, ResourceCompile, PreLinkEvent, CustomBuildStep, Manifest, ProjectConfiguration, NativeReference, ProjectReference, Compile, EmbeddedResource, Content, Page, Resource, ApplicationDefinition, None, BaseApplicationManifest, Folder, Import, Service, WebReferences, WebReferenceUrl, FileAssociation, BootstrapperFile, PublishFile, CodeAnalysisDependentAssemblyPaths, CodeAnalysisDictionary, CodeAnalysisImport, Link, ResourceCompile, PreBuildEvent, PostBuildEvent’ in namespace ‘http://schemas.microsoft.com/developer/msbuild/2003’. C:\work\CUDATest\CUDATest\Cuda.targets 4 6 Miscellaneous Files
    ——————————-

    And I getting “unresolved external ***” errors while compiling .cu files.

    Had I missed something the configuration?

    Kind regards,
    Andrei

    By Andrei on Sep 21, 2010

  4. Andrei,

    NVidia just released CUDA 3.2 SDK which supports VS 2010 (although you still need the VC++ 9 compiler installed). I would skip my hacked up solution and get 3.2. I’ve been using it for a few days and it seems to be working great.

    Ade

    By Ade Miller on Sep 22, 2010

  5. Hi,

    is it possible to upload those updated targets, props and xml files? seems that i am unable to find them in the toolkit or the sdk. or are those files only in nsight 1.5 to which i don’t have access to?

    i searched the whole internet for those damn files..
    would be a really nice move :-)

    greetings from germany

    By ConfusedGuy on Oct 14, 2010

  6. They got deleted from the tip when I moved to CUDA 3.2 SDK with VS 2010 support. I really recommend you do the same thing. My solution was a band aid, whereas 3.2 gives you full CUDA support on 2010.

    I’ve updated the file links but I regard this as largely an example of how to play with custom build rules and not a supported way of using CUDA with VS 2010.

    Thanks,

    Ade

    By Ade Miller on Oct 15, 2010

  7. Hi,
    seems that i was not clear enough on my post. iam looking for those files which you said are supplied by the sdk because i don’t find them.

    regards :-)

    By ConfusedGuy on Oct 18, 2010

  8. hi,

    forget my last post nsight 1.5 is now available for public and the files are included there.

    But thanks :-)

    By ConfusedGuy on Oct 19, 2010

  9. Hi Ade Miller,

    I came across your page via google search trying to understand just how VS 2010, nSight 1.5 and CUDA3.2 get along.

    I had to install VS2008 just to get the 9.0 compiler error to go away, and now the command line ‘nvcc’ command seems to work fine.

    I can’t seem to get the same done through VS 2010. Could you help me out as to how I can configure the Studio to run CUDA programs? (nSight is installed).

    Thanks,
    Best,
    Suraj

    By Suraj on Oct 28, 2010

  10. Suraj,

    Yes, you need VS 2008 installed because the CUDA project in VS 2010 actually builds against the 2008 compiler.

    I’ll be writing a blog post about this soon hopefully but in the meantime you need to set the project to use the v90 Platform Toolset. This is in the Project Properties | Configuration Properties | General tab.

    Ade

    By Ade Miller on Oct 28, 2010

  11. Hi again!

    I tried that. I made an empty (also tried it with a console application) app and added my .cu file, changed the build optimizations to include cuda 3.2 for targets, changed the .cu file’s itemtype to CUDA and also changed the compiler settings to v90. There are errors marked around every CUDA specific keyword like __global__ , blockIdx. This exact same program works via command line.

    Do you think the order in which I installed the studio, sdk and nsight would matter?

    Looking forward to your post on VS2010 and Cuda 3.2. (this is difficult because I am not used to the Visual Studio environment and never used VC++ before.)

    Thanks,
    Best,
    Suraj

    By Suraj on Oct 31, 2010

  12. Hi Ade,
    Interesting post. Looks like everyone is having a problem with this. Let me throw in more confusion into the fray. I have a 64-bit Windows 7 Machine. How can I get Visual Studio 2010 to co-exist with CUDA 3.2 RC. Getting VS 2008 to install on a Windows 7 machine is a nightmare all to itself. And even when it does it forces user to upgrade to 2010 (by prompting on startup and exiting application).

    With the above rules files, vs 2010 was still complaining about the formatting on the command line. I had to manually type it in a cmd window to get the error “Need 9.0 Compiler”.

    -Alex

    By Alex Pandian on Nov 1, 2010

  13. Alex,

    I’m not sure what th issue is with installing VS 2008 on Win7. I’m running VS 2008 and 2010 on Win7 x64 without any issues. The CUDA SDK 6.2 RC comes with newer rules files you should use these not mine. I’d also recommend using the NVIDIA Nsight toolset rather than the SDK as it gives you debugging etc.

    Ade

    By Ade Miller on Nov 3, 2010

  14. Are you planning to update your blog for SDK 3.2? I have been unable to make it work. Thanks in advance for all the info you provide.

    By Kibara on Nov 4, 2010

  15. Hi Ade
    I am sorry for asking the same question. My problem is I have notebook version graphic card (Quadro NVS 3100M)this is not supporting to Paralle Nsight 1.5 yet.
    But I tried to run some example cuda code in command front it give the same error message like:
    “nvcc fatal: nvcc can not find a supported cl version. Only MSVC 8.0 and MSVC 9.0 are supported”
    My system is with Win7 64bit,VC++2010 also i installed VC++ 2008 compiler too.
    I am really very new to C++ and visual studio!!!! Should I have to any setting to run cuda in command front???
    I kindly wait for your answers!!
    Greets
    vishva

    By vishva on Nov 14, 2010

  16. Vishva,

    You should be able to get this to work if you set install the 3.2 SDK and it’s custom build rules and then set the Platform Toolset property in the general settings to “v90” to tell VS to build with the VS 2008 compiler.

    I’m going to write a blog post on this but haven’t had the time to do so yet.

    Ade

    By Ade Miller on Nov 15, 2010

  17. Hi.

    Could someone point me to a guide which helps me configure Visual Studio 2010 so that it can compile CUDA C code? I am a beginner to Visual Studio 2010…

    Thanks.

    Vivek

    By Vivek Saxena on Jan 21, 2011

  18. You’ll find a complete guide here:

    http://blog.cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/

    By Jawad on Feb 24, 2011

  19. Thanks Jawad,

    This looks really useful!

    Ade

    By Ade Miller on Mar 1, 2011

  1. 1 Trackback(s)

  2. Sep 1, 2010: Dew Drop – September 1, 2010 | Alvin Ashcraft's Morning Dew

Sorry, comments for this entry are closed at this time.