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 viewtopic.php?f=8&t=465 for book info and to download the examples. Post your feedback about the book and examples to this forum

Problem to share variables for a loop

Postby syens » Thu Jun 10, 2010 7:42 am

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

Postby pQB » Fri Jun 11, 2010 2:56 am

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!

Return to Using OpenMP - The Book and Examples

Who is online

Users browsing this forum: No registered users and 1 guest