Wednesday 29 April 2009

Visual Studio 2008 and CUDA

As promised I received my Visual Studio 2008 on Friday from polyhedron and set about installing it on Saturday. This post will describe how to get VS 2008 to work with CUDA.  I use CUDA 2.2 which is still under NDA so I wont be making any comments about its performance improvements today. Rather I will describe how to set up syntax highlighting, building and Intellisense.



Visual Studio is now distributed in DVD format which makes the installation process much easier due to not having to swop disks every few minutes. The installation went smoothly and quickly as I had the prerequisites (.net etc) installed already. I usually install software on my D drive and it didn't have any complaints or issues with that either.

I already had the CUDA 2.2 SDK and toolkit installed along with the 185 drivers so I could move directly to configuring my Visual Studio to use CUDA:

Syntax highlighting:  By default a .cu file is displayed all in black which makes editing rather tricky. NVidia supply a usertype.dat file which you will find under: "NVIDIA Corporation\NVIDIA CUDA SDK\doc\syntax_highlighting\visual_studio_8"  copy this file to: "\Program Files\Microsoft Visual Studio 9.0\Common7\IDE".  Now open up Visual Studio and navigate to:  tools->options->text editor->file extension.    In the extension box type "cu", make sure you select "Microsoft Visual C++" in the drop down and not the default of "Microsoft Visual C#".  If you do select the C# one they syntax highlighting will still work but you wont see all the "__syncthreads()" etc highlighted.

Building: This is rather simple to configure, you can either specify the standard "nvcc " command line for each .cu file in your project by right clicking ->properties->custom build.  A better solution is to select a custom build rule for your project by selecting "project -> custom build rules -> find existing".  Again NVidia supply a rules file which you will find under the "\NVIDIA Corporation\NVIDIA CUDA SDK\common" directory.  Select this build rule in your project properties and it makes it much easier to configure options for your building of CUDA projects.

Intellisense: Getting intellisense to work on .cu and .cuh files proved to be the most tricky step. I don't actually use it very much as I try and keep my code fairly simple but its still nice to have when you can't remember parameters etc. Logically I thought by adding .cu to the file extensions lists in the options wherever there was .c,.cpp etc listed and by the step used in enabling syntax highlighting it would just magically work. Sadly this was not the case. I did find a suggestion on the web mentioning a process that would enable it but it seemed rather clumsy to me. It involves writing your CUDA code in a .c file and then just as you hit compile a pre-build step copies the .c file to a .cu extension.  To me it seemed odd that the Intellisense would work with .c,.cpp,.h etc and not with .cu where it is clearly just a different extension but essentially C code. As I ran out of option dialogs in Visual Studio I decided to look in the registry. After about 15 minutes I found what turned out to be the key. The "fix" is really simple: open the registry editor and navigate to "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++" then go to the string entry "NCB Default C/C++ Extensions" and add ";.cu;.cuh" to the end of the list. Restart Visual Studio and Intellisense will work for your CUDA files. Keep in mind it doesnt know about CUDA specific extensions but will still help with all the normal C bits.

20 comments:

  1. Thanks! Very nice article. I didn't know about the possibility to make Intellisence work! But my processor directives now are not visible. if i define directive in cuda properties for the project, it is not visible in the file and marked as disabledm but compiles correctly.

    ReplyDelete
  2. Thanks for your comment.

    I've noticed the behaviour with the directives too but am not sure if it is just in cuda projects as I've had something similar happen with a .c file in a .cpp project. As yet I havent had much of a chance to work out why - if you find out please let me know :)

    /Barrett

    ReplyDelete
  3. Thank you. This is very good instruction materials.

    ReplyDelete
  4. Hi,

    Thanks for posting usefull information such as this. However I can't seem to be able to set the custom build like you describe it. So with Visual Studio 9, I don't see this option: 'roject -> custom build rules -> find existing'. It doesn't help that my version is in French, but I see the custom build rules in the CUDA SDK projects so I should be able to do the same. Could you help me with this?

    Thanks so much!
    Pascale

    ReplyDelete
  5. Hi Pascale,

    Have you tried right clicking on the project name under the solution name in the Solution Explorer (left pane on my visual studio). The 5th item down is "Custom Build Rules" which should get you to the same screen as the other method.

    I have no idea why yours isn't showing under the project menu, I would have thought the French one would be the same just translated...

    Let me know if this helps - else I can have a look in the xml of one of the projects with the build rules set already.

    /Barrett

    ReplyDelete
  6. Great!!! Thanks Barrett. I was looking only under the .cu file properties and now find it under my dll project name. So in French it's: Règles de génération personnalisée. Thanks for answering so quickly!

    Pascale

    ReplyDelete
  7. Hi Pascale,

    No problem - glad its working for you :)
    Thanks for sharing the place to find it on the French menus - hopefully it will benefit someone else.

    /Barrett

    ReplyDelete
  8. Thanks - fantastic post!
    Saved me a lot of time.

    ReplyDelete
  9. Hi!
    Thanks for this post!
    I followed all your instructions carefully and now both Syntax highlighting and Intellisense are working.

    However, I'm still not able to build CUDA projects in VS 2008.
    I get the error message:
    fatal error LNK1181: cannot open input file 'cutil32D.lib'

    I looked for this library and found it at C:\Documents and Settings\All Users\Dados de aplicativos\NVIDIA Corporation\NVIDIA CUDA SDK\common\lib

    As there are so many spaces in this path, I wonder how I could make it work in VS.

    I'm running CUDA 2.3 on Windows XP 32bit.

    I wonder if you could help me with this.. :)

    Thank you!

    ReplyDelete
  10. Hi Marcos,

    As you've noticed it can't link against the missing lib. The best solution would be to put quotation marks around your lib path for example: ""

    This should work.
    I do set environment variables for my paths - just so its easy to change without having to redo every path in every project.
    Once you have set an environment var use it in visual studio settings like this:
    "$(yourvar)\LIBDIR\whateverlib.lib"

    This gives great flexibility, especially if you build on a different machine to your dev one.

    /Barrett

    ReplyDelete
  11. Hello
    I'm totally new to cuda. I like this article. However I cant get nvcc to work with visual studio. I did setup custom build rules for the project and it shows up but I am missing some other settings I think. I get cutil.h missing errors mostly.
    I can do everything from the command shell.

    thanks

    ReplyDelete
  12. Since you are working on visual studio 2003 ,
    Please guide me to configure visual studio 2003 with cuda as for some application i need to use vs2003.

    Thanks in advance.

    ReplyDelete
  13. Hey !! Thanks for ur gr8 post!! I have installed Visual C++ Express edition with the CUDA 3.0 toolkit and CUDA 2.3 sdk.
    Thanks for the gr8 article...It really saved me lot of time. I am stuck in a problem , if I copy cuda kernel in .cpp file it says that the commands are undefined as expected. If I rename the file as .cu then it says following error

    1>NVCC Compiling...
    1>practice.cu
    1>c1xx : fatal error C1083: Cannot open source file: 'practice.cu': No such file or directory
    1>Project : error PRJ0019: A tool returned an error code from "NVCC Compiling..."
    1>Build log was saved at "file://c:\Users\ankit\Documents\Visual Studio 2008\Projects\ankit\ankit\Debug\BuildLog.htm"
    1>ankit - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I m using Visual C++ 2008 express edition.
    Also when I click on Properties>Configuration and enable emulatiion mode and then when I compile I get the following statement ">NOTE: device emulation mode is deprecated in this release
    and will be removed in a future release."

    I m really stuck with this. Pls Help!! thanks
    Also I was not able to use the intellisense as you had written coz I didnt get the following directory HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\
    Instead there was Download instead of Languages

    Ankit

    ReplyDelete
  14. Salman,

    You probably need to set your path to the CUDA toolkit in the include file settings and linker settings of your visual studio project. Its probably best to use an environment variable to do this. On my machine the CUDA includes live in C:\CUDA\include
    This is not the same dir where the sdk examples are installed to in the newer releases.

    Keep in mind if you are using 32bit or 64bit you need to point your project to the correct version of the includes/libs.

    /Barrett

    ReplyDelete
  15. Aman,

    I currently only have vs2008 installed. But the installation of CUDA into vs2003 should be similar with the possible exception of the custom build rules.
    You may need to set the .cu's to be excluded from the build and then compile the project with nvcc.

    Sorry this is not terribly helpful but I havent used vs2003 in a while.

    /Barrett

    ReplyDelete
  16. Hi Ankit,

    I'm glad the original article helped.

    It may be a good idea to install the same version of the toolkit and SDK as I think some of the new examples depend on the new toolkit.

    Does your practice.cu file have a custom build rule associated with it in your project? I've also found that if you include a .cu file from another one make sure the included one if excluded from the build - nvcc will read and compile it.

    I've only got the PRJ0019 error when one of my paths was incorrect. Or possibly missing "" around it.

    Lastly emulation mode is being deprecated (removed) in future releases. This does make testing on windows a bit different but then I suppose NVidia wants everyone to switch to Nexus ( why win7 only?? ). Another good reason could be that the emulator and non-emulator usually produce different outputs and led many a developer to go on a wild goose chase.

    By the way printf does work from cuda code now - so useful for debugging if you don't have Nexus.

    Hope this help.
    /Barrett

    ReplyDelete
  17. hello ,

    i`m new to cuda , but i`m trying to learn some new stuff ,
    i found this post and i followed the steps , but my text isn`t highlighted .

    i am using ms visual studio 2008 prof . i ve installed the gpu driver (gt 320M) , the cuda toolkit (3.1 i think) , the cuda sdk and ran the bandwidthTest which worked and also worked to build and run the bandwidthTest project, too , but the bandwidthTest.cu text in visual studio is still all black ... even after i tried doing what you said right here :

    - copied the usertype.dat file to \IDE folder
    - added the .cu extension (...and selected the C++ in the drop down menu)
    - selected the CUDA rules from the Custom Build Rules...
    - and done the thing with the regedit , even though that is for the intellisense

    if i missed something , please point it out for me ...

    thanks in advance .

    //adrian

    ReplyDelete
  18. Thank you very much , this article is great .

    ReplyDelete
  19. Hello. I have the following error:

    Here is the output window after compiling in VS2008

    Compiling with CUDA Build Rule...
    "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin\nvcc.exe" -arch sm_10 -ccbin "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin" -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MTd " -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\include" -I"C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 3.2\C\common\inc" -maxrregcount=32 --compile -o "Debug\CUDA1.cu.obj" "c:\Users\xxxxname\Documents\Project1\Project1\CUDA1.cu"
    nvcc fatal : Failed to open output file 'C:\Users\xxxxname\AppData\Local\TempC:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\Bin\win32\Debug;C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\Bin\win32\EmuDebug;:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\Bin\win32\Release;C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\Bin\win32\EmuRelease/tmpxft_00001200_00000000'
    Internal error
    Linking...
    LINK : fatal error LNK1181: cannot open input file '.\Debug\CUDA1.cu.obj'

    Thank you.

    ReplyDelete
  20. I can’t find this path “HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++” in my system.
    I can only reach upto “HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0″ and the next folder in the tree is DOWNLOADS. not LANGUAGES. can someone please help?

    ReplyDelete