miércoles, 1 de diciembre de 2010

Tutorial CUDA 3.2 and Visual Studio 2008: The first cuda program!

This brief tutorial I will compile a simple CUDA example using Visual Studio 2010 express.

First Step :
I assume you already have Visual Studio 2010 installed

Problema with VS2010 : "nvcc fatal : nvcc cannot find a supported cl version. Only MSVC 8.0 and MSVC 9.0 are supported"

Since CUDA dont supports it, you need to install visual express c++ 2008 so CUDA cound find the  MSVC 9.0 ( NVCC says it supports only MSVC 8.0 and 9.0 compilers)

Download Visual Studio Express 2008

Second Step :

You will have to install latest nVidia CUDA Driver for your Graphics Hardware which supports desired CUDA version. Then you will have to install the CUDA Toolkit which includes the CUDA Compiler, Include file, lib file and binary files to develop your CUDA application.

Third Step :

In you VS project Tools->Options... , in VC++ directories add "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\include" or where you installed the cude toolkit.

Fouth Step :
Copy *.rules from "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\extras\visual_studio_integration\rules" to "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCProjectDefaults"

This  "teaches" Visual Studio how to compile and link any .cu files in your project into your application.

  • Add the NvCudaRuntime.rules (right click on the project, Custom Build Rules, tick the relevant box)
  • Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add $(CUDA_PATH)\lib\$(PlatformName) to the Additional Library Directories 
  • In Linker -> Input add cudart.lib to the Additional Dependencies), see notes [2] and [3]
  • Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then inC/C++ -> General add $(CUDA_PATH)\include to the Additional Include Directories)
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically

Fifth Step:
"Hello world" test code :

// This is the REAL "hello world" for CUDA!
// It takes the string "Hello ", prints it, then passes it to CUDA with an array
// of offsets. Then the offsets are added in parallel to produce the string "World!"
// By Ingemar Ragnemalm 2010
#include "stdafx.h"
const int N = 16; 
const int blocksize = 16; 
void hello(char *a, int *b) 
	a[threadIdx.x] += b[threadIdx.x];
int _tmain(int argc, _TCHAR* argv[])
	char a[N] = "Hello \0\0\0\0\0\0";
	int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	char *ad;
	int *bd;
	const int csize = N*sizeof(char);
	const int isize = N*sizeof(int);
	printf("%s", a);
	cudaMalloc( (void**)&ad, csize ); 
	cudaMalloc( (void**)&bd, isize ); 
	cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice ); 
	cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice ); 
	dim3 dimBlock( blocksize, 1 );
	dim3 dimGrid( 1, 1 );
	hello<<>>(ad, bd);
	cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost ); 
	cudaFree( ad );
	printf("%s\n", a);

Google Analytics Alternative