Home > Hardware-Architecture, Programming > Run CUDA C program with Visual Studio 2008

Run CUDA C program with Visual Studio 2008

CUDA is NVIDIA’s parallel computing architecture. It enables dramatic increases in computing performance by harnessing the power of the GPU.

With millions of CUDA-enabled GPUs sold to date, software developers, scientists and researchers are finding broad-ranging uses for CUDA, including image and video processing, computational biology and chemistry, fluid dynamics simulation, CT image reconstruction, seismic analysis, ray tracing, and much more. [1]

To run CUDA program, you need to make sure that an Nvidia GPU has been attached to your computer. It’s GeForce 9800 GTX+ in my case. The CUDA Toolkit, which can be found here, should also be installed (all CUDA Toolkit, developer driver and GPU Computing SDK).

It’s time to create a new CUDA project, click File –>New –>Project like this:

Add a CUDA rule then:

Goto Tools –> Options –> Projects and Solutions –> VC++ Directories and make the following config:

Open Project –> Properties –> Configuration Properties –> Linker, add things like this:

Now everything is done. You just create a CUDA file(.cu) with the following content[2]:

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

int num_elements = 16;

int num_bytes = num_elements * sizeof(int);

int *device_array = 0;

int *host_array = 0;

// malloc host memory

host_array = (int*)malloc(num_bytes);

// cudaMalloc device memory

cudaMalloc((void**)&device_array, num_bytes);

// zero out the device array with cudaMemset

cudaMemset(device_array, 0, num_bytes);

// copy the contents of the device array to the host

cudaMemcpy(host_array, device_array, num_bytes, cudaMemcpyDeviceToHost);

// print out the result element by element

for(int i = 0; i < num_elements; ++i)

printf(“%d “, host_array[i]);

// use free to deallocate the host array

free(host_array);

// use cudaFree to deallocate the device array

cudaFree(device_array);

return 0;

}

Build and run the program, you’ll get this result:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

References:

[1] http://developer.nvidia.com/what-cuda

[2] http://code.google.com/p/stanford-cs193g-sp2010/wiki/TutorialAHeterogeneousProgrammingModel

  1. March 23, 2013 at 3:51 PM

    I’m curious to find out what blog platform you happen to be utilizing? I’m experiencing some small security
    issues with my latest website and I would like to find something more safeguarded.
    Do you have any recommendations?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: