How to add Thrust (GPU parallel computing) to your Visual Studio project
This tutorial assumes you know your way around Visual Studio’s options and have the ability to Google any errors you encounter.
First things first: what is GPU parallel computing? The basic idea was developed by NVIDIA (the graphics card manufacturer) and it means: get large chunk of data -> perform operation -> get output chunk. They called it CUDA. Other manufacturers have released similar systems, but we’re going to focus on CUDA today.
In the world of computing in which we live, we’re only using half our computer for rendering: the CPU. The GPU is perfectly capable of doing calculations, especially when it’s not doing anything else (for instance in raytracing).
We’re going to use Thrust 1.1, which is a wrapper to CUDA, giving it similar functionality to Boost. In fact, if you’re familiar with Boost, you’ll have no trouble adapting to Thrust. Thrust is open source and and freely available at Google Code.
Keep in mind that CUDA will only work on newer NVIDIA hardware. In fact, you might have hardware that supports CUDA, but NVIDIA simply hasn’t released drivers for it.
Finally, every single programming teacher I asked thought this was a bad idea, because GPU’s are going to fade out and everybody’s going to focus on the CPU again.
Alright, everybody clear? Let’s do this.
1. Compatibility and drivers
Go get the latest drivers for your videocard. And even then, even though you just updated your videocard (NVIDIA 9600M GT in my case), the drivers may not support the latest version of CUDA! I had to go to http://laptopvideo2go.com/ and get experimental drivers to get it to work. Why don’t you release those drivers for every one of your cards, NVIDIA?
2. Downloads
Download and install the NVIDIA driver with CUDA support, the CUDA Toolkit and the CUDA SDK
Download and install the CUDA Visual Studio Wizard, which gives you a new option in Visual Studio’s new projects (CUDAWinApp) and, more importantly, a set of build rules.
Download Thrust 1.1 from the Google Code site. Unpack the zip and copy the “thrust” folder to the include folder in the CUDA Toolkit. For instance: F:\Programs\CUDA\include.
3. Setting up your project
First, you will need some dependencies:
Go to the lib folder of the CUDA Toolkit (F:\Programs\CUDA\lib) and copy “cudart.lib” to your project’s lib folder in a folder labeled “CUDA”. (F:\Files\C++\Raytracer\lib\CUDA). Go to the bin folder and copy “cudart.dll” to your project’s release folder (F:\Files\C++\Raytracer\output).
Do the same for “cutil32.lib”, which can be found in the CUDA SDK folder. (F:\Programs\NVIDIA GPU Computing SDK\C\common\lib).
While you’re there, also grab a copy of “cutil32.dll” and copy it to your project’s release folder (F:\Files\C++\Raytracer\output).
Go back to Visual Studio.
Right click on the project (Solution “Raytracer” -> Project “Main”) and select “Custom Build Rules”. At the top should be an option labeled “CUDA”. If it doesn’t show up, close Visual Studio and reload your project. Enable it.
Go to the properties page of your project. In Configuration Options -> Linker -> Input, add “cudart.lib cutil32.lib”.
Go to Options -> Linker -> General and add “lib\CUDA” to the Additional Library Dependencies.
Now, at the bottom of the properties page, a new option has appeared labeled “CUDA”. Go to CUDA -> Backends and add “/MD” to Compiler Options. This will prevent a linker error when compiling later on.
Go to CUDA -> Output File Name and enter a obj name, for instance “$(IntDir)\cuda.obj”
Alright, everything is set up! Time to start coding! But wait… there’s a catch.
4. Enabling syntax highlighting
CUDA doesn’t compile from .cpp files, it compiles from .cu files. And those don’t have syntax highlighting enabled by default. But we can fix that. Go to Visual Studio’s program folder and then Common7 -> IDE (F:\Programs\Microsoft Visual Studio 9.0\Common7\IDE).
Find a file labeled “usertype.dat”. If it doesn’t exist, create it.
Append the following: usertype.dat (1 kb)
Restart Visual Studio and you will have syntax highlighting in .cu files.
5. Examples
Create two new files in your project: “example.cu” and “example.h”.
Go to example.cu’s options and select CUDA in Configuration Options -> General -> Tool.
Now EVERYTHING is set up and we can start coding.
In example.cu, add the following:
#include <thrust/version.h>
#include <thrust/thrust_version.h>
#include <iostream>
#include "example.h"
void HelloWorld()
{
printf("Hello World!\nThrust version: %i.%i", THRUST_MAJOR_VERSION, THRUST_MINOR_VERSION);
// load some stuff into a host vector
thrust::host_vector hostA(4);
// add data
hostA[0] = "1.f";
hostA[1] = "23.2f";
hostA[2] = "3.14f";
hostA[3] = "666.f";
}
This works pretty much like a .cpp file, and it needs a header:
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_
extern "C" void HelloWorld();
#endif
Treat the header the same way you would a class definition, except with the syntax outlined above.
In your main cpp, include example.h.
Now you can call “HelloWorld()” and it will display the following:
Hello World!
Thrust version: 1.1
I can do GPU computing mate. :)
6. Done
For an introduction to Thrust, start with the tutorial pages.
Happy coding. :D







