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 #include "ComputeMsmMsa.h"
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
00029 #define MIN_DEBUG_LEVEL 3
00030 #include "Debug.h"
00031
00032
00033
00034 PatchMgr::PatchMgr()
00035 {
00036
00037
00038
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
00048 patchMap = PatchMap::Instance();
00049 patchMap->registerPatchMgr(this);
00050
00051
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
00083
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
00096
00097
00098 delete p;
00099 homePatches.del(HomePatchElem(patchId));
00100
00101
00102 CProxy_PatchMgr cp(thisgroup);
00103 cp[nodeId].recvMovePatches(msg);
00104 }
00105
00106
00107
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
00121
00122
00123 delete p;
00124 homePatches.del(HomePatchElem(m->pid));
00125
00126
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
00135 createHomePatch(msg->pid, msg->atom);
00136 delete msg;
00137
00138
00139
00140
00141
00142 }
00143
00144
00145
00146
00147
00148
00149
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;
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
00171
00172 void PatchMgr::sendMigrationMsgs(PatchID src, MigrationInfo *m, int numMsgs) {
00173
00174
00175
00176
00177
00178 if ( ! migrationCountdown )
00179 {
00180
00181 numHomePatches = patchMap->numHomePatches();
00182 migrationCountdown = numHomePatches;
00183 combineMigrationDestPes.resize(0);
00184 }
00185 for (int i=0; i < numMsgs; i++) {
00186 int destNodeID = m[i].destNodeID;
00187 if ( 1 )
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
00199 }
00200 }
00201 migrationCountdown -= 1;
00202
00203 if ( ! migrationCountdown )
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
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
00257 for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
00258 HomePatch *hp = elem->patch;
00259 hp->lattice = msg->lattice;
00260 }
00261
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