简体   繁体   English

OpenMP:在并行内部进行同步

[英]OpenMP: synchronization inside parallel for

I have a code that reads like this 我有一个这样的代码

void h(particles *p) {    
    #pragma omp parallel for
    for (int i = 0; i < maxThreads; ++i) {
       int id = omp_get_thread_num();
       for (int j = 0; j < dtnum; ++j) {
           f( p, id);
           if ( j % 50 == 0 ) {
               if (id == 0) {
                   g(p);
               }
               #pragma omp barrier
           }
        }
    }
}
void f(particles *p, int id) {
   for (int i = id * prt_thread; i < (id + 1)*prt_thread; ++i) {
       x(p[i]);
   }
}

Basically I want to: 1)spawn a given amount of threads. 基本上我想:1)产生给定数量的线程。 each thread will process a chuck of p according to thread's id 2)each element of p must be processed dtnum times. 每个线程将根据线程的ID处理一个p的卡盘2)p的每个元素必须被处理dtnum次。 The processing involve random events 3)every 50 iterations, one thread must perform another operation, while the other threads wait 该处理涉及随机事件3)每50次迭代,一个线程必须执行另一种操作,而其他线程等待

Problem: gcc says warning: barrier region may not be closely nested inside of work-sharing, critical, ordered, master or explicit task region 问题:gcc发出警告:障碍区域可能未紧密嵌套在工作共享,关键,有序,主要或明确任务区域内

what can I do? 我能做什么?

I think your code is wrong. 我认为您的代码是错误的。 You said : 你说 :

each element of p must be processed dtnum times. p的每个元素必须处理dtnum次。

But each element of p will be execute maxThreads*dtnum times. 但是p的每个元素将被执行maxThreads * dtnum次。

Could you be more explicit on what your code's supposed to do ? 您能更清楚地了解代码应该做什么吗?

It's hard to tell from the very schematic code, but if all you want to do is sync up every so many iterations, it seems easiest to pull the iteration loop out of the parallel omp for loop - which seems clearer anyway - and just do 很难从非常简单的代码中看出来,但是如果您想要做的就是每隔这么多次迭代就进行同步,那么将迭代循环从并行omp for循环中拉出来似乎是最简单的-无论如何这似乎更加清晰-只需执行

const int iterblocks=50;

#pragma omp parallel shared(p, dtnum) default(none)
for (int jblock=0; jblock<dtnum/iterblocks; jblock++) {
    for (int j=0; j<iterblocks; j++) {
       #pragma omp for nowait
       for (int i=0; i<prt; i++)
           x(p[i]);
    }
    #pragma omp barrier
    #pragma omp single
     g(p);
    #pragma omp barrier
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM