如何保证动态分配的数组是私人的OpenMP [英] How to ensure a dynamically allocated array is private in openmp

查看:1225
本文介绍了如何保证动态分配的数组是私人的OpenMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C使用OpenMP使用Linux机器上的gcc工作。在fo​​r循环OpenMP并行,我可以声明一个静态分配的数组为私有。考虑code片段:

I'm working in C with openMP using gcc on a linux machine. In an openmp parallel for loop, I can declare a statically allocated array as private. Consider the code fragment:

int a[10];
#pragma omp parallel for shared(none) firstprivate(a)
for(i=0;i<4;i++){

和一切正常。但是,如果不是我分配一个动态,

And everything works as expected. But if instead I allocate a dynamically,

int * a = (int *) malloc(10*sizeof(int));
#pragma omp parallel for shared(none) firstprivate(a)

的值的(至少一个[1 ... 9])没有被保护,但是充当如果它们共享。这是可以理解为没有在编译命令似乎告诉OMP的数组有多大是需要是私有的。我怎么能将此信息传递给OpenMP的?我如何申报了整个的动态分配的数组为私有?

the values of a (at least a[1...9]) are not protected but act as if they are shared. This is understandable as nothing in the pragma command seems to tell omp how big the array a is that needs to be private. How can I pass this information to openmp? How do I declare the entire the dynamically allocated array as private?

推荐答案

我不认为你做的 - 我做了什么来解决这个问题,使用并行区域的#pragma OMP PARALLEL共享(。 ..)民​​营(...)和动态分配的并行区域内的阵列。试试这个:

I don't think you do - what I did to solve this problem was used a parallel region #pragma omp parallel shared(...) private(...) and allocated the array dynamically inside the parallel region. Try this:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/* compile with gcc -o test2 -fopenmp test2.c */

int main(int argc, char** argv)
{
    int i = 0;
    int size = 20;
    int* a = (int*) calloc(size, sizeof(int));
    int* b = (int*) calloc(size, sizeof(int));
    int* c;

    for ( i = 0; i < size; i++ )
    {
        a[i] = i;
        b[i] = size-i;
        printf("[BEFORE] At %d: a=%d, b=%d\n", i, a[i], b[i]);
    }

    #pragma omp parallel shared(a,b) private(c,i)
    {
        c = (int*) calloc(3, sizeof(int));

        #pragma omp for
        for ( i = 0; i < size; i++ )
        {
            c[0] = 5*a[i];
            c[1] = 2*b[i];
            c[2] = -2*i;
            a[i] = c[0]+c[1]+c[2];

            c[0] = 4*a[i];
            c[1] = -1*b[i];
            c[2] = i;
            b[i] = c[0]+c[1]+c[2];
        }

        free(c);
    }

    for ( i = 0; i < size; i++ )
    {
        printf("[AFTER] At %d: a=%d, b=%d\n", i, a[i], b[i]);
    }
}

这对我产生了相同的结果,我先前的实验计划:

That to me produced the same results as my earlier experiment program:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/* compile with gcc -o test1 -fopenmp test1.c */

int main(int argc, char** argv)
{
    int i = 0;
    int size = 20;
    int* a = (int*) calloc(size, sizeof(int));
    int* b = (int*) calloc(size, sizeof(int));

    for ( i = 0; i < size; i++ )
    {
        a[i] = i;
        b[i] = size-i;
        printf("[BEFORE] At %d: a=%d, b=%d\n", i, a[i], b[i]);
    }

    #pragma omp parallel for shared(a,b) private(i)
    for ( i = 0; i < size; i++ )
    {
        a[i] = 5*a[i]+2*b[i]-2*i;
        b[i] = 4*a[i]-b[i]+i;
    }

    for ( i = 0; i < size; i++ )
    {
        printf("[AFTER] At %d: a=%d, b=%d\n", i, a[i], b[i]);
    }
}

在猜测我会说,因为OpenMP的不能推导出数组的大小就不能私有 - 只有编译时数组可以这样做。我得到段错误,当我尝试私人动态分配的数组,presumably因为访问违规。分配,如果你写这个的pthreads使用每个线程的阵列是有道理的,解决这个问题。

At a guess I'd say because OpenMP can't deduce the size of the array it can't be private - only compile-time arrays can be done this way. I get segfaults when I try to private a dynamically allocated array, presumably because of access violations. Allocating the array on each thread as if you'd written this using pthreads makes sense and solves the issue.

这篇关于如何保证动态分配的数组是私人的OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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