I am working with a simulation that consists of a cells, objects that interact with each other and evolve over time. I am looking at parallelizing my code using the OpenMP parallel for imperative and want my cells (objects) to be updated per timestep in parallel, below you can see part of the code.
#pragma omp parallel for default(none) shared(cells, maxCellReach, parameters, cellCellJunctions, timestepDistance) firstprivate(timestep), reduction(+ : centerOfMass)
for (auto it = cells.begin(); it != cells.end(); it++) {
maxCellReach = std::max(it->getNucleus()->getCellReach(), maxCellReach);
it->ApplyIntercellularForce(cellCellJunctions, Constants::junctionCreateDistance, parameters);
it->step(timestep, parameters);
centerOfMass += it->GetCenterOfMass();
}
I have read that the loop iterator variable of a openMP parallel for loop is always private. I however, want all the cells to be updated together. Does this code achieve that or do I need a different approach with openMP?
I looked around online, but could not find anything on making loop iterators shared variables or something similar in a for loop.
maxCellReach(otherwise there is a race condition). Note that the iterator needs to be a random iterator. Note that Input/Forward/Bidirectional iterators cannot be used in such a parallel loop (and need more advanced and more expensive constructs).maxCellReach. Would you also know how to apply a reduction to a object that's part ofcell? Each cell consists of 100beads, which have interactions and their forces are updated by their own interactions and interactions with beads of other cells (usingforce += someValue). The datastructure is:std::vector<std::shared_ptr<bead>> beadswhere beads has a private force vector.shared_ptrare generally not great in term of performance, especially in a parallel loop as they tends to cause atomic accesses.shared_ptr? I believe that they are used, because different classes need to access the beads (but I am newish to C++). Previous author's state: "The spring class "saves" the two bead at the end points of the spring as two shared pointers of the bead class called bead1 and bead2"