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

PatchMgr.C

Go to the documentation of this file.
00001 
00007 #include "InfoStream.h"
00008 #include "PatchMgr.decl.h"
00009 #include "PatchMgr.h"
00010 
00011 #include "NamdTypes.h"
00012 //#include "Compute.h"
00013 #include "HomePatch.h"
00014 #include "PatchMap.h"
00015 #include "AtomMap.h"
00016 
00017 #include "ComputeMsmMsa.h"  // needed for MsmMsaData definition
00018 #include "main.decl.h"
00019 #include "main.h"
00020 
00021 #include "WorkDistrib.decl.h"
00022 #include "WorkDistrib.h"
00023 #include "Node.h"
00024 #include "SimParameters.h"
00025 
00026 #include "packmsg.h"
00027 
00028 // #define DEBUGM
00029 #define MIN_DEBUG_LEVEL 3
00030 #include "Debug.h"
00031 
00032 
00033 // BOC constructor
00034 PatchMgr::PatchMgr()
00035 {
00036     // CkPrintf("[%d] PatchMgr Created\n", CkMyPe());
00037 
00038     // Singleton pattern
00039     if (CkpvAccess(PatchMgr_instance) == NULL) {
00040         CkpvAccess(PatchMgr_instance) = this;
00041     } else {
00042         iout << iFILE << iERROR << iPE 
00043           << "PatchMgr instanced twice on same processor!" << endi;
00044         CkExit();
00045     }
00046 
00047     // Get PatchMap singleton started
00048     patchMap = PatchMap::Instance();
00049     patchMap->registerPatchMgr(this);
00050 
00051     // Message combining initialization
00052     migrationCountdown = 0;
00053     combineMigrationMsgs = new MigrateAtomsCombinedMsg*[CkNumPes()];
00054     int numPes = CkNumPes();
00055     for ( int i = 0; i < numPes; ++i ) combineMigrationMsgs[i] = 0;
00056 }
00057 
00058 PatchMgr::~PatchMgr()
00059 {
00060     HomePatchListIter hi(homePatches);
00061     for ( hi = hi.begin(); hi != hi.end(); hi++) {
00062       HomePatchElem* elem = homePatches.find(HomePatchElem(hi->pid));
00063       delete elem->patch;
00064     }
00065     delete [] combineMigrationMsgs;
00066 }
00067 
00068 void PatchMgr::preCreateHomePatch(PatchID pid, int atomCnt){
00069     HomePatch *patch = new HomePatch(pid, atomCnt);
00070     homePatches.load(HomePatchElem(pid, patch));
00071     patchMap->registerPatch(pid, patch);
00072 }
00073 
00074 void PatchMgr::createHomePatch(PatchID pid, FullAtomList a) 
00075 {
00076     HomePatch *patch = new HomePatch(pid, a);
00077     homePatches.load(HomePatchElem(pid, patch));
00078     patchMap->registerPatch(pid, patch);
00079 }
00080 
00081 
00082 // Add a HomePatch to a list of patches to be moved 
00083 // HomePatches are actually moved by invoking sendMovePatches() below
00084 void PatchMgr::movePatch(PatchID pid, NodeID nodeID) 
00085 {
00086     move.load(MovePatch(pid,nodeID));
00087 }
00088 
00089 void PatchMgr::sendOneHomePatch(int patchId, int nodeId){
00090     HomePatch *p = homePatch(patchId);
00091     patchMap->unregisterPatch(patchId, p);
00092 
00093     MovePatchesMsg *msg = new MovePatchesMsg(patchId, p->atom);
00094 
00095     // Deleting the HomePatchElem will call a destructor for clean up
00096     // but the msg elements are safe since they use a container template
00097     // that uses ref counting.
00098     delete p;
00099     homePatches.del(HomePatchElem(patchId)); 
00100 
00101     // Sending to PatchMgr::recvMovePatches on remote node
00102     CProxy_PatchMgr cp(thisgroup);
00103     cp[nodeId].recvMovePatches(msg);
00104 }
00105 
00106 // Uses list constructed by movePatch() and dispatches
00107 // HomePatch(es) to new nodes
00108 void PatchMgr::sendMovePatches() 
00109 {
00110     if (! move.size())
00111         return;
00112 
00113     MovePatchListIter m(move);
00114     for ( m = m.begin(); m != m.end(); m++) {
00115       HomePatch *p = homePatch(m->pid);
00116       patchMap->unregisterPatch(m->pid, p);
00117 
00118       MovePatchesMsg *msg = new MovePatchesMsg(m->pid, p->atom);
00119 
00120       // Deleting the HomePatchElem will call a destructor for clean up
00121       // but the msg elements are safe since they use a container template
00122       // that uses ref counting.
00123       delete p;
00124       homePatches.del(HomePatchElem(m->pid)); 
00125 
00126       // Sending to PatchMgr::recvMovePatches on remote node
00127       CProxy_PatchMgr cp(thisgroup);
00128       cp[m->nodeID].recvMovePatches(msg);
00129     }
00130     move.resize(0);
00131 }
00132 
00133 void PatchMgr::recvMovePatches(MovePatchesMsg *msg) {
00134     // Make a new HomePatch
00135     createHomePatch(msg->pid, msg->atom);
00136     delete msg;
00137 
00138     // Tell sending PatchMgr we received MovePatchMsg
00139 //    AckMovePatchesMsg *ackmsg = 
00140 //      new AckMovePatchesMsg;
00141 //    CSendMsgBranch(PatchMgr,ackMovePatches, ackmsg, thisgroup, msg->fromNodeID);
00142 }
00143     
00144 
00145 //void PatchMgr::ackMovePatches(AckMovePatchesMsg *msg)
00146 //{
00147 //    delete msg;
00148 //    if (! --ackMovePending) 
00149 //      WorkDistrib::messageMovePatchDone();
00150 //}
00151 
00152 
00153 void PatchMgr::sendAtoms(PatchID pid, FullAtomList &a) {
00154 
00155       MovePatchesMsg *msg = new MovePatchesMsg(pid, a);
00156 
00157       FullAtomList empty;
00158       a = empty;  // eliminate reference in sender
00159 
00160       CProxy_PatchMgr cp(thisgroup);
00161       cp[patchMap->node(pid)].recvAtoms(msg);
00162 
00163 }
00164 
00165 void PatchMgr::recvAtoms(MovePatchesMsg *msg) {
00166     patchMap->homePatch(msg->pid)->reinitAtoms(msg->atom);
00167     delete msg;
00168 }
00169 
00170 // Called by HomePatch to migrate atoms off to new patches
00171 // Message combining occurs here
00172 void PatchMgr::sendMigrationMsgs(PatchID src, MigrationInfo *m, int numMsgs) {
00173 /*
00174   for (int i=0; i < numMsgs; i++) {
00175     PatchMgr::Object()->sendMigrationMsg(src, m[i]);
00176   }
00177 */
00178   if ( ! migrationCountdown )  // (re)initialize
00179   {
00180     // DebugM(3,"migrationCountdown (re)initialize\n");
00181     numHomePatches = patchMap->numHomePatches();
00182     migrationCountdown = numHomePatches;
00183     combineMigrationDestPes.resize(0);
00184   }
00185   for (int i=0; i < numMsgs; i++) {  // buffer messages
00186     int destNodeID = m[i].destNodeID;
00187     if ( 1 ) // destNodeID != CkMyPe() )
00188     {
00189       if ( ! combineMigrationMsgs[destNodeID] )
00190       {
00191         combineMigrationMsgs[destNodeID] = new MigrateAtomsCombinedMsg();
00192         combineMigrationDestPes.add(destNodeID);
00193       }
00194       combineMigrationMsgs[destNodeID]->add(src,m[i].destPatchID,m[i].mList);
00195     }
00196     else
00197     {
00198         // for now buffer local messages too
00199     }
00200   }
00201   migrationCountdown -= 1;
00202   // DebugM(3,"migrationCountdown = " << migrationCountdown << "\n");
00203   if ( ! migrationCountdown )  // send out combined messages
00204   {
00205     int n = combineMigrationDestPes.size();
00206     for ( int i = 0; i < n; ++i ) {
00207         int destNodeID = combineMigrationDestPes[i];
00208         DebugM(3,"Sending MigrateAtomsCombinedMsg to node " << destNodeID << "\n");
00209         CProxy_PatchMgr cp(thisgroup);
00210         cp[destNodeID].recvMigrateAtomsCombined(combineMigrationMsgs[destNodeID]);
00211         combineMigrationMsgs[destNodeID] = 0;
00212     }
00213   }
00214 }
00215 
00216 void PatchMgr::recvMigrateAtomsCombined (MigrateAtomsCombinedMsg *msg)
00217 {
00218   DebugM(3,"Received MigrateAtomsCombinedMsg with " << msg->srcPatchID.size() << " messages.\n");
00219   msg->distribute();
00220   delete msg;
00221 }
00222 
00223 void PatchMgr::moveAtom(MoveAtomMsg *msg) {
00224   LocalID lid = AtomMap::Object()->localID(msg->atomid);
00225   if ( lid.pid != notUsed ) {
00226     HomePatch *hp = patchMap->homePatch(lid.pid);
00227     if ( hp ) {
00228       FullAtom &a = hp->atom[lid.index];
00229       if ( msg->moveto ) {
00230         a.fixedPosition = msg->coord;
00231       } else {
00232         a.fixedPosition = hp->lattice.reverse_transform(a.position,a.transform);
00233         a.fixedPosition += msg->coord;
00234       }
00235       a.position = hp->lattice.apply_transform(a.fixedPosition,a.transform);
00236     }
00237   }
00238   delete msg;
00239 }
00240 
00241 void PatchMgr::moveAllBy(MoveAllByMsg *msg) {
00242   // loop over homePatches, moving every atom
00243   for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
00244     HomePatch *hp = elem->patch;
00245     for (int i=0; i<hp->getNumAtoms(); i++) {
00246       FullAtom &a = hp->atom[i];
00247       a.fixedPosition = hp->lattice.reverse_transform(a.position,a.transform);
00248       a.fixedPosition += msg->offset;
00249       a.position = hp->lattice.apply_transform(a.fixedPosition,a.transform);
00250     }
00251   }
00252   delete msg;
00253 }
00254 
00255 void PatchMgr::setLattice(SetLatticeMsg *msg) {
00256   // loop over homePatches, setting the lattice to the new value.
00257   for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
00258     HomePatch *hp = elem->patch;
00259     hp->lattice = msg->lattice;
00260   }
00261   // Must also do this for SimParameters in order for pressure profile to work!
00262   Node::Object()->simParameters->lattice = msg->lattice;
00263 }
00264 
00265 PACK_MSG(MovePatchesMsg,
00266   PACK(fromNodeID);
00267   PACK(pid);
00268   PACK_RESIZE(atom);
00269 )
00270 
00271 
00272 #include "PatchMgr.def.h"
00273 

Generated on Fri May 25 04:07:16 2012 for NAMD by  doxygen 1.3.9.1