我应该使用哪个 openMP 编译指示来阻塞 for 循环?
我的算法(求解泊松方程)是完全可并行的——前提是所有线程在每次迭代结束时同步。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。来自 OpenMP 规范(例如,v 3.1,但这已在从一开始),在“工作共享结构:”下
也就是说,在 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:"
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 executef=fNext
until all threads are done the for loop.