-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollectionMaster.h
477 lines (401 loc) · 12.8 KB
/
CollectionMaster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
*** Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by
*** The Board of Trustees of the University of Illinois.
*** All rights reserved.
**/
#ifndef COLLECTIONMASTER_H
#define COLLECTIONMASTER_H
#include "charm++.h"
#include "main.h"
#include "NamdTypes.h"
#include "Lattice.h"
#include "ProcessorPrivate.h"
#include "PatchMap.h"
#include "PatchMap.inl"
#include "CollectionMaster.decl.h"
#include <stdio.h>
#include "Node.h"
#include "ParallelIOMgr.decl.h"
#include "ParallelIOMgr.h"
#include "Output.h"
class CollectVectorMsg : public CMessage_CollectVectorMsg
{
public:
int seq;
int aid_size;
int data_size;
int fdata_size;
AtomID *aid;
Vector *data;
FloatVector *fdata;
};
class DataStreamMsg;
class CollectionMaster : public Chare
{
public:
static CollectionMaster *Object() {
return CkpvAccess(CollectionMaster_instance);
}
CollectionMaster();
~CollectionMaster(void);
void receivePositions(CollectVectorMsg *msg);
void receiveVelocities(CollectVectorMsg *msg);
void receiveForces(CollectVectorMsg *msg);
void receiveDataStream(DataStreamMsg *msg);
void enqueuePositions(int seq, Lattice &lattice);
void enqueueVelocities(int seq);
void enqueueForces(int seq);
class CollectVectorInstance;
void disposePositions(CollectVectorInstance *c);
void disposeVelocities(CollectVectorInstance *c);
void disposeForces(CollectVectorInstance *c);
/////Beginning of declarations for comm with CollectionMidMaster/////
void receiveOutputPosReady(int seq);
void receiveOutputVelReady(int seq);
void receiveOutputForceReady(int seq);
//totalT is the time taken to do file I/O for each output workflow -Chao Mei
void startNextRoundOutputPos(double totalT);
void startNextRoundOutputVel(double totalT);
void startNextRoundOutputForce(double totalT);
void wrapCoorFinished();
enum OperationStatus {NOT_PROCESSED, IN_PROCESS, HAS_PROCESSED};
/////End of declarations for comm with CollectionMidMaster/////
#ifdef MEM_OPT_VERSION
class CollectVectorInstance
{
public:
CollectVectorInstance(void) : seq(-10) { ; }
CollectVectorInstance(int s) { reset(s); }
void free() { seq = -10; status = HAS_PROCESSED; }
int notfree() { return ( seq != -10 ); }
void reset(int s) {
if ( s == -10 ) NAMD_bug("seq == free in CollectionMaster");
seq = s;
CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
ParallelIOMgr *ioMgr = io.ckLocalBranch();
remaining = ioMgr->getNumOutputProcs();
status = NOT_PROCESSED;
}
void append(){ --remaining; }
int ready(void) { return ( ! remaining ); }
int seq;
Lattice lattice;
//mainly used for tracking the progress of wrap_coor operation
//the write to files will not happen until the wrap_coor is finished,
//and the CollectVectorInstance is freed. -Chao Mei
OperationStatus status;
private:
int remaining;
}; //end of declaration for CollectionMaster::CollectVectorInstance
class CollectVectorSequence
{
public:
void submitData(int seq){
CollectVectorInstance **c = data.begin();
CollectVectorInstance **c_e = data.end();
for( ; c != c_e && (*c)->seq != seq; ++c );
if ( c == c_e )
{
c = data.begin();
for( ; c != c_e && (*c)->notfree(); ++c );
if ( c == c_e ) {
data.add(new CollectVectorInstance(seq));
c = data.end() - 1;
}
(*c)->reset(seq);
}
(*c)->append();
}
void enqueue(int seq, Lattice &lattice) {
queue.add(seq);
latqueue.add(lattice);
}
CollectVectorInstance* removeReady(void)
{
//it is equal to
//if(getReady()) removeFirstReady();
//But it should be not used in memory
//optimized version as the ready instance
//is delayed to be freed at the start of
//next round of output -Chao Mei
return NULL;
}
//only get the ready instance, not remove their info
//from timestep queue and lattice queue
CollectVectorInstance* getReady(void)
{
CollectVectorInstance *o = 0;
if ( queue.size() )
{
int seq = queue[0];
CollectVectorInstance **c = data.begin();
CollectVectorInstance **c_e = data.end();
for( ; c != c_e && (*c)->seq != seq; ++c );
if ( c != c_e && (*c)->ready() )
{
o = *c;
o->lattice = latqueue[0];
}
}
return o;
}
//the function is intended to be used after "getReady"
//to remove the info regarding the timestep and lattice.
//So, it removes the front ready one. -Chao Mei
int removeFirstReady(){
int seq = queue[0];
queue.del(0,1);
latqueue.del(0,1);
return seq;
}
ResizeArray<CollectVectorInstance*> data;
ResizeArray<int> queue;
ResizeArray<Lattice> latqueue;
}; //end of declaration for CollectionMaster::CollectVectorSequence
#else
class CollectVectorInstance
{
public:
CollectVectorInstance(void) : seq(-10) { ; }
CollectVectorInstance(int s) { reset(s); }
void free() { seq = -10; }
int notfree() { return ( seq != -10 ); }
void reset(int s) {
if ( s == -10 ) NAMD_bug("seq == free in CollectionMaster");
seq = s;
remaining = (PatchMap::Object())->numNodesWithPatches();
data.resize(0);
fdata.resize(0);
}
// true -> send it and delete it!
void append(CollectVectorMsg *msg, int max_index)
{
AtomID *a = msg->aid;
Vector *d = msg->data;
FloatVector *fd = msg->fdata;
int size = msg->aid_size;
if ( msg->data_size ) {
data.resize(max_index);
Vector *ptr = data.begin();
for( int i = 0; i < size; ++i ) { ptr[a[i]] = d[i]; }
}
if ( msg->fdata_size ) {
fdata.resize(max_index);
FloatVector *ptr = fdata.begin();
for( int i = 0; i < size; ++i ) { ptr[a[i]] = fd[i]; }
}
--remaining;
}
int ready(void) { return ( ! remaining ); }
int seq;
Lattice lattice;
ResizeArray<Vector> data;
ResizeArray<FloatVector> fdata;
private:
int remaining;
};
class CollectVectorSequence
{
public:
void submitData(CollectVectorMsg *msg, int max_index)
{
int seq = msg->seq;
CollectVectorInstance **c = data.begin();
CollectVectorInstance **c_e = data.end();
for( ; c != c_e && (*c)->seq != seq; ++c );
if ( c == c_e )
{
c = data.begin();
for( ; c != c_e && (*c)->notfree(); ++c );
if ( c == c_e ) {
data.add(new CollectVectorInstance(seq));
c = data.end() - 1;
}
(*c)->reset(seq);
}
(*c)->append(msg, max_index);
}
void enqueue(int seq, Lattice &lattice) {
queue.add(seq);
latqueue.add(lattice);
}
CollectVectorInstance* removeReady(void)
{
CollectVectorInstance *o = 0;
if ( queue.size() )
{
int seq = queue[0];
CollectVectorInstance **c = data.begin();
CollectVectorInstance **c_e = data.end();
for( ; c != c_e && (*c)->seq != seq; ++c );
if ( c != c_e && (*c)->ready() )
{
o = *c;
o->lattice = latqueue[0];
queue.del(0,1);
latqueue.del(0,1);
}
}
return o;
}
ResizeArray<CollectVectorInstance*> data;
ResizeArray<int> queue;
ResizeArray<Lattice> latqueue;
};
#endif
private:
CollectVectorSequence positions;
CollectVectorSequence velocities;
CollectVectorSequence forces;
int posTimings, velTimings, forceTimings;
FILE *dataStreamFile;
#ifdef MEM_OPT_VERSION
int wrapCoorDoneCnt;
ParOutput *parOut;
double posOutTime; //record the output time
double velOutTime; //record the output time
double forceOutTime; //record the output time
double posIOTime; //record the max time spent on real file IO for one output
double velIOTime; //record the max time spent on real file IO for one output
double forceIOTime; //record the max time spent on real file IO for one output
//for the sake of simultaneous writing to the same file
int posDoneCnt;
int velDoneCnt;
int forceDoneCnt;
void checkPosReady();
void checkVelReady();
void checkForceReady();
#endif
};
class DataStreamMsg : public CMessage_DataStreamMsg {
public:
ResizeArray<char> data;
static void* pack(DataStreamMsg* msg);
static DataStreamMsg* unpack(void *ptr);
};
//Use varsize message to be more SMP safe
class CollectVectorVarMsg : public CMessage_CollectVectorVarMsg
{
public:
enum DataStatus {VectorValid, FloatVectorValid, BothValid};
public:
int seq;
int size;
DataStatus status;
AtomID *aid;
Vector *data;
FloatVector *fdata;
};
#ifdef MEM_OPT_VERSION
class CollectMidVectorInstance{
public:
CollectMidVectorInstance(void) : seq(-10) {
CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
ParallelIOMgr *ioMgr = io.ckLocalBranch();
ioMgr->getMyAtomsRangeOnOutput(fromAtomID, toAtomID);
}
CollectMidVectorInstance(int s) {
CProxy_ParallelIOMgr io(CkpvAccess(BOCclass_group).ioMgr);
ParallelIOMgr *ioMgr = io.ckLocalBranch();
ioMgr->getMyAtomsRangeOnOutput(fromAtomID, toAtomID);
reset(s);
}
void free() { seq = -10; }
int notfree() { return ( seq != -10 ); }
void reset(int s) {
if ( s == -10 ) NAMD_bug("seq == free in CollectionMidMaster");
seq = s;
remaining = toAtomID-fromAtomID+1;
data.resize(0);
fdata.resize(0);
}
// if 1 is returned, indicates all the expected atoms for a
// timestep have been received
int append(int size, AtomID *a, Vector *d, FloatVector *fd){
if (d) {
if ( size ) data.resize(toAtomID-fromAtomID+1);
Vector *ptr = data.begin();
for(int i = 0; i < size; ++i) { ptr[a[i]-fromAtomID] = d[i]; }
}
if (fd) {
if ( size ) fdata.resize(toAtomID-fromAtomID+1);
FloatVector *ptr = fdata.begin();
for(int i = 0; i < size; ++i) { ptr[a[i]-fromAtomID] = fd[i]; }
}
remaining -= size;
return ready();
}
int ready(void) { return ( ! remaining ); }
int seq;
Lattice lattice;
ResizeArray<Vector> data;
ResizeArray<FloatVector> fdata;
//indicates the range of atoms this object is responsible for collecting
int fromAtomID;
int toAtomID;
private:
int remaining;
};//end of declaration for CollectMidVectorInstance
//An object of this class will be a member of Parallel IO Manager
//It is responsible to buffer the position/coordinates data that is
//going to be written to file system.
//In particular, the instance of this class will be on output procs.
//It will communicate via parallel IO manager with the CollectionMaster
//object on PE0 to be notified on when and what to write --Chao Mei
class CollectionMidMaster{
public:
CollectionMidMaster(ParallelIOMgr *pIO_) : pIO(pIO_) { parOut = new ParOutput(pIO_->myOutputRank); }
~CollectionMidMaster(void) { delete parOut; }
int receivePositions(CollectVectorVarMsg *msg) {return positions.submitData(msg);}
int receiveVelocities(CollectVectorVarMsg *msg) {return velocities.submitData(msg);}
int receiveForces(CollectVectorVarMsg *msg) {return forces.submitData(msg);}
void disposePositions(int seq);
void disposeVelocities(int seq);
void disposeForces(int seq);
CollectMidVectorInstance *getReadyPositions(int seq) { return positions.getReady(seq); }
//containing an array of CollectVectorInstance and their corresponding
//timestep value and lattice value
class CollectVectorSequence{
public:
int submitData(CollectVectorVarMsg *msg){
int seq = msg->seq;
CollectMidVectorInstance **c = data.begin();
CollectMidVectorInstance **c_e = data.end();
for( ; c != c_e && (*c)->seq != seq; ++c );
if ( c == c_e ){
c = data.begin();
for( ; c != c_e && (*c)->notfree(); ++c );
if ( c == c_e ) {
data.add(new CollectMidVectorInstance(seq));
c = data.end() - 1;
}
(*c)->reset(seq);
}
AtomID *i = msg->aid;
Vector *d = msg->data;
FloatVector *fd = msg->fdata;
if(msg->status==CollectVectorVarMsg::VectorValid) {
fd = NULL;
}else if(msg->status==CollectVectorVarMsg::FloatVectorValid){
d = NULL;
}
return (*c)->append(msg->size,i,d,fd);
}
CollectMidVectorInstance* getReady(int seq){
CollectMidVectorInstance **c = data.begin();
CollectMidVectorInstance **c_e = data.end();
for( ; c != c_e && (*c)->seq != seq; ++c );
CmiAssert(c != c_e);
return *c;
}
ResizeArray<CollectMidVectorInstance*> data;
};//end of declaration for CollectionMidMaster::CollectVectorSequence
private:
CollectVectorSequence positions;
CollectVectorSequence velocities;
CollectVectorSequence forces;
ParallelIOMgr *pIO;
ParOutput *parOut;
}; //end of declaration for CollectionMidMaster
#endif
#endif