Skip to content

Commit

Permalink
Group indexes to make memory access faster
Browse files Browse the repository at this point in the history
GiovanniBussi committed Mar 13, 2024
1 parent 8701a7d commit b2a66a5
Showing 3 changed files with 85 additions and 19 deletions.
80 changes: 66 additions & 14 deletions src/core/ActionAtomistic.cpp
Original file line number Diff line number Diff line change
@@ -98,6 +98,24 @@ void ActionAtomistic::requestAtoms(const std::vector<AtomNumber> & a, const bool
if( atom_value_ind[i].first==0 ) unique.push_back(indexes[i]);
else if( atom_value_ind[i].second>0 ) error("action atomistic is not set up to deal with multiple vectors in position input");
}

atom_value_ind_grouped.clear();

if(atom_value_ind.size()>0) {
auto nn = atom_value_ind[0].first;
auto kk = atom_value_ind[0].second;
atom_value_ind_grouped.push_back(std::pair<std::size_t,std::vector<std::size_t>>(nn, {}));
atom_value_ind_grouped.back().second.push_back(kk);
auto prev_nn=nn;
for(unsigned i=1; i<atom_value_ind.size(); i++) {
auto nn = atom_value_ind[i].first;

Check notice

Code scanning / CodeQL

Declaration hides variable Note

Variable nn hides another variable of the same name (on
line 122
).
auto kk = atom_value_ind[i].second;

Check notice

Code scanning / CodeQL

Declaration hides variable Note

Variable kk hides another variable of the same name (on
line 123
).
if(nn!=prev_nn) atom_value_ind_grouped.push_back(std::pair<std::size_t,std::vector<std::size_t>>(nn, {}));
atom_value_ind_grouped.back().second.push_back(kk);
prev_nn=nn;
}
}

// Add the dependencies to the actions that we require
Tools::removeDuplicates(unique); value_depends.resize(0);
for(unsigned i=0; i<requirements.size(); ++i ) {
@@ -271,15 +289,34 @@ void ActionAtomistic::retrieveAtoms( const bool& force ) {
ActionToPutData* cv = chargev[0]->getPntrToAction()->castToActionToPutData();
if(cv) chargesWereSet=cv->hasBeenSet();
unsigned j = 0;
for(const auto & a : atom_value_ind) {
std::size_t nn = a.first, kk = a.second;
positions[j][0] = xpos[nn]->data[kk];
positions[j][1] = ypos[nn]->data[kk];
positions[j][2] = zpos[nn]->data[kk];
charges[j] = chargev[nn]->data[kk];
masses[j] = masv[nn]->data[kk];
j++;

// for(const auto & a : atom_value_ind) {
// std::size_t nn = a.first, kk = a.second;
// positions[j][0] = xpos[nn]->data[kk];
// positions[j][1] = ypos[nn]->data[kk];
// positions[j][2] = zpos[nn]->data[kk];
// charges[j] = chargev[nn]->data[kk];
// masses[j] = masv[nn]->data[kk];
// j++;
// }

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

for(const auto & a : atom_value_ind_grouped) {
const auto nn=a.first;
auto & xp=xpos[nn]->data;
auto & yp=ypos[nn]->data;
auto & zp=zpos[nn]->data;
auto & ch=chargev[nn]->data;
auto & ma=masv[nn]->data;
for(const auto & kk : a.second) {
positions[j][0] = xp[kk];
positions[j][1] = yp[kk];
positions[j][2] = zp[kk];
charges[j] = ch[kk];
masses[j] = ma[kk];
j++;
}
}

}

void ActionAtomistic::setForcesOnAtoms(const std::vector<double>& forcesToApply, unsigned& ind) {
@@ -289,13 +326,28 @@ void ActionAtomistic::setForcesOnAtoms(const std::vector<double>& forcesToApply,
ypos[value_depends[i]]->hasForce = true;
zpos[value_depends[i]]->hasForce = true;
}
for(const auto & a : atom_value_ind) {
plumed_dbg_massert( ind<forcesToApply.size(), "problem setting forces in " + getLabel() );
std::size_t nn = a.first, kk = a.second;
xpos[nn]->inputForce[kk] += forcesToApply[ind]; ind++;
ypos[nn]->inputForce[kk] += forcesToApply[ind]; ind++;
zpos[nn]->inputForce[kk] += forcesToApply[ind]; ind++;

// for(const auto & a : atom_value_ind) {
// plumed_dbg_massert( ind<forcesToApply.size(), "problem setting forces in " + getLabel() );
// std::size_t nn = a.first, kk = a.second;
// xpos[nn]->inputForce[kk] += forcesToApply[ind]; ind++;
// ypos[nn]->inputForce[kk] += forcesToApply[ind]; ind++;
// zpos[nn]->inputForce[kk] += forcesToApply[ind]; ind++;
// }

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

for(const auto & a : atom_value_ind_grouped) {
const auto nn=a.first;
plumed_dbg_assert(ind<forcesToApply.size()) << "problem setting forces in " << getLabel();
auto & xp=xpos[nn]->inputForce;
auto & yp=ypos[nn]->inputForce;
auto & zp=zpos[nn]->inputForce;
for(const auto & kk : a.second) {
xp[kk] += forcesToApply[ind]; ind++;
yp[kk] += forcesToApply[ind]; ind++;
zp[kk] += forcesToApply[ind]; ind++;
}
}

setForcesOnCell( forcesToApply, ind );
}

1 change: 1 addition & 0 deletions src/core/ActionAtomistic.h
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ class ActionAtomistic :
std::vector<AtomNumber> indexes; // the set of needed atoms
std::vector<std::size_t> value_depends; // The list of values that are being used
std::vector<std::pair<std::size_t, std::size_t > > atom_value_ind; // The list of values and indices for the atoms that are being used
std::vector<std::pair<std::size_t,std::vector<std::size_t>>> atom_value_ind_grouped;
/// unique should be an ordered set since we later create a vector containing the corresponding indexes
std::vector<AtomNumber> unique;
/// unique_local should be an ordered set since we later create a vector containing the corresponding indexes
23 changes: 18 additions & 5 deletions src/core/ActionWithVirtualAtom.cpp
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "ActionWithVirtualAtom.h"
#include <array>
#include <iostream>

namespace PLMD {

@@ -71,12 +72,24 @@ void ActionWithVirtualAtom::apply() {
double xf = xval->inputForce[0];
double yf = yval->inputForce[0];
double zf = zval->inputForce[0];
for(const auto & a : atom_value_ind) {
std::size_t nn = a.first, kk = a.second;
xpos[nn]->inputForce[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
ypos[nn]->inputForce[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
zpos[nn]->inputForce[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
// for(const auto & a : atom_value_ind) {
// std::size_t nn = a.first, kk = a.second;
// xpos[nn]->inputForce[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
// ypos[nn]->inputForce[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
// zpos[nn]->inputForce[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
// }

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
for(const auto & a : atom_value_ind_grouped) {
const auto nn=a.first;
auto & xp=xpos[nn]->inputForce;
auto & yp=ypos[nn]->inputForce;
auto & zp=zpos[nn]->inputForce;
for(const auto & kk : a.second) {
xp[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
yp[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
zp[kk] += xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++;
}
}

std::array<double,9> virial;
for(unsigned i=0; i<9; ++i) { virial[i] = xf*xval->data[1+k] + yf*yval->data[1+k] + zf*zval->data[1+k]; k++; }
unsigned ind = 0; setForcesOnCell( virial.data(), virial.size(), ind );

1 comment on commit b2a66a5

@PlumedBot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found broken examples in automatic/ADAPTIVE_PATH.tmp
Found broken examples in automatic/ANGLES.tmp
Found broken examples in automatic/ANN.tmp
Found broken examples in automatic/AROUND.tmp
Found broken examples in automatic/CAVITY.tmp
Found broken examples in automatic/CLUSTER_DIAMETER.tmp
Found broken examples in automatic/CLUSTER_DISTRIBUTION.tmp
Found broken examples in automatic/CLUSTER_PROPERTIES.tmp
Found broken examples in automatic/CONSTANT.tmp
Found broken examples in automatic/CONTACT_MATRIX.tmp
Found broken examples in automatic/CONVERT_TO_FES.tmp
Found broken examples in automatic/COORDINATIONNUMBER.tmp
Found broken examples in automatic/DFSCLUSTERING.tmp
Found broken examples in automatic/DISTANCE_FROM_CONTOUR.tmp
Found broken examples in automatic/DUMPCUBE.tmp
Found broken examples in automatic/DUMPGRID.tmp
Found broken examples in automatic/EDS.tmp
Found broken examples in automatic/EMMI.tmp
Found broken examples in automatic/ENVIRONMENTSIMILARITY.tmp
Found broken examples in automatic/FIND_CONTOUR.tmp
Found broken examples in automatic/FIND_CONTOUR_SURFACE.tmp
Found broken examples in automatic/FIND_SPHERICAL_CONTOUR.tmp
Found broken examples in automatic/FOURIER_TRANSFORM.tmp
Found broken examples in automatic/FUNCPATHGENERAL.tmp
Found broken examples in automatic/FUNCPATHMSD.tmp
Found broken examples in automatic/FUNNEL.tmp
Found broken examples in automatic/FUNNEL_PS.tmp
Found broken examples in automatic/GHBFIX.tmp
Found broken examples in automatic/GPROPERTYMAP.tmp
Found broken examples in automatic/HBOND_MATRIX.tmp
Found broken examples in automatic/HISTOGRAM.tmp
Found broken examples in automatic/INCLUDE.tmp
Found broken examples in automatic/INCYLINDER.tmp
Found broken examples in automatic/INENVELOPE.tmp
Found broken examples in automatic/INSPHERE.tmp
Found broken examples in automatic/INTERPOLATE_GRID.tmp
Found broken examples in automatic/LOCAL_AVERAGE.tmp
Found broken examples in automatic/LOCAL_Q3.tmp
Found broken examples in automatic/LOCAL_Q4.tmp
Found broken examples in automatic/LOCAL_Q6.tmp
Found broken examples in automatic/MAZE_OPTIMIZER_BIAS.tmp
Found broken examples in automatic/MAZE_RANDOM_ACCELERATION_MD.tmp
Found broken examples in automatic/MAZE_SIMULATED_ANNEALING.tmp
Found broken examples in automatic/MAZE_STEERED_MD.tmp
Found broken examples in automatic/MULTICOLVARDENS.tmp
Found broken examples in automatic/OUTPUT_CLUSTER.tmp
Found broken examples in automatic/PAMM.tmp
Found broken examples in automatic/PARABETARMSD.tmp
Found broken examples in automatic/PATH.tmp
Found broken examples in automatic/PCAVARS.tmp
Found broken examples in automatic/PIV.tmp
Found broken examples in automatic/PLUMED.tmp
Found broken examples in automatic/PYCVINTERFACE.tmp
Found broken examples in automatic/PYTHONFUNCTION.tmp
Found broken examples in automatic/QUATERNION.tmp
Found broken examples in automatic/REWEIGHT_BIAS.tmp
Found broken examples in automatic/REWEIGHT_METAD.tmp
Found broken examples in automatic/SPRINT.tmp
Found broken examples in automatic/TETRAHEDRALPORE.tmp
Found broken examples in automatic/TORSION.tmp
Found broken examples in automatic/TORSIONS.tmp
Found broken examples in automatic/WHAM_HISTOGRAM.tmp
Found broken examples in automatic/WHAM_WEIGHTS.tmp
Found broken examples in AnalysisPP.md
Found broken examples in CollectiveVariablesPP.md
Found broken examples in MiscelaneousPP.md

Please sign in to comment.