How to ensure that a dynamically allocated array is private in openmp

I am working in C with openMP using gcc on a Linux machine. In the openmp parallel loop, I can declare a statically allocated array as closed. Consider a piece of code:

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 dynamically,

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

the values ​​of a (at least [1 ... 9]) are not protected, but act as if they were separated. This is understandable because nothing in the pragma command tells omp how large the array a is to be closed. How to transfer this information to openmp? How to declare an entire dynamically allocated array as closed?

+9
c malloc parallel-processing openmp
Feb 28 '10 at 22:13
source share
2 answers

I don’t think what you did - what I did to solve this problem was used in the parallel area #pragma omp parallel shared(...) private(...) and dynamically allocated the array inside the parallel area. Try the following:

 #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 they gave me the same results as my previous experimental 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]); } } 

Under the assumption that I would say because OpenMP cannot determine the size of the array, it cannot be private - only compile-time arrays can be executed this way. I get segfaults when I try to privatize a dynamically allocated array, presumably due to access violations. Allocating an array in each thread, as if you wrote it with pthreads, makes sense and solves the problem.

+12
Feb 28 '10 at 23:29
source share

You told OpenMP that the pointer a is private, that is, it is replicated in each thread. Your array is just some arbitrary data a , and OpenMP will not replicate it (perhaps because it would require allocating and freeing the replicated arrays).

+6
Feb 17 '11 at 7:12
source share



All Articles