I would like to run following code in parallel in openMP:
while (!stopcondition())
{
work_1();
work_2(); //work_2 has to be run after work_1
}
where the work() function is easy to parallelize:
work_i()
{
//...some linear stuff...//
#pragma omp parallel for schedule(static, 2) // make a piece of size 2 for each thread
for(int n=0; n<N; ++n)
printf(" %d", omp_get_thread_num() );
}
It is inconvenient to set the parallel region inside the while loop or the work() function since it causes the threads to split each time, which results in big overhead.
When I tried:
#pragma omp parallel
{
#pragma omp single
{
while (!stopcondition())
{
work_1();
work_2();
}
}
}
Then both work_1 and work_2 always prints the same id, thus it is certainly executed by the same thread.
1) How can I overcome it? 2) Shall I just extract the // linear stuff // from work() and put it inside #pragma omp single?
3) Where is the best place to initialize the parallel region?
----------------------edit-----------------
It seems that the only way is to remove the stuff from work() and enclose it in a common parallel region:
while (!stopcondition())
{
#pragma omp parallel
{
#pragma omp parallel for schedule(static, 2) // make a piece of size 2 for each thread
for(int n=0; n<N; ++n)
printf("Piece of work_1 %d", omp_get_thread_num() );
#pragma omp parallel for schedule(static, 2) // make a piece of size 2 for each thread
for(int n=0; n<N; ++n)
printf("Piece of work_2 %d", omp_get_thread_num() );
}
//...rest of linear stuff...//
}
#pragma omp single
? – damienfrancois