gpt4 book ai didi

c++ - 我使用哪个 openMP pragma 来阻塞 for 循环?

转载 作者:太空狗 更新时间:2023-10-29 20:46:49 25 4
gpt4 key购买 nike

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

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 是否确保线程同步?

最佳答案

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

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

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

关于c++ - 我使用哪个 openMP pragma 来阻塞 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7039189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com