I am using OpenMP and I want to spawn threads such that one thread executes one piece of code and finishes, in parallel with N threads running the iterations of a parallel-for loop.
Execution should be like this:
Section A (one thread) || Section B (parallel-for, multiple threads)
| || | | | | | | | | | |
| || | | | | | | | | | |
| || | | | | | | | | | |
| || | | | | | | | | | |
| || | | | | | | | | | |
V || V V V V V V V V V V
I cannot just write a parallel-for with a #pragma omp once
because I do not want the thread that executes section A to execute the for-loop.
I have tried this:
#pragma omp parallel sections {
#pragma omp section
{
// Section A
}
#pragma omp section
{
// Section B;
#pragma omp parallel for
for (int i = 0; i < x; ++i)
something();
}
}
However, the parallel-for always executes with only one thread (I know because I made the body of the loop print omp_get_thread_num()
and they are all the same number, either 1 or 0 depending on which thread of the two executed the second parallel section).
I have also tried
#pragma omp sections {
#pragma omp section
{
// Section A
}
#pragma omp section
{
// Section B;
#pragma omp parallel for
for (int i = 0; i < x; ++i)
something();
}
}
Which allows the for-loop to execute with multiple threads, but it makes the sections non-parallel, and the first section is executed sequentially before the second section.
What I need is a combination of the two approaches, where each iteration of the for-loop and the first section are all run in parallel.