I am quite new with OpenMP and c++ and perhaps because of this I am having some really basic problems.
I am trying to do a static schedule with all variables being private (just in case, in order to verify that the result obtained is the same as the non-parallel one).
The problem arises when I see variables such as bodies
which I do not know where they came from, as they are not previously defined.
Is it possible to define all the appearing variables, such as bodies
, as private? How could that be done
std::vector<phys_vector> forces(bodies.size());
size_t i, j; double dist, f, alpha;
#pragma omp parallel for schedule(static) private(i, j, dist, f, alpha)
for (i=0; i<bodies.size(); ++i) {
for (j = i+1; j<bodies.size(); ++j) {
dist = distance(bodies[i], bodies[j]);
if (dist > param.min_distance()) {
f = attraction(bodies[i], bodies[j], param.gravity(), dist);
alpha = angle(bodies[i],bodies[j]);
phys_vector deltaf{ f * cos(alpha) , f * sin(alpha) };
forces[i] += deltaf;
forces[j] -= deltaf;
}
}
}
return forces;
}
PS: with the current code, the execution result varies from the non-parallel execution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…