openmp并行for循环,具有两个或多个减少 [英] openmp parallel for loop with two or more reductions
问题描述
您好,我想知道这是否是进行常规for循环但有两个简化的正确方法,这是下面的正确方法吗?这项工作是否也可以进行两次以上的简化.有一个更好的方法吗? 还有机会将其与MPI_ALLREDUCE命令集成吗?
Hi just wondering if this is the right way to go going about having a regular for loop but with two reductions , is this the right approach below? Would this work with more then two reductions as well. Is there a better way to do this? also is there any chance to integrate this with an MPI_ALLREDUCE command?
heres the psuedo code
#pragma omp parallel for \
default(shared) private(i) \
//todo first reduction(+:sum)
//todo second reduction(+:result)
for loop i < n; i ++; {
y = fun(x,z,i)
sum += fun2(y,x)
result += fun3(y,z)
}
推荐答案
您可以通过指定多个以逗号分隔的变量(即列表)来简化操作:
You can do reduction by specifying more than one variable separated by a comma, i.e. a list:
#pragma omp parallel for default(shared) reduction(+:sum,result) ...
将为sum
和result
创建专用线程变量,这些变量将使用+
组合在一起,并在线程块的末尾分配给原始全局变量.
Private thread variables will be created for sum
and result
that will be combined using +
and assigned to the original global variables at the end of the thread block.
此外,变量y
应该标记为私有.
Also, variable y
should be marked private.
请参见 https://computing.llnl.gov/tutorials/openMP/#REDUCTION
这篇关于openmp并行for循环,具有两个或多个减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!