NAMD
CollectionMgr.C
Go to the documentation of this file.
1 
7 #include "CollectionMgr.decl.h"
8 #include "CollectionMgr.h"
9 #include "CollectionMaster.decl.h"
10 #include "CollectionMaster.h"
11 #include "Node.h"
12 #include "SimParameters.h"
13 
14 #include "PatchData.h"
15 
16 #include "ParallelIOMgr.decl.h"
17 #include "ParallelIOMgr.h"
18 #define MIN_DEBUG_LEVEL 3
19 //#define DEBUGM
20 #include "Debug.h"
21 
22 CollectionMgr::CollectionMgr(SlaveInitMsg *msg) : master(msg->master)
23 {
24  delete msg;
25  if (CkpvAccess(CollectionMgr_instance) == 0) {
26  CkpvAccess(CollectionMgr_instance) = this;
27  } else {
28  DebugM(1, "CollectionMgr::CollectionMgr() - another instance of CollectionMgr exists!\n");
29  }
30 }
31 
32 
34 {
35 }
36 
37 #ifdef MEM_OPT_VERSION
38 //1. record the dest output rank of each atom
39 //2. distribute the atoms to the corresponding output procs
40 //1 and 2 are both needed for positions and velocities
41 
43  Lattice l, int prec)
44 {
45  int numAtoms = a.size();
46  AtomIDList aid(numAtoms);
47  ResizeArray<int> oRank(numAtoms);
48  PositionList d(numAtoms);
49  for ( int i=0; i<numAtoms; ++i ) {
50  aid[i] = a[i].id;
51  oRank[i] = a[i].outputRank;
52  d[i] = l.reverse_transform(a[i].position,a[i].transform);
53  }
54  CollectVectorInstance *c;
55  if ( ( c = positions.submitData(seq,aid,oRank,d,prec) ) )
56  {
57  CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
58  ParallelIOMgr *ioMgr = io.ckLocalBranch();
59 
60  //construct per output proc atoms list
61  AtomIDList *perOList = new AtomIDList[ioMgr->numOutputProcs];
62  for(int i=0; i<c->aid.size(); i++){
63  perOList[c->outRank[i]].add(i);
64  }
66  if(prec==1||prec==4){
68  }else if(prec==2){
70  }else{
72  }
73  //send msg to output proc if there's one
74  for(int i=0; i<ioMgr->numOutputProcs; i++){
75  int numAtoms = perOList[i].size();
76  if(!numAtoms && ioMgr->numProxiesPerOutputProc == 0) continue;
78  if( vstatus == CollectVectorVarMsg::VectorValid){
79  msg = new(numAtoms, numAtoms, 0, 0)CollectVectorVarMsg;
80  for(int j=0; j<numAtoms; j++){
81  int lIdx = perOList[i][j];
82  msg->aid[j] = c->aid[lIdx];
83  msg->data[j] = c->data[lIdx];
84  }
85  }else if(vstatus == CollectVectorVarMsg::FloatVectorValid){
86  msg = new(numAtoms, 0, numAtoms, 0)CollectVectorVarMsg;
87  for(int j=0; j<numAtoms; j++){
88  int lIdx = perOList[i][j];
89  msg->aid[j] = c->aid[lIdx];
90  msg->fdata[j] = c->fdata[lIdx];
91  }
92  }else{
93  msg = new(numAtoms, numAtoms, numAtoms, 0)CollectVectorVarMsg;
94  for(int j=0; j<numAtoms; j++){
95  int lIdx = perOList[i][j];
96  msg->aid[j] = c->aid[lIdx];
97  msg->data[j] = c->data[lIdx];
98  msg->fdata[j] = c->fdata[lIdx];
99  }
100  }
101  msg->seq = c->seq;
102  msg->size = numAtoms;
103  msg->status = vstatus;
104  io[ioMgr->myOutputProxies[i]].receivePositions(msg);
105  }
106  c->free();
107  delete [] perOList;
108  }
109 }
110 
111 void CollectionMgr::submitVelocities(int seq, int zero, FullAtomList &a)
112 {
113  int numAtoms = a.size();
114  AtomIDList aid(numAtoms);
115  ResizeArray<int> oRank(numAtoms);
116  PositionList d(numAtoms);
117  for ( int i=0; i<numAtoms; ++i ) {
118  aid[i] = a[i].id;
119  oRank[i] = a[i].outputRank;
120  if ( zero ) d[i] = 0.;
121  else d[i] = a[i].velocity;
122  }
123  CollectVectorInstance *c;
124  if ( ( c = velocities.submitData(seq,aid,oRank,d) ) )
125  {
126  CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
127  ParallelIOMgr *ioMgr = io.ckLocalBranch();
128 
129  //construct per output proc atoms list
130  AtomIDList *perOList = new AtomIDList[ioMgr->numOutputProcs];
131  for(int i=0; i<c->aid.size(); i++){
132  perOList[c->outRank[i]].add(i);
133  }
135  //send msg to output proc if there's one
136  for(int i=0; i<ioMgr->numOutputProcs; i++){
137  int numAtoms = perOList[i].size();
138  if(!numAtoms && ioMgr->numProxiesPerOutputProc == 0) continue;
139  CollectVectorVarMsg *msg;
140  msg = new(numAtoms, numAtoms, 0, 0)CollectVectorVarMsg;
141  msg->seq = c->seq;
142  msg->size = numAtoms;
143  msg->status = vstatus;
144  for(int j=0; j<numAtoms; j++){
145  int lIdx = perOList[i][j];
146  msg->aid[j] = c->aid[lIdx];
147  msg->data[j] = c->data[lIdx];
148  }
149  io[ioMgr->myOutputProxies[i]].receiveVelocities(msg);
150  }
151  c->free();
152  delete [] perOList;
153  }
154 }
155 
156 void CollectionMgr::submitForces(int seq, FullAtomList &a, int maxForceUsed, ForceList *f)
157 {
158  int numAtoms = a.size();
159  AtomIDList aid(numAtoms);
160  ResizeArray<int> oRank(numAtoms);
161  ForceList d(numAtoms);
162  for ( int i=0; i<numAtoms; ++i ) {
163  aid[i] = a[i].id;
164  oRank[i] = a[i].outputRank;
165  d[i] = 0.;
166  }
167  for ( int j=0; j<=maxForceUsed; ++j ) {
168  Force *fptr = f[j].begin();
169  for ( int i=0; i<numAtoms; ++i ) {
170  d[i] += fptr[i];
171  }
172  }
173  if ( Node::Object()->simParameters->fixedAtomsOn && !Node::Object()->simParameters->fixedAtomsForceOutput) {
174  for ( int i=0; i<numAtoms; ++i ) {
175  if ( a[i].atomFixed ) d[i] = 0.;
176  }
177  }
178  CollectVectorInstance *c;
179  if ( ( c = forces.submitData(seq,aid,oRank,d) ) )
180  {
181  CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
182  ParallelIOMgr *ioMgr = io.ckLocalBranch();
183 
184  //construct per output proc atoms list
185  AtomIDList *perOList = new AtomIDList[ioMgr->numOutputProcs];
186  for(int i=0; i<c->aid.size(); i++){
187  perOList[c->outRank[i]].add(i);
188  }
190  //send msg to output proc if there's one
191  for(int i=0; i<ioMgr->numOutputProcs; i++){
192  int numAtoms = perOList[i].size();
193  if(!numAtoms && ioMgr->numProxiesPerOutputProc == 0) continue;
194  CollectVectorVarMsg *msg;
195  msg = new(numAtoms, numAtoms, 0, 0)CollectVectorVarMsg;
196  msg->seq = c->seq;
197  msg->size = numAtoms;
198  msg->status = vstatus;
199  for(int j=0; j<numAtoms; j++){
200  int lIdx = perOList[i][j];
201  msg->aid[j] = c->aid[lIdx];
202  msg->data[j] = c->data[lIdx];
203  }
204  io[ioMgr->myOutputProxies[i]].receiveForces(msg);
205  }
206  c->free();
207  delete [] perOList;
208  }
209 }
210 
211 #else
213  Lattice l, int prec, int dcdSelectiontag)
214 {
215  int numAtomsTotal = a.size();
216  int numAtomsDcdSelection = 0;
217  int numAtoms;
218  int dcdSelectionindex=dcdSelectiontag-1;
219  if(prec == 4) // count up the selection intersection in this atomlist
220  {
221  int bitmask = 1 << dcdSelectionindex;
222  for ( int i=0; i<numAtomsTotal; ++i ) {
223  if(a[i].flags.dcdSelection & bitmask)
224  numAtomsDcdSelection++;
225  }
226  numAtoms = numAtomsDcdSelection;
227  }
228  else
229  {
230  numAtoms = numAtomsTotal;
231  }
232  AtomIDList aid(numAtoms);
233  PositionList d(numAtoms);
234  if(prec == 4)
235  {
236  // only load atoms in the selection list
237  int selectionCounter=0;
238  int bitmask = 1 << dcdSelectionindex;
239  DebugM(3,"seq " << seq << " prec " << prec <<" numAtomsTotal " << numAtomsTotal<< " dcdSelectionIndex "<< dcdSelectionindex <<" bitmask "<< bitmask <<"\n");
240  for (int i=0; i<numAtomsTotal; ++i ) {
241  if(a[i].flags.dcdSelection & bitmask)
242  {
243  aid[selectionCounter] = a[i].id;
244  d[selectionCounter] = l.reverse_transform(a[i].position,a[i].transform);
245  ++selectionCounter;
246  }
247  }
248  DebugM(3, "["<<CkMyPe()<<"]"<< " selection count "<<selectionCounter<<"\n");
249  }
250  else
251  {
252  for ( int i=0; i<numAtoms; ++i ) {
253  aid[i] = a[i].id;
254  d[i] = l.reverse_transform(a[i].position,a[i].transform);
255  }
256  }
257  auto c= positions.submitData(seq,aid,d,prec) ;
258  if ( c )
259  {
260  int aid_size = c->aid.size();
261  int data_size = c->data.size();
262  int fdata_size = c->fdata.size();
263  CollectVectorMsg *msg
264  = new (aid_size, data_size, fdata_size,0) CollectVectorMsg;
265  msg->index=dcdSelectionindex;
266 
267  msg->seq = c->seq;
268  msg->aid_size = aid_size;
269  msg->data_size = data_size;
270  msg->fdata_size = fdata_size;
271  DebugM(3, "["<<CkMyPe()<<"]"<< " msg index "<<msg->index<< " fdata_size " << msg->fdata_size<<"\n");
272  memcpy(msg->aid,c->aid.begin(),aid_size*sizeof(AtomID));
273  memcpy(msg->data,c->data.begin(),data_size*sizeof(Vector));
274  memcpy(msg->fdata,c->fdata.begin(),fdata_size*sizeof(FloatVector));
275 #ifdef NODEGROUP_FORCE_REGISTER
276  if (Node::Object()->simParameters->CUDASOAintegrate) {
277  CProxy_PatchData cpdata(CkpvAccess(BOCclass_group).patchData);
278  CollectionMaster *pcm = cpdata.ckLocalBranch()->ptrCollectionMaster;
279  pcm->lock();
280  if(prec == 4)
282  else
283  pcm->receivePositions(msg);
284  pcm->unlock();
285  }
286  else
287 #endif
288  {
289  CProxy_CollectionMaster cm(master);
290  if(prec == 4)
291  cm.receivePositionsDcdSelection(msg);
292  else
293  cm.receivePositions(msg);
294  }
295  c->free();
296  }
297 }
298 
300 {
301  int numAtoms = a.size();
302  AtomIDList aid(numAtoms);
303  PositionList d(numAtoms);
304  for ( int i=0; i<numAtoms; ++i ) {
305  aid[i] = a[i].id;
306  if ( zero ) d[i] = 0.;
307  else d[i] = a[i].velocity;
308  }
310  if ( ( c = velocities.submitData(seq,aid,d) ) )
311  {
312  int aid_size = c->aid.size();
313  int data_size = c->data.size();
314  CollectVectorMsg *msg = new (aid_size, data_size, 0, 0) CollectVectorMsg;
315  msg->seq = c->seq;
316  msg->aid_size = aid_size;
317  msg->data_size = data_size;
318  msg->fdata_size = 0;
319  memcpy(msg->aid,c->aid.begin(),aid_size*sizeof(AtomID));
320  memcpy(msg->data,c->data.begin(),data_size*sizeof(Vector));
321 #ifdef NODEGROUP_FORCE_REGISTER
322  if (Node::Object()->simParameters->CUDASOAintegrate) {
323  CProxy_PatchData cpdata(CkpvAccess(BOCclass_group).patchData);
324  CollectionMaster *pcm = cpdata.ckLocalBranch()->ptrCollectionMaster;
325  pcm->lock();
326  pcm->receiveVelocities(msg);
327  pcm->unlock();
328  }
329  else
330 #endif
331  {
332  CProxy_CollectionMaster cm(master);
333  cm.receiveVelocities(msg);
334  }
335  c->free();
336  }
337 }
338 
339 void CollectionMgr::submitForces(int seq, FullAtomList &a, int maxForceUsed, ForceList *f)
340 {
341  int numAtoms = a.size();
342  AtomIDList aid(numAtoms);
343  ForceList d(numAtoms);
344  for ( int i=0; i<numAtoms; ++i ) {
345  aid[i] = a[i].id;
346  d[i] = 0.;
347  }
348  for ( int j=0; j<=maxForceUsed; ++j ) {
349  Force *fptr = f[j].begin();
350  for ( int i=0; i<numAtoms; ++i ) {
351  d[i] += fptr[i];
352  }
353  }
354  if ( Node::Object()->simParameters->fixedAtomsOn && !Node::Object()->simParameters->fixedAtomsForceOutput) {
355  for ( int i=0; i<numAtoms; ++i ) {
356  if ( a[i].atomFixed ) d[i] = 0.;
357  }
358  }
360  if ( ( c = forces.submitData(seq,aid,d) ) )
361  {
362  int aid_size = c->aid.size();
363  int data_size = c->data.size();
364  CollectVectorMsg *msg = new (aid_size, data_size, 0, 0) CollectVectorMsg;
365  msg->seq = c->seq;
366  msg->aid_size = aid_size;
367  msg->data_size = data_size;
368  msg->fdata_size = 0;
369  memcpy(msg->aid,c->aid.begin(),aid_size*sizeof(AtomID));
370  memcpy(msg->data,c->data.begin(),data_size*sizeof(Vector));
371 #ifdef NODEGROUP_FORCE_REGISTER
372  if (Node::Object()->simParameters->CUDASOAintegrate) {
373  CProxy_PatchData cpdata(CkpvAccess(BOCclass_group).patchData);
374  CollectionMaster *pcm = cpdata.ckLocalBranch()->ptrCollectionMaster;
375  pcm->lock();
376  pcm->receiveForces(msg);
377  pcm->unlock();
378  }
379  else
380 #endif
381  {
382  CProxy_CollectionMaster cm(master);
383  cm.receiveForces(msg);
384  }
385  c->free();
386  }
387 }
388 #endif
389 
390 void CollectionMgr::sendDataStream(const char *data) {
391  DataStreamMsg *msg = new DataStreamMsg;
392  msg->data.resize(strlen(data)+1);
393  strcpy(msg->data.begin(),data);
394  CProxy_CollectionMaster cm(master);
395  cm.receiveDataStream(msg);
396 }
397 
398 #include "CollectionMgr.def.h"
399 
static Node * Object()
Definition: Node.h:86
Bool fixedAtomsForceOutput
int size(void) const
Definition: ResizeArray.h:131
NAMD_HOST_DEVICE Position reverse_transform(Position data, const Transform &t) const
Definition: Lattice.h:143
void receiveVelocities(CollectVectorMsg *msg)
Definition: Vector.h:72
SimParameters * simParameters
Definition: Node.h:181
void receivePositions(CollectVectorMsg *msg)
void receivePositionsDcdSelection(CollectVectorMsg *msg)
#define DebugM(x, y)
Definition: Debug.h:75
void submitVelocities(int seq, int zero, FullAtomList &a)
int add(const Elem &elem)
Definition: ResizeArray.h:101
CollectVectorInstance * submitData(int seq, AtomIDList &i, ResizeArray< Vector > &d, int prec=2)
void resize(int i)
Definition: ResizeArray.h:84
void receiveForces(CollectVectorMsg *msg)
~CollectionMgr(void)
Definition: CollectionMgr.C:33
void submitForces(int seq, FullAtomList &a, int maxForceUsed, ForceList *f)
ResizeArray< char > data
iterator begin(void)
Definition: ResizeArray.h:36
int32 AtomID
Definition: NamdTypes.h:35
void submitPositions(int seq, FullAtomList &a, Lattice l, int prec, int dcdSelectionIndex)
CollectionMgr(SlaveInitMsg *msg)
Definition: CollectionMgr.C:22
void sendDataStream(const char *)
FloatVector * fdata