OpenMP 循环中的数据成员

发布于 2024-11-05 04:11:30 字数 621 浏览 0 评论 0原文

我有以下类:

Class L{
    public:
    bool foo(vector<bool> & data);
    private:
    C** cArray;
}

并希望并行化函数 foo 中的 for 循环,在创建 L 的对象并初始化 cArray 中的所有元素后,该函数称为 somtime 。

bool L::foo(vector<int> & data){
int row, col;
#pragma omp parallel shared(SIZE, cArray, data) private(row, col)
    for (row=0, row<SIZE; ++row)
    {
        for (col=0; col<SIZE; ++col)
        {
            cArray[row][col].computeScore(data);
        }
    }
}    

但这给出了一个错误: 错误 C3028:“L::cArray”:数据共享子句中只能使用变量或静态数据成员。

假设我不想使 cArray 静态,有什么可以做的吗?

I have the following class:

Class L{
    public:
    bool foo(vector<bool> & data);
    private:
    C** cArray;
}

and would like to parallelize the for loop in the function foo which is called somtime after an object of L is created and all the elements in cArray are initialized.

bool L::foo(vector<int> & data){
int row, col;
#pragma omp parallel shared(SIZE, cArray, data) private(row, col)
    for (row=0, row<SIZE; ++row)
    {
        for (col=0; col<SIZE; ++col)
        {
            cArray[row][col].computeScore(data);
        }
    }
}    

But this gives an error:
error C3028: 'L::cArray' : only a variable or static data member can be used in a data-sharing clause.

Is there anything that can be done about this assuming I don't want to make cArray static?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-11-12 04:11:30

这个问题之前已经出现过好几次了。问题是,类数据成员可能不会在编译时实例化。如果它们是共享的,那么就没有问题,因为在 OpenMP 中默认情况下共享变量(除非您将默认值更改为私有(在 C 中无法做到这一点)或无)。但是,如果它们被定义为私有,则编译器需要知道如何制作私有副本,并且此信息在编译时并不总是可用。

不幸的是,如果您想要限制所有数据的范围(使用显式数据范围子句),那么您就会遇到问题。作用域子句只能处理变量,而类数据成员则不能。只要默认值保持共享,就可以将它们排除在任何数据作用域子句之外。如果您希望它们是私有的,那么您就不走运了,需要将类数据成员定义为变量。不幸的是,由于 OpenMP 不是基础语言的一部分,因此我认为这种情况不会很快发生变化。

This question has come up several times before. The problem is, that class data members may not be instantiated at compile time. If they are being shared, then there is no problem, because variables are shared by default in OpenMP (unless you change the default to private - which you can't do in C - or none). However, if they are defined as private, then the compiler needs to know how to make private copies and this information is not always available at compile time.

Unfortunately, if you want to scope all your data (using explicit data scoping clauses), which you should, then you have a problem. The scoping clauses can only handle variables - which class data members aren't. Leaving them off any data scoping clause works as long as the default remains shared. If you want them to be private, then you are out of luck and need to define the class data members as variables. Unfortunately, since OpenMP is not part of the base language, I don't see this changing anytime soon.

听风念你 2024-11-12 04:11:30

您可以像下面一样使用 C++ 11 thread_local。然后一切都应该按预期工作。

// .h

Class L{

public:
bool foo(vector<bool> & data);
private:
static thread_local C** cArray;

}

// .cpp

thread_local C** cArray;

You can use C++ 11 thread_local like following. Then it should all work as intended.

// .h

Class L{

public:
bool foo(vector<bool> & data);
private:
static thread_local C** cArray;

}

// .cpp

thread_local C** cArray;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文