## 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

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)
{
Sphere_Sphere(i,j,k);
force_X[i] += FX[k];
force_X[j] -= FX[k];
}
else if (_paire[k]==1)
{
Sphere_WallY0(i,k);
force_X[i] -= FX[k];
WallY0_f[i] -= FY[k];
}
else if (_paire[k]==2)
{
Sphere_WallY1(i,k);
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.

Regards
Jf

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

Posts: 3
Joined: Wed Jun 09, 2010 2:19 pm
Location: Lausanne

### Re: Problem to share variables for a loop

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!
pQB

Posts: 63
Joined: Thu Jun 03, 2010 11:25 am
Location: Spain

Return to Using OpenMP - The Book and Examples

### Who is online

Users browsing this forum: No registered users and 0 guests