NAMD
ReductionMgr.h
Go to the documentation of this file.
1 
7 #ifndef REDUCTIONMGR_H
8 #define REDUCTIONMGR_H
9 
10 #include "charm++.h"
11 
12 #include "main.h"
13 #include "NamdTypes.h"
14 #include "BOCgroup.h"
15 #include "ProcessorPrivate.h"
16 
17 #define VECTOR(A) A ## _X, A ## _Y, A ## _Z
18 #define TENSOR(A) A ## _XX, A ## _XY, A ## _XZ, \
19  A ## _YX, A ## _YY, A ## _YZ, \
20  A ## _ZX, A ## _ZY, A ## _ZZ
21 
22 #define ADD_VECTOR(R,RL,D,DL) \
23  R->item( RL ## _X ) += D[ DL ## _X ]; \
24  R->item( RL ## _Y ) += D[ DL ## _Y ]; \
25  R->item( RL ## _Z ) += D[ DL ## _Z ]
26 
27 #define ADD_VECTOR_OBJECT(R,RL,D) \
28  R->item( RL ## _X ) += D.x; \
29  R->item( RL ## _Y ) += D.y; \
30  R->item( RL ## _Z ) += D.z
31 
32 #define ADD_TENSOR(R,RL,D,DL) \
33  R->item( RL ## _XX) += D[ DL ## _XX ]; \
34  R->item( RL ## _XY) += D[ DL ## _XY ]; \
35  R->item( RL ## _XZ) += D[ DL ## _XZ ]; \
36  R->item( RL ## _YX) += D[ DL ## _YX ]; \
37  R->item( RL ## _YY) += D[ DL ## _YY ]; \
38  R->item( RL ## _YZ) += D[ DL ## _YZ ]; \
39  R->item( RL ## _ZX) += D[ DL ## _ZX ]; \
40  R->item( RL ## _ZY) += D[ DL ## _ZY ]; \
41  R->item( RL ## _ZZ) += D[ DL ## _ZZ ]
42 
43 #define ADD_TENSOR_OBJECT(R,RL,D) \
44  R->item( RL ## _XX) += D.xx; \
45  R->item( RL ## _XY) += D.xy; \
46  R->item( RL ## _XZ) += D.xz; \
47  R->item( RL ## _YX) += D.yx; \
48  R->item( RL ## _YY) += D.yy; \
49  R->item( RL ## _YZ) += D.yz; \
50  R->item( RL ## _ZX) += D.zx; \
51  R->item( RL ## _ZY) += D.zy; \
52  R->item( RL ## _ZZ) += D.zz
53 
54 #define GET_VECTOR(O,R,A) \
55  O.x = R->item( A ## _X ); \
56  O.y = R->item( A ## _Y ); \
57  O.z = R->item( A ## _Z )
58 
59 #define GET_TENSOR(O,R,A) \
60  O.xx = R->item( A ## _XX); \
61  O.xy = R->item( A ## _XY); \
62  O.xz = R->item( A ## _XZ); \
63  O.yx = R->item( A ## _YX); \
64  O.yy = R->item( A ## _YY); \
65  O.yz = R->item( A ## _YZ); \
66  O.zx = R->item( A ## _ZX); \
67  O.zy = R->item( A ## _ZY); \
68  O.zz = R->item( A ## _ZZ)
69 
70 typedef enum
71 {
72  // energy
90  // REDUCTION_THOLE_ENERGY - Drude model "correction" to electrostatic energy
91  // REDUCTION_ANISO_ENERGY - Drude model add into bond energy
110  // pressure
111  TENSOR(REDUCTION_VIRIAL_NORMAL),
112  TENSOR(REDUCTION_VIRIAL_NBOND),
113  TENSOR(REDUCTION_VIRIAL_SLOW),
114  TENSOR(REDUCTION_VIRIAL_AMD_DIHE),
115 #ifdef ALTVIRIAL
116  TENSOR(REDUCTION_ALT_VIRIAL_NORMAL),
117  TENSOR(REDUCTION_ALT_VIRIAL_NBOND),
118  TENSOR(REDUCTION_ALT_VIRIAL_SLOW),
119 #endif
120  TENSOR(REDUCTION_INT_VIRIAL_NORMAL),
121  TENSOR(REDUCTION_INT_VIRIAL_NBOND),
122  TENSOR(REDUCTION_INT_VIRIAL_SLOW),
123  VECTOR(REDUCTION_EXT_FORCE_NORMAL),
124  VECTOR(REDUCTION_EXT_FORCE_NBOND),
125  VECTOR(REDUCTION_EXT_FORCE_SLOW),
126  // momentum
127  VECTOR(REDUCTION_MOMENTUM),
128  TENSOR(REDUCTION_MOMENTUM_SQUARED), // Multigrator
129  VECTOR(REDUCTION_ANGULAR_MOMENTUM),
130  VECTOR(REDUCTION_HALFSTEP_MOMENTUM),
132  // used for minimization
137  // used for pair interaction calculations
138  VECTOR(REDUCTION_PAIR_VDW_FORCE),
139  VECTOR(REDUCTION_PAIR_ELECT_FORCE),
140  // checksum
147  REDUCTION_THOLE_CHECKSUM, // Drude model
148  REDUCTION_ANISO_CHECKSUM, // Drude model
152 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
154 #endif
158  // semaphore (must be last)
160 } ReductionTag;
161 
162 typedef enum {
164  TENSOR(MULTIGRATOR_REDUCTION_MOMENTUM_SQUARED),
165  // semaphore
168 
169 // Later this can be dynamic
170 enum {
177  REDUCTIONS_AMD, // for accelMD
181  // semaphore (must be last)
183 };
184 
185 // Later this can be dynamic
186 #define REDUCTION_MAX_CHILDREN 4
187 
189 class ReductionSubmitMsg;
190 class SubmitReduction;
191 class RequireReduction;
192 
193 // Queue element which stores data for a particular sequence number
195 public:
200  ReductionSetData(int seqNum, int size) {
201  sequenceNumber = seqNum;
202  submitsRecorded = 0;
203  data = new BigReal[size];
204  for ( int i = 0; i < size; ++i ) { data[i] = 0; }
205  next = 0;
206  }
208  delete [] data;
209  }
210 };
211 
212 // Stores the submit queue for a particular set of reductions
214 public:
218  int dataSize;
220  ReductionSetData* getData(int seqNum);
221  ReductionSetData* removeData(int seqNum); // removes from queue
222  int requireRegistered; // is a thread subscribed on this node?
223  int threadIsWaiting; // is there a thread waiting on this?
224  int waitingForSequenceNumber; // sequence number waited for
225  CthThread waitingThread;
226  ReductionSet(int setID, int size,int numChildren);
227  ~ReductionSet();
229 };
230 
231 // Top level class
232 // don't derive from CBase_ReductionMgr to avoid .decl.h file
233 class ReductionMgr : public Group
234 {
235 private:
236  friend class SubmitReduction;
237  friend class RequireReduction;
238 
239  ReductionSet * (reductionSets[REDUCTION_MAX_SET_ID]);
240 
241  int myParent; // parent node or -1 if none
242 #if 0
243  int firstChild, lastChild; // firstChild <= children < lastChild
244  int isMyChild(int nodeID) const {
245  return ( nodeID >= firstChild && nodeID < lastChild );
246  }
247 #endif
248  int numChildren;
249  int *children;
250  int isMyChild(int nodeID) const {
251  int i;
252  for(i=0;i<numChildren;i++)
253  if (children[i]==nodeID) return 1;
254  return 0;
255  }
256  int childIndex(int nodeID) const {
257  int i;
258  for(i=0;i<numChildren;i++)
259  if (children[i]==nodeID) return i;
260  return -1;
261  }
262  int isRoot(void) const { return ( myParent == -1 ); }
263 
264  ReductionSet* getSet(int setID, int size);
265  void delSet(int setID);
266 
267  void mergeAndDeliver(ReductionSet *set, int seqNum);
268 
269  void submit(SubmitReduction*);
270  void remove(SubmitReduction*);
271 
272  void require(RequireReduction*);
273  void remove(RequireReduction*);
274 
275 public:
276 
277  // Singleton Access method
278  inline static ReductionMgr *Object(void) {
279  return CkpvAccess(ReductionMgr_instance);
280  }
281 
282  ReductionMgr();
283  ~ReductionMgr();
284 
285  void buildSpanTree(const int pe,
286  const int max_intranode_children,
287  const int max_internode_children,
288  int* parent,
289  int* num_children,
290  int** children);
291 
292  // client interface
293  SubmitReduction* willSubmit(int setID, int size = -1);
294  RequireReduction* willRequire(int setID, int size = -1);
295 
296  // message entry points
299  void remoteSubmit(ReductionSubmitMsg *msg);
300 
301 };
302 
303 // Client handle for submissions
305 private:
306  friend class ReductionMgr;
307  int reductionSetID;
308  int sequenceNumber;
309  ReductionMgr *master;
310  BigReal *data; // managed explicitly by master
311 public:
312  inline BigReal& item(int i) {
313  return data[i];
314  }
315  inline void max(int i, BigReal v) {
316  if ( v > data[i] ) {
317  data[i] = v;
318  }
319  }
320  void add(int nitems, const BigReal *arr) {
321  for (int i=0; i<nitems; i++) data[i] += arr[i];
322  }
323  void submit(void) {
324  master->submit(this);
325  }
326  ~SubmitReduction(void) { master->remove(this); }
327 };
328 
329 // Client handle for requires
331 private:
332  friend class ReductionMgr;
333  int reductionSetID;
334  int sequenceNumber;
335  ReductionMgr *master;
336  ReductionSetData *currentData;
337  BigReal *data;
338  RequireReduction(void) { currentData = 0; data = 0; }
339 public:
340  BigReal item(int i) const { return data[i]; }
341  void require(void) {
342  master->require(this);
343  }
344  ~RequireReduction(void) { delete currentData; master->remove(this); }
345 };
346 
347 
348 #endif
349 
void max(int i, BigReal v)
Definition: ReductionMgr.h:315
void buildSpanTree(const int pe, const int max_intranode_children, const int max_internode_children, int *parent, int *num_children, int **children)
Definition: ReductionMgr.C:119
int nextSequenceNumber
Definition: ReductionMgr.h:216
ReductionSetData * next
Definition: ReductionMgr.h:199
ReductionSetData(int seqNum, int size)
Definition: ReductionMgr.h:200
int * addToRemoteSequenceNumber
Definition: ReductionMgr.h:228
MultigratorReductionTag
Definition: ReductionMgr.h:162
~SubmitReduction(void)
Definition: ReductionMgr.h:326
ReductionSetData * getData(int seqNum)
Definition: ReductionMgr.C:88
BigReal & item(int i)
Definition: ReductionMgr.h:312
ReductionSet(int setID, int size, int numChildren)
Definition: ReductionMgr.C:57
void remoteRegister(ReductionRegisterMsg *msg)
Definition: ReductionMgr.C:413
SubmitReduction * willSubmit(int setID, int size=-1)
Definition: ReductionMgr.C:365
static ReductionMgr * Object(void)
Definition: ReductionMgr.h:278
void add(int nitems, const BigReal *arr)
Definition: ReductionMgr.h:320
void require(void)
Definition: ReductionMgr.h:341
CthThread waitingThread
Definition: ReductionMgr.h:225
int waitingForSequenceNumber
Definition: ReductionMgr.h:224
int requireRegistered
Definition: ReductionMgr.h:222
ReductionSetData * removeData(int seqNum)
Definition: ReductionMgr.C:103
int submitsRegistered
Definition: ReductionMgr.h:217
BigReal item(int i) const
Definition: ReductionMgr.h:340
void remoteSubmit(ReductionSubmitMsg *msg)
Definition: ReductionMgr.C:447
#define TENSOR(A)
Definition: ReductionMgr.h:18
ReductionTag
Definition: ReductionMgr.h:70
RequireReduction * willRequire(int setID, int size=-1)
Definition: ReductionMgr.C:526
#define VECTOR(A)
Definition: ReductionMgr.h:17
void submit(void)
Definition: ReductionMgr.h:323
friend class RequireReduction
Definition: ReductionMgr.h:237
void remoteUnregister(ReductionRegisterMsg *msg)
Definition: ReductionMgr.C:432
double BigReal
Definition: common.h:114
ReductionSetData * dataQueue
Definition: ReductionMgr.h:219