通过多个并行区域的 OpenMP 线程亲和性
OpenMP thread Affinity through multiple parallel regions
这个问题是关于特定情况下的线程关联。假设我们有如下代码。
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;
}
线程亲和性是否会像上例中那样在多个并行区域中保持不变?也就是说,如果第一个parallel region中tid0映射到CPU0的core0,是否保证tid0在第二个parallel region中映射到CPU0的同一个core0?
是。该规范要求所谓的 "threadprivate persistence",这使得运行时在随后的并行区域中重用相同的线程,如解释的那样 here:threadprivate 数据可以在某些条件下跨活动并行区域持续存在:
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.
这个问题是关于特定情况下的线程关联。假设我们有如下代码。
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;
}
线程亲和性是否会像上例中那样在多个并行区域中保持不变?也就是说,如果第一个parallel region中tid0映射到CPU0的core0,是否保证tid0在第二个parallel region中映射到CPU0的同一个core0?
是。该规范要求所谓的 "threadprivate persistence",这使得运行时在随后的并行区域中重用相同的线程,如解释的那样 here:threadprivate 数据可以在某些条件下跨活动并行区域持续存在:
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.