GPU Parallel Programming in VS2012 with NVIDIA CUDA
Posted by Hemprasad Y. Badgujar on March 4, 2013
Here I will share to you my first experience in creating a CUDA-based C++ program on Windows using Visual Studio 2012. CUDA is an acronym of Compute Unified Device Architecture, which is NVIDIA’s general purpose computing API for their graphics card hardware. This simple program is taken from the example code of NVIDIA’s samples, which is basically doing fill and copy operation with a big size matrix. Before continuing, you should have installed the required CUDA drivers, toolkits and SDK from here:
Or, if you’d rather choose to install the latest CUDA toolkit, head over here:
You should also have a working C++ compiler. I am using Visual Studio 2012 on Windows 8 64-bit. Please be advised that CUDA-based applications won’t run unless the appropriate NVIDIA GPU hardware supporting CUDA is present in your system.
2. Setting up Visual Studio 2012
Basically everything should be set up automatically by the installer. However, with the current release of CUDA version 5.0, you might not be able to compile/build your project successfully. This is because nvcc.exe does not currently support the new cl.exe compiler version. If you try to compile any samples from the SDK there will be errors about target and props file not found or missing. For this, you should manually deploy those files according to the instructions from “C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v5.0\extras\visual_studio_integration”
Those files still need some modifications for a successful compilation. You can download the modified files here: BuildCustomizations.rar. Extract the contents to the folder “C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\”.
If you prefer to modify the files manually, follow these instructions carefully:
- Copy all the build customization files somewhere
- Open “CUDA 5.0.props”. Search for the following lines:
"'$(PlatformToolset)' == 'v90'"
"'$(PlatformToolset)' == 'v100'"
and add this new line:
"'$(PlatformToolset)' == 'v110'"
- Open “CUDA 5.0.targets”. Search for the text “CudaCleanDependsOn” and replace the tag content with these lines:
- In the same file, search for “GenerateRelocatableDeviceCode”. Replace the line with the following:
- Go down a bit and look for “CodeGeneration”. Replace the line with this:
- Again search for “CommandLineTemplate”. It should be somewhere near the end of the file. Replace the line with this:
CommandLineTemplate=""$(CudaToolkitNvccPath)" %(CudaCompile.BuildCommandLineTemplate) %(CudaCompile.ApiCommandLineTemplate) %(CudaCompile.CleanCommandLineTemplate)" />
- Copy all modified files here: “C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\”
Also, modify line 90 of the file “host_config.h” located in the folder:
“C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include\”
by changing the value ’1600′ to ’1700′.
Note: Remove the ‘x86′ inside paths if you use 64-bit CUDA toolkit
To have a fancy C++ syntax highlighting feature enabled, follow these steps:
- Select the menu “Tools->Options…”. Open “Text Editor” in the tree view on the left, and click on “File Extension”.
- Type “cu” in the “Extension” box, set the editor to “Microsoft Visual C++” and click “Add”. Click “OK” on the dialog box.
- Restart Visual Studio and your CUDA code should now have syntax highlighting.
3. Creating the App
Make sure you have installed all required SDKs. If everything is ok, then start by creating a simple console project and type this code:
Before compiling, make a reference to the CUDA library by specifying its location and name in the project’s properties page:
- Navigate to the “Configuration Properties\Linker\General” option
- In the “Additional Library Directories” field, add “$(CUDA_PATH)lib\$(PlatformName)”
- Go to the “Configuration Properties\Linker\Input” option
- Lastly in the “Additional Dependencies” field, add “cudart.lib”
The code should compile successfully.