[英]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.