如何在 OpenMP 中将对象或结构定义为 threadprivate?

How to define a object or struct as threadprivate in OpenMP?(如何在 OpenMP 中将对象或结构定义为 threadprivate?)

本文介绍了如何在 OpenMP 中将对象或结构定义为 threadprivate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将结构或对象设为线程私有,我正在做的事情会产生错误:

I don't know how to make a struct or object as threadprivate, what I'm doing generates a error:

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

我声明了一个静态结构并尝试使它们成为线程私有的

I declare a static structure and try to make them threadprivate

    static  point2d myPoint;
    #pragma omp threadprivate(myPoint)

它产生一个错误:

错误 C3057:myPoint":当前不支持threadprivate"符号的动态初始化

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

这是否意味着当前的 openmp 编译器不支持将 struct threadprivate 设为私有?或者我在做什么是错误的.有没有其他方法可以传递结构体或对象?

Does it means that current openmp compiler doesn't support this to make a struct threadprivate? Or what I'm doing is wrong. Is there any alternate way to pass a struct or object?

这是我的代码的其余部分:

Here's rest part of my codes:

    void myfunc(){
        printf("myPoint at %p
",&myPoint);
    }

    void main(){
    #pragma omp parallel
       {
           printf("myPoint at %p
",&myPoint);
           myfunc();
       }

    }

推荐答案

在 C++ 中,带有方法的结构体是一个默认为公共的类.它不是 plain-old-data (POD).MSVC 似乎暗示它可以处理线程私有对象(即非 POD) 但我似乎无法让它工作.我确实让它在 GCC 中像这样工作:

In C++ a struct with methods is a Class where the default is public. It's not plain-old-data (POD). MSVC seems to imply that it can handle threadprivate objects (i.e. non-POD) but I can't seem to get it to work. I did get it working in GCC like this:

extern point2d myPoint;
#pragma omp threadprivate(myPoint)
point2d myPoint;

但是有一种解决方法可以与 MSVC(以及 GCC 和 ICC)一起使用.您可以使用线程私有指针.

But there is a work around which will work with MSVC (as well as GCC and ICC). You can use threadprivate pointers.

threadprivate 的目的是为每个线程拥有一个对象/类型的私有版本,并且在并行区域之间具有持久性的值.您可以通过声明一个指向 point2d 的指针,使该线程私有,然后为并行区域中的每个线程的私有指针分配内存来实现这一点.确保在上次并行调用时删除分配的内存.

The purpuse of threadprivate is to have private version of an object/type for each thread and have the values persistent between parallel regions. You can do that by delcaring a pointer to point2d, making that threadprivate, and then allocating memory for the private pointer for each thread in a parallel region. Make sure you delete the allocated memory at your last parallel call.

#include <stdio.h>
#include <omp.h>

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

static point2d *myPoint;
#pragma omp threadprivate(myPoint)

int main() {

    #pragma omp parallel 
    {
        myPoint = new point2d();
        myPoint->x = omp_get_thread_num();
        myPoint->y = omp_get_thread_num()*10;
        #pragma omp critical
        {
            printf("thread %d myPoint->x %d myPoint->y %d
", omp_get_thread_num(),myPoint->x, myPoint->y);
        }
    }   
    #pragma omp parallel
    {
        #pragma omp critical
        {
            printf("thread %d myPoint->x %d myPoint->y %d
", omp_get_thread_num(),myPoint->x, myPoint->y);
        }
        delete myPoint;
    }
}

这篇关于如何在 OpenMP 中将对象或结构定义为 threadprivate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 OpenMP 中将对象或结构定义为 threadprivate?

基础教程推荐