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

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

问题描述

我不知道如何将struct或对象作为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;
        }
    };      

我声明一个静态结构,并尝试让它们为threadprivate

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\n",&myPoint);
    }

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

    }


推荐答案

在C ++中,带方法的结构是一个类,其默认值为public。这不是普通数据(POD) MSVC似乎暗示它可以处理threadprivate对象(即非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)一起工作。你可以使用threadprivate指针。

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

threadprivate的purpuse是每个线程有一个对象/类型的私有版本,并且在平行区域之间持久化。你可以通过del caring指向 point2d 的指针,使该threadprivate,然后为并行区域中的每个线程的私有指针分配内存。请确保您在上次并行调用时删除已分配的内存。

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\n", omp_get_thread_num(),myPoint->x, myPoint->y);
        }
    }   
    #pragma omp parallel
    {
        #pragma omp critical
        {
            printf("thread %d myPoint->x %d myPoint->y %d\n", omp_get_thread_num(),myPoint->x, myPoint->y);
        }
        delete myPoint;
    }
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆