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)
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?
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:That to me produced the same results as my earlier experiment program:
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.