问题描述
在成员函数中,我可以使用共享成员变量 int * x
像这样
In a member function, I can parallelize using the shared member variable int *x
like this
#pragma omp parallel for default(shared)
for(int i=0;i<size;i++) {
x[i]=i;
}
但如果我尝试
#pragma omp parallel for default(none) shared(x,size)
for(int i=0;i<size;i++) {
x[i]=i;
}
我得到错误:'obj :: x '不是子句'shared'
中的变量。
我喜欢第二个版本,因为它宣布它正在使用的共享变量,提醒我确保没有竞争条件或类似的问题。
I get the error: '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.
OpenMP声称 obj :: x
不是变量是怎么回事?
What is going on that OpenMP claims that obj::x
is not a variable?
推荐答案
OpenMP的大多数实现概述了并行区域。也就是说,它们使它成为一个函数。私有变量通常传递给此函数,共享变量可以传递或在函数的范围内。类数据成员的问题是它们与变量不同。
Most implementations of OpenMP outline the parallel region. 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.
当编译器概述并行区域时,变量具有定义的存储位置,编译器可以设置传递给函数。直到在程序的执行期间调用类时,数据成员可以不被实例化(即,分配存储)。这意味着编译器不能自己私有化数据成员。它也必须在运行时完成,这将导致更多的工作,并将影响串行和并行程序的性能。到目前为止还没有任何实现尝试做这项工作,并且由于OpenMP规范是以协商一致方式编写的,所以决定禁止所有子句中的数据成员。否则,它似乎太混乱,说他们是允许在共享子句,但没有其他条款。
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 (i.e., 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. Otherwise, it seemed too confusing to say that they are allowed in shared clauses, but no other clause.
这篇关于为什么类成员变量x不允许在OpenMP中共享(x)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!