-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComputeHomeTuples.h
444 lines (387 loc) · 14.7 KB
/
ComputeHomeTuples.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
*** Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by
*** The Board of Trustees of the University of Illinois.
*** All rights reserved.
**/
#ifndef COMPUTEHOMETUPLES_H
#define COMPUTEHOMETUPLES_H
#include "NamdTypes.h"
#include "common.h"
#include "structures.h"
#include "Compute.h"
#include "HomePatch.h"
#include "Box.h"
#include "OwnerBox.h"
#include "UniqueSet.h"
#include "Node.h"
#include "SimParameters.h"
#include "PatchMap.inl"
#include "AtomMap.h"
#include "ComputeHomeTuples.h"
#include "PatchMgr.h"
#include "HomePatchList.h"
#include "Molecule.h"
#include "Parameters.h"
#include "ReductionMgr.h"
#include "UniqueSet.h"
#include "UniqueSetIter.h"
#include "Priorities.h"
#include "LdbCoordinator.h"
class TuplePatchElem {
public:
PatchID patchID;
Patch *p;
Box<Patch,CompAtom> *positionBox;
Box<Patch,CompAtom> *avgPositionBox;
Box<Patch,Results> *forceBox;
CompAtom *x;
CompAtomExt *xExt;
CompAtom *x_avg;
Results *r;
Force *f;
Force *af;
int hash() const { return patchID; }
TuplePatchElem(PatchID pid = -1) {
patchID = pid;
p = NULL;
positionBox = NULL;
avgPositionBox = NULL;
forceBox = NULL;
x = NULL;
xExt = NULL;
x_avg = NULL;
r = NULL;
f = NULL;
af = NULL;
}
TuplePatchElem(Patch *p_param, Compute *cid) {
patchID = p_param->getPatchID();
p = p_param;
positionBox = p_param->registerPositionPickup(cid);
avgPositionBox = p_param->registerAvgPositionPickup(cid);
forceBox = p_param->registerForceDeposit(cid);
x = NULL;
xExt = NULL;
x_avg = NULL;
r = NULL;
f = NULL;
af = NULL;
}
~TuplePatchElem() {};
int operator==(const TuplePatchElem &elem) const {
return (elem.patchID == patchID);
}
int operator<(const TuplePatchElem &elem) const {
return (patchID < elem.patchID);
}
};
typedef UniqueSet<TuplePatchElem> TuplePatchList;
typedef UniqueSetIter<TuplePatchElem> TuplePatchListIter;
class AtomMap;
class ReductionMgr;
#ifdef MEM_OPT_VERSION
template <class T> struct ElemTraits {
typedef AtomSignature signature;
static signature* get_sig_pointer(Molecule *mol) { return mol->atomSigPool; }
static int get_sig_id(const CompAtomExt &a) { return a.sigId; }
};
template <> struct ElemTraits <ExclElem> {
typedef ExclusionSignature signature;
static signature* get_sig_pointer(Molecule *mol) { return mol->exclSigPool; }
static int get_sig_id(const CompAtomExt &a) { return a.exclId; }
};
#endif
template <class T, class S, class P> class ComputeHomeTuples : public Compute {
protected:
virtual void loadTuples(void) {
int numTuples;
#ifdef MEM_OPT_VERSION
typename ElemTraits<T>::signature *allSigs;
#else
int32 **tuplesByAtom;
/* const (need to propagate const) */ S *tupleStructs;
#endif
const P *tupleValues;
Node *node = Node::Object();
#ifdef MEM_OPT_VERSION
allSigs = ElemTraits<T>::get_sig_pointer(node->molecule);
#else
T::getMoleculePointers(node->molecule,
&numTuples, &tuplesByAtom, &tupleStructs);
#endif
T::getParameterPointers(node->parameters, &tupleValues);
tupleList.resize(0);
LocalID aid[T::size];
const int lesOn = node->simParameters->lesOn;
Real invLesFactor = lesOn ?
1.0/node->simParameters->lesFactor :
1.0;
// cycle through each patch and gather all tuples
TuplePatchListIter ai(tuplePatchList);
for ( ai = ai.begin(); ai != ai.end(); ai++ )
{
// CompAtom *atom = (*ai).x;
Patch *patch = (*ai).p;
int numAtoms = patch->getNumAtoms();
CompAtomExt *atomExt = (*ai).xExt; //patch->getCompAtomExtInfo();
// cycle through each atom in the patch and load up tuples
for (int j=0; j < numAtoms; j++)
{
/* cycle through each tuple */
#ifdef MEM_OPT_VERSION
typename ElemTraits<T>::signature *thisAtomSig =
&allSigs[ElemTraits<T>::get_sig_id(atomExt[j])];
TupleSignature *allTuples;
T::getTupleInfo(thisAtomSig, &numTuples, &allTuples);
for(int k=0; k<numTuples; k++) {
T t(atomExt[j].id, &allTuples[k], tupleValues);
#else
/* get list of all tuples for the atom */
int32 *curTuple = tuplesByAtom[atomExt[j].id];
for( ; *curTuple != -1; ++curTuple) {
T t(&tupleStructs[*curTuple],tupleValues);
#endif
register int i;
aid[0] = atomMap->localID(t.atomID[0]);
int homepatch = aid[0].pid;
int samepatch = 1;
int has_les = lesOn && node->molecule->get_fep_type(t.atomID[0]);
for (i=1; i < T::size; i++) {
aid[i] = atomMap->localID(t.atomID[i]);
samepatch = samepatch && ( homepatch == aid[i].pid );
has_les |= lesOn && node->molecule->get_fep_type(t.atomID[i]);
}
if ( samepatch ) continue;
t.scale = has_les ? invLesFactor : 1;
for (i=1; i < T::size; i++) {
homepatch = patchMap->downstream(homepatch,aid[i].pid);
}
if ( homepatch != notUsed && isBasePatch[homepatch] ) {
TuplePatchElem *p;
for (i=0; i < T::size; i++) {
t.p[i] = p = tuplePatchList.find(TuplePatchElem(aid[i].pid));
if ( ! p ) {
#ifdef MEM_OPT_VERSION
iout << iWARN << "Tuple with atoms ";
#else
iout << iWARN << "Tuple " << *curTuple << " with atoms ";
#endif
int erri;
for( erri = 0; erri < T::size; erri++ ) {
iout << t.atomID[erri] << "(" << aid[erri].pid << ") ";
}
iout << "missing patch " << aid[i].pid << "\n" << endi;
break;
}
t.localIndex[i] = aid[i].index;
}
if ( ! p ) continue;
#ifdef MEM_OPT_VERSION
//avoid adding Tuples whose atoms are all fixed
if(node->simParameters->fixedAtomsOn &&
!node->simParameters->fixedAtomsForces) {
int allfixed = 1;
for(i=0; i<T::size; i++){
CompAtomExt *one = &(t.p[i]->xExt[aid[i].index]);
allfixed = allfixed & one->atomFixed;
}
if(!allfixed) tupleList.add(t);
}else{
tupleList.add(t);
}
#else
tupleList.add(t);
#endif
}
}
}
}
}
int doLoadTuples;
protected:
ResizeArray<T> tupleList;
TuplePatchList tuplePatchList;
PatchMap *patchMap;
AtomMap *atomMap;
SubmitReduction *reduction;
int accelMDdoDihe;
SubmitReduction *pressureProfileReduction;
BigReal *pressureProfileData;
int pressureProfileSlabs;
char *isBasePatch;
ComputeHomeTuples(ComputeID c) : Compute(c) {
patchMap = PatchMap::Object();
atomMap = AtomMap::Object();
reduction = ReductionMgr::Object()->willSubmit(REDUCTIONS_BASIC);
SimParameters *params = Node::Object()->simParameters;
accelMDdoDihe=false;
if (params->accelMDOn) {
if (params->accelMDdihe || params->accelMDdual) accelMDdoDihe=true;
}
if (params->pressureProfileOn) {
pressureProfileSlabs = T::pressureProfileSlabs =
params->pressureProfileSlabs;
int n = T::pressureProfileAtomTypes = params->pressureProfileAtomTypes;
pressureProfileReduction = ReductionMgr::Object()->willSubmit(
REDUCTIONS_PPROF_BONDED, 3*pressureProfileSlabs*((n*(n+1))/2));
int numAtomTypePairs = n*n;
pressureProfileData = new BigReal[3*pressureProfileSlabs*numAtomTypePairs];
} else {
pressureProfileReduction = NULL;
pressureProfileData = NULL;
}
doLoadTuples = false;
isBasePatch = 0;
}
ComputeHomeTuples(ComputeID c, PatchIDList &pids) : Compute(c) {
patchMap = PatchMap::Object();
atomMap = AtomMap::Object();
reduction = ReductionMgr::Object()->willSubmit(REDUCTIONS_BASIC);
SimParameters *params = Node::Object()->simParameters;
accelMDdoDihe=false;
if (params->accelMDOn) {
if (params->accelMDdihe || params->accelMDdual) accelMDdoDihe=true;
}
if (params->pressureProfileOn) {
pressureProfileSlabs = T::pressureProfileSlabs =
params->pressureProfileSlabs;
int n = T::pressureProfileAtomTypes = params->pressureProfileAtomTypes;
pressureProfileReduction = ReductionMgr::Object()->willSubmit(
REDUCTIONS_PPROF_BONDED, 3*pressureProfileSlabs*((n*(n+1))/2));
int numAtomTypePairs = n*n;
pressureProfileData = new BigReal[3*pressureProfileSlabs*numAtomTypePairs];
} else {
pressureProfileReduction = NULL;
pressureProfileData = NULL;
}
doLoadTuples = false;
int nPatches = patchMap->numPatches();
isBasePatch = new char[nPatches];
int i;
for (i=0; i<nPatches; ++i) { isBasePatch[i] = 0; }
for (i=0; i<pids.size(); ++i) { isBasePatch[pids[i]] = 1; }
}
public:
virtual ~ComputeHomeTuples() {
delete reduction;
delete [] isBasePatch;
delete pressureProfileReduction;
delete pressureProfileData;
}
//======================================================================
// initialize() - Method is invoked only the first time
// atom maps, patchmaps etc are ready and we are about to start computations
//======================================================================
virtual void initialize(void) {
// Start with empty list
tuplePatchList.clear();
int nPatches = patchMap->numPatches();
int pid;
for (pid=0; pid<nPatches; ++pid) {
if ( isBasePatch[pid] ) {
Patch *patch = patchMap->patch(pid);
tuplePatchList.add(TuplePatchElem(patch, this));
}
}
// Gather all proxy patches (neighbors, that is)
PatchID neighbors[PatchMap::MaxOneOrTwoAway];
for (pid=0; pid<nPatches; ++pid) if ( isBasePatch[pid] ) {
int numNeighbors = patchMap->upstreamNeighbors(pid,neighbors);
for ( int i = 0; i < numNeighbors; ++i ) {
if ( ! tuplePatchList.find(TuplePatchElem(neighbors[i])) ) {
Patch *patch = patchMap->patch(neighbors[i]);
tuplePatchList.add(TuplePatchElem(patch, this));
}
}
}
setNumPatches(tuplePatchList.size());
doLoadTuples = true;
basePriority = COMPUTE_PROXY_PRIORITY; // no patch dependence
}
//======================================================================
// atomUpdate() - Method is invoked after anytime that atoms have been
// changed in patches used by this Compute object.
//======================================================================
void atomUpdate(void) {
doLoadTuples = true;
}
//-------------------------------------------------------------------
// Routine which is called by enqueued work msg. It wraps
// actualy Force computation with the apparatus needed
// to get access to atom positions, return forces etc.
//-------------------------------------------------------------------
virtual void doWork(void) {
LdbCoordinator::Object()->startWork(ldObjHandle);
// Open Boxes - register that we are using Positions
// and will be depositing Forces.
UniqueSetIter<TuplePatchElem> ap(tuplePatchList);
for (ap = ap.begin(); ap != ap.end(); ap++) {
ap->x = ap->positionBox->open();
ap->xExt = ap->p->getCompAtomExtInfo();
if ( ap->p->flags.doMolly ) ap->x_avg = ap->avgPositionBox->open();
ap->r = ap->forceBox->open();
ap->f = ap->r->f[Results::normal];
if (accelMDdoDihe) ap->af = ap->r->f[Results::amdf]; // for dihedral-only or dual-boost accelMD
}
BigReal reductionData[T::reductionDataSize];
int tupleCount = 0;
int numAtomTypes = T::pressureProfileAtomTypes;
int numAtomTypePairs = numAtomTypes*numAtomTypes;
for ( int i = 0; i < T::reductionDataSize; ++i ) reductionData[i] = 0;
if (pressureProfileData) {
memset(pressureProfileData, 0, 3*pressureProfileSlabs*numAtomTypePairs*sizeof(BigReal));
// Silly variable hiding of the previous iterator
UniqueSetIter<TuplePatchElem> newap(tuplePatchList);
newap = newap.begin();
const Lattice &lattice = newap->p->lattice;
T::pressureProfileThickness = lattice.c().z / pressureProfileSlabs;
T::pressureProfileMin = lattice.origin().z - 0.5*lattice.c().z;
}
if ( ! Node::Object()->simParameters->commOnly ) {
if ( doLoadTuples ) {
loadTuples();
doLoadTuples = false;
}
// take triplet and pass with tuple info to force eval
T *al = tupleList.begin();
const int ntuple = tupleList.size();
for (int i=0; i<ntuple; ++i) {
al[i].computeForce(reductionData, pressureProfileData);
}
tupleCount += ntuple;
}
LdbCoordinator::Object()->endWork(ldObjHandle);
T::submitReductionData(reductionData,reduction);
reduction->item(T::reductionChecksumLabel) += (BigReal)tupleCount;
reduction->submit();
if (pressureProfileReduction) {
// For ease of calculation we stored interactions between types
// i and j in (ni+j). For efficiency now we coalesce the
// cross interactions so that just i<=j are stored.
const int arraysize = 3*pressureProfileSlabs;
const BigReal *data = pressureProfileData;
for (int i=0; i<numAtomTypes; i++) {
for (int j=0; j<numAtomTypes; j++) {
int ii=i;
int jj=j;
if (ii > jj) { int tmp=ii; ii=jj; jj=tmp; }
const int reductionOffset =
(ii*numAtomTypes - (ii*(ii+1))/2 + jj)*arraysize;
for (int k=0; k<arraysize; k++) {
pressureProfileReduction->item(reductionOffset+k) += data[k];
}
data += arraysize;
}
}
pressureProfileReduction->submit();
}
// Close boxes - i.e. signal we are done with Positions and
// AtomProperties and that we are depositing Forces
for (ap = ap.begin(); ap != ap.end(); ap++) {
ap->positionBox->close(&(ap->x));
if ( ap->p->flags.doMolly ) ap->avgPositionBox->close(&(ap->x_avg));
ap->forceBox->close(&(ap->r));
}
}
};
#endif