I have the following the code:
int myArray[] = {0, 0, 0, 0, 0, 0};
double EV = 0;
for(short a1 = 1; a1 <= 6; ++a1)
{
++myArray[a1-1];
if(....)
{
--myArray[a1-1];
continue;
}
EV = myEVFunc();
if(EV...)
{
for(short a2 = 1; a2 <=6 ; ++a2)
{
++myArray[a2-1];
if(....)
{
--myArray[a2-1];
continue;
}
EV = myEVFunc();
if(EV...)
{
for(short a3 = 1; a3 <= 6; ++a3)
{
++myArray[a3-1];
if(....)
{
--myArray[a3-1];
continue;
}
EV = myEVFunc();
}
}
}
}
}
I am trying to use OpenMP to parallelize the loops. the code compiles fine when i place
#pragma omp parallel for in front of the outermost for loop. However it gives incorrect results. I suspect two issues the continue statements inside the loops and the fact that there are shared variables in the nested loops.
Is it possible to use OpenMP with this code snippet, if so can anyone please give me the correct syntax. Thanks in advance.
I’ll point out a couple obvious things:
1.)
double EV = 0;is declared outside the outer loop. Therefore it will be shared by all the threads. So you’ll have a race condition atEV = myEVFunc();and at each access toEV.The solution to this is to declare it inside the loop. That will make it private to each thread.
2.) Another (sorta) issue is that your outer-loop only has 6 iterations. So you won’t get more than 6 threads. Furthermore, you could get load-balancing issues with say 4 cores…