In C++ Openmp how could someone run in parallel multiple code blocks where each block contains omp single and omp for loops? More precisely, I have 3 functions:
block1();
block2();
block3();
I want each of these 3 functions to run in parallel. However I do not want each one of these functions to be assigned a single thread. If I wanted each one of them to use a single thread I could enclose them in three "#pragma omp single nowait" followed by a "#pragma barrier" at the end. Instead each one of these three functions may look something like this:
#pragma omp single
{
//some code here
}
#pragma omp for nowait
for(std::size_t i=0;i<numloops;i++)
{
//some code here
}
Notice in the above code that I need an omp single region to be executed before each parallel for loop. If I did not have this constraint I could have simply added a "nowait" to the "omp single". Instead because I have the "omp single" without a "nowait" I do not want block2() to have to wait for the "omp single" region in block1() to complete. Nor do I want block3() to have to wait for the "omp single" region in block2() to complete. Any ideas? Thanks
The best solution is using tasks. Run each block()
in different tasks, so they run parallel:
#pragma omp parallel
#pragma omp single nowait
{
#pragma omp task
block1();
#pragma omp task
block2();
#pragma omp task
block3();
}
In block()
you can set some code, which is executed before the for
loop and you can use taskloop
to distribute work among the available threads.
void block1()
{
//single thread code here
{
//.... this code runs before the loop and independent of block2 and block3
}
#pragma omp taskloop
for(std::size_t i=0;i<numloops;i++)
{
//some code here - this is distributed among the remaining threads
}
}