Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

CollectionMaster.h

Go to the documentation of this file.
00001 
00007 #ifndef COLLECTIONMASTER_H
00008 #define COLLECTIONMASTER_H
00009 
00010 #include "charm++.h"
00011 #include "main.h"
00012 #include "NamdTypes.h"
00013 #include "Lattice.h"
00014 #include "ProcessorPrivate.h"
00015 #include "PatchMap.h"
00016 #include "PatchMap.inl"
00017 #include "CollectionMaster.decl.h"
00018 #include <stdio.h>
00019 
00020 class CollectVectorMsg;
00021 class DataStreamMsg;
00022 class EnqueueDataMsg;
00023 
00024 class CollectionMaster : public Chare
00025 {
00026 public:
00027 
00028   static CollectionMaster *Object() { 
00029     return CpvAccess(CollectionMaster_instance); 
00030   }
00031   CollectionMaster();
00032   ~CollectionMaster(void);
00033 
00034   void receivePositions(CollectVectorMsg *msg);
00035   void receiveVelocities(CollectVectorMsg *msg);
00036 
00037   void receiveDataStream(DataStreamMsg *msg);
00038 
00039   void enqueuePositions(int seq, Lattice &lattice);
00040   void enqueueVelocities(int seq);
00041 
00042   void enqueuePositionsFromHandler(EnqueueDataMsg *msg);
00043   void enqueueVelocitiesFromHandler(int seq);
00044 
00045   class CollectVectorInstance;
00046   void disposePositions(CollectVectorInstance *c);
00047   void disposeVelocities(CollectVectorInstance *c);
00048 
00049   class CollectVectorInstance
00050   {
00051   public:
00052 
00053     CollectVectorInstance(void) : seq(-10) { ; }
00054 
00055     CollectVectorInstance(int s) { reset(s); }
00056 
00057     void free() { seq = -10; }
00058     int notfree() { return ( seq != -10 ); }
00059 
00060     void reset(int s) {
00061         if ( s == -10 ) NAMD_bug("seq == free in CollectionMaster");
00062         seq = s;
00063         remaining = (PatchMap::Object())->numNodesWithPatches(); 
00064         data.resize(0);
00065         fdata.resize(0);
00066     }
00067 
00068     // true -> send it and delete it!
00069     void append(AtomIDList &a, ResizeArray<Vector> &d, ResizeArray<FloatVector> &fd)
00070     {
00071       int size = a.size();
00072       if ( d.size() ) {
00073         for( int i = 0; i < size; ++i ) { data.item(a[i]) = d[i]; }
00074       }
00075       if ( fd.size() ) {
00076         for( int i = 0; i < size; ++i ) { fdata.item(a[i]) = fd[i]; }
00077       }
00078       --remaining;
00079     }
00080 
00081     int ready(void) { return ( ! remaining ); }
00082 
00083     int seq;
00084     Lattice lattice;
00085 
00086     ResizeArray<Vector> data;
00087     ResizeArray<FloatVector> fdata;
00088 
00089   private:
00090     int remaining;
00091 
00092   };
00093 
00094   class CollectVectorSequence
00095   {
00096   public:
00097 
00098     void submitData(
00099         int seq, AtomIDList &i, ResizeArray<Vector> &d, ResizeArray<FloatVector> &fd)
00100     {
00101       CollectVectorInstance **c = data.begin();
00102       CollectVectorInstance **c_e = data.end();
00103       for( ; c != c_e && (*c)->seq != seq; ++c );
00104       if ( c == c_e )
00105       {
00106         c = data.begin();
00107         for( ; c != c_e && (*c)->notfree(); ++c );
00108         if ( c == c_e ) {
00109           data.add(new CollectVectorInstance(seq));
00110           c = data.end() - 1;
00111         }
00112         (*c)->reset(seq);
00113       }
00114       (*c)->append(i,d,fd);
00115     }
00116 
00117     void enqueue(int seq, Lattice &lattice) {
00118       queue.add(seq);
00119       latqueue.add(lattice);
00120     }
00121 
00122     CollectVectorInstance* removeReady(void)
00123     {
00124       CollectVectorInstance *o = 0;
00125       if ( queue.size() )
00126       {
00127         int seq = queue[0];
00128         CollectVectorInstance **c = data.begin();
00129         CollectVectorInstance **c_e = data.end();
00130         for( ; c != c_e && (*c)->seq != seq; ++c );
00131         if ( c != c_e && (*c)->ready() )
00132         {
00133           o = *c;
00134           o->lattice = latqueue[0];
00135           queue.del(0,1);
00136           latqueue.del(0,1);
00137         }
00138       }
00139       return o;
00140     }
00141 
00142     ResizeArray<CollectVectorInstance*> data;
00143     ResizeArray<int> queue;
00144     ResizeArray<Lattice> latqueue;
00145 
00146   };
00147 private:
00148 
00149   CollectVectorSequence positions;
00150   CollectVectorSequence velocities;
00151   FILE *dataStreamFile;
00152 
00153 };
00154 
00155 
00156 /* 
00157  * This class is used as a proxy to the CollectionMaster classi.
00158  * In the memory optimized version, the instance of CollectionMaster is 
00159  * moved to other processors. However, this instance is assumed to be on pe 0
00160  * and the Controller object contains the pointer to the instance. As a
00161  * result, a proxy class is needed to bridge the gap between the Controller
00162  * object on pe0 and the CollectionMaster object (which handles outputing
00163  * coordinates/velocities) on the other processor.
00164  */
00165 class CollectionMasterHandler : public Chare
00166 {
00167 public:
00168 
00169   static CollectionMasterHandler *Object() { 
00170     return CpvAccess(CollectionMasterHandler_instance); 
00171   }
00172   CollectionMasterHandler(MasterHandlerInitMsg *);
00173   ~CollectionMasterHandler(void);
00174 
00175   void enqueuePositions(EnqueueDataMsg *msg);
00176   void enqueueVelocities(int seq);
00177   void enqueuePositions(CkQdMsg *msg);
00178   void enqueueVelocities(CkQdMsg *msg);
00179 
00180   void setRealMaster(CkChareID m) { realMaster = m ;}
00181 private:
00182   CkChareID realMaster;
00183   int enqueuePhase;
00184 };
00185 
00186 class CollectVectorMsg : public CMessage_CollectVectorMsg
00187 {
00188 public:
00189 
00190   int seq;
00191   AtomIDList aid;
00192   ResizeArray<Vector> data;
00193   ResizeArray<FloatVector> fdata;
00194 
00195   static void* pack(CollectVectorMsg* msg);
00196   static CollectVectorMsg* unpack(void *ptr);
00197 
00198 };
00199 
00200 
00201 class DataStreamMsg : public CMessage_DataStreamMsg {
00202 public:
00203 
00204   ResizeArray<char> data;
00205 
00206   static void* pack(DataStreamMsg* msg);
00207   static DataStreamMsg* unpack(void *ptr);
00208 
00209 };
00210 
00211 class EnqueueDataMsg: public CMessage_EnqueueDataMsg {
00212 public:
00213   int timestep;
00214   Lattice l;
00215   static void* pack(EnqueueDataMsg* msg);
00216   static EnqueueDataMsg* unpack(void *ptr);
00217 };
00218 
00219 class MasterHandlerInitMsg: public CMessage_MasterHandlerInitMsg {
00220 public:
00221     CkChareID master;
00222 };
00223 
00224 #endif
00225 

Generated on Fri Jul 4 04:07:14 2008 for NAMD by  doxygen 1.3.9.1