I have a nested loop and im confused on which variables should be shared or private. I believe that i
and j
should be private but im confused on whether other variables also need to be shared or privated in my code:
y = p->yMax;
#pragma omp parallel for private(i,j)
for (i = 0; i < p->height; i ) {
x = p->xMin;
for (j = 0; j < p->width; j ) {
p->carray[i * p->width j] = x y * I;
x = p->step;
}
y -= p->step;
}
for (i = 0; i < p->maxIter; i ) {
p->histogram[i] = 0;
}
}
Cheers
CodePudding user response:
- Assuming that you do not need the values of i and j after the loops and observing that you reset i and j to zero at the beginning of the loops, I would simply define the i and j within the loop. If so, you don't have to state the behaviour of i and j when entering and exiting the loop (because it is automatically private).
- I think that the value of x before entering the loop is not needed as well, so you can also go for private here or define x as a local varieble.
- If y is treated similar as x and has to be reset to p->yMax in every step, you can either declare y as private and move into the loop, same as x. Or you have to declare y as firstprivate such that the previous value is not lost when entering the parallel region. Using shared is not an option for both x and y since you = and -= change the value of these variables.
- p should be shared, as you need the values of p->xMin, p->step and p->yMax and you probably also want to use the values of p->carray after exiting the loop.
``
#pragma omp parallel for shared(p)
for (size_t i = 0; i < p->height; i ) {
auto x = p->xMin;
auto y = p->yMax;
for (size_t j = 0; j < p->width; j ) {
p->carray[i * p->width j] = x y * I;
x = p->step;
}
y -= p->step;
}
//This one isn't in the parallel region
for (size_t i = 0; i < p->maxIter; i ) {
p->histogram[i] = 0;
}