问题
I assume thread creation and deletion could be costly. Does OpenMP try reuse existing threads? For example,
#pragma omp parallel sections num_threads(4)
{
#pragma omp section
{ ... worker A ... }
#pragma omp section
{ ... worker B ... }
}
#pragma omp parallel sections num_threads(4)
{
#pragma omp section
{ ... worker C ... }
#pragma omp section
{ ... worker D ... }
}
In execution, does OpenMP allocate 5 threads or 3 (in which C and D reuse the threads that A and B used)?
回答1:
In your example, a team of 4 "working" threads will be created/activated upon entry of your first parallel section, and 2 of them will be doing some work: one doing A and another doing B. The 2 others will be waiting idle at the end of the section. The 4 threads are then destroyed/deactivated upon exit of the section. Then a new team of 4 threads will be created/activated upon entry of the second parallel section and the same will happen... Now I said created/activated because since as you guessed, creating threads is costly, the standard allows compilers to create threads once and to only put threads to sleep between parallel sections if they want. But this is an implementation detail that should be transparent for the programmer.
At the end of the day, there's no way of knowing which thread will deal with A, B, C and D... You can only be sure that A and B will be handled by 2 different threads and that C and D by two different threads as well.
来源:https://stackoverflow.com/questions/32572043/how-does-openmp-reuse-threads