Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize grouping indexes #1044

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 66 additions & 14 deletions src/core/ActionAtomistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@
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 ) {
Expand Down Expand Up @@ -271,15 +289,34 @@
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++;
// }
Comment on lines +293 to +301

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) {
Expand All @@ -289,13 +326,28 @@
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++;
// }
Comment on lines +330 to +336

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 );
}

Expand Down
1 change: 1 addition & 0 deletions src/core/ActionAtomistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions src/core/ActionWithValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "tools/Exception.h"
#include "tools/OpenMP.h"
#include "tools/Communicator.h"
#include "blas/blas.h"

namespace PLMD {

Expand Down Expand Up @@ -296,12 +297,26 @@
for(unsigned i=rank; i<nvalsWithForce; i+=stride) {
double ff=values[valsToForce[i]]->inputForce[0];
std::vector<double> & thisderiv( values[valsToForce[i]]->data );
if( nt>1 ) for(unsigned j=0; j<nder; ++j) omp_f[j] += ff*thisderiv[1+j];
else for(unsigned j=0; j<nder; ++j) forcesForApply[j] += ff*thisderiv[1+j];
int nn=nder;
int one1=1;
int one2=1;
if( nt>1 ) plumed_blas_daxpy(&nn,&ff,thisderiv.data()+1,&one1,omp_f.data(),&one2);
else plumed_blas_daxpy(&nn,&ff,thisderiv.data()+1,&one1,forcesForApply.data(),&one2);
// if( nt>1 ) for(unsigned j=0; j<nder; ++j) omp_f[j] += ff*thisderiv[1+j];
//else for(unsigned j=0; j<nder; ++j) forcesForApply[j] += ff*thisderiv[1+j];
Comment on lines +305 to +306

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
}
#pragma omp critical
{
if( nt>1 ) for(unsigned j=0; j<forcesForApply.size(); ++j) forcesForApply[j]+=omp_f[j];
if( nt>1 ) {
int nn=forcesForApply.size();
double one0=1.0;
int one1=1;
int one2=1;
plumed_blas_daxpy(&nn,&one0,omp_f.data(),&one1,forcesForApply.data(),&one2);
}
// for(unsigned j=0; j<forcesForApply.size(); ++j) {
// forcesForApply[j]+=omp_f[j];
// }
Comment on lines +317 to +319

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/core/ActionWithVirtualAtom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "ActionWithVirtualAtom.h"
#include <array>
#include <iostream>

namespace PLMD {

Expand Down Expand Up @@ -71,12 +72,24 @@
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++;
// }
Comment on lines +75 to +80

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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
USE=config tools lepton small_vector
USE=config tools lepton small_vector blas

# generic makefile
include ../maketools/make.module
Expand Down
Loading