Reputation: 43
If I have a loop like this
int main (){
....
for (i=0; i< N; i++)
{
/*Do some calculations*/
for (j=0; j<M; j++)
{
/*Do more calculations*/
compute_x( some pointers as args );
}
compute_y(some pointer as args);
}
...
return value;
}
and
void compute_x( some pointers as args )
{
/* some calculations*/
for (h=0; h<COUNT; h++)
{
...
}
}
}
and compute_y() is similar.
My question is if I parallelize the outer loop in the main using the OpenMP directives,
#pragma omp parallel for schedule (runtime) private ( ...)
for (i=0; i< N; i++)
{
...
}
What is going to be the behavior of the function compute_x()
and compute_y()
?? As far as I understand they are going to be executed by each thread, therefore the for loop in compute_x()
is going to be executed from 0 to COUNT by each thread.
If that is right, what can I do to share the work load also in the for loop in function compute_x()
(supposing there is no data dependency). My first guess is to make the function compute_x() and compute_y() inline but the functions are quite big, and they also call other functions that fortunately can be also executed in parallel.
Upvotes: 0
Views: 801
Reputation: 1681
If your some pointers as args
are different for each thread (different for each i
) or if they are with const
modifier, that is no thread can modify the contents by pointer, then nothing unexpected would happen.
The bad thing is when your different threads share the same pointers and simultaneously write/read data pointed by them, then you may have an unpredicted result.
Upvotes: 1
Reputation: 62459
If the outer loop has sufficient iterations to keep all cores busy there is no reason to extend the parallelization to the inner loops. This will only create more threads that are not necessary.
Upvotes: 2