In the previous post, I talked about the first part of the Performance Lab for the Computer Science 2021 course. Part II is optimizing the smooth operation. Each pixel has a red, blue, green value. For smooth, we average the values of all the pixels around one particular pixel and replace that one pixel by the average, hence ‘smoothing.’
The baseline code is given to us again and we need to optimize it to make it run faster.
void naive_smooth(int dim, pixel *src, pixel *dst)
{
int i j;
for ( i = 0; i < dim; i++)
for ( j = 0; j < dim; i++)
dst[RIDX(i, j, dim)] = avg(dim, i, j, src);
return;
}
The baseline code simple traverses all the pixels – but we do better. What I did was first create a two-dimensional array the size of the image and first calculate all the values of each pixel and store it in the array. The second part of the operation, I can call from the array and calculate the average and then make the replacement of that pixel with the average. It is not incredibly efficient, and probably won’t pass your speedup benchmark, but you could implement some loop unrolling, code motion or blocking to speed it up.
The key, however, is to figure out how to calculate pixels on the edge of the image (all images are squares for this lab). The difference is, if a pixel is on the edge of the image, the number of surrounding pixels is different. If you’re smoothing a pixel on a corner, there would be three. For a pixel on the edge, but not a corner, it would be five. If the pixel is not on an edge that number would be eight.
