简体   繁体   English

为什么不允许在OpenMP中共享类成员变量x(x)?

[英]Why is class member variable x not allowed to be shared(x) in OpenMP?

In a member function, I can parallelize using the shared member variable int *x like this 在成员函数中,我可以像这样使用共享成员变量int *x进行并行化

#pragma omp parallel for default(shared)
for(int i=0;i<size;i++) {
  x[i]=i;
}

But if I try 但是,如果我尝试

#pragma omp parallel for default(none) shared(x,size)
for(int i=0;i<size;i++) {
  x[i]=i;
}

I get the error: 'obj::x' is not a variable in clause 'shared' . 我得到错误: 'obj::x' is not a variable in clause 'shared' I would prefer the second version because it announces the shared variables it is working with, reminding me to make sure there are no race conditions or similar problems. 我更喜欢第二个版本,因为它宣布它正在使用的共享变量,提醒我确保没有竞争条件或类似的问题。

What is going on that OpenMP claims that obj::x is not a variable? OpenMP声称obj::x不是变量怎么回事?

Most implementations of OpenMP outline the parallel region. OpenMP的大多数实现概述了并行区域。 That is, they make it a function. 也就是说,它们使它成为一种功能。 Private variables are generally passed to this function and shared variables may be passed or be within the function's scope. 私有变量通常传递给此函数,共享变量可以传递或在函数的范围内。 The problem with class data members is that they are not the same as variables. 类数据成员的问题是它们与变量不同。

When the compiler outlines a parallel region, variables have storage locations defined that the compiler can set up to pass to the function. 当编译器概述并行区域时,变量具有定义的存储位置,编译器可以将其设置为传递给函数。 Data members may not be instantiated (ie, storage allocated) until the class is called during execution of the program. 在程序执行期间调用类之前,可能不会实例化数据成员(即,分配存储)。 This means that the compiler cannot privatize data members by itself. 这意味着编译器不能自己私有化数据成员。 It would also have to be done in the runtime and this would cause a lot more work and would affect performance of both serial and parallel programs. 它还必须在运行时完成,这将导致更多的工作,并将影响串行和并行程序的性能。 So far no implementation has tried to do this work and since the OpenMP spec is written by consensus the decision was made to disallow data members in all clauses. 到目前为止,没有任何实现尝试执行此工作,并且由于OpenMP规范是以协商一致方式编写的,因此决定禁止所有子句中的数据成员。 Otherwise, it seemed too confusing to say that they are allowed in shared clauses, but no other clause. 否则,说它们被允许共享条款似乎太令人困惑,但没有其他条款。

不知道 - 但是请看看32个OpenMP陷阱,以便C ++开发人员解决大多数OpenMP问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 x 成员变量不明确? - Why is the x member variable ambiguous? 为什么“使用命名空间 X;” 不允许在类/结构级别内? - Why "using namespace X;" is not allowed inside class/struct level? 为什么不允许在类内部初始化静态成员变量,但允许初始化const静态成员? - why initialization of static member variable is not allowed inside class but initialization of const static members is allowed? 为什么 OpenMP 不允许使用 != 运算符? - Why is the != operator not allowed with OpenMP? “ X类”没有成员“ Y” - 'class X' has no member 'Y' 为什么要赋值给std :: function <X()> 当它是X类的成员时不编译? - Why assignment to std::function<X()> doesn't compile when it is a member of class X? 当对象引用为常数时,为什么允许更改指向另一个类的指针的成员变量? - Why member variable which is a pointer to another class is allowed to be changed when the object reference is constant? 静态库中的静态类成员变量不共享? - Static class member variable in static library not shared? Y类的功能不是X的成员,但X类是Y的朋友 - function of class Y is not a member of X, but class X is a friend of Y 为什么&#39;X x; X();&#39; 允许,当&#39;X&#39;定义转换为函数指针时,但不是,当它定义转换为仿函数时? - Why is 'X x; x();' allowed, when 'X' defines a conversion to function pointer, but not, when it defines a conversion to a functor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM