Tuesday, May 12, 2009

bv2.co.uk » Blog Archive » Visual Studio 2008 and CUDA

CUDA with Visual Studio 2008

bv2.co.uk » Blog Archive » Visual Studio 2008 and CUDA

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.

No comments: