[Solved] OPenMP: When can a Loop be parallelized


I will not do your HW, but I will give a hint. When playing around with OpenMp for loops, you should be alert about the scope of the variables. For example:

#pragma omp parallel for
for(int x=0; x < width; x++)
{
    for(int y=0; y < height; y++)
    {
        finalImage[x][y] = RenderPixel(x,y, &sceneData);
    }
}

is OK, since x and y are private variables.

What about

int x,y;
#pragma omp parallel for
for(x=0; x < width; x++)
{
    for(y=0; y < height; y++)
    {
        finalImage[x][y] = RenderPixel(x,y, &sceneData);
    }
}

?

Here, we have defined x and y outside of the for loop. Now consider y. Every thread will access/write it without any synchronization, thus data races will occur, which are very likely to result in logical errors.

Read more here and good luck with your HW.

solved OPenMP: When can a Loop be parallelized