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