ReductionMgr.h

Go to the documentation of this file.
00001 
00007 #ifndef REDUCTIONMGR_H
00008 #define REDUCTIONMGR_H
00009 
00010 #include "charm++.h"
00011 
00012 #include "main.h"
00013 #include "NamdTypes.h"
00014 #include "BOCgroup.h"
00015 #include "ProcessorPrivate.h"
00016 
00017 #define VECTOR(A) A ## _X, A ## _Y, A ## _Z
00018 #define TENSOR(A) A ## _XX, A ## _XY, A ## _XZ, \
00019                   A ## _YX, A ## _YY, A ## _YZ, \
00020                   A ## _ZX, A ## _ZY, A ## _ZZ
00021 
00022 #define ADD_VECTOR(R,RL,D,DL) \
00023   R->item( RL ## _X ) += D[ DL ## _X ]; \
00024   R->item( RL ## _Y ) += D[ DL ## _Y ]; \
00025   R->item( RL ## _Z ) += D[ DL ## _Z ]
00026 
00027 #define ADD_VECTOR_OBJECT(R,RL,D) \
00028   R->item( RL ## _X ) += D.x; \
00029   R->item( RL ## _Y ) += D.y; \
00030   R->item( RL ## _Z ) += D.z
00031 
00032 #define ADD_TENSOR(R,RL,D,DL) \
00033   R->item( RL ## _XX) += D[ DL ## _XX ]; \
00034   R->item( RL ## _XY) += D[ DL ## _XY ]; \
00035   R->item( RL ## _XZ) += D[ DL ## _XZ ]; \
00036   R->item( RL ## _YX) += D[ DL ## _YX ]; \
00037   R->item( RL ## _YY) += D[ DL ## _YY ]; \
00038   R->item( RL ## _YZ) += D[ DL ## _YZ ]; \
00039   R->item( RL ## _ZX) += D[ DL ## _ZX ]; \
00040   R->item( RL ## _ZY) += D[ DL ## _ZY ]; \
00041   R->item( RL ## _ZZ) += D[ DL ## _ZZ ]
00042 
00043 #define ADD_TENSOR_OBJECT(R,RL,D) \
00044   R->item( RL ## _XX) += D.xx; \
00045   R->item( RL ## _XY) += D.xy; \
00046   R->item( RL ## _XZ) += D.xz; \
00047   R->item( RL ## _YX) += D.yx; \
00048   R->item( RL ## _YY) += D.yy; \
00049   R->item( RL ## _YZ) += D.yz; \
00050   R->item( RL ## _ZX) += D.zx; \
00051   R->item( RL ## _ZY) += D.zy; \
00052   R->item( RL ## _ZZ) += D.zz
00053 
00054 #define GET_VECTOR(O,R,A) \
00055   O.x = R->item( A ## _X ); \
00056   O.y = R->item( A ## _Y ); \
00057   O.z = R->item( A ## _Z )
00058 
00059 #define GET_TENSOR(O,R,A) \
00060   O.xx = R->item( A ## _XX); \
00061   O.xy = R->item( A ## _XY); \
00062   O.xz = R->item( A ## _XZ); \
00063   O.yx = R->item( A ## _YX); \
00064   O.yy = R->item( A ## _YY); \
00065   O.yz = R->item( A ## _YZ); \
00066   O.zx = R->item( A ## _ZX); \
00067   O.zy = R->item( A ## _ZY); \
00068   O.zz = R->item( A ## _ZZ)
00069 
00070 typedef enum
00071 {
00072  // energy
00073   REDUCTION_ANGLE_ENERGY,
00074   REDUCTION_BOND_ENERGY,
00075   REDUCTION_BONDED_ENERGY_F,
00076   REDUCTION_BONDED_ENERGY_TI_1,
00077   REDUCTION_BONDED_ENERGY_TI_2,
00078   REDUCTION_DIHEDRAL_ENERGY,
00079   REDUCTION_ELECT_ENERGY,
00080   REDUCTION_ELECT_ENERGY_F,
00081   REDUCTION_ELECT_ENERGY_TI_1,
00082   REDUCTION_ELECT_ENERGY_TI_2,
00083   REDUCTION_ELECT_ENERGY_SLOW,
00084   REDUCTION_ELECT_ENERGY_SLOW_F,
00085   REDUCTION_ELECT_ENERGY_SLOW_TI_1,
00086   REDUCTION_ELECT_ENERGY_SLOW_TI_2,
00087   REDUCTION_ELECT_ENERGY_PME_TI_1,
00088   REDUCTION_ELECT_ENERGY_PME_TI_2,
00089   REDUCTION_IMPROPER_ENERGY,
00090   // REDUCTION_THOLE_ENERGY - Drude model "correction" to electrostatic energy
00091   // REDUCTION_ANISO_ENERGY - Drude model add into bond energy
00092   REDUCTION_CROSSTERM_ENERGY,
00093   REDUCTION_HALFSTEP_KINETIC_ENERGY,
00094   REDUCTION_CENTERED_KINETIC_ENERGY,
00095   REDUCTION_INT_HALFSTEP_KINETIC_ENERGY,
00096   REDUCTION_INT_CENTERED_KINETIC_ENERGY,
00097   REDUCTION_DRUDECOM_CENTERED_KINETIC_ENERGY,
00098   REDUCTION_DRUDEBOND_CENTERED_KINETIC_ENERGY,
00099   REDUCTION_LJ_ENERGY,
00100   REDUCTION_LJ_ENERGY_F,
00101   REDUCTION_LJ_ENERGY_F_LEFT,
00102   REDUCTION_LJ_ENERGY_TI_1,
00103   REDUCTION_LJ_ENERGY_TI_2,
00104   REDUCTION_BC_ENERGY,
00105   REDUCTION_MISC_ENERGY,
00106   REDUCTION_GRO_LJ_ENERGY,
00107   REDUCTION_GRO_GAUSS_ENERGY,
00108   REDUCTION_GO_NATIVE_ENERGY,
00109   REDUCTION_GO_NONNATIVE_ENERGY,
00110  // pressure
00111   TENSOR(REDUCTION_VIRIAL_NORMAL),
00112   TENSOR(REDUCTION_VIRIAL_NBOND),
00113   TENSOR(REDUCTION_VIRIAL_SLOW),
00114   TENSOR(REDUCTION_VIRIAL_AMD_DIHE),
00115 #ifdef ALTVIRIAL
00116   TENSOR(REDUCTION_ALT_VIRIAL_NORMAL),
00117   TENSOR(REDUCTION_ALT_VIRIAL_NBOND),
00118   TENSOR(REDUCTION_ALT_VIRIAL_SLOW),
00119 #endif
00120   TENSOR(REDUCTION_INT_VIRIAL_NORMAL),
00121   TENSOR(REDUCTION_INT_VIRIAL_NBOND),
00122   TENSOR(REDUCTION_INT_VIRIAL_SLOW),
00123   VECTOR(REDUCTION_EXT_FORCE_NORMAL),
00124   VECTOR(REDUCTION_EXT_FORCE_NBOND),
00125   VECTOR(REDUCTION_EXT_FORCE_SLOW),
00126  // momentum
00127   VECTOR(REDUCTION_MOMENTUM),
00128   TENSOR(REDUCTION_MOMENTUM_SQUARED),    // Multigrator
00129   VECTOR(REDUCTION_ANGULAR_MOMENTUM),
00130   VECTOR(REDUCTION_HALFSTEP_MOMENTUM),
00131   REDUCTION_MOMENTUM_MASS,
00132  // used for minimization
00133   REDUCTION_MIN_F_DOT_F,
00134   REDUCTION_MIN_F_DOT_V,
00135   REDUCTION_MIN_V_DOT_V,
00136   REDUCTION_MIN_HUGE_COUNT,
00137  // used for pair interaction calculations
00138   VECTOR(REDUCTION_PAIR_VDW_FORCE),
00139   VECTOR(REDUCTION_PAIR_ELECT_FORCE),
00140  // checksum
00141   REDUCTION_ATOM_CHECKSUM,
00142   REDUCTION_COMPUTE_CHECKSUM,
00143   REDUCTION_BOND_CHECKSUM,
00144   REDUCTION_ANGLE_CHECKSUM,
00145   REDUCTION_DIHEDRAL_CHECKSUM,
00146   REDUCTION_IMPROPER_CHECKSUM,
00147   REDUCTION_THOLE_CHECKSUM,  // Drude model
00148   REDUCTION_ANISO_CHECKSUM,  // Drude model
00149   REDUCTION_CROSSTERM_CHECKSUM,
00150   REDUCTION_GRO_LJ_CHECKSUM,
00151   REDUCTION_EXCLUSION_CHECKSUM,
00152 #ifdef NAMD_CUDA
00153   REDUCTION_EXCLUSION_CHECKSUM_CUDA,
00154 #endif
00155   REDUCTION_MARGIN_VIOLATIONS,
00156   REDUCTION_PAIRLIST_WARNINGS,
00157   REDUCTION_STRAY_CHARGE_ERRORS,
00158  // semaphore (must be last)
00159   REDUCTION_MAX_RESERVED
00160 } ReductionTag;
00161 
00162 typedef enum {
00163   MULTIGRATOR_REDUCTION_KINETIC_ENERGY,
00164   TENSOR(MULTIGRATOR_REDUCTION_MOMENTUM_SQUARED),
00165   // semaphore
00166   MULTIGRATOR_REDUCTION_MAX_RESERVED
00167 } MultigratorReductionTag;
00168 
00169 // Later this can be dynamic
00170 enum {
00171   REDUCTIONS_BASIC,
00172   REDUCTIONS_MINIMIZER,
00173   REDUCTIONS_PPROF_BONDED,
00174   REDUCTIONS_PPROF_NONBONDED,
00175   REDUCTIONS_PPROF_INTERNAL,
00176   REDUCTIONS_PPROF_KINETIC,
00177   REDUCTIONS_AMD,   // for accelMD
00178   REDUCTIONS_USER1,
00179   REDUCTIONS_USER2,
00180   REDUCTIONS_MULTIGRATOR,
00181  // semaphore (must be last)
00182   REDUCTION_MAX_SET_ID
00183 };
00184 
00185 // Later this can be dynamic
00186 #define REDUCTION_MAX_CHILDREN 4
00187 
00188 class ReductionRegisterMsg;
00189 class ReductionSubmitMsg;
00190 class SubmitReduction;
00191 class RequireReduction;
00192 
00193 // Queue element which stores data for a particular sequence number
00194 class ReductionSetData {
00195 public:
00196   int sequenceNumber;
00197   int submitsRecorded;
00198   BigReal *data;
00199   ReductionSetData *next;
00200   ReductionSetData(int seqNum, int size) {
00201     sequenceNumber = seqNum;
00202     submitsRecorded = 0;
00203     data = new BigReal[size];
00204     for ( int i = 0; i < size; ++i ) { data[i] = 0; }
00205     next = 0;
00206   }
00207   ~ReductionSetData() {
00208     delete [] data;
00209   }
00210 };
00211 
00212 // Stores the submit queue for a particular set of reductions
00213 class ReductionSet {
00214 public:
00215   int reductionSetID;
00216   int nextSequenceNumber;
00217   int submitsRegistered;
00218   int dataSize;
00219   ReductionSetData *dataQueue;
00220   ReductionSetData* getData(int seqNum);
00221   ReductionSetData* removeData(int seqNum);  // removes from queue
00222   int requireRegistered;  // is a thread subscribed on this node?
00223   int threadIsWaiting;  // is there a thread waiting on this?
00224   int waitingForSequenceNumber;  // sequence number waited for
00225   CthThread waitingThread;
00226   ReductionSet(int setID, int size,int numChildren);
00227   ~ReductionSet();
00228   int *addToRemoteSequenceNumber;
00229 };
00230 
00231 // Top level class
00232 // don't derive from CBase_ReductionMgr to avoid .decl.h file
00233 class ReductionMgr : public Group
00234 {
00235 private:
00236   friend class SubmitReduction;
00237   friend class RequireReduction;
00238 
00239   ReductionSet * (reductionSets[REDUCTION_MAX_SET_ID]);
00240 
00241   int myParent;  // parent node or -1 if none
00242 #if 0
00243   int firstChild, lastChild;  // firstChild <= children < lastChild
00244   int isMyChild(int nodeID) const {
00245     return ( nodeID >= firstChild && nodeID < lastChild );
00246   }
00247 #endif
00248   int numChildren;
00249   int *children;
00250   int isMyChild(int nodeID) const {
00251     int i;
00252     for(i=0;i<numChildren;i++)
00253       if (children[i]==nodeID) return 1;
00254     return 0;
00255   }      
00256   int childIndex(int nodeID) const {
00257     int i;
00258     for(i=0;i<numChildren;i++)
00259       if (children[i]==nodeID) return i;
00260     return -1;
00261   }
00262   int isRoot(void) const { return ( myParent == -1 ); }
00263 
00264   ReductionSet* getSet(int setID, int size);
00265   void delSet(int setID);
00266 
00267   void mergeAndDeliver(ReductionSet *set, int seqNum);
00268 
00269   void submit(SubmitReduction*);
00270   void remove(SubmitReduction*);
00271 
00272   void require(RequireReduction*);
00273   void remove(RequireReduction*);
00274 
00275 public:
00276 
00277   // Singleton Access method
00278   inline static ReductionMgr *Object(void) {
00279     return CkpvAccess(ReductionMgr_instance);
00280   }
00281 
00282   ReductionMgr();
00283   ~ReductionMgr();
00284 
00285   void buildSpanTree(const int pe, 
00286                      const int max_intranode_children,
00287                      const int max_internode_children,
00288                      int* parent, 
00289                      int* num_children, 
00290                      int** children);
00291 
00292   // client interface
00293   SubmitReduction* willSubmit(int setID, int size = -1);
00294   RequireReduction* willRequire(int setID, int size = -1);
00295 
00296   // message entry points
00297   void remoteRegister(ReductionRegisterMsg *msg);
00298   void remoteUnregister(ReductionRegisterMsg *msg);
00299   void remoteSubmit(ReductionSubmitMsg *msg);
00300 
00301 };
00302 
00303 // Client handle for submissions
00304 class SubmitReduction {
00305 private:
00306   friend class ReductionMgr;
00307   int reductionSetID;
00308   int sequenceNumber;
00309   ReductionMgr *master;
00310   BigReal *data;  // managed explicitly by master
00311 public:
00312   inline BigReal& item(int i) {
00313     return data[i];
00314   }
00315   inline void max(int i, BigReal v) {
00316     if ( v > data[i] ) {
00317       data[i] = v;
00318     }
00319   }
00320   void add(int nitems, const BigReal *arr) {
00321     for (int i=0; i<nitems; i++) data[i] += arr[i];
00322   }
00323   void submit(void) {
00324     master->submit(this);
00325   }
00326   ~SubmitReduction(void) { master->remove(this); }
00327 };
00328 
00329 // Client handle for requires
00330 class RequireReduction {
00331 private:
00332   friend class ReductionMgr;
00333   int reductionSetID;
00334   int sequenceNumber;
00335   ReductionMgr *master;
00336   ReductionSetData *currentData;
00337   BigReal *data;
00338   RequireReduction(void) { currentData = 0; data = 0; }
00339 public:
00340   BigReal item(int i) const { return data[i]; }
00341   void require(void) {
00342     master->require(this);
00343   }
00344   ~RequireReduction(void) { delete currentData; master->remove(this); }
00345 };
00346 
00347 
00348 #endif
00349 

Generated on Tue Sep 19 01:17:14 2017 for NAMD by  doxygen 1.4.7