Wednesday, July 10, 2013

GPGPU

GPGPU stands for General Purpose computing using Graphical Processing Unit. Consider that our system has CPU (Central Processing Unit) and a GPU (Graphical Processing Unit). GPGPU is used for reducing the workload of CPU. The activities which can be done in parallel are transferred from CPU to GPU. Thus work pressure on CPU is decreased, and so speed of operation is increased.

Consider a matrix multiplication for 100 elements. So a normal C code which is executed on CPU will be 

void matrix_mult_cpu (int* a, int *b, int *c, int n)
{
int i=0;
for(i=0; i<n; i++)    //Let n=100
{
a[i] = b[i] * c[i];
}
}

This FOR loop will be executed 100 times. If 1 iteration of loop takes 1 ms (millisecond), then the entire loop takes 100 ms. Because the loop iterates for 100 times.

Matrix multiplication is an example of data parallelism. Here the operation to be performed (multiplication) is same for all elements. Only the data changes. If this code is transferred to GPU, it can handle all the 100 iterations within 1 cycle. Because GPU contains a collection of individual elements called work-items. Data for performing each iteration is taken, and then given to each work-item.
  • First work-item will receive the data b[0] and c[0], using which it will calculate a[0]. 
  • Second work-item will receive the data b[1] and c[1], using which it will calculate a[1] and 
  • finally 100th work-item will receive the data b[99] and c[99], using which it will calculate a[99]. 

The time for performing one multiplication is 1 ms. Since all the work-items operate at the same time in GPU, the result of 100 multiplications will be obtained in 1 ms itself. The same operation when performed on CPU took 100 ms !!!

In this manner when doing GPGPU programming, all the activities which can be done in parallel must be given to GPU and only serial tasks must be given to CPU.


No comments:

Post a Comment