在C ++中对复杂变量进行OpenMP压缩的优雅(和典型)解决方法? [英] Elegant (and typical) workaround for OpenMP reduction on complex variable in C++?

查看:172
本文介绍了在C ++中对复杂变量进行OpenMP压缩的优雅(和典型)解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到缩小只能用于C ++中的POD类型。

I realize that reduction is only usable for POD types in C++. What would you do to implement a reduction for a complex type accumulator?

complex<double> x(0.0,0.0), y(1.0,1.0);
#pragma omp parallel for reduction(+:x)
for(int i=0; i<5; i++)
{
    x += y;
}

(注意我可能遗漏了一些语法)。看起来一个明显的解决方案是将实部和虚部分成临时双精度,然后累加在那些。我想我正在寻找优雅,这似乎...不太漂亮。这是典型的方法吗?

(noting that I may have left some syntax out). It seems an obvious solution would be to split real and imaginary components into temporary doubles, then accumulate on those. I guess I'm looking for elegance, and that seems ... less than pretty. Would that be the typical approach here?

推荐答案

在没有用户定义的缩减在OpenMP甚至比你建议的更丑。通常,在并行区域之前,人们创建一个(至少)与该区域中的线程一样多的元素的数组,使用 omp_get_thread_num()作为数组的索引,并且在并行区域之后的循环中执行累积结果的最终缩减。

The typical workaround in absence of user-defined reductions in OpenMP is even uglier than what you suggested. Usually, prior to the parallel region people create an array of (at least) as many elements as there will be threads in the region, accumulate partial results separately for each thread using omp_get_thread_num() as an index to the array, and do final reduction of the accumulated results in a loop after the parallel region.

据我所知,OpenMP语言委员会致力于在规范中添加用户定义的缩减,所以也许在几年后终于解决。

As far as I know, OpenMP language committee works on adding user-defined reductions to the specification, so maybe it will be finally resolved in a few years.

这篇关于在C ++中对复杂变量进行OpenMP压缩的优雅(和典型)解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆