英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论