-
Notifications
You must be signed in to change notification settings - Fork 0
/
htram_group.C
666 lines (588 loc) · 22.4 KB
/
htram_group.C
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "htram_group.h"
#include <thread>
#include <mutex>
//#define DEBUG 1
CkReductionMsg* msgStatsCollection(int nMsg, CkReductionMsg** rdmsgs) {
double *msg_stats;
msg_stats = (double*)rdmsgs[0]->getData();
// CkPrintf("\nInside reducer: %lf, %lf, %lf, %lf", msg_stats[0], msg_stats[1], msg_stats[2], msg_stats[3]);
for (int i = 1; i < nMsg; i++) {
CkAssert(rdmsgs[i]->getSize() == STATS_COUNT*sizeof(double));
if (rdmsgs[i]->getSize() != STATS_COUNT*sizeof(double)) {
CkPrintf("Error!!! Reduction not correct. Msg size is %d\n",
rdmsgs[i]->getSize());
CkAbort("Incorrect Reduction size in MetaBalancer\n");
}
double* m = (double *)rdmsgs[i]->getData();
msg_stats[TOTAL_LATENCY] += m[TOTAL_LATENCY];
msg_stats[MAX_LATENCY] = max(m[MAX_LATENCY], msg_stats[MAX_LATENCY]);
msg_stats[MIN_LATENCY] = min(m[MIN_LATENCY], msg_stats[MIN_LATENCY]);
msg_stats[TOTAL_MSGS] += m[TOTAL_MSGS];
}
return CkReductionMsg::buildNew(rdmsgs[0]->getSize(), NULL, rdmsgs[0]->getReducer(), rdmsgs[0]);
}
/*global*/ CkReduction::reducerType msgStatsCollectionType;
/*initnode*/ void registerMsgStatsCollection(void) {
msgStatsCollectionType = CkReduction::addReducer(msgStatsCollection, true, "msgStatsCollection");
}
void periodic_tflush(void *htram_obj, double time);
HTram::HTram(CkGroupID recv_ngid, CkGroupID src_ngid, int buffer_size, bool enable_buffer_flushing, double time_in_ms, bool ret_item, bool req, CkCallback start_cb) {
request = req;
// TODO: Implement variable buffer sizes and timed buffer flushing
flush_time = time_in_ms;
// client_gid = cgid;
enable_flush = enable_buffer_flushing;
msg_stats[MIN_LATENCY] = 100.0;
agg_msg_count = 0;
flush_msg_count = 0;
// if(thisIndex==0) CkPrintf("\nbuf_type = %d, type %d,%d,%d,%d", buf_type, use_src_grouping, use_src_agg, use_per_destpe_agg, use_per_destnode_agg);
/*
if(use_per_destnode_agg)
if(thisIndex==0) CkPrintf("\nDest-node side grouping/sorting enabled (1 buffer per src-pe, per dest-node)\n");
*/
ret_list = !ret_item;
agg = PNs;//PP; NNs;
myPE = CkMyPe();
msgBuffers = (new HTramMessage*[CkNumPes()]);
if(thisIndex == 0) {
if(agg == PNs) CkPrintf("\nAggregation type: PNs with buffer size %d", BUFSIZE);
else if(agg == NNs) CkPrintf("\nAggregation type: NNs with buffer size %d",BUFSIZE);
else if(agg == PP) CkPrintf("\nAggregation type: PP with buffer size %d", BUFSIZE);
else if(agg = PsN) CkPrintf("\nAggregation type: PsN with buffer size %d", BUFSIZE);
}
localMsgBuffer = new HTramMessage();
for(int i=0;i<CkNumPes();i++)
msgBuffers[i] = new HTramMessage();
localBuffers = new std::vector<itemT>[CkNumPes()];
local_buf = new HTramLocalMessage*[CkNumNodes()];
for(int i=0;i<CkNumNodes();i++)
{
local_buf[i] = new HTramLocalMessage();
local_idx[i] = 0;
}
nodeGrpProxy = CProxy_HTramRecv(recv_ngid);
srcNodeGrpProxy = CProxy_HTramNodeGrp(src_ngid);
srcNodeGrp = (HTramNodeGrp*)srcNodeGrpProxy.ckLocalBranch();
nodeGrp = (HTramRecv*)nodeGrpProxy.ckLocalBranch();
CkGroupID my_gid = ckGetGroupID();
// CkPrintf("\nmy_gid = %d", my_gid);
// if(thisIndex==0)
nodeGrp->setTramProxy(my_gid);
if(enable_flush)
periodic_tflush((void *) this, flush_time);
#ifdef IDLE_FLUSH
CkCallWhenIdle(CkIndex_HTram::idleFlush(), this);
#endif
contribute(start_cb);
}
bool HTram::idleFlush() {
// if(thisIndex==0) CkPrintf("\nCalling idleflush");
#ifdef IDLE_FLUSH
tflush(true);
#endif
return true;
}
void HTram::reset_stats(int btype, int buf_size, int agtype) {
std::fill_n(msg_stats, STATS_COUNT, 0.0);
msg_stats[MIN_LATENCY] = 100.0;
std::fill_n(nodeGrp->msg_stats, STATS_COUNT, 0.0);
nodeGrp->msg_stats[MIN_LATENCY] = 100.0;
agg = agtype;
int buf_count = CkNumNodes();
if(agg == PP) buf_count = CkNumPes();
for(int i=0;i<buf_count;i++)
msgBuffers[i] = new HTramMessage();
//if(thisIndex==0) CkPrintf("\nbuf_type = %d, size = %d, type = %d", buf_type, BUFSIZE, agg);
}
void HTram::avgLatency(CkCallback cb){
return_cb = cb;
msg_stats[TOTAL_LATENCY] /= (2*msg_stats[TOTAL_MSGS]);
// CkPrintf("\n%lf, %lf, %lf, %lf", msg_stats[0], msg_stats[1], msg_stats[2], msg_stats[3]);
// double avg = total_latency/total_msg_count;
// CkCallback cb(CkReductionTarget(MetaBalancer, ReceiveMinStats),
// thisProxy[0]);
contribute(STATS_COUNT*sizeof(double), msg_stats, msgStatsCollectionType, cb);
// CkPrintf("\navg = %lf", avg);
// contribute(sizeof(double), &avg, CkReduction::sum_double, CkCallback(CkReductionTarget(HTram, printAvgLatency), thisProxy));
}
void HTramRecv::avgLatency(CkCallback cb){
return_cb = cb;
msg_stats[TOTAL_LATENCY] /= (2*msg_stats[TOTAL_MSGS]);
// CkPrintf("\n%lf, %lf, %lf, %lf", msg_stats[0], msg_stats[1], msg_stats[2], msg_stats[3]);
// double avg = total_latency/total_msg_count;
// contribute(sizeof(double), &avg, CkReduction::sum_double, CkCallback(CkReductionTarget(HTramRecv, printAvgLatency), thisProxy));
contribute(STATS_COUNT*sizeof(double), msg_stats, msgStatsCollectionType, cb);
}
HTram::HTram(CkGroupID cgid, CkCallback ecb){
client_gid = cgid;
// cb = delivercb;
endCb = ecb;
myPE = CkMyPe();
localMsgBuffer = new HTramMessage();
#ifndef NODE_SRC_BUFFER
msgBuffers = new HTramMessage*[CkNumNodes()];
for(int i=0;i<CkNumNodes();i++)
msgBuffers[i] = new HTramMessage();
#endif
}
void HTram::set_func_ptr(void (*func)(void*, datatype), void* obPtr) {
cb = func;
objPtr = obPtr;
}
void HTram::set_func_ptr_retarr(void (*func)(void*, datatype*, int), void* obPtr) {
cb_retarr = func;
objPtr = obPtr;
if(CkMyRank()==0)
nodeGrp->set_func_ptr_retarr(func, obPtr);
}
void HTramRecv::set_func_ptr_retarr(void (*func)(void*, datatype*, int), void* obPtr) {
cb_retarr = func;
objPtr = obPtr;
}
HTram::HTram(CkMigrateMessage* msg) {}
//one per node, message, fixed
//Client inserts
void HTram::insertToProcess(datatype value, int destNode){
HTramMessage *destMsg = msgBuffers[destNode];
destMsg->buffer[destMsg->next].payload = value;
destMsg->next++;
if(destMsg->next == BUFSIZE) {
nodeGrpProxy[destNode].receiveOnProc(destMsg);
msgBuffers[destNode] = new HTramMessage();
}
}
void HTram::insertValue(datatype value, int dest_pe) {
// CkPrintf("\nInserting on PE-%d", dest_pe);
int destNode = dest_pe/CkNodeSize(0); //find safer way to find dest node,
// node size is not always same
if(agg == NNs) {
int increment = 1;
int idx = -1;
int idx_dnode = local_idx[destNode];
if(idx_dnode<=LOCAL_BUFSIZE-1) {
local_buf[destNode]->buffer[idx_dnode].payload = value;
local_buf[destNode]->buffer[idx_dnode].destPe = dest_pe;
local_idx[destNode]++;
}
bool local_buf_full = false;
if(local_idx[destNode] == LOCAL_BUFSIZE)
local_buf_full = true;
increment = LOCAL_BUFSIZE;
int done_idx = -1;
if(local_buf_full) {
// CkPrintf("\n[PE-%d]Copying for dest node %d, size = %d", CkMyPe(), destNode, increment);
copyToNodeBuf(destNode, increment);
}
}
else {
HTramMessage *destMsg = msgBuffers[destNode];
if(agg == PP)
destMsg = msgBuffers[dest_pe];
if(agg == PsN) {
itemT itm = {dest_pe, value};
localBuffers[dest_pe].push_back(itm);
} else if (agg == PP) {//change msg type to not include destPE
destMsg->buffer[destMsg->next].payload = value;
} else {
destMsg->buffer[destMsg->next].payload = value;
destMsg->buffer[destMsg->next].destPe = dest_pe;
}
#if 0
if(*(destMsg->next) == 0 || *(destMsg->next) == BUFSIZE-1) {
if(*(destMsg->next) == 0)
destMsg->getTimer()[0] = CkWallTimer();
else
destMsg->getTimer()[1] = CkWallTimer();
}
#endif
destMsg->next++;
if(destMsg->next == BUFSIZE) {
agg_msg_count++;
if(agg == PsN) {
int sz = 0;
for(int i=0;i<CkNodeSize(0);i++) {
std::vector<itemT> localMsg = localBuffers[destNode*CkNodeSize(0)+i];
std::copy(localMsg.begin(), localMsg.end(), &(destMsg->buffer[sz]));
sz += localMsg.size();
// destMsg->getIndex()[i] = sz;
localBuffers[destNode*CkNodeSize(0)+i].clear();
}
}
// ((envelope *)UsrToEnv(destMsg))->setUsersize(0);//destMsg->next-20)*4+32);
int dest_idx = dest_pe;
if(agg == PP) {
// CkPrintf("\nmsg size = %d", *destMsg->next);
thisProxy[dest_pe].receiveOnPE(destMsg);
} else {
dest_idx = destNode;
if(agg == PsN) {
nodeGrpProxy[destNode].receive_no_sort(destMsg);
// nodeGrpProxy[destNode].receive_no_sort(destMsg);
} else {
// if(request) CkPrintf("\nSending msg from requesting htram");
// else CkPrintf("\nSending msg from responding htram");
nodeGrpProxy[destNode].receive(destMsg);
}
}
msgBuffers[dest_idx] = new HTramMessage();
}
}
}
void HTram::registercb() {
CcdCallFnAfter(periodic_tflush, (void *) this, flush_time);
}
std::mutex node_mutex;
void HTram::copyToNodeBuf(int destnode, int increment) {
// Get atomic index
int idx = srcNodeGrp->get_idx[destnode].fetch_add(increment, std::memory_order_relaxed);
while(idx >= BUFSIZE) {
idx = srcNodeGrp->get_idx[destnode].fetch_add(increment, std::memory_order_relaxed);
}
#if 0
if(idx==0 || idx == BUFSIZE-increment)
srcNodeGrp->msgBuffers[destnode]->getTimer()[idx/(BUFSIZE-increment)] = CkWallTimer();
#endif
// Copy data into node buffer from PE-local buffer
// node_mutex.lock();
int i;
for(i=0;i<increment;i++) {
srcNodeGrp->msgBuffers[destnode]->buffer[idx+i].payload = local_buf[destnode]->buffer[i].payload;
srcNodeGrp->msgBuffers[destnode]->buffer[idx+i].destPe = local_buf[destnode]->buffer[i].destPe;
}
int done_count = srcNodeGrp->done_count[destnode].fetch_add(increment, std::memory_order_relaxed);
// node_mutex.unlock();
std::atomic_store_explicit(&(srcNodeGrp->mailbox_receiver[CkMyRank()+(destnode*CkNodeSize(0))]), (srcNodeGrp->mailbox_receiver[CkMyRank()+(destnode*CkNodeSize(0))])+increment, std::memory_order_release);
if(done_count+increment == BUFSIZE) {
#if 1
int count = 0;
while(count < BUFSIZE) {
count = 0;
for (int i = 0; i < CkNodeSize(0); ++i) {
// CkPrintf("\nAccessing idx %d", i+(destnode*CkNodeSize(0)));
count += std::atomic_load_explicit(&(srcNodeGrp->mailbox_receiver[i+(destnode*CkNodeSize(0))]), std::memory_order_relaxed);
// CkPrintf("\nFor buffer for destnode %d, the count so far is %d", destnode, count);
// synchronize with just one writer
std::atomic_thread_fence(std::memory_order_acquire);
}
}
for (int i = 0; i < CkNodeSize(0); ++i)
std::atomic_store_explicit(&(srcNodeGrp->mailbox_receiver[i+(destnode*CkNodeSize(0))]), 0, std::memory_order_relaxed);
#endif
agg_msg_count++;
srcNodeGrp->msgBuffers[destnode]->next = BUFSIZE;
nodeGrpProxy[destnode].receive(srcNodeGrp->msgBuffers[destnode]);
srcNodeGrp->msgBuffers[destnode] = new HTramMessage();
srcNodeGrp->done_count[destnode] = 0;
srcNodeGrp->get_idx[destnode] = 0;
}
local_idx[destnode] = 0;
}
void HTram::enableIdleFlush() {
#ifdef IDLE_FLUSH
CkCallWhenIdle(CkIndex_HTram::idleFlush(), this);
#endif
}
void HTram::tflush(bool idleflush) {
// CkPrintf("\nCalling flush on PE-%d", thisIndex); fflush(stdout);
if(agg == NNs) {
#if 1
int flush_count = srcNodeGrp->flush_count.fetch_add(1, std::memory_order_seq_cst);
//Send your local buffer
for(int i=0;i<CkNumNodes();i++) {
local_buf[i]->next = local_idx[i];
nodeGrpProxy[i].receive_small(local_buf[i]);
local_buf[i] = new HTramLocalMessage();
local_idx[i] = 0;
}
//If you're last rank on node to flush, then flush your buffer and by setting idx to a high count, node level buffer as well
// if(flush_count+1==CkNodeSize(0))
{
for(int i=0;i<CkNumNodes();i++) {
#if 1
if(srcNodeGrp->done_count[i]) {
flush_msg_count++;
#if 1
int idx = srcNodeGrp->get_idx[i].fetch_add(BUFSIZE, std::memory_order_relaxed);
int done_count = srcNodeGrp->done_count[i].fetch_add(0, std::memory_order_relaxed);
if(idx >= BUFSIZE) continue;
while(idx!=done_count) { done_count = srcNodeGrp->done_count[i].fetch_add(0, std::memory_order_relaxed);}
#if 1
if(done_count == idx) {
int count = 0;
while(count < done_count) {
count = 0;
for (int j = 0; j < CkNodeSize(0); ++j) {
count += std::atomic_load_explicit(&(srcNodeGrp->mailbox_receiver[j+(i*CkNodeSize(0))]), std::memory_order_relaxed);
// synchronize with just one writer
std::atomic_thread_fence(std::memory_order_acquire);
}
}
for (int j = 0; j < CkNodeSize(0); ++j)
std::atomic_store_explicit(&(srcNodeGrp->mailbox_receiver[j+(i*CkNodeSize(0))]), 0, std::memory_order_relaxed);
}
#endif
#endif
// CkPrintf("\nCalling TFLUSH---\n");
srcNodeGrp->msgBuffers[i]->next = srcNodeGrp->done_count[i];
((envelope *)UsrToEnv(srcNodeGrp->msgBuffers[i]))->setUsersize(sizeof(int)+sizeof(envelope)+sizeof(itemT)*srcNodeGrp->msgBuffers[i]->next);
/*
CkPrintf("\n[PE-%d]TF-Sending out data with size = %d", thisIndex, srcNodeGrp->msgBuffers[i]->next);
for(int j=0;j<srcNodeGrp->msgBuffers[i]->next;j++)
CkPrintf("\nTFvalue=%d, pe=%d", srcNodeGrp->msgBuffers[i]->buffer[j].payload, srcNodeGrp->msgBuffers[i]->buffer[j].destPe);
*/
// *(srcNodeGrp->msgBuffers[i]->getDoTimer()) = 0;
nodeGrpProxy[i].receive(srcNodeGrp->msgBuffers[i]);
// srcNodeGrp->msgBuffers[i] = new HTramMessageSmall();//new HTramMessage();//localMsgBuffer;
srcNodeGrp->msgBuffers[i] = new HTramMessage();
srcNodeGrp->done_count[i] = 0;
srcNodeGrp->flush_count = 0;
srcNodeGrp->get_idx[i] = 0;
}
#endif
// localMsgBuffer = new HTramMessage();
}
}
#endif
}
else {
int buf_count = CkNumNodes();
if(agg == PP) buf_count = CkNumPes();
for(int i=0;i<buf_count;i++) {
// if(msgBuffers[i]->next)
#ifdef IDLE_FLUSH
if((!idleflush && msgBuffers[i]->next) || (idleflush && msgBuffers[i]->next > BUFSIZE*PARTIAL_FLUSH))
#else
if(!idleflush && msgBuffers[i]->next)
#endif
{
flush_msg_count++;
// if(idleflush) CkPrintf("\n[PE-%d] flushing buf[%d] at %d", CkMyPe(), i, msgBuffers[i]->next);
// else CkPrintf("\nReg[PE-%d] flushing buf[%d] at %d", CkMyPe(), i, msgBuffers[i]->next);
HTramMessage *destMsg = msgBuffers[i];
// *destMsg->getDoTimer() = 0;
if(agg == PsN) {
int destNode = i;
int sz = 0;
for(int k=0;k<CkNodeSize(0);k++) {
std::vector<itemT> localMsg = localBuffers[destNode*CkNodeSize(0)+k];
std::copy(localMsg.begin(), localMsg.end(), &(destMsg->buffer[sz]));
sz += localMsg.size();
// destMsg->getIndex()[k] = sz;
localBuffers[destNode*CkNodeSize(0)+k].clear();
}
nodeGrpProxy[i].receive_no_sort(destMsg);
}
else if(agg == PNs)
{
((envelope *)UsrToEnv(destMsg))->setUsersize(sizeof(int)+sizeof(envelope)+sizeof(itemT)*(destMsg->next));
// nodeGrpProxy[i].receive(destMsg); //todo - Resize only upto next
nodeGrpProxy[i].receive(destMsg);
} else if(agg == PP) {
((envelope *)UsrToEnv(destMsg))->setUsersize(sizeof(int)+sizeof(envelope)+sizeof(itemT)*destMsg->next);
// CkPrintf("\nmsg size = %d", *destMsg->next);
thisProxy[i].receiveOnPE(destMsg);
}
//msgBuffers[i] = new HTramMessageSmall();//new HTramMessage();
msgBuffers[i] = new HTramMessage();
}
}
}
}
HTramNodeGrp::HTramNodeGrp() {
msgBuffers = new HTramMessage*[CkNumNodes()];
for(int i=0;i<CkNumNodes();i++) {
msgBuffers[i] = new HTramMessage();
get_idx[i] = 0;
done_count[i] = 0;
}
for(int i=0;i<1024;i++)
mailbox_receiver[i] = 0;
}
HTramNodeGrp::HTramNodeGrp(CkMigrateMessage* msg) {}
HTramRecv::HTramRecv(){
msg_stats[MIN_LATENCY] = 100.0;
}
#if 0
bool comparePayload(itemT a, itemT b)
{
return (a.payload > b.payload);
}
bool lower(itemT a, double value) {
return a.payload < value;
}
bool upper(itemT a, double value) {
return a.payload > value;
}
#endif
HTramRecv::HTramRecv(CkMigrateMessage* msg) {}
//#ifdef SRC_GROUPING
void HTramRecv::receive_no_sort(HTramMessage* agg_message) {
#if 0
if(agg_message->do_timer) {
double time_stamp = CkWallTimer();
double latency[2];
for(int i=0;i<2;i++) {
latency[i] = time_stamp - agg_message->timer[i];
msg_stats[TOTAL_LATENCY] += latency[i];
}
double max = std::max(latency[0], latency[1]);
if(msg_stats[MAX_LATENCY] < max)
msg_stats[MAX_LATENCY] = max;
double min = std::min(latency[0], latency[1]);
if(msg_stats[MIN_LATENCY] > min)
msg_stats[MIN_LATENCY] = min;
msg_stats[TOTAL_MSGS] += 1.0;
}
#endif
for(int i=CkNodeFirst(CkMyNode()); i < CkNodeFirst(CkMyNode())+CkNodeSize(0);i++) {
HTramMessage* tmpMsg = (HTramMessage*)CkReferenceMsg(agg_message);
_SET_USED(UsrToEnv(tmpMsg), 0);
tram_proxy[i].receivePerPE(tmpMsg);
}
CkFreeMsg(agg_message);
}
void HTram::receivePerPE(HTramMessage* msg) {
int llimit = 0;
int rank = CkMyRank();
if(rank > 0) llimit = 0;//msg->getIndex()[rank-1];
int ulimit = 0;//msg->getIndex()[rank];
for(int i=llimit; i<ulimit;i++){
cb(objPtr, msg->buffer[i].payload);
}
CkFreeMsg(msg);
}
//#elif defined PER_DESTPE_BUFFER
void HTram::receiveOnPE(HTramMessage* msg) {
// CkPrintf("\ntotal_latency=%lfs",total_latency);
#if 0
if(msg->do_timer) {
double time_stamp = CkWallTimer();
double latency[2];
for(int i=0;i<2;i++) {
latency[i] = time_stamp - msg->timer[i];
msg_stats[TOTAL_LATENCY] += latency[i];
}
double max = std::max(latency[0], latency[1]);
if(msg_stats[MAX_LATENCY] < max)
msg_stats[MAX_LATENCY] = max;
double min = std::min(latency[0], latency[1]);
if(msg_stats[MIN_LATENCY] > min)
msg_stats[MIN_LATENCY] = min;
msg_stats[TOTAL_MSGS] += 1;
}
#endif
// CkPrintf("\nrcv-msg size = %d", msg->next);
for(int i=0;i<msg->next;i++)
cb(objPtr, msg->buffer[i].payload);
delete msg;
}
void HTramRecv::receiveOnProc(HTramMessage* agg_message) {
datatype* buf = new datatype[agg_message->next];
for(int i=0;i<agg_message->next;i++)
buf[i] = agg_message->buffer[i].payload;
cb_retarr(objPtr, buf, agg_message->next);
delete agg_message;
}
void HTramRecv::receive(HTramMessage* agg_message) {
#if 0
// CkPrintf("\nReceived msg of size %d on node%d", agg_message->next, thisIndex);
if(agg_message->do_timer) {
double time_stamp = CkWallTimer();
double latency[2];
for(int i=0;i<2;i++) {
latency[i] = time_stamp - agg_message->timer[i];
// if(latency[i] > 0.1) CkPrintf("\nlatency = %lfs (%lf - %lf)", latency[i], time_stamp, agg_message->timer[i]);
msg_stats[TOTAL_LATENCY] += latency[i];
}
double max = std::max(latency[0], latency[1]);
if(msg_stats[MAX_LATENCY] < max)
msg_stats[MAX_LATENCY] = max;
double min = std::min(latency[0], latency[1]);
if(msg_stats[MIN_LATENCY] > min)
msg_stats[MIN_LATENCY] = min;
msg_stats[TOTAL_MSGS] += 1;
}
#endif
//broadcast to each PE and decr refcount
//nodegroup //reference from group
int rank0PE = CkNodeFirst(thisIndex);
HTramNodeMessage* sorted_agg_message = new HTramNodeMessage();
int sizes[PPN_COUNT] = {0};
for(int i=0;i<agg_message->next;i++) {
int rank = agg_message->buffer[i].destPe - rank0PE;
sizes[rank]++;
}
sorted_agg_message->offset[0] = 0;
for(int i=1;i<CkNodeSize(0);i++)
sorted_agg_message->offset[i] = sorted_agg_message->offset[i-1]+sizes[i-1];
for(int i=0;i<agg_message->next;i++) {
int rank = agg_message->buffer[i].destPe - rank0PE;
sorted_agg_message->buffer[sorted_agg_message->offset[rank]++] = agg_message->buffer[i].payload;
}
delete agg_message;
sorted_agg_message->offset[0] = sizes[0];
for(int i=1;i<CkNodeSize(0);i++)
sorted_agg_message->offset[i] = sorted_agg_message->offset[i-1] + sizes[i];
for(int i=CkNodeFirst(CkMyNode()); i < CkNodeFirst(CkMyNode())+CkNodeSize(0);i++) {
HTramNodeMessage* tmpMsg = (HTramNodeMessage*)CkReferenceMsg(sorted_agg_message);
_SET_USED(UsrToEnv(tmpMsg), 0);
tram_proxy[i].receivePerPE(tmpMsg);
}
CkFreeMsg(sorted_agg_message);
}
void HTramRecv::receive_small(HTramLocalMessage* agg_message) {
//broadcast to each PE and decr refcount
//nodegroup //reference from group
int rank0PE = CkNodeFirst(thisIndex);
HTramNodeMessage* sorted_agg_message = new HTramNodeMessage();
int sizes[PPN_COUNT] = {0};
for(int i=0;i<agg_message->next;i++) {
int rank = agg_message->buffer[i].destPe - rank0PE;
// CkPrintf("\nrank=%d, i=%d, next=%d", rank, i, agg_message->next);
sizes[rank]++;
}
sorted_agg_message->offset[0] = 0;
for(int i=1;i<CkNodeSize(0);i++)
sorted_agg_message->offset[i] = sorted_agg_message->offset[i-1]+sizes[i-1];
for(int i=0;i<agg_message->next;i++) {
int rank = agg_message->buffer[i].destPe - rank0PE;
sorted_agg_message->buffer[sorted_agg_message->offset[rank]++] = agg_message->buffer[i].payload;
}
delete agg_message;
sorted_agg_message->offset[0] = sizes[0];
for(int i=1;i<CkNodeSize(0);i++)
sorted_agg_message->offset[i] = sorted_agg_message->offset[i-1] + sizes[i];
for(int i=CkNodeFirst(CkMyNode()); i < CkNodeFirst(CkMyNode())+CkNodeSize(0);i++) {
HTramNodeMessage* tmpMsg = (HTramNodeMessage*)CkReferenceMsg(sorted_agg_message);
_SET_USED(UsrToEnv(tmpMsg), 0);
tram_proxy[i].receivePerPE(tmpMsg);
}
CkFreeMsg(sorted_agg_message);
}
void HTramRecv::setTramProxy(CkGroupID tram_gid) {
tram_proxy = CProxy_HTram(tram_gid);
}
void HTram::receivePerPE(HTramNodeMessage* msg) {
int llimit = 0;
int rank = CkMyRank();
if(rank > 0) llimit = msg->offset[rank-1];
int ulimit = msg->offset[rank];
if(!ret_list) {
for(int i=llimit; i<ulimit;i++)
cb(objPtr, msg->buffer[i]);
} else
cb_retarr(objPtr, &msg->buffer[llimit], ulimit-llimit);
CkFreeMsg(msg);
}
//#endif
void HTram::stop_periodic_flush() {
enable_flush = false;
}
void periodic_tflush(void *htram_obj, double time) {
// CkPrintf("\nIn callback_fn on PE#%d at time %lf",CkMyPe(), CkWallTimer());
HTram *proper_obj = (HTram *)htram_obj;
proper_obj->tflush();
if(proper_obj->enable_flush)
proper_obj->registercb();
}
#include "htram_group.def.h"