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 "ParallelIOMgr.decl.h"
15 #include "ParallelIOMgr.h"
16 
17 //#define DEBUGM
18 #include "Debug.h"
19 
20 CollectionMgr::CollectionMgr(SlaveInitMsg *msg) : master(msg->master)
21 {
22  delete msg;
23  if (CkpvAccess(CollectionMgr_instance) == 0) {
24  CkpvAccess(CollectionMgr_instance) = this;
25  } else {
26  DebugM(1, "CollectionMgr::CollectionMgr() - another instance of CollectionMgr exists!\n");
27  }
28 }
29 
30 
32 {
33 }
34 
35 #ifdef MEM_OPT_VERSION
36 //1. record the dest output rank of each atom
37 //2. distribute the atoms to the corresponding output procs
38 //1 and 2 are both needed for positions and velocities
39 
41  Lattice l, int prec)
42 {
43  int numAtoms = a.size();
44  AtomIDList aid(numAtoms);
45  ResizeArray<int> oRank(numAtoms);
46  PositionList d(numAtoms);
47  for ( int i=0; i<numAtoms; ++i ) {
48  aid[i] = a[i].id;
49  oRank[i] = a[i].outputRank;
50  d[i] = l.reverse_transform(a[i].position,a[i].transform);
51  }
52  CollectVectorInstance *c;
53  if ( ( c = positions.submitData(seq,aid,oRank,d,prec) ) )
54  {
55  CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
56  ParallelIOMgr *ioMgr = io.ckLocalBranch();
57 
58  //construct per output proc atoms list
59  AtomIDList *perOList = new AtomIDList[ioMgr->numOutputProcs];
60  for(int i=0; i<c->aid.size(); i++){
61  perOList[c->outRank[i]].add(i);
62  }
64  if(prec==1){
66  }else if(prec==2){
68  }else{
70  }
71  //send msg to output proc if there's one
72  for(int i=0; i<ioMgr->numOutputProcs; i++){
73  int numAtoms = perOList[i].size();
74  if(!numAtoms && ioMgr->numProxiesPerOutputProc == 0) continue;
76  if( vstatus == CollectVectorVarMsg::VectorValid){
77  msg = new(numAtoms, numAtoms, 0, 0)CollectVectorVarMsg;
78  for(int j=0; j<numAtoms; j++){
79  int lIdx = perOList[i][j];
80  msg->aid[j] = c->aid[lIdx];
81  msg->data[j] = c->data[lIdx];
82  }
83  }else if(vstatus == CollectVectorVarMsg::FloatVectorValid){
84  msg = new(numAtoms, 0, numAtoms, 0)CollectVectorVarMsg;
85  for(int j=0; j<numAtoms; j++){
86  int lIdx = perOList[i][j];
87  msg->aid[j] = c->aid[lIdx];
88  msg->fdata[j] = c->fdata[lIdx];
89  }
90  }else{
91  msg = new(numAtoms, numAtoms, numAtoms, 0)CollectVectorVarMsg;
92  for(int j=0; j<numAtoms; j++){
93  int lIdx = perOList[i][j];
94  msg->aid[j] = c->aid[lIdx];
95  msg->data[j] = c->data[lIdx];
96  msg->fdata[j] = c->fdata[lIdx];
97  }
98  }
99  msg->seq = c->seq;
100  msg->size = numAtoms;
101  msg->status = vstatus;
102  io[ioMgr->myOutputProxies[i]].receivePositions(msg);
103  }
104  c->free();
105  delete [] perOList;
106  }
107 }
108 
109 void CollectionMgr::submitVelocities(int seq, int zero, FullAtomList &a)
110 {
111  int numAtoms = a.size();
112  AtomIDList aid(numAtoms);
113  ResizeArray<int> oRank(numAtoms);
114  PositionList d(numAtoms);
115  for ( int i=0; i<numAtoms; ++i ) {
116  aid[i] = a[i].id;
117  oRank[i] = a[i].outputRank;
118  if ( zero ) d[i] = 0.;
119  else d[i] = a[i].velocity;
120  }
121  CollectVectorInstance *c;
122  if ( ( c = velocities.submitData(seq,aid,oRank,d) ) )
123  {
124  CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
125  ParallelIOMgr *ioMgr = io.ckLocalBranch();
126 
127  //construct per output proc atoms list
128  AtomIDList *perOList = new AtomIDList[ioMgr->numOutputProcs];
129  for(int i=0; i<c->aid.size(); i++){
130  perOList[c->outRank[i]].add(i);
131  }
133  //send msg to output proc if there's one
134  for(int i=0; i<ioMgr->numOutputProcs; i++){
135  int numAtoms = perOList[i].size();
136  if(!numAtoms && ioMgr->numProxiesPerOutputProc == 0) continue;
137  CollectVectorVarMsg *msg;
138  msg = new(numAtoms, numAtoms, 0, 0)CollectVectorVarMsg;
139  msg->seq = c->seq;
140  msg->size = numAtoms;
141  msg->status = vstatus;
142  for(int j=0; j<numAtoms; j++){
143  int lIdx = perOList[i][j];
144  msg->aid[j] = c->aid[lIdx];
145  msg->data[j] = c->data[lIdx];
146  }
147  io[ioMgr->myOutputProxies[i]].receiveVelocities(msg);
148  }
149  c->free();
150  delete [] perOList;
151  }
152 }
153 
154 void CollectionMgr::submitForces(int seq, FullAtomList &a, int maxForceUsed, ForceList *f)
155 {
156  int numAtoms = a.size();
157  AtomIDList aid(numAtoms);
158  ResizeArray<int> oRank(numAtoms);
159  ForceList d(numAtoms);
160  for ( int i=0; i<numAtoms; ++i ) {
161  aid[i] = a[i].id;
162  oRank[i] = a[i].outputRank;
163  d[i] = 0.;
164  }
165  for ( int j=0; j<=maxForceUsed; ++j ) {
166  Force *fptr = f[j].begin();
167  for ( int i=0; i<numAtoms; ++i ) {
168  d[i] += fptr[i];
169  }
170  }
171  if ( Node::Object()->simParameters->fixedAtomsOn && !Node::Object()->simParameters->fixedAtomsForceOutput) {
172  for ( int i=0; i<numAtoms; ++i ) {
173  if ( a[i].atomFixed ) d[i] = 0.;
174  }
175  }
176  CollectVectorInstance *c;
177  if ( ( c = forces.submitData(seq,aid,oRank,d) ) )
178  {
179  CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
180  ParallelIOMgr *ioMgr = io.ckLocalBranch();
181 
182  //construct per output proc atoms list
183  AtomIDList *perOList = new AtomIDList[ioMgr->numOutputProcs];
184  for(int i=0; i<c->aid.size(); i++){
185  perOList[c->outRank[i]].add(i);
186  }
188  //send msg to output proc if there's one
189  for(int i=0; i<ioMgr->numOutputProcs; i++){
190  int numAtoms = perOList[i].size();
191  if(!numAtoms && ioMgr->numProxiesPerOutputProc == 0) continue;
192  CollectVectorVarMsg *msg;
193  msg = new(numAtoms, numAtoms, 0, 0)CollectVectorVarMsg;
194  msg->seq = c->seq;
195  msg->size = numAtoms;
196  msg->status = vstatus;
197  for(int j=0; j<numAtoms; j++){
198  int lIdx = perOList[i][j];
199  msg->aid[j] = c->aid[lIdx];
200  msg->data[j] = c->data[lIdx];
201  }
202  io[ioMgr->myOutputProxies[i]].receiveForces(msg);
203  }
204  c->free();
205  delete [] perOList;
206  }
207 }
208 
209 #else
211  Lattice l, int prec)
212 {
213  int numAtoms = a.size();
214  AtomIDList aid(numAtoms);
215  PositionList d(numAtoms);
216  for ( int i=0; i<numAtoms; ++i ) {
217  aid[i] = a[i].id;
218  d[i] = l.reverse_transform(a[i].position,a[i].transform);
219  }
221  if ( ( c = positions.submitData(seq,aid,d,prec) ) )
222  {
223  int aid_size = c->aid.size();
224  int data_size = c->data.size();
225  int fdata_size = c->fdata.size();
226  CollectVectorMsg *msg
227  = new (aid_size, data_size, fdata_size,0) CollectVectorMsg;
228  msg->seq = c->seq;
229  msg->aid_size = aid_size;
230  msg->data_size = data_size;
231  msg->fdata_size = fdata_size;
232  memcpy(msg->aid,c->aid.begin(),aid_size*sizeof(AtomID));
233  memcpy(msg->data,c->data.begin(),data_size*sizeof(Vector));
234  memcpy(msg->fdata,c->fdata.begin(),fdata_size*sizeof(FloatVector));
235  CProxy_CollectionMaster cm(master);
236  cm.receivePositions(msg);
237  c->free();
238  }
239 }
240 
242 {
243  int numAtoms = a.size();
244  AtomIDList aid(numAtoms);
245  PositionList d(numAtoms);
246  for ( int i=0; i<numAtoms; ++i ) {
247  aid[i] = a[i].id;
248  if ( zero ) d[i] = 0.;
249  else d[i] = a[i].velocity;
250  }
252  if ( ( c = velocities.submitData(seq,aid,d) ) )
253  {
254  int aid_size = c->aid.size();
255  int data_size = c->data.size();
256  CollectVectorMsg *msg = new (aid_size, data_size, 0, 0) CollectVectorMsg;
257  msg->seq = c->seq;
258  msg->aid_size = aid_size;
259  msg->data_size = data_size;
260  msg->fdata_size = 0;
261  memcpy(msg->aid,c->aid.begin(),aid_size*sizeof(AtomID));
262  memcpy(msg->data,c->data.begin(),data_size*sizeof(Vector));
263  CProxy_CollectionMaster cm(master);
264  cm.receiveVelocities(msg);
265  c->free();
266  }
267 }
268 
269 void CollectionMgr::submitForces(int seq, FullAtomList &a, int maxForceUsed, ForceList *f)
270 {
271  int numAtoms = a.size();
272  AtomIDList aid(numAtoms);
273  ForceList d(numAtoms);
274  for ( int i=0; i<numAtoms; ++i ) {
275  aid[i] = a[i].id;
276  d[i] = 0.;
277  }
278  for ( int j=0; j<=maxForceUsed; ++j ) {
279  Force *fptr = f[j].begin();
280  for ( int i=0; i<numAtoms; ++i ) {
281  d[i] += fptr[i];
282  }
283  }
284  if ( Node::Object()->simParameters->fixedAtomsOn && !Node::Object()->simParameters->fixedAtomsForceOutput) {
285  for ( int i=0; i<numAtoms; ++i ) {
286  if ( a[i].atomFixed ) d[i] = 0.;
287  }
288  }
290  if ( ( c = forces.submitData(seq,aid,d) ) )
291  {
292  int aid_size = c->aid.size();
293  int data_size = c->data.size();
294  CollectVectorMsg *msg = new (aid_size, data_size, 0, 0) CollectVectorMsg;
295  msg->seq = c->seq;
296  msg->aid_size = aid_size;
297  msg->data_size = data_size;
298  msg->fdata_size = 0;
299  memcpy(msg->aid,c->aid.begin(),aid_size*sizeof(AtomID));
300  memcpy(msg->data,c->data.begin(),data_size*sizeof(Vector));
301  CProxy_CollectionMaster cm(master);
302  cm.receiveForces(msg);
303  c->free();
304  }
305 }
306 #endif
307 
308 void CollectionMgr::sendDataStream(const char *data) {
309  DataStreamMsg *msg = new DataStreamMsg;
310  msg->data.resize(strlen(data)+1);
311  strcpy(msg->data.begin(),data);
312  CProxy_CollectionMaster cm(master);
313  cm.receiveDataStream(msg);
314 }
315 
316 #include "CollectionMgr.def.h"
317 
static Node * Object()
Definition: Node.h:86
Bool fixedAtomsForceOutput
int AtomID
Definition: NamdTypes.h:29
Definition: Vector.h:64
SimParameters * simParameters
Definition: Node.h:178
#define DebugM(x, y)
Definition: Debug.h:59
void submitVelocities(int seq, int zero, FullAtomList &a)
CollectVectorInstance * submitData(int seq, AtomIDList &i, ResizeArray< Vector > &d, int prec=2)
void submitPositions(int seq, FullAtomList &a, Lattice l, int prec)
~CollectionMgr(void)
Definition: CollectionMgr.C:31
void submitForces(int seq, FullAtomList &a, int maxForceUsed, ForceList *f)
int add(const Elem &elem)
Definition: ResizeArray.h:97
ResizeArray< char > data
void resize(int i)
Definition: ResizeArray.h:84
Position reverse_transform(Position data, const Transform &t) const
Definition: Lattice.h:138
ResizeArray< FloatVector > fdata
Definition: CollectionMgr.h:93
CollectionMgr(SlaveInitMsg *msg)
Definition: CollectionMgr.C:20
int size(void) const
Definition: ResizeArray.h:127
void sendDataStream(const char *)
FloatVector * fdata
iterator begin(void)
Definition: ResizeArray.h:36