一个类的私有数据成员能在OpenMP的数据共享子句中使用吗?

huangapple go评论72阅读模式
英文:

can a private data member of a class be used in data sharing clause in openMP?

问题

它说明,“作为另一个变量的一部分(如数组或结构元素)的变量,除非数据共享属性子句与类非静态成员函数中的构造相关联,且该变量是对象的可访问数据成员,否则不能私有化。”

我无法理解这句话。请问有人可以详细解释一下吗?

英文:

Referring to this: https://www.openmp.org/spec-html/5.0/openmpsu105.html#x138-5520002.19.3
It states that, "A variable that is part of another variable (as an array or structure element) cannot be privatized except if the data-sharing attribute clause is associated with a construct within a class non-static member function and the variable is an accessible data member of the object for which the non-static member function is invoked."

I am not able to get this. Could anyone of you please elaborate this?

答案1

得分: 1

考虑你有一个类A,其中有一个数据成员x

class A {
    int x;
};

A a;

这个规则涉及到x何时可以私有化。它规定,一个变量(x),如果它是另一个变量(a)的一部分,只有当private(x)子句是一个非静态成员函数的一部分时,它才能被私有化。在这种情况下,变量x还必须是调用该非静态成员函数的对象的可访问数据成员(即公共成员)。如果不满足这些条件,变量就不能被私有化。

这里,我为你展示了一些例子:

a) 在这个例子中,x从一个非静态成员函数(foo)中被访问,并且x是可访问的,所以它可以被编译而不会有任何问题:

class A {
   public:
      int x;
      void foo() {
        //没问题,非静态成员函数可以将可访问数据成员私有化
         #pragma omp parallel private(x) 
         {
            x = omp_get_thread_num();
         }
      }
};

b) 然而,一个静态成员函数不能将x私有化:

class A {
   public:
      int x;
      static void foo() {
        //错误,静态类成员函数不能将x私有化
         #pragma omp parallel private(x) 
         {
            x = omp_get_thread_num();
         }
      }
};

c) 非类成员函数不能将x私有化:

class A {
      public:
      int x;
};

int main() {
   A a;
    
   // 错误,只有类成员函数才能将x私有化 
   #pragma omp parallel private(a.x)
   {
       x = omp_get_thread_num();
   }

   return 0;
}
英文:

Consider that you have a class A, which has a data member x:

class A {
      int x;
};

A a;

This rule concerns when x can be privatized. It states that a variable (x) that is a part of another variable (a), cannot be made private unless the data-sharing attribute clause (i.e. private(x) clause) is part of a non-static member function of a class. In this case, the variable x must also be an accessible data member (i.e. public) of the object for which the non-static member function is invoked. If these conditions are not met, the variable cannot be privatized.

Here, I show you examples:

a) In this example x is accessed from a non-static member function (foo) and x is accessible, so it can be compiled without any problem:

class A {
   public:
      int x;
      void foo() {
        //It is fine, a non-static member function can privatize an accessible data member
         #pragma omp parallel private(x) 
         {
            x = omp_get_thread_num();
         }
      }
};

b) However, a static member function cannot privatize x:

class A {
   public:
      int x;
      static void foo() {
        //Error, a static class member function cannot privatize x
         #pragma omp parallel private(x) 
         {
            x = omp_get_thread_num();
         }
      }
};

c) non class member functions cannot privatize x:

class A {
      public:
      int x;
};

int main() {
   A a;
    
   // Error, only a class member function can privatize x 
   #pragma omp parallel private(a.x)
   {
       x = omp_get_thread_num();
   }

   return 0;
}

huangapple
  • 本文由 发表于 2023年2月6日 13:18:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定