如何在 OpenMP threadprivate 指令中使用对象的直接初始化?

How to use direct initialization of an object in OpenMP threadprivate directives?

关于this问题,其中一个答案引用了OpenMP标准的以下部分:

A threadprivate variable with class type must have:

  • an accessible, unambiguous default constructor in case of default initialization without a given initializer;
  • an accessible, unambiguous constructor accepting the given argument in case of direct initialization;
  • an accessible, unambiguous copy constructor in case of copy initialization with an explicit initializer.

在发布的问题上使用(几乎)相同的例子,我想做的是:

struct point2d{
        int x;
        int y;
        point2d(){
            x = 0;
            y = 0;
        }
        //copy constructor
        point2d(point2d& p){
            x = p.x;
            y = p.y;
        }
    }; 

并声明两个 point 类型的变量 point2d:

point2d global_point;
point2d local_point(global_point);

#pragma omp threadprivate(local_point)

我看到在发布的问题中使用的示例中,由于 OpenMP 标准引用部分的第一项(如答案中所指出的),代码失败。

我的问题是,就我而言,如何使用 OpenMP 标准的第二点直接初始化私有 local_point 变量(全部使用 global_point)?

此外,它是否有意义,或者我完全错过了那个答案中的重点?

由于在您链接的 post 中讨论的原因,对于某些编译器,您也不能这样做。我想这就是重点。这是一个缺失的特性,编译器甚至没有试图隐藏它:

C3057: dynamic initialization of 'threadprivate' symbols is not currently supported

你想达到什么目的?使用这个简单的结构,您可以

const int gx = 3;
const int gy = -2;
point2d local_point = {gx, gx+gy};
#pragma omp threadprivate ( local_point )

但你必须坚持 const 基本类型。如果你想在一个函数中初始化你的 threadprivate 变量(必须是 static),你可以使用 copyin (它自然会使用第三项,复制赋值)

void foo()
{
    static point2d local_point;
#pragma omp threadprivate( local_point )
    local_point = global_point;
#pragma omp parallel copyin( local_point )
    { // do something

甚至更好

{
    point2d local_point( global_point );
#pragma omp parallel firstprivate( local_point )
    { // do something

摆脱束缚