Archive for the ‘Uni’ category

Shared Memory Tip

February 7th, 2011

As usual, I’m knee deep in CUDA optimising a fair few algorithms from various papers. Recently, I’ve been implementing the algorithms from this paper with the aim of improving them later/creating my own based from their concepts. The algorithm is an All Pairs Shortest Path algorithm with a nested loop in the kernel. Each time the second loop executes, two values from shared memory are added together and the resulted is evaluated against another variable stored in a register on the appropriate core. For some reason the code was running a lot slower than the results posted in the paper.

My friend here at Durham who is also working with CUDA suggested taking the addition out of the loop and storing the result in a register before the conditional. Much to my surprise, this worked a treat and instantly gave me comparable results with the paper.

Here is the original code before the change:

for (int i = 0; i < gridDim.x; i ++)
  1. {
  2.   __shared__ int row[blockWidth][blockHeight];
  3.   __shared__ int column[blockWidth][blockHeight];
  4.  
  5.   //Code here fills row and column
  6.  
  7.   __syncthreads();
  8.  
  9.   for(int k = 0; k &lt; blockWidth; k ++)
  10.   {
  11.    if(row[threadIdx.y][k] + column[k][threadIdx.x] &lt; value)
  12.    {
  13.     value = row[threadIdx.y][k] + column[k][threadIdx.x];
  14.    }
  15.   }
  16.  }

Here, we can see the change needed to drastically improve the running time of the algorithm:

unsigned int sum;
  1. for(unsigned int k = 0; k &lt; blockWidth; k ++)
  2.  {
  3.   sum = row[threadIdx.y][k] + column[k][threadIdx.x];
  4.   if(sum &lt; value)
  5.   {
  6.    value = sum;
  7.   }
  8.  }

Given that shared memory is so quick on CUDA, similar to an L1 cache on CPU, I wouldn’t have thought that it would have made any difference at all. Obviously, I was wrong! So watch out for things like this when using CUDA or any parallel computing platform.

Om Nom Urgh

November 29th, 2009

Usually food at uni is pretty good but last night I got served this thing (we’re catered). God knows what was going on in the kitchen but it was terrible. Who serves a chicken boob with no sauce or any kind of seasoning!? Whats more, the “vegetables” consisted of cauliflower and yet more cauliflower. There was nothing else on offer apart from some bread. WTF?

The Abomination

The Abomination

Here’s to hoping this remains the worst uni food I will ever receive!