Reputation: 336
This question is about thread affinity on a specific situation. Suppose we have the following code.
int main(){
omp_set_num_threads(NTHREADS);
somestruct *array = (somestruct*)malloc(sizeof(somestruct) * NTHREADS);
#pragma omp parallel default(none) shared(array)
{
// each thread initializes their corresponding struct data
int tid = omp_get_thread_num();
initialize_struct(array, tid);
// ...
}
// some sequential computation
// ....
// ....
// parallel region again
#pragma omp parallel default(none) shared(array)
{
int tid = omp_get_thread_num();
// each thread does some computation on their corresponding struct
do_computation(array, tid);
}
return 0;
}
Will thread affinity be conserved among multiple parallel regions as in the above example? In other words, if in the first parallel region tid0 mapped to core0 of CPU0, is it guaranteed that tid0 will map to the same core0 of CPU0 for the second parallel region?
Upvotes: 3
Views: 571
Reputation: 6537
Yes. The specification requires so-called "threadprivate persistence" which makes runtimes to reuse the same threads across consequent parallel regions as explained here: threadprivate data can persist across active parallel regions under certain conditions:
The values of data in the threadprivate variables of non-initial threads are guaranteed to persist between two consecutive active parallel regions only if all the following conditions hold:
• Neither parallel region is nested inside another explicit parallel region.
• The number of threads used to execute both parallel regions is the same.
• The value of the dyn-var internal control variable in the enclosing task region is false at entry to both parallel regions.
If these conditions all hold, and if a threadprivate variable is referenced in both regions, then threads with the same thread number in their respective regions will reference the same copy of that variable.
Upvotes: 1