Problem to share variables for a loop

Use this forum to discuss the book: Using OpenMP - Portable Shared Memory Parallel Programming, by Barbara Chapman, Gabriele Jost and Ruud van der Pas Read the for book info and to download the examples. Post your feedback about the book and examples to this forum
Post Reply

Problem to share variables for a loop

Post by syens »

Hi everyone,

I have a problem with a loop which contains various variables, I want to share them. See the part of my code in C++ here:

Code: Select all

double *force_X;
double *WallY0_f;
double *WallY1_f ;

force_X = (double *)   calloc ((size_t) (nb), sizeof(double));
WallY0_f = (double *)   calloc ((size_t) (nb), sizeof(double));
WallY1_f = (double *)   calloc ((size_t) (nb), sizeof(double));

int i,j,k;

#pragma omp parallel for shared(force_X, force_Y, force_Z, WallY0_f, WallY1_f) private(k)
for (k = 0; k < nbreInteractions ; ++k) {

    i = particle[k];
    j = Voisin[k];

    if (_paire[k]==0)
        force_X[i] += FX[k];
        force_X[j] -= FX[k];
     else if (_paire[k]==1)
        force_X[i] -= FX[k];
        WallY0_f[i] -= FY[k];
     else if (_paire[k]==2)
        force_X[i] += FX[k];
        WallY1_f[i] += FY[k];
I am not an expert in openmp and my results are wrongs when I use openmp .
Do you see an error into my loop ?
Thanks for your request.


PS: the value of FX[k] and FY[k] is calculted into the functions: Sphere_WallY1, Sphere_WallY0 and Sphere_Sphere.


Re: Problem to share variables for a loop

Post by pQB »

Hello! also a new in openMP here :)

Taking a look at my notes, global scope variables are shared by default, but i,j should be private as each thread need its own copy.

You could try this way:

#pragma omp parallel for default(shared) private(i, j, k)

Variable 'k' is the outer loop index so it will be also private.

Good luck!

Post Reply