nV News Forums


nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   CUDA Tutorial (http://www.nvnews.net/vbulletin/showthread.php?t=167346)

ViN86 10-13-11 06:39 PM

CUDA Tutorial

I am not a programmer by trade, I am a novice. So if you have examples/ideas to include, let me know I will include them here. :)

This tutorial will assume a basic knowledge of C programming (variable types, for/while loops, pointers, etc.). If you are new to C, I recommend you check out this site.

1. Setup/installation
2. Intro and "Hello world" application

ViN86 10-14-11 03:05 PM

Re: Adventures in CUDA on Windows 7 x64

When I first started, I had many issues getting code to compile and getting CUDA setup. The runtimes are included in drivers from NVIDIA, but the CUDA SDK and toolkit needs to be downloaded.

You will need these files from the CUDA downloads page: http://developer.nvidia.com/cuda-toolkit-40

CUDA Toolkit (in 32-bit or 64-bit, if you use an express version you must use 32-bit)
CUDA Toolkit 4.0 Build Customization BUG Fix (follow instructions inside)
NVIDIA Parallel Nsight (recommended) from http://developer.nvidia.com/nvidia-parallel-nsight (you need to register)

Please note if you decide to use an express version of Visual Studio, you need to use the 32-bit CUDA Toolkit and SDK. For 64-bit, make sure you use the Full option when installing Visual Studio 2008. Furthermore, install the Windows 7 SDK from the following: http://www.microsoft.com/download/en...en&id=3138Also, make sure you follow their guide (link below) as you need the following environmental variables described below.

Hit Start, then right-click on Computer and select properties. Then click Advanced System Settings and select the Advanced tab. Hit the Environmental Variables button. Under User Variables, click New. For installation, check this PDF from NVIDIA: http://developer.download.nvidia.com...tedWindows.pdf. However, use the following variables.

Set the value to C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v4.0\bin

Set the value to C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include

Set the value to C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v4.0\lib

I recommend using Nsight from NVIDIA (you need to register to download it). But note that it is only compatible with Visual Studio 2008 SP1 and 2010 (not Express versions). You should be able to open project files that are located in C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\src. Please be sure you open the correct file for your respective version of Visual Studio (2008 or 2010).

You can test the samples they provide which are located in the C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\bin directory.

Now I wasn't able to get Visual Studio Express versions to recognize the CUDA project type. However, with Visual Studio 2008, I do have a CUDA project option, which creates a basic project you can edit to your liking.

The sample project demonstrates how to add a kernel and it should compile and run a simple matrix addition example. I will be learning more about CUDA as I go, so I will post examples/explanations I come across as I learn them. :)

It's a lot easier to use the command prompt to compile .cu files. Compiling is quite simple really. The NVIDIA CUDA Toolkit installs the nvcc compiler linker. It's compatible with Visual Studio 8.0 and 9.0 C compilers. If you have Visual Studio 2008 SP1, you can compile a file via the following:
  1. Click Start, then go to Programs, expand the Visual Studio tools folder in the Visual Studio 2008 folder, then open the Visual Studio 2008 x64 Command Prompt (this sets all the necessary variables needed by the compiler cl.exe)
  2. Navigate to your directory where your mysamplecudaprogram.cu file is located (obviously use your own file name, note that Tab auto complete makes life much easier when navigating)
  3. Type the following and hit enter: nvcc mycudaprogram.cu (you can also specify output options and pass other parameters to the compiler, including any include directories containing any headers you may need)
  4. Run the program (which by default is a.exe) by typing the following and pressing Enter: a.exe

This should output your program in the command prompt.

ViN86 10-18-11 12:50 AM

Re: CUDA Guide/Tutorials
1 Attachment(s)
Intro and "Hello world" application

If you're new to CUDA (like I am) you should definitely check out some books. A good starter is CUDA by Example. It goes through the basics and is a good introduction. They also provide sample code from the book and it is easy to follow. You can obtain that from here. I recommend you check out the samples as they are a good introduction. I will however use my own examples and will try to attach them to the posts so you can download/compile them. Without further ado, let's begin.

Regular C code and CUDA code work together seamlessly. You can use nvcc to compile regular C programs. For example, let's take regular C code and compile with nvcc.


#include <stdio.h>

int main(){
        // Say hello to the world
        printf("Hello, world!\n");
        return 0;


Compiling via nvcc helloworld.c should produce a program labeled a.exe. If you run this program, you are greeted with the following.


Hello, world!
Now we can see that the CUDA compiler recognizes regular C code as it should. Now let's look at a piece of code which actually uses the GPU, although we won't do anything just yet.


#include <stdio.h>

__global__ void gpuhw( void ){
        // Do nothing on the GPU... for now

int main(){
        // Call GPU function gpuhw, which does nothing
        // Call our printf statement
        printf("Hello, world!\n");
        return 0;


Here we see a few new things which we need to take note of. First, we include a new function above with the prefix __global__. This tells our compiler that this code can be called to run on the device (your GPU) from the host. Later we will see __device__ which notifies the compiler of functions that can be called by the GPU. Also, we see a call to this function gpuhw with some funny brackets after this. These numbers specify the number of blocks and threads, respectively, for the card to use. We will discuss later what this means.

Compiling the helloworldgpu.cu file and running the executable, should output the same thing.


Hello, world!
Congrats, you just ran your first CUDA code. That concludes this basic introduction. I recommend using .cu extensions for your CUDA code so you can keep them organized and separate from your C code, but obviously it's up to your preference.

In the future we will learn more about how to handle blocks and threads per block on the GPU. Stay tuned. :)

All times are GMT -5. The time now is 01:39 AM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.