00001
00007 #include "InfoStream.h"
00008 #include "PatchMgr.decl.h"
00009 #include "PatchMgr.h"
00010
00011 #include "NamdTypes.h"
00012
00013 #include "HomePatch.h"
00014 #include "PatchMap.h"
00015 #include "AtomMap.h"
00016
00017 #ifdef USE_COMM_LIB
00018 #include "ComlibManager.h"
00019 #endif
00020
00021 #include "main.decl.h"
00022 #include "main.h"
00023
00024 #include "WorkDistrib.decl.h"
00025 #include "WorkDistrib.h"
00026 #include "Node.h"
00027 #include "SimParameters.h"
00028
00029 #include "packmsg.h"
00030
00031
00032 #define MIN_DEBUG_LEVEL 3
00033 #include "Debug.h"
00034
00035
00036
00037 PatchMgr::PatchMgr()
00038 {
00039
00040
00041
00042 if (CkpvAccess(PatchMgr_instance) == NULL) {
00043 CkpvAccess(PatchMgr_instance) = this;
00044 } else {
00045 iout << iFILE << iERROR << iPE
00046 << "PatchMgr instanced twice on same processor!" << endi;
00047 CkExit();
00048 }
00049
00050
00051 patchMap = PatchMap::Instance();
00052 patchMap->registerPatchMgr(this);
00053
00054
00055 migrationCountdown = 0;
00056 combineMigrationMsgs = new MigrateAtomsCombinedMsg*[CkNumPes()];
00057 }
00058
00059 PatchMgr::~PatchMgr()
00060 {
00061 HomePatchListIter hi(homePatches);
00062 for ( hi = hi.begin(); hi != hi.end(); hi++) {
00063 HomePatchElem* elem = homePatches.find(HomePatchElem(hi->pid));
00064 delete elem->patch;
00065 }
00066 delete [] combineMigrationMsgs;
00067 }
00068
00069 void PatchMgr::preCreateHomePatch(PatchID pid, int atomCnt){
00070 HomePatch *patch = new HomePatch(pid, atomCnt);
00071 homePatches.load(HomePatchElem(pid, patch));
00072 patchMap->registerPatch(pid, patch);
00073 }
00074
00075 void PatchMgr::createHomePatch(PatchID pid, FullAtomList a)
00076 {
00077 HomePatch *patch = new HomePatch(pid, a);
00078 homePatches.load(HomePatchElem(pid, patch));
00079 patchMap->registerPatch(pid, patch);
00080 }
00081
00082
00083
00084
00085 void PatchMgr::movePatch(PatchID pid, NodeID nodeID)
00086 {
00087 move.load(MovePatch(pid,nodeID));
00088 }
00089
00090 void PatchMgr::sendOneHomePatch(int patchId, int nodeId){
00091 HomePatch *p = homePatch(patchId);
00092 patchMap->unregisterPatch(patchId, p);
00093
00094 MovePatchesMsg *msg = new MovePatchesMsg(patchId, p->atom);
00095
00096
00097 CProxy_PatchMgr cp(thisgroup);
00098 cp[nodeId].recvMovePatches(msg);
00099
00100
00101
00102
00103 delete p;
00104 homePatches.del(HomePatchElem(patchId));
00105 }
00106
00107
00108
00109 void PatchMgr::sendMovePatches()
00110 {
00111 if (! move.size())
00112 return;
00113
00114 MovePatchListIter m(move);
00115 for ( m = m.begin(); m != m.end(); m++) {
00116 HomePatch *p = homePatch(m->pid);
00117 patchMap->unregisterPatch(m->pid, p);
00118
00119 MovePatchesMsg *msg = new MovePatchesMsg(m->pid, p->atom);
00120
00121
00122 CProxy_PatchMgr cp(thisgroup);
00123 cp[m->nodeID].recvMovePatches(msg);
00124
00125
00126
00127
00128 delete p;
00129 homePatches.del(HomePatchElem(m->pid));
00130 }
00131 move.resize(0);
00132 }
00133
00134 void PatchMgr::recvMovePatches(MovePatchesMsg *msg) {
00135
00136 createHomePatch(msg->pid, msg->atom);
00137 delete msg;
00138
00139
00140
00141
00142
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 void PatchMgr::sendAtoms(PatchID pid, FullAtomList a) {
00155
00156 MovePatchesMsg *msg = new MovePatchesMsg(pid, a);
00157
00158 CProxy_PatchMgr cp(thisgroup);
00159 cp[patchMap->node(pid)].recvAtoms(msg);
00160
00161 }
00162
00163 void PatchMgr::recvAtoms(MovePatchesMsg *msg) {
00164 patchMap->homePatch(msg->pid)->reinitAtoms(msg->atom);
00165 delete msg;
00166 }
00167
00168
00169
00170
00171 void PatchMgr::sendMigrationMsg(PatchID src, MigrationInfo m) {
00172 MigrateAtomsMsg *msg = new MigrateAtomsMsg(src,m.destPatchID,m.mList);
00173 CProxy_PatchMgr cp(thisgroup);
00174 cp[m.destNodeID].recvMigrateAtoms(msg);
00175 }
00176
00177
00178
00179 void PatchMgr::sendMigrationMsgs(PatchID src, MigrationInfo *m, int numMsgs) {
00180
00181
00182
00183
00184
00185 if ( ! migrationCountdown )
00186 {
00187
00188 numHomePatches = patchMap->numHomePatches();
00189 migrationCountdown = numHomePatches;
00190 int numPes = CkNumPes();
00191 for ( int i = 0; i < numPes; ++i ) combineMigrationMsgs[i] = 0;
00192 }
00193 for (int i=0; i < numMsgs; i++) {
00194 int destNodeID = m[i].destNodeID;
00195 if ( 1 )
00196 {
00197 if ( ! combineMigrationMsgs[destNodeID] )
00198 {
00199 combineMigrationMsgs[destNodeID] = new MigrateAtomsCombinedMsg();
00200 }
00201 combineMigrationMsgs[destNodeID]->add(src,m[i].destPatchID,m[i].mList);
00202 }
00203 else
00204 {
00205
00206 }
00207 }
00208 migrationCountdown -= 1;
00209
00210 if ( ! migrationCountdown )
00211 {
00212 int numPes = CkNumPes();
00213 for ( int destNodeID = 0; destNodeID < numPes; ++destNodeID )
00214 if ( combineMigrationMsgs[destNodeID] )
00215 {
00216 DebugM(3,"Sending MigrateAtomsCombinedMsg to node " << destNodeID << "\n");
00217 CProxy_PatchMgr cp(thisgroup);
00218 cp[destNodeID].recvMigrateAtomsCombined(combineMigrationMsgs[destNodeID]);
00219 }
00220 }
00221 }
00222
00223
00224 void PatchMgr::recvMigrateAtoms (MigrateAtomsMsg *msg) {
00225
00226 PatchMap::Object()->homePatch(msg->destPatchID)->depositMigration(msg);
00227 }
00228
00229 void PatchMgr::recvMigrateAtomsCombined (MigrateAtomsCombinedMsg *msg)
00230 {
00231 DebugM(3,"Received MigrateAtomsCombinedMsg with " << msg->srcPatchID.size() << " messages.\n");
00232 msg->distribute();
00233 delete msg;
00234 }
00235
00236 void PatchMgr::moveAtom(MoveAtomMsg *msg) {
00237 LocalID lid = AtomMap::Object()->localID(msg->atomid);
00238 if ( lid.pid != notUsed ) {
00239 HomePatch *hp = patchMap->homePatch(lid.pid);
00240 if ( hp ) {
00241 FullAtom &a = hp->atom[lid.index];
00242 if ( msg->moveto ) {
00243 a.fixedPosition = msg->coord;
00244 } else {
00245 a.fixedPosition = hp->lattice.reverse_transform(a.position,a.transform);
00246 a.fixedPosition += msg->coord;
00247 }
00248 a.position = hp->lattice.apply_transform(a.fixedPosition,a.transform);
00249 }
00250 }
00251 delete msg;
00252 }
00253
00254 void PatchMgr::moveAllBy(MoveAllByMsg *msg) {
00255
00256 for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
00257 HomePatch *hp = elem->patch;
00258 for (int i=0; i<hp->getNumAtoms(); i++) {
00259 FullAtom &a = hp->atom[i];
00260 a.fixedPosition = hp->lattice.reverse_transform(a.position,a.transform);
00261 a.fixedPosition += msg->offset;
00262 a.position = hp->lattice.apply_transform(a.fixedPosition,a.transform);
00263 }
00264 }
00265 delete msg;
00266 }
00267
00268 void PatchMgr::setLattice(SetLatticeMsg *msg) {
00269
00270 for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
00271 HomePatch *hp = elem->patch;
00272 hp->lattice = msg->lattice;
00273 }
00274
00275 Node::Object()->simParameters->lattice = msg->lattice;
00276 }
00277
00278 PACK_MSG(MovePatchesMsg,
00279 PACK(fromNodeID);
00280 PACK(pid);
00281 PACK_RESIZE(atom);
00282 )
00283
00284
00285 #include "PatchMgr.def.h"
00286