NAMD
PatchMgr.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "PatchMgr.decl.h"
9 #include "PatchMgr.h"
10 
11 #include "NamdTypes.h"
12 //#include "Compute.h"
13 #include "HomePatch.h"
14 #include "PatchMap.h"
15 #include "AtomMap.h"
16 
17 #include "ComputeMsmMsa.h" // needed for MsmMsaData definition
18 #include "main.decl.h"
19 #include "main.h"
20 
21 #include "WorkDistrib.decl.h"
22 #include "WorkDistrib.h"
23 #include "Node.h"
24 #include "SimParameters.h"
25 
26 #include "packmsg.h"
27 
28 // #define DEBUGM
29 #define MIN_DEBUG_LEVEL 3
30 #include "Debug.h"
31 
32 
33 // BOC constructor
35 {
36  // CkPrintf("[%d] PatchMgr Created\n", CkMyPe());
37 
38  // Singleton pattern
39  if (CkpvAccess(PatchMgr_instance) == NULL) {
40  CkpvAccess(PatchMgr_instance) = this;
41  } else {
42  NAMD_bug("PatchMgr instanced twice on same processor!");
43  }
44 
45  // Get PatchMap singleton started
46  patchMap = PatchMap::Instance();
47  patchMap->registerPatchMgr(this);
48 
49  recvCheckpointReq_index = CmiRegisterHandler((CmiHandler)recvCheckpointReq_handler);
50  recvCheckpointLoad_index = CmiRegisterHandler((CmiHandler)recvCheckpointLoad_handler);
51  recvCheckpointStore_index = CmiRegisterHandler((CmiHandler)recvCheckpointStore_handler);
52  recvCheckpointAck_index = CmiRegisterHandler((CmiHandler)recvCheckpointAck_handler);
53 
54  recvExchangeReq_index = CmiRegisterHandler((CmiHandler)recvExchangeReq_handler);
55  recvExchangeMsg_index = CmiRegisterHandler((CmiHandler)recvExchangeMsg_handler);
56 
57  // Message combining initialization
58  migrationCountdown = 0;
59  combineMigrationMsgs = new MigrateAtomsCombinedMsg*[CkNumPes()];
60  int numPes = CkNumPes();
61  for ( int i = 0; i < numPes; ++i ) combineMigrationMsgs[i] = 0;
62 }
63 
65 {
66  HomePatchListIter hi(homePatches);
67  for ( hi = hi.begin(); hi != hi.end(); hi++) {
68  HomePatchElem* elem = homePatches.find(HomePatchElem(hi->pid));
69  delete elem->patch;
70  }
71  delete [] combineMigrationMsgs;
72 }
73 
75 {
76  HomePatch *patch = new HomePatch(pid, a);
77  homePatches.load(HomePatchElem(pid, patch));
78  patchMap->registerPatch(pid, patch);
79 }
80 
81 
82 // Add a HomePatch to a list of patches to be moved
83 // HomePatches are actually moved by invoking sendMovePatches() below
85 {
86  move.load(MovePatch(pid,nodeID));
87 }
88 
89 void PatchMgr::sendOneHomePatch(int patchId, int nodeId){
90  HomePatch *p = homePatch(patchId);
91  patchMap->unregisterPatch(patchId, p);
92 
93  MovePatchesMsg *msg = new MovePatchesMsg(patchId, p->atom);
94 
95  // Deleting the HomePatchElem will call a destructor for clean up
96  // but the msg elements are safe since they use a container template
97  // that uses ref counting.
98  delete p;
99  homePatches.del(HomePatchElem(patchId));
100 
101  if ( msg->atom.shared() ) NAMD_bug("shared message array in PatchMgr::sendOneHomePatch");
102 
103  // Sending to PatchMgr::recvMovePatches on remote node
104  CProxy_PatchMgr cp(thisgroup);
105  cp[nodeId].recvMovePatches(msg);
106 }
107 
108 // Uses list constructed by movePatch() and dispatches
109 // HomePatch(es) to new nodes
111 {
112  if (! move.size())
113  return;
114 
115  MovePatchListIter m(move);
116  for ( m = m.begin(); m != m.end(); m++) {
117  HomePatch *p = homePatch(m->pid);
118  patchMap->unregisterPatch(m->pid, p);
119 
120  MovePatchesMsg *msg = new MovePatchesMsg(m->pid, p->atom);
121 
122  // Deleting the HomePatchElem will call a destructor for clean up
123  // but the msg elements are safe since they use a container template
124  // that uses ref counting.
125  delete p;
126  homePatches.del(HomePatchElem(m->pid));
127 
128  if ( msg->atom.shared() ) NAMD_bug("shared message array in PatchMgr::sendMovePatches");
129 
130  // Sending to PatchMgr::recvMovePatches on remote node
131  CProxy_PatchMgr cp(thisgroup);
132  cp[m->nodeID].recvMovePatches(msg);
133  }
134  move.resize(0);
135 }
136 
138  // Make a new HomePatch
139  createHomePatch(msg->pid, msg->atom);
140  delete msg;
141 
142  // Tell sending PatchMgr we received MovePatchMsg
143 // AckMovePatchesMsg *ackmsg =
144 // new AckMovePatchesMsg;
145 // CSendMsgBranch(PatchMgr,ackMovePatches, ackmsg, thisgroup, msg->fromNodeID);
146 }
147 
148 
149 //void PatchMgr::ackMovePatches(AckMovePatchesMsg *msg)
150 //{
151 // delete msg;
152 // if (! --ackMovePending)
153 // WorkDistrib::messageMovePatchDone();
154 //}
155 
156 
158 
159  MovePatchesMsg *msg = new MovePatchesMsg(pid, a);
160 
161  if ( msg->atom.shared() ) NAMD_bug("shared message array in PatchMgr::sendAtoms");
162 
163  CProxy_PatchMgr cp(thisgroup);
164  cp[patchMap->node(pid)].recvAtoms(msg);
165 
166 }
167 
169  patchMap->homePatch(msg->pid)->reinitAtoms(msg->atom);
170  delete msg;
171 }
172 
173 // Called by HomePatch to migrate atoms off to new patches
174 // Message combining occurs here
176 /*
177  for (int i=0; i < numMsgs; i++) {
178  PatchMgr::Object()->sendMigrationMsg(src, m[i]);
179  }
180 */
181  // The migration countdown is getting crapped up for some reason.
182  // The migrationCountdown should be zero everytime I need to do a migration step here
183  if ( ! migrationCountdown ) // (re)initialize
184  {
185  // DebugM(3,"migrationCountdown (re)initialize\n");
186  numHomePatches = patchMap->numHomePatches();
187  migrationCountdown = numHomePatches;
188  combineMigrationDestPes.resize(0);
189  }
190  for (int i=0; i < numMsgs; i++) { // buffer messages
191  int destNodeID = m[i].destNodeID;
192  if ( 1 ) // destNodeID != CkMyPe() )
193  {
194  if ( ! combineMigrationMsgs[destNodeID] )
195  {
196  combineMigrationMsgs[destNodeID] = new MigrateAtomsCombinedMsg();
197  combineMigrationDestPes.add(destNodeID);
198  }
199  combineMigrationMsgs[destNodeID]->add(src,m[i].destPatchID,m[i].mList);
200  }
201  else
202  {
203  // for now buffer local messages too
204  }
205  }
206  migrationCountdown -= 1;
207  // DebugM(3,"migrationCountdown = " << migrationCountdown << "\n");
208  if ( ! migrationCountdown ) // send out combined messages
209  {
210  int n = combineMigrationDestPes.size();
211  for ( int i = 0; i < n; ++i ) {
212  int destNodeID = combineMigrationDestPes[i];
213  DebugM(3,"Sending MigrateAtomsCombinedMsg to node " << destNodeID << "\n");
214  CProxy_PatchMgr cp(thisgroup);
215  cp[destNodeID].recvMigrateAtomsCombined(combineMigrationMsgs[destNodeID]);
216  combineMigrationMsgs[destNodeID] = 0;
217  }
218  }
219 }
220 
222 {
223  DebugM(3,"Received MigrateAtomsCombinedMsg with " << msg->srcPatchID.size() << " messages.\n");
224  msg->distribute();
225  delete msg;
226 }
227 
229  LocalID lid = AtomMap::Object()->localID(msg->atomid);
230  if ( lid.pid != notUsed ) {
231  HomePatch *hp = patchMap->homePatch(lid.pid);
232  if ( hp ) {
233  FullAtom &a = hp->atom[lid.index];
234  if ( msg->moveto ) {
235  a.fixedPosition = msg->coord;
236  } else {
238  a.fixedPosition += msg->coord;
239  }
241  }
242  }
243  delete msg;
244 }
245 
247  // loop over homePatches, moving every atom
248  for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
249  HomePatch *hp = elem->patch;
250  for (int i=0; i<hp->getNumAtoms(); i++) {
251  FullAtom &a = hp->atom[i];
253  a.fixedPosition += msg->offset;
255  }
256  }
257  delete msg;
258 }
259 
261  // loop over homePatches, setting the lattice to the new value.
262  for (HomePatchElem *elem = homePatches.begin(); elem != homePatches.end(); elem++) {
263  HomePatch *hp = elem->patch;
264  hp->lattice = msg->lattice;
265  }
266  // Must also do this for SimParameters in order for pressure profile to work!
268 }
269 
270 
271 // initiating replica
272 void PatchMgr::sendCheckpointReq(int pid, int remote, const char *key, int task) {
273  CheckpointAtomsReqMsg *msg = new (1+strlen(key),0) CheckpointAtomsReqMsg;
274  msg->pid = pid;
275  msg->pe = CkMyPe();
276  msg->replica = CmiMyPartition();
277  msg->task = task;
278  strcpy(msg->key,key);
279  envelope *env = UsrToEnv(CheckpointAtomsReqMsg::pack(msg));
280  CmiSetHandler(env,recvCheckpointReq_index);
281 #if CMK_HAS_PARTITION
282  CmiInterSyncSendAndFree(CkMyPe(),remote,env->getTotalsize(),(char*)env);
283 #else
284  CmiSyncSendAndFree(CkMyPe(),env->getTotalsize(),(char*)env);
285 #endif
286 }
287 
288 // responding replica
289 extern "C" {
290  void recvCheckpointReq_handler(envelope *env) {
291  PatchMgr::Object()->recvCheckpointReq(CheckpointAtomsReqMsg::unpack(EnvToUsr(env)));
292  }
293 }
294 
295 // responding replica
297  int patchnode = patchMap->node(msg->pid);
298  if ( CkMyPe() != patchnode ) {
299  thisProxy[patchnode].recvCheckpointReq(msg);
300  } else {
301  HomePatch *hp = patchMap->homePatch(msg->pid);
302  if ( ! hp ) NAMD_bug("null HomePatch pointer in PatchMgr::recvCheckpointReq");
303  hp->recvCheckpointReq(msg->task, msg->key, msg->replica, msg->pe);
304  delete msg;
305  }
306 }
307 
308 
309 // responding replica
310 void PatchMgr::sendCheckpointLoad(CheckpointAtomsMsg *msg, int dst, int dstpe) {
311  envelope *env = UsrToEnv(CheckpointAtomsMsg::pack(msg));
312  CmiSetHandler(env,recvCheckpointLoad_index);
313 #if CMK_HAS_PARTITION
314  CmiInterSyncSendAndFree(dstpe,dst,env->getTotalsize(),(char*)env);
315 #else
316  CmiSyncSendAndFree(dstpe,env->getTotalsize(),(char*)env);
317 #endif
318 }
319 
320 // initiating replica
321 extern "C" {
322  void recvCheckpointLoad_handler(envelope *env) {
323  PatchMgr::Object()->recvCheckpointLoad(CheckpointAtomsMsg::unpack(EnvToUsr(env)));
324  }
325 }
326 
327 // initiating replica
329  HomePatch *hp = patchMap->homePatch(msg->pid);
330  hp->recvCheckpointLoad(msg);
331 }
332 
333 
334 // initiating replica
335 void PatchMgr::sendCheckpointStore(CheckpointAtomsMsg *msg, int dst, int dstpe) {
336  envelope *env = UsrToEnv(CheckpointAtomsMsg::pack(msg));
337  CmiSetHandler(env,recvCheckpointStore_index);
338 #if CMK_HAS_PARTITION
339  CmiInterSyncSendAndFree(dstpe,dst,env->getTotalsize(),(char*)env);
340 #else
341  CmiSyncSendAndFree(dstpe,env->getTotalsize(),(char*)env);
342 #endif
343 }
344 
345 // responding replica
346 extern "C" {
347  void recvCheckpointStore_handler(envelope *env) {
348  PatchMgr::Object()->recvCheckpointStore(CheckpointAtomsMsg::unpack(EnvToUsr(env)));
349  }
350 }
351 
352 // responding replica
354  HomePatch *hp = patchMap->homePatch(msg->pid);
355  hp->recvCheckpointStore(msg);
356 }
357 
358 
359 // responding replica
360 void PatchMgr::sendCheckpointAck(int pid, int dst, int dstpe) {
362  msg->pid = pid;
363  envelope *env = UsrToEnv(CheckpointAtomsReqMsg::pack(msg));
364  CmiSetHandler(env,recvCheckpointAck_index);
365 #if CMK_HAS_PARTITION
366  CmiInterSyncSendAndFree(dstpe,dst,env->getTotalsize(),(char*)env);
367 #else
368  CmiSyncSendAndFree(dstpe,env->getTotalsize(),(char*)env);
369 #endif
370 }
371 
372 // initiating replica
373 extern "C" {
374  void recvCheckpointAck_handler(envelope *env) {
375  PatchMgr::Object()->recvCheckpointAck(CheckpointAtomsReqMsg::unpack(EnvToUsr(env)));
376  }
377 }
378 
379 // initiating replica
381  HomePatch *hp = patchMap->homePatch(msg->pid);
382  if ( ! hp ) NAMD_bug("null HomePatch pointer in PatchMgr::recvCheckpointAck");
383  hp->recvCheckpointAck();
384  delete msg;
385 }
386 
387 
388 void PatchMgr::sendExchangeReq(int pid, int src) {
390  msg->pid = pid;
391  msg->dstpe = CkMyPe();
392  envelope *env = UsrToEnv(ExchangeAtomsReqMsg::pack(msg));
393  CmiSetHandler(env,recvExchangeReq_index);
394 #if CMK_HAS_PARTITION
395  CmiInterSyncSendAndFree(CkMyPe(),src,env->getTotalsize(),(char*)env);
396 #else
397  CmiSyncSendAndFree(CkMyPe(),env->getTotalsize(),(char*)env);
398 #endif
399 }
400 
401 extern "C" {
402  void recvExchangeReq_handler(envelope *env) {
403  PatchMgr::Object()->recvExchangeReq(ExchangeAtomsReqMsg::unpack(EnvToUsr(env)));
404  }
405 }
406 
408  int patchnode = patchMap->node(msg->pid);
409  if ( CkMyPe() != patchnode ) {
410  thisProxy[patchnode].recvExchangeReq(msg);
411  } else {
412  HomePatch *hp = patchMap->homePatch(msg->pid);
413  if ( ! hp ) NAMD_bug("null HomePatch pointer in PatchMgr::recvExchangeReq");
414  hp->recvExchangeReq(msg->dstpe);
415  delete msg;
416  }
417 }
418 
419 void PatchMgr::sendExchangeMsg(ExchangeAtomsMsg *msg, int dst, int dstpe) {
420  envelope *env = UsrToEnv(ExchangeAtomsMsg::pack(msg));
421  CmiSetHandler(env,recvExchangeMsg_index);
422 #if CMK_HAS_PARTITION
423  CmiInterSyncSendAndFree(dstpe,dst,env->getTotalsize(),(char*)env);
424 #else
425  CmiSyncSendAndFree(dstpe,env->getTotalsize(),(char*)env);
426 #endif
427 }
428 
429 extern "C" {
430  void recvExchangeMsg_handler(envelope *env) {
431  PatchMgr::Object()->recvExchangeMsg(ExchangeAtomsMsg::unpack(EnvToUsr(env)));
432  }
433 }
434 
436  HomePatch *hp = patchMap->homePatch(msg->pid);
437  hp->recvExchangeMsg(msg);
438 }
439 
440 void PatchMgr::setHomePatchFixedAtomNum(int patchId, int numFixed){
441  HomePatch *thisHomePatch = patchMap->homePatch(patchId);
442  thisHomePatch->setNumFixedAtoms(numFixed);
443 }
444 
446  PACK(fromNodeID);
447  PACK(pid);
448  PACK_RESIZE(atom);
449 )
450 
451 
452 #include "PatchMgr.def.h"
453 
static Node * Object()
Definition: Node.h:86
void recvCheckpointLoad(CheckpointAtomsMsg *msg)
Definition: HomePatch.C:5295
int getNumAtoms() const
Definition: Patch.h:105
void recvCheckpointReq_handler(envelope *env)
Definition: PatchMgr.C:290
HomePatch * patch
Definition: HomePatchList.h:23
int size(void) const
Definition: ResizeArray.h:131
NAMD_HOST_DEVICE Position reverse_transform(Position data, const Transform &t) const
Definition: Lattice.h:143
Position fixedPosition
Definition: NamdTypes.h:202
Lattice & lattice
Definition: Patch.h:127
void setHomePatchFixedAtomNum(int patchId, int numFixed)
Definition: PatchMgr.C:440
SimParameters * simParameters
Definition: Node.h:181
void sendOneHomePatch(int patchId, int nodeId)
Definition: PatchMgr.C:89
void recvExchangeReq_handler(envelope *env)
Definition: PatchMgr.C:402
void createHomePatch(PatchID pid, FullAtomList &a)
Definition: PatchMgr.C:74
void sendAtoms(PatchID pid, FullAtomList &a)
Definition: PatchMgr.C:157
void add(PatchID source, PatchID destination, MigrationList &m)
#define DebugM(x, y)
Definition: Debug.h:75
void recvExchangeMsg(ExchangeAtomsMsg *msg)
Definition: PatchMgr.C:435
void sendExchangeReq(int pid, int src)
Definition: PatchMgr.C:388
int atomid
Definition: PatchMgr.h:55
Position position
Definition: NamdTypes.h:77
ResizeArrayIter< T > begin(void) const
void recvExchangeReq(ExchangeAtomsReqMsg *msg)
Definition: PatchMgr.C:407
#define PACK_MSG(MSGTYPE, MSGDATA)
Definition: packmsg.h:35
HomePatch * homePatch(PatchID pid)
Definition: PatchMap.h:249
int add(const Elem &elem)
Definition: ResizeArray.h:101
void recvCheckpointReq(int task, const char *key, int replica, int pe)
Definition: HomePatch.C:5265
FullAtomList atom
Definition: PatchMgr.h:38
void movePatch(PatchID, NodeID)
Definition: PatchMgr.C:84
void recvCheckpointAck()
Definition: HomePatch.C:5359
void resize(int i)
Definition: ResizeArray.h:84
Vector offset
Definition: PatchMgr.h:62
int32 index
Definition: NamdTypes.h:290
void unregisterPatch(PatchID pid, HomePatch *pptr)
Definition: PatchMap.C:796
void sendCheckpointAck(int pid, int dst, int dstpe)
Definition: PatchMgr.C:360
void recvCheckpointLoad(CheckpointAtomsMsg *msg)
Definition: PatchMgr.C:328
void recvCheckpointStore(CheckpointAtomsMsg *msg)
Definition: PatchMgr.C:353
PatchID pid
Definition: PatchMgr.h:37
void recvAtoms(MovePatchesMsg *msg)
Definition: PatchMgr.C:168
void recvMigrateAtomsCombined(MigrateAtomsCombinedMsg *)
Definition: PatchMgr.C:221
NAMD_HOST_DEVICE Position apply_transform(Position data, const Transform &t) const
Definition: Lattice.h:137
void NAMD_bug(const char *err_msg)
Definition: common.C:195
ResizeArray< PatchID > srcPatchID
void sendMovePatches()
Definition: PatchMgr.C:110
void recvCheckpointLoad_handler(envelope *env)
Definition: PatchMgr.C:322
NodeID destNodeID
Definition: Migration.h:21
LocalID localID(AtomID id)
Definition: AtomMap.h:78
int load(const Elem &elem)
Definition: SortedArray.h:50
void setNumFixedAtoms(int numFixed)
Definition: Patch.h:113
void sendMigrationMsgs(PatchID, MigrationInfo *, int)
Definition: PatchMgr.C:175
int moveto
Definition: PatchMgr.h:56
void setLattice(SetLatticeMsg *msg)
Definition: PatchMgr.C:260
void del(const Elem &elem)
Definition: SortedArray.h:59
void recvCheckpointAck_handler(envelope *env)
Definition: PatchMgr.C:374
PatchID pid
Definition: NamdTypes.h:289
int32 NodeID
Definition: NamdTypes.h:279
static AtomMap * Object()
Definition: AtomMap.h:37
void sendCheckpointLoad(CheckpointAtomsMsg *msg, int dst, int dstpe)
Definition: PatchMgr.C:310
void moveAtom(MoveAtomMsg *msg)
Definition: PatchMgr.C:228
#define PACK_RESIZE(DATA)
Definition: packmsg.h:125
void recvCheckpointAck(CheckpointAtomsReqMsg *msg)
Definition: PatchMgr.C:380
iterator begin(void)
Definition: ResizeArray.h:36
void recvCheckpointStore_handler(envelope *env)
Definition: PatchMgr.C:347
static void registerPatchMgr(PatchMgr *pmgr)
Definition: PatchMap.h:49
int numHomePatches(void)
Definition: PatchMap.C:432
Elem * find(const Elem &elem)
Definition: SortedArray.h:94
iterator end(void)
Definition: ResizeArray.h:37
~PatchMgr()
Definition: PatchMgr.C:64
#define PACK(DATA)
Definition: packmsg.h:123
void recvExchangeReq(int req)
Definition: HomePatch.C:5385
void moveAllBy(MoveAllByMsg *msg)
Definition: PatchMgr.C:246
HomePatch * homePatch(PatchID pid)
Definition: PatchMgr.h:165
bool shared() const
Definition: ResizeArray.h:72
PatchMgr()
Definition: PatchMgr.C:34
void registerPatch(PatchID pid, HomePatch *pptr)
Definition: PatchMap.C:786
int node(int pid) const
Definition: PatchMap.h:114
int32 PatchID
Definition: NamdTypes.h:277
ResizeArrayIter< T > end(void) const
static PatchMap * Instance()
Definition: PatchMap.C:32
void sendCheckpointStore(CheckpointAtomsMsg *msg, int dst, int dstpe)
Definition: PatchMgr.C:335
Lattice lattice
Definition: PatchMgr.h:67
void recvCheckpointReq(CheckpointAtomsReqMsg *msg)
Definition: PatchMgr.C:296
void sendCheckpointReq(int pid, int remote, const char *key, int task)
Definition: PatchMgr.C:272
static PatchMgr * Object()
Definition: PatchMgr.h:152
void recvExchangeMsg_handler(envelope *env)
Definition: PatchMgr.C:430
Vector coord
Definition: PatchMgr.h:57
void sendExchangeMsg(ExchangeAtomsMsg *msg, int dst, int dstpe)
Definition: PatchMgr.C:419
void recvCheckpointStore(CheckpointAtomsMsg *msg)
Definition: HomePatch.C:5345
void recvExchangeMsg(ExchangeAtomsMsg *msg)
Definition: HomePatch.C:5396
Transform transform
Definition: NamdTypes.h:219
void recvMovePatches(MovePatchesMsg *msg)
Definition: PatchMgr.C:137