减少变量在外部上下文中是私有的 [英] reduction variable is private in outer context

查看:66
本文介绍了减少变量在外部上下文中是私有的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

void simulation (MD *md){

    double sum;
    #pragma omp parallel private (move)
    {

        for(move = 0; move < maxIterations; ++move)
        {
                cicleDoMove(md);
                cicleForces(md);
                cicleMkekin(md,sum);
                // ...
        }
    }
}

哪里:

void cicleMkekin(Md *md, double sum){

    #pragma omp for reduction(+ : sum)
    for (i = 0; i < md->mdsize; i++)
    {
        sum += mkekin(..);
    }
    // .. 
}

我收到以下错误:

"reduction variable 'sum' is private in outer context"

变量sum是shared而不是private,其实如果我把模拟代码改成:

The variable sum is shared not private, in fact if I change the simulation code to:

 void simulation (MD *md){

        double sum;
        #pragma omp parallel private (move)
        {

            for(move = 0; move < maxIterations; ++move)
            {
                    cicleDoMove(md);
                    cicleForces(md);

                    #pragma omp for reduction(+ : sum)
                    for (i = 0; i < md->mdsize; i++)
                    {
                         sum += mkekin(..);
                    }
                    // ...
            }
        }
    }

效果很好.

无论如何我可以使用我的第一个代码版本而不会出现该错误?还是我做错了什么?

Is there anyway I can used my first code version without getting that error? or am I doing something wrong?

推荐答案

OpenMP 在这种特殊情况下可能会有点混乱.规范规定(第 2.14.3.6 节):

OpenMP can be a bit confusing in this particular case. The specification prescribes (§2.14.3.6) that:

出现在工作共享的缩减条款中的列表项构造必须在其中任何一个的并行区域中共享工作共享结构绑定产生的工作共享区域.

A list item that appears in a reduction clause of a worksharing construct must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.

此外,它说 (§2.14.1.1),对于 C 和 C++,

Furthermore it says (§2.14.1.1), for C and C++, that

在范围内声明的具有自动存储持续时间的变量构造是私有的.

Variables with automatic storage duration that are declared in a scope inside the construct are private.

在您的情况下,变量 sum 在函数 cicleMkekin 的调用范围内声明,并且作为函数参数,具有自动存储持续时间.因此,当您从并行区域内调用 cicleMkekin 时(或者,就此而言,从与程序执行一致的隐式顶级并行区域),sum 被认为是私有变量.因此,您的归约条款确实是非法的,并且您得到的错误消息虽然可能令人困惑,但实际上是正确的.

In your case, the variable sum is declared in the scope for invocations of the function cicleMkekin and, as a function parameter, has automatic storage duration. Hence, when you call cicleMkekin from within your parallel region (or, for that matter, from the implicit top-level parallel region that coincides with the execution of your program), sum is considered to be a private variable. As a result, your reduction clause is indeed illegal and the error message you are getting is, confusing as it may be, in fact spot on.

在您手动内联对 cicleMkekin 的调用的代码版本中,您在并行区域之外声明变量 sum.这样的变量,在没有 default 子句或该变量的所谓明确确定的数据共享属性的情况下,确实是共享的(第 2.14.1 节),因此,reduction 子句在您的代码版本中是合法的.

In the version of your code in which you have manually inlined the call to cicleMkekin, you declare the variable sum outside of the parallel region. Such a variable, in absence of a default clause or a so-called explicitly determined data-sharing attribute for that variable, are indeed shared (§2.14.1) and, so, the reduction clause in that version of your code is legal.

这篇关于减少变量在外部上下文中是私有的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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