Showing posts with label CUDA. Show all posts
Showing posts with label CUDA. Show all posts

Monday, March 18, 2013

Intro to CUDA programming

I have already posted one blog on basic introduction to CUDA in http://itsitrc.blogspot.in/2013/01/compute-unified-device-architecture-cuda.html. Now in this blog we will be discussing about basic concepts in CUDA programming. Firstly we have to study some of  identifiers/keywords in CUDA:
  1. __global__ :-  The function or variable defined as __global__ is executed in GPU but can be called from host(CPU).
  2. __shared__:- The variable defined as __shared__ is resides in shared memory of threads.   
  3. __host__:- The function or variable defined as __host__ is stored on host memory and executed in only host.
  4. __device__:- The function or variable defined as __device__is stored on device(GPU) memory and runs on device only.
  5. __constant__:- The variable defined as __constant__ is same as in C programming.
  6. function<<<n,m>>>:- This syntax is responsible for calling kernel by creating function. here n is number of threads to be created and m is the number of thread blocks.
  7. cudamalloc():- This function allows us to allocate memory in device.
  8. cudafree():- This function is responsible for releasing memory occupied by using cudamalloc().
  9. cudamemcpy():- This function is responsible for coping data from device memory to cpu memory and vice-vars.    
Example:-

__global__ void add(int *a, int *b, int *c)
{
*c = *a + *b;
}
int main(void)
{
int a, b, c;                                             // host copies of a, b, c
int *d_a, *d_b, *d_c;                             // device copies of a, b, c
int size = sizeof(int);
                                                             // Allocate space for device copies of a, b, c
cudaMalloc((void **)&d_a, size);
cudaMalloc((void **)&d_b, size);
cudaMalloc((void **)&d_c, size);
                                                             // Setup input values
a = 2;
b = 7;
                                                             // Copy inputs to device
cudamemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
cudamemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
                                                             // Launch add() kernel on GPU
add<<<1,1>>>(d_a, d_b, d_c);
                                                             // Copy result back to host
cudamemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);
                                                             // Cleanup
cudafree(d_a); cudaFree(d_b); cudaFree(d_c);
return 0;
}

Sunday, January 6, 2013

Compute Unified Device Architecture (CUDA)


At the start of multi core CPU's and GPUs the processor chips have become parallel systems. But speed of the program will be increased, if software exploits parallelism provided by the underlying multiprocessor architecture. Hence there is a big need to design and develop the software so that it uses multithreading, each thread running concurrently on a processor, potentially increasing the speed of the program dramatically. To develop such a scalable parallel applications, a parallel programming model is required that supports parallel multicore programming environment.

CUDA stands for Compute Unified Device Architecture. It is a parallel programming paradigm released in 2007 by NVIDIA. It is used to develop software for graphics processors and is used to develop a variety of general purpose applications for GPUs that are highly parallel in nature and run on hundreds of GPU’s processor cores.

CUDA has some specific functions, called kernels. A kernel can be a function or a full program invoked by the CPU. It is executed “n” number of times in parallel on GPU by using “n” number of threads. CUDA also provides shared memory and also does synchronization among threads.

The CUDA parallel computing platform provides a few simple C and C++ extensions that enable expressing fine-grained and coarse-grained data and task parallelism. The programmer can choose to express the parallelism in high-level languages such as C, C++, and FORTRAN.