I’ve recently tried to experiment with OpenMP in Visual Studio to learn how to multi-thread my program.
If I try to execute this code serially:
int totalSum = 0;
for(int x=0; x < 100; x++)
{
for(int y=0; y < 100; y++)
{
totalSum = totalSum + x + y;
}
}
What I end up with is that totalSum = 990000
When I try to merely add OpenMP functionality by saying:
#pragma omp parallel for
for(int x=0; x < 100; x++)
{
for(int y=0; y < 100; y++)
{
totalSum = totalSum + x + y;
}
}
I end up with totalSum = 491293 or 596865 or 638260 etc…
Clearly what is happening is that race conditions seem to be occurring and depending on which thread accesses totalSum first, the final answer differs.
What am I doing incorrectly? x and y are correctly defined as private variables (since they are created within the parallel region).
What can I do to ensure that I get the same answer when I am multi-threading the program compared to when I am executing it serially?
The fix is to use the
reductionclause:Read up on the OpenMP reduction clause and then you’ll understand how it works.