Skip to content
July 30, 2012 / Karthikeyan Natarajan

Simple CUDA program

I have started learning CUDA for a while. Here i will describe how to create a simple CUDA program & compile and run.

I assume that you have setup your system with CUDA environment & GPU , drivers installed. (I will describe those steps in another post).

Here is the program> save as

__global__ void kernel( void )
int a=threadIdx.x;
int b=blockIdx.x;
int main( void ){
        printf(“Hello, World!\n”);
        return 0;
This is a very simple program which creates 2×5=10 threads in GPU. Each Thread will run the same code but with different Idx (threadIdx.x and blockIdx.x).

There are some basic rules in CUDA kernel.

1) CUDA kernel function does not return any value. It should be void.
2) Memory of GPU and CPU are physically different. Values can be transfered between them using Memory copy functions provided by CUDA.
3) Basically kernels are organized in two dimensions in blocks and grids. A Grid consists of number of blocks (Grid Dimension). A block consists of number of threads (Block Dimension). So, each thread in the kernel has unique threadIdx and blockIdx. Grid dimension can be 2D again (x and y). Block dimension can be 3D again( x,y,z).

This kernel function merely shows existence of unique variable threadIdx.x and blockIdx.x (since i used 1D for both Grid and Block dimensions).
Here the Grid dimension is 2. i.e, no of blocks = 2; Block dimension is 2. i.e, no of threads per block = 5.
Total 2 x 5 = 10 threads.

__global__ is to tell the compiler that kernel() function can be called anywhere from the whole CUDA file.

Compile: nvcc

Execute: ./a.out

This will invoke simple kernel function in your GPU.

To learn CUDA, you can start with “CUDA by Example” book. For more detailed study, you can look into “Programming Massively Parallel processors” book.


One Comment

Leave a Comment
  1. Arkapravo Bhaumik / Aug 3 2013 4:51 am

    Very nice blog ! I will use this as a reference when I start with CUDA.

Leave a Reply

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

You are commenting using your 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: