我应该使用哪个 openMP 编译指示来阻塞 for 循环?

发布于 2024-11-29 00:58:35 字数 409 浏览 1 评论 0原文

我的算法(求解泊松方程)是完全可并行的——前提是所有线程在每次迭代结束时同步。

Function f, fNext;
init(f);
#pragma omp parallel
for(int step=0; step<maxITER; step++) {
#pragma omp for
   for(int i=0; i<N; i++) {
      for(int j=0; j<N; j++) {
         fNext(i,j) = someOperator( f(i,j) );
      }
   }
   f = fNext;
}//Threads must synchronize here

#pragma omp for 在继续下一次迭代之前是否确保线程同步?

My algorithm (solving Poisson's equation) is completely parallelizable--provided that all the threads sync at the end of each iteration.

Function f, fNext;
init(f);
#pragma omp parallel
for(int step=0; step<maxITER; step++) {
#pragma omp for
   for(int i=0; i<N; i++) {
      for(int j=0; j<N; j++) {
         fNext(i,j) = someOperator( f(i,j) );
      }
   }
   f = fNext;
}//Threads must synchronize here

Does #pragma omp for ensure thread synchronization before continuing to the next iteration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-12-06 00:58:35

是的。来自 OpenMP 规范(例如,v 3.1,但这已在从一开始),在“工作共享结构:”下

循环结构末尾有一个隐式屏障,除非
指定了 nowait 子句。

也就是说,在 for 循环结束时,除非您执行类似 #pragma omp for nowait 的操作,否则会存在隐含的屏障,因此没有线程会执行 f=fNext直到所有线程都完成 for 循环。

Yes. From the OpenMP Spec (eg, v 3.1, but this has been in since the beginning), under "worksharing constructs:"

There is an implicit barrier at the end of a loop construct unless
a nowait clause is specified.

That is, at the end of the for loop, unless you do something like #pragma omp for nowait, there is an implied barrier so that no thread will execute f=fNext until all threads are done the for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文