Skip to content

Commit 0511455

Browse files
committed
Same trick on wholemolecules
speedup 3%, but code is a mess
1 parent b2a66a5 commit 0511455

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

src/core/ActionAtomistic.h

+10
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class ActionAtomistic :
103103
/// \warning Should be only used by actions that need to modify the shared position array.
104104
/// This array is insensitive to local changes such as makeWhole(), numerical derivatives, etc.
105105
void setGlobalPosition(const std::pair<std::size_t,std::size_t>&, const Vector& pos);
106+
std::array<double*,3> getValueData(std::size_t nn);
106107
/// Get total number of atoms, including virtual ones.
107108
/// Can be used to make a loop on modifyGlobalPosition or getGlobalPosition.
108109
unsigned getTotAtoms()const;
@@ -277,6 +278,15 @@ void ActionAtomistic::setGlobalPosition(const std::pair<std::size_t, std::size_t
277278
zpos[a.first]->data[a.second]=pos[2];
278279
}
279280

281+
inline
282+
std::array<double*,3> ActionAtomistic::getValueData(std::size_t nn) {
283+
std::array<double*,3> ptr;
284+
ptr[0]=xpos[nn]->data.data();
285+
ptr[1]=ypos[nn]->data.data();
286+
ptr[2]=zpos[nn]->data.data();
287+
return ptr;
288+
}
289+
280290
inline
281291
Vector ActionAtomistic::getForce( const std::pair<std::size_t, std::size_t>& a ) const {
282292
Vector f;

src/generic/WholeMolecules.cpp

+55-24
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ class WholeMolecules:
103103
public ActionPilot,
104104
public ActionAtomistic
105105
{
106-
std::vector<std::vector<std::pair<std::size_t,std::size_t> > > p_groups;
107-
std::vector<std::vector<std::pair<std::size_t,std::size_t> > > p_roots;
106+
std::vector<std::vector<std::pair<std::size_t,std::vector<std::size_t>>>> p_groups;
107+
std::vector<std::vector<std::pair<std::size_t,std::vector<std::size_t>>>> p_roots;
108108
std::vector<Vector> refs;
109109
bool doemst, addref;
110110
public:
@@ -221,17 +221,35 @@ WholeMolecules::WholeMolecules(const ActionOptions&ao):
221221
// Convert groups to p_groups
222222
p_groups.resize( groups.size() );
223223
for(unsigned i=0; i<groups.size(); ++i) {
224-
p_groups[i].resize( groups[i].size() );
225-
for(unsigned j=0; j<groups[i].size(); ++j) p_groups[i][j] = getValueIndices( groups[i][j] );
224+
std::size_t prev_nn=0;
225+
bool first=true;
226+
for(unsigned j=0; j<groups[i].size(); ++j) {
227+
auto a = getValueIndices( groups[i][j] );
228+
auto nn=a.first;
229+
auto kk=a.second;
230+
if(first || nn!=prev_nn) p_groups.back().push_back(std::pair<std::size_t,std::vector<std::size_t>>(nn,{}));
231+
p_groups.back().back().second.push_back(kk);
232+
first=false;
233+
prev_nn=nn;
234+
}
226235
}
236+
227237
// Convert roots to p_roots
228238
p_roots.resize( roots.size() );
229239
for(unsigned i=0; i<roots.size(); ++i) {
230-
p_roots[i].resize( roots[i].size() );
231-
for(unsigned j=0; j<roots[i].size(); ++j) p_roots[i][j] = getValueIndices( roots[i][j] );
240+
bool first=true;
241+
std::size_t prev_nn=0;
242+
for(unsigned j=0; j<roots[i].size(); ++j) {
243+
auto a = getValueIndices( roots[i][j] );
244+
auto nn=a.first;
245+
auto kk=a.second;
246+
if(first || nn!=prev_nn) p_roots.back().push_back(std::pair<std::size_t,std::vector<std::size_t>>(nn,{}));
247+
p_roots.back().back().second.push_back(kk);
248+
first=false;
249+
prev_nn=nn;
250+
}
232251
}
233252

234-
235253
checkRead();
236254
Tools::removeDuplicates(merge);
237255
requestAtoms(merge);
@@ -241,25 +259,38 @@ WholeMolecules::WholeMolecules(const ActionOptions&ao):
241259

242260
void WholeMolecules::calculate() {
243261
for(unsigned i=0; i<p_groups.size(); ++i) {
244-
Vector first = getGlobalPosition(p_groups[i][0]);
245-
if(addref) {
246-
first = refs[i]+pbcDistance(refs[i],first);
247-
setGlobalPosition( p_groups[i][0], first );
248-
}
249-
if(!doemst) {
250-
for(unsigned j=1; j<p_groups[i].size(); ++j) {
251-
Vector second=getGlobalPosition(p_groups[i][j]);
252-
first = first+pbcDistance(first,second);
253-
setGlobalPosition(p_groups[i][j], first );
254-
}
255-
} else {
256-
for(unsigned j=1; j<p_groups[i].size(); ++j) {
257-
Vector first=getGlobalPosition(p_roots[i][j-1]);
258-
Vector second=getGlobalPosition(p_groups[i][j]);
259-
second=first+pbcDistance(first,second);
260-
setGlobalPosition(p_groups[i][j], second );
262+
bool start=true;
263+
// if(addref) {
264+
// first = refs[i]+pbcDistance(refs[i],first);
265+
// setGlobalPosition( p_groups[i][0], first );
266+
// }
267+
// if(!doemst) {
268+
269+
Vector first;
270+
for(unsigned j=0; j<p_groups[i].size(); ++j) {
271+
auto data = getValueData(p_groups[i][j].first);
272+
for(const auto kk : p_groups[i][j].second) {
273+
Vector second{
274+
data[0][kk],
275+
data[1][kk],
276+
data[2][kk],
277+
};
278+
if(start) first=second;
279+
else first += pbcDistance(first,second);
280+
start=false;
281+
data[0][kk]=first[0];
282+
data[1][kk]=first[1];
283+
data[2][kk]=first[2];
261284
}
262285
}
286+
// } else {
287+
// for(unsigned j=1; j<p_groups[i].size(); ++j) {
288+
// Vector first=getGlobalPosition(p_roots[i][j-1]);
289+
// Vector second=getGlobalPosition(p_groups[i][j]);
290+
// second=first+pbcDistance(first,second);
291+
// setGlobalPosition(p_groups[i][j], second );
292+
// }
293+
// }
263294
}
264295
}
265296

0 commit comments

Comments
 (0)