Reputation: 1567
I am trying to have a parallel region which inside it has first a parallel for, then a function call with a parallel for inside and lastly another parallel for.
A simplified example could be this
#pragma parallel
{
#pragma omp for
for(int i=0;i<1000;i++)
position[i]+=velocity[i];
calculateAccelerationForAll();
#pragma omp for
for(int i=0;i<1000;i++)
velocity[i]+=acceleration[i];
}
calculateAccelerationForAll()
{
#pragma parallel omp for
for(int i=0;i<1000;i++)
for(int j=0;j<1000;j++)
acceleration[i]=docalculation
}
The issue here being that I would want the existing threads to jump over into calculateAccelerationForAll and execute the for loop there, rather than having three separated parallel regions. I could ensure that only the first thread actually calls the function, and have a barrier after the function call, but then only that thread executes the for loop inside the function.
The question is really if my assumption, that putting the first and last loop in their own paralle region and making the function call have its own region as well, is inefficient, is false... or if it is correct, how I can then make one regions thread go through it all the way.
Might add, that if I just took the contents of the function and put it inside the main paralle region, between the two existing loops, then it would not be an issue. The problem (for me at least) is that I have to use a function call and make then run in parallel as well.
Upvotes: 1
Views: 1235
Reputation: 1567
It helped typing out the problem, it seems.
The obvious answer is to change the pragma in the function
from #pragma parallel for
to #pragma for
That makes that for loop use the existing threads from the existing calling parallel section, and it works perfectly.
Upvotes: 2