Friday, February 12, 2010

Integrating C++ and CUDA

In this post I will show you how to get CUDA set up so you are able to use C++ files along with .cu files in the same project. In the previous post, we set up the project so that we could compile filename.cu files with the CUDA compiler. Now, to build an C++/CUDA application using resources from the SDK you will need to also include the libraries from the SDK (both the toolkit and SDK).

Now lets see if we can integrate a .cpp into our project. Right-click on your project source folder and click add->new item to make a main.cpp file. After you add the .cpp file you will have to include the libraries to your project. This will add a C\C++ field in the project properties where you will need to include CUDA files.

main.cpp:
#include
int main(int argc, char** argv)
{  
     printf("Hello World!\n"); 
     int i; 
     std::cin >> i; 
}

When the CUDA SDK is installed it also adds environment variables. Because I am using a 64-bit machine I added additional path variables as discussed in the previous post. In order to use the lib (library) and inc files (header files) from the CUDA SDK you can you can either copy the files from the SDK folders to the toolkit folders such as: $(NVSDKCUDA_ROOT)\common\lib folder to $CUDA\lib or set up paths using the environment variables as follows. Here is how to do it using environment variables. 

When the SDK installs, there is an environment variable:

NVSDKCUDA_ROOT

which points to:

C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C

From the SDK you will need the library, include files, and binaries (cutil.dll, cutil32D.dll, etc). 

Project->Properties->C/C++->Additional Include Directories (field):
    $(NVSDKCUDA_ROOT)\common\inc
Project->Properties->Linker->General->Additional Library Directories (field):
    $(NVSDKCUDA_ROOT)\common\lib

Your project properties should look like:




Now all you need to do is include the additional input dependent libraries for the linker. For now, we will only include cudart.lib (we will need other libraries later for the linker but for now we are taking it one step at a time). 


But in order to compile you will need to include the binaries (dlls) for the linker . To do this I created added the both the debug and release binaries for 32-bit which is my target to the PATH environment variable.
On windows 7:

start->right-click on computer->properties->Advanced system settings->Environment Variables->PATH
and add to the end:
    ;%NVSDKCUDA_ROOT%\bin\Win32\Debug
    ;%NVSDKCUDA_ROOT%\bin\Win32\Release
 Note the semi-colon. There is no semi-colon at the end of the path. The above point to:


    C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\bin\Win32\Debug
    C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\bin\Win32\Release


 Now you need to restart your system for the SDK dlls to be available to your system.

No comments:

Post a Comment