-
Notifications
You must be signed in to change notification settings - Fork 2
/
DLS.c
executable file
·1053 lines (900 loc) · 35.6 KB
/
DLS.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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************************
* Copyright (c) 2007 *
* Ricolindo L. Carino and Ioana Banicescu *
* Mississippi State University, USA *
* All rights reserved. *
* *
* Modified - Oct. 2018 *
* Ali Mohammed <ali.mohammed@unibas.ch> *
* University of Basel, Switzerland *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the license (GNU LGPL) which comes with this package. *
*********************************************************************************/
#include <stdio.h>
#include <math.h>
#include "mpi.h"
#include "DLS.h"
int DLS_MethodCount = 15;
char *DLS_ShortName [] = {
"STATIC", "SS", "FSC", "mFSC", "GSS", "TSS","FAC", "WF", "AWF",
"AWF-B", "AWF-C", "AWF-D", "AWF-E", "AF", "SimAS"
};
char *DLS_LongName [] = {
"STATIC SCHEDULING",
"SELF-SCHEDULING",
"FIXED SIZE-SCHEDULING", "MODIFIED FSC", "GUIDED SELF-SCHEDULING",
"Trapezoid Self-Scheduling","FACTORING", "WEIGHTED FACTORING", "ADAPTIVE WEIGHTED FACTORING",
"BATCH AWF", "CHUNK AWF",
"BATCH AWF (chunk times)" ,"CHUNK AWF (chunk times)", "ADAPTIVE FACTORING", "Simulation-assisted"
};
char * get_DLS_ShortName(int DLS)
{
return DLS_ShortName[DLS];
}
void Partition (MPI_Comm world, int coordinator,
MPI_Comm *jcomm, MPI_Comm *icomm)
{
char procName[maxProcs][MPI_MAX_PROCESSOR_NAME];
MPI_Status tStatus;
int grpSize[maxProcs+1], grpIdx[maxProcs+1];
int foremanOf[maxProcs], localRank[maxProcs];
int ierr, idx, i, j, mrg1, mrg2, minGrpRank, nGrps;
int gRank, gSize;
MPI_Comm_rank (world, &gRank);
MPI_Comm_size (world, &gSize);
MPI_Get_processor_name (procName[gRank], &i);
if (gRank==coordinator) {
/* printf("%d - %s is coordinator\n", gRank, procName[coordinator]); */
for (i=1;i<gSize;i++) {
MPI_Probe(MPI_ANY_SOURCE, HNM_TAG, world, &tStatus);
MPI_Get_count (&tStatus, MPI_CHAR, &mrg1);
mrg2 = tStatus.MPI_SOURCE;
MPI_Recv (procName[mrg2], mrg1, MPI_CHAR,
mrg2, HNM_TAG, world, &tStatus);
/* printf("%d - %s\n", mrg2, procName[mrg2]); */
}
for (i=0;i<gSize;i++) {
foremanOf[i] = -1;
grpSize[i] = 0;
}
nGrps = 0;
for (i=0;i<gSize;i++) {
if (foremanOf[i]==-1) {
nGrps = nGrps+1;
foremanOf[i] = nGrps;
grpSize[nGrps] = 1;
/* printf("i=%d nGrps=%d size=%d name=%s\n",
i, nGrps, grpSize[nGrps], procName[i]); */
/* idx = strchr(procName[i],"-")+3; */
idx = strstr(procName[i],".");
for (j=i+1;j<gSize;j++) {
if (0 == strncmp(procName[j], procName[i], idx)) {
/* impose max group size */
if (grpSize[nGrps]==maxGrpSize) {
nGrps = nGrps+1;
grpSize[nGrps] = 1;
}
else grpSize[nGrps] = 1+grpSize[nGrps];
foremanOf[j] = nGrps;
/* printf("i=%d nGrps=%d size=%d name=%s\n",
j, nGrps, grpSize[nGrps], procName[j]); */
}
}
}
}
/* impose min group size without violating max group size */
while (1) {
/* sort groups according to size */
for (i=1;i<maxProcs;i++) grpIdx[i] = i;
for (i=1;i<nGrps;i++) {
for (j=i+1;j<=nGrps;j++)
if (grpSize[grpIdx[i]]<grpSize[grpIdx[j]]) {
ierr = grpIdx[i];
grpIdx[i] = grpIdx[j];
grpIdx[j] = ierr;
}
}
/*
printf("%d groupings\n",nGrps);
for (i=1;i<=nGrps;i++) {
printf("Group %d has size %d\n", grpIdx[i], grpSize[grpIdx[i]]);
for (j=0;j<gSize;j++)
if (foremanOf[j]==grpIdx[i])
printf("%d - %s\n", j, procName[j]);
}
*/
/* find two smallest group */
mrg1 = grpIdx[nGrps];
mrg2 = grpIdx[nGrps-1];
if (grpSize[mrg1]>=minGrpSize) break;
if ((grpSize[mrg1]+grpSize[mrg2])>maxGrpSize) break;
/*
printf("Merge grp1: (%d,%d) with grp2: (%d,%d)\n",
mrg1, grpSize[mrg1], mrg2, grpSize[mrg2]); */
/* merge groups */
for (j=0;j<gSize;j++)
if (foremanOf[j]==mrg1) foremanOf[j] = mrg2;
grpSize[mrg2] = grpSize[mrg1]+grpSize[mrg2];
/* renumber groups */
for (i=mrg1;i<nGrps;i++) {
for (j=0;j<gSize;j++)
if (foremanOf[j]==(i+1)) foremanOf[j]=i;
grpSize[i] = grpSize[i+1];
}
nGrps = nGrps-1;
}
/* assign lowest ranks in groups as foremen */
foremanOf[gRank] = 0;
for (i=1;i<=nGrps;i++) {
minGrpRank = gSize;
idx = grpIdx[i];
ierr = 0;
for (j=0;j<gSize;j++) {
if (foremanOf[j]==idx) {
ierr = ierr+1;
if (j<minGrpRank) minGrpRank = j;
}
}
/* printf("Group %d, foreman=%d, size=%d\n", idx, minGrpRank, ierr); */
for (j=0;j<gSize;j++) {
if (foremanOf[j]==idx) foremanOf[j] = gSize+minGrpRank;
}
}
for (j=0;j<gSize;j++)
foremanOf[j] = foremanOf[j]-gSize;
foremanOf[gRank] = 0;
for (i=0;i<gSize;i++) { /* send colors */
if (i!=gRank) MPI_Send (foremanOf, gSize, MPI_INT, i, CLR_TAG, world);
}
}
else { /* send proc name to coordinator, wait for group color */
idx = strlen(procName[gRank]);
MPI_Sendrecv (procName[gRank], idx, MPI_CHAR, coordinator, HNM_TAG,
foremanOf, gSize, MPI_INT, coordinator, CLR_TAG, world, &tStatus);
}
/* map global ranks to local ranks */
for (i=0;i<gSize;i++) localRank[i] = -1;
for (i=0;i<gSize;i++) {
idx = foremanOf[i]; /* increment no. of workers of foremanOf idx */
localRank[idx] = localRank[idx]+1; /* temporary counter; reset in next loop */
localRank[i] = localRank[idx]; /* this worker's rank */
}
/* reset local rank of a foremanOf to 0 */
for (i=0;i<gSize;i++)
if (foremanOf[i]==i) localRank[i]=0;
/* split processors into work groups */
if (gRank==coordinator)
idx = MPI_UNDEFINED;
else
idx = foremanOf[gRank];
MPI_Comm_split(world, idx, gRank, icomm);
/* make control group for the foremen and coordinator */
if ((gRank==coordinator) || (foremanOf[gRank]==gRank))
idx = 0;
else
idx = MPI_UNDEFINED;
MPI_Comm_split(world, idx, gRank, jcomm);
}
void GetChunkSize ( infoDLS *info, int rank, int *chunkSize )
{
int i, tChunk, rem;
double bigD, bigT, awap, trw, weight, K;
rem = info->N-info->itersScheduled;
switch ( info->method ) {
case STATIC:
tChunk = ceil((double) info->N/ (double) info->commSize);
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
case SS:
tChunk = 1;
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
case FSC:
tChunk = min( info->chunkFSC, rem);
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
case mFSC:
tChunk = min( info->chunkMFSC, rem);
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
case GSS:
tChunk = max( (rem+info->commSize-1)/info->commSize, info->minChunkSize );
tChunk = min ( rem, tChunk );
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
case TSS:
tChunk = info->TSSchunk;
tChunk = min(rem, tChunk);
tChunk = max( info->minChunkSize, tChunk);
info->TSSchunk = tChunk - info->TSSdelta;
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
case FAC:
if (info->batchRem == 0) {
tChunk = max ( info->minChunkSize, (rem+2*info->commSize-1)/(2*info->commSize) );
info->batchSize = info->commSize*tChunk;
info->batchRem = min (info->batchSize, rem);
}
/* else use current batchSize */
tChunk = max( info->minChunkSize, info->batchSize/info->commSize );
tChunk = min ( rem, tChunk );
break;
case WF:
case AWF:
if (info->batchRem == 0) {
tChunk = max ( info->minChunkSize, (rem+2*info->commSize-1)/(2*info->commSize) );
info->batchSize = info->commSize*tChunk;
info->batchRem = min (info->batchSize, rem);
}
/* else use current batchSize */
tChunk = max( info->minChunkSize, info->batchSize/info->commSize*info->weights[rank]);
tChunk = min ( rem, tChunk );
break;
case AWF_B:
case AWF_D:
if (info->stats[3*rank] < 0.0) {
tChunk = info->minChunkSize;
info->batchSize = min(rem, tChunk);
info->batchRem = info->batchSize;
}
else { /* all ranks have wap */
awap = 0.0; /* average weighted performance */
for (i=info->firstRank;i<=info->lastRank;i++)
awap = awap + info->stats[3*i];
awap = awap/info->commSize;
trw = 0.0; /* total ref weight (refwt(i) = awap/info->stats[3*i] */
for (i=info->firstRank;i<=info->lastRank;i++)
trw = trw + awap/info->stats[3*i];
/* normalized weight for rank */
weight = ((awap/info->stats[3*rank])*info->commSize)/trw;
if (info->batchRem == 0) {
tChunk = max( info->minChunkSize, (rem+2*info->commSize-1)/(2*info->commSize) );
info->batchSize = info->commSize*tChunk;
info->batchRem = min (info->batchSize, rem);
}
/* else use current batchSize */
tChunk = weight*(info->batchSize/info->commSize) + 0.55;
tChunk = max( info->minChunkSize, tChunk);
tChunk = min ( rem, tChunk );
}
break;
case AWF_C:
case AWF_E:
if (info->stats[3*rank] < 0.0)
tChunk = info->minChunkSize;
else { /* all ranks have wap */
awap = 0.0; /* average weighted performance */
for (i=info->firstRank;i<=info->lastRank;i++)
awap = awap + info->stats[3*i];
awap = awap/info->commSize;
trw = 0.0; /* total ref weight (refwt(i) = awap/info->stats[3*i) */
for (i=info->firstRank;i<=info->lastRank;i++)
trw = trw + awap/info->stats[3*i];
/* normalized weight for rank */
weight = ((awap/info->stats[3*rank])*info->commSize)/trw;
tChunk = weight*((rem+2*info->commSize-1)/(2*info->commSize)) + 0.55;
}
tChunk = max( info->minChunkSize, tChunk);
info->batchSize = tChunk;
info->batchRem = min(rem, tChunk);
break;
case AF:
if (info->stats[3*rank] < 0.0)
tChunk = info->minChunkSize;
else {
bigD = 0.0;
bigT = 0.0;
for (i=info->firstRank;i<=info->lastRank;i++) {
bigD = bigD + info->stats[3*i+1]/info->stats[3*i];
bigT = bigT + 1.0/info->stats[3*i];
}
bigT = 1.0/bigT;
/* compute chunk size for rank */
tChunk = 0.55 + (0.5*(bigD + 2.0*bigT*rem -
sqrt(bigD*(bigD + 4.0*bigT*rem)))/info->stats[3*rank]);
tChunk = min( info->maxChunkSize, tChunk);
}
tChunk = max( info->minChunkSize, tChunk);
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
break;
default:
printf("Unsupported DLS technique, fall back to STATIC\n");
tChunk = (info->N+info->commSize-1)/info->commSize;
i = info->N % info->commSize;
if ((i>0) && (rank>=i) ) tChunk=tChunk-1;
tChunk = min( tChunk, rem);
info->batchSize = tChunk;
info->batchRem = min( info->batchSize, rem);
}
//printf("Rank: %d, Method: %d, chunk size is %d \n", rank, info->method, tChunk);
*chunkSize = min(info->batchRem, tChunk);
/* adjust remaining in batch */
info->batchRem = info->batchRem - *chunkSize;
if ( (info->batchRem > 0) && (info->batchRem <= info->minChunkSize) ) {
*chunkSize = *chunkSize + info->batchRem;
info->batchRem = 0;
}
}
void SendChunk ( infoDLS *info, int worker )
{
int chunkSize, chunkInfo[2];
GetChunkSize (info, worker, &chunkSize);
chunkInfo[0] = info->chunkStart;
chunkInfo[1] = chunkSize;
//printf("send to worker %d \n", worker);
//
if(worker == info->foreman)
{
if (info->wSize == 0) // no pending chunk
{
info->t0 = MPI_Wtime(); // elapsed time for chunk starts here
info->wStart = chunkInfo[0];
info->wSize = chunkInfo[1];
info->rStart = info->wStart;
info->rSize = info->wSize;
info->req4WRKsent = 0; // cancel request for work
SetBreaks(info);
//printf("WRK_TAG recv by %d %d %d", info->myRank, info->wStart, info->wSize);
info->sumt1 = 0.0; //for mu/wap
info->sumt2 = 0.0; // for sigma
}
else //current chunk is not finished save as next chunk
{
info->nextStart = chunkInfo[0];
info->nextSize = chunkInfo[1];
info->nextWRKrcvd = 1;
//printf('WRK_TAG (adv) recv by %d, %d, %d \n", info->myRank,info->nextStart,info->nextSize);
}
}
else
{
MPI_Send (chunkInfo, 2, MPI_INT, worker, WRK_TAG, info->comm);
}
//printf("message sent \n");
info->chunkStart = info->chunkStart + chunkSize;
info->itersScheduled = info->itersScheduled + chunkSize;
info->numChunks = info->numChunks + 1;
// info->chunkMap[2] = info->numChunks;
// info->chunkMap[3*info->numChunks ] = chunkInfo[0];
// info->chunkMap[3*info->numChunks+1] = chunkInfo[1];
// info->chunkMap[3*info->numChunks+2] = worker;
// printf("... sent start=%d, size=%d to %d\n", chunkInfo[0], chunkInfo[1], worker);
}
void SetBreaks ( infoDLS *info )
{
if (info->myRank == info->foreman) {
/* when to check for messages */
if (info->breakAfter<0)
info->probeFreq = max( 1, (info->wSize+info->commSize-1)/info->commSize/4 );
else
info->probeFreq = max( 1, info->breakAfter);
/* how many iterates left before requesting next chunk */
if (info->requestWhen<0)
info->sendRequest = info->probeFreq;
else
info->sendRequest = info->requestWhen;
}
else { /* not the foreman */
/* how many iterates left before requesting next chunk */
if (info->requestWhen<0)
info->sendRequest = max( 1, (15*info->wSize)/100 );
else
info->sendRequest = info->requestWhen;
/* when to check for messages */
info->probeFreq = max( 1, info->wSize-info->sendRequest);
}
}
void DLS_Setup ( MPI_Comm icomm, infoDLS *info )
{
int tP;
MPI_Comm_size(icomm, &tP);
MPI_Comm_rank(icomm, &(info->myRank));
info->comm = icomm;
info->crew = MPI_COMM_NULL;
info->commSize = tP;
info->firstRank = 0;
info->lastRank = tP-1;
info->foreman = 0;
info->breakAfter = -1;
info->probeFreq -1;
info->minChunk = tP;
info->stats = malloc(3*tP*sizeof(double));
info->weights = malloc(tP*sizeof(double));
info->timeStep = 0;
}
void DLS_Parameters_Setup( MPI_Comm icomm, infoDLS *info, int numProcs, int requestWhen, int breakAfter, int minChunk, double h_overhead, double sigma, int nKNL, double Xeon_speed, double KNL_speed )
{
int tP;
double total_sum = 0.0;
double core_speed = 0.0;
int i;
MPI_Comm_size(icomm, &tP);
MPI_Comm_rank(icomm, &(info->myRank));
info->comm = icomm;
info->crew = MPI_COMM_NULL;
info->commSize = tP;
info->firstRank = 0;
info->lastRank = tP-1;
info->foreman = 0;
info->breakAfter = breakAfter;
info->requestWhen= requestWhen;
info->probeFreq -1;
info->minChunk = minChunk;
info->stats = malloc(3*numProcs*sizeof(double));
// h and sigma for FSC orginial equation
info->h_overhead = h_overhead;
info->sigma = sigma;
// calculate weights for WF .. assume two types of processors, Xeon and KNL
info->weights = malloc(numProcs*sizeof(double));
info->timeStep = 0;
total_sum = nKNL * KNL_speed + (numProcs - nKNL) * Xeon_speed ;
for (i = 0; i < numProcs; i++)
{
//initialize mu, sigma, and performance data count
info->stats[3*i] = -1; //mu
info->stats[3*i+1] = -1; //sigma
info->stats[3*i+2] = 0; //performance data count
// initialize weights
if(i<(numProcs - nKNL))
{core_speed = Xeon_speed;}
else
{core_speed = KNL_speed; }
info->weights[i] = core_speed/total_sum * numProcs;
}
}
void DLS_GroupSetup ( MPI_Comm world, int coordinator,
infoDLS *GS, infoDLS *LS )
{
MPI_Comm jcomm, icomm;
int tP;
Partition(world, coordinator, &jcomm, &icomm);
GS->comm = jcomm;
GS->crew = icomm;
LS->comm = icomm;
LS->crew = icomm;
if (icomm!=MPI_COMM_NULL) {
DLS_Setup (icomm, LS);
MPI_Comm_size(icomm, &(GS->crewSize));
}
else {
LS->myRank = -1;
LS->commSize = 0;
LS->crewSize = 0;
GS->crewSize = 0;
}
if (jcomm!=MPI_COMM_NULL) {
MPI_Comm_size(jcomm, &tP);
MPI_Comm_rank(jcomm, &(GS->myRank));
GS->commSize = tP-1;
GS->firstRank = 1;
GS->lastRank = tP-1;
GS->breakAfter = -1;
GS->probeFreq -1;
GS->minChunk = tP-1;
}
else {
GS->myRank = -1;
GS->commSize = 0;
}
}
void DLS_StartLoop ( infoDLS *info, int firstIter, int lastIter, int imeth )
{
int tSize, worker;
int NULLloc;
double K;
info->wSize = 0; /* remaining iterates in current chunk */
info->gotWork = 1; /*.true.; */
info->workTime = 0.0;
info->myIters = 0;
info->N = lastIter - firstIter + 1;
info->timeStep = info->timeStep + 1;
int i;
double awap, trw;
if ( (info->comm==MPI_COMM_NULL) || (info->N<=0) ) return;
info->method = imeth;
if ( (imeth>DLS_MethodCount-1) || (imeth<0) )
info->method = 0;
else
info->method = imeth;
info->firstIter = firstIter;
info->lastIter = lastIter;
info->N = lastIter - firstIter + 1;
// TSS
info->TSSchunk = ceil((double) info->N / ((double) 2*info->commSize));
int n = ceil(2*info->N/(info->TSSchunk+1)); //n=2N/f+l
info->TSSdelta = (double) (info->TSSchunk - 1)/(double) (n-1);
// printf("ID: %d, TSS chunk %d, delta: %d\n",info->myRank,info->TSSchunk,info->TSSdelta);
// info->chunkMap[0] = firstIter; /* start of data */
// info->chunkMap[1] = info->N; /* size of data */
// info->chunkMap[2] = 0; /* chunks in this rank */
// calculate AWF weights
if ( (info->method == AWF) && (info->myRank == info->foreman))
{
if (info->timeStep == 1) //first timeStep
{
for( i = info->firstRank; i<=info->lastRank; i++)
{
info->weights[i] = 1.0;
}
}
else // all ranks have wap
{
awap = 0.0; // average weighted performance
for(i=info->firstRank; i<= info->lastRank; i++)
{
//printf("rank %d: %lf", i, info->stat[3*i]);
awap = awap + info->stats[3*i];
}
awap = awap/info->commSize;
trw = 0.0; // total ref weight (refwt(i) = awap/info%stats(3*i)
for( i=info->firstRank; i<=info->lastRank; i++)
{
trw = trw + awap/info->stats[3*i];
}
for( i=info->firstRank; i<=info->lastRank; i++)
{
info->weights[i] = ((awap/info->stats[3*i])*info->commSize)/trw;
}
}
}
info->numChunks = 0;
info->myExecs = 0;
info->mySumTimes = 0.0;
info->mySumSizes = 0.0;
tSize = (info->N+info->commSize-1)/info->commSize;
info->chunkMFSC = (0.55+tSize*log(2.0)/log( (1.0*tSize) ) );
info->kopt0 = sqrt(2.0)*info->N/( info->commSize*sqrt(log(1.0*info->commSize)) );
//calculate FSC chunk
K=(sqrt(2)*info->N*info->h_overhead)/(info->sigma*info->commSize*sqrt(log(info->commSize)));
K=pow(K, 2.0/3.0);
info->chunkFSC = (int) ceil(K);
info->nextWRKrcvd = 0;
info->req4WRKsent = 0;
info->finishedOne = 0;
info->probeFreq = max(1, info->breakAfter);
info->sendRequest = max(1, info->requestWhen);
if (info->myRank == info->foreman) {
info->chunkStart = firstIter;
info->itersScheduled = 0;
info->batchSize = 0;
info->batchRem = 0;
info->numENDed = 0;
info->numChunks = 0;
if (info->minChunk>0)
info->minChunkSize = info->minChunk;
else
info->minChunkSize = max(1,info->chunkMFSC/2); /* default min chunk size */
info->maxChunkSize = (info->N+2*info->commSize-1)/(2*info->commSize);
/* send initial work to each processor */
for (worker=info->firstRank;worker<=info->lastRank;worker++)
{
if (info->chunkStart < info->lastIter)
{
SendChunk (info, worker);
}
else
{
MPI_Send (&NULLloc, 0, MPI_INT, worker, END_TAG, info->comm); //end worker
info->numENDed++; // increment ended workers
}
}
}
}
int DLS_Terminated ( infoDLS *info ) {
int i, done;
MPI_Status tStatus;
if (info->N<=0) done=1;
else {
if ( (info->comm==MPI_COMM_NULL) && (info->crew!=MPI_COMM_NULL) )
MPI_Recv (&done, 1, MPI_INT, 0, TRM_TAG, info->crew, &tStatus);
else
if ( (info->comm!=MPI_COMM_NULL) && (info->crew!=MPI_COMM_NULL) ) {
done = (info->gotWork==0) && (info->wSize==0);
for (i=1;i<info->crewSize;i++)
MPI_Send (&done, 1, MPI_INT, i, TRM_TAG, info->crew);
}
else
done = (info->gotWork==0) && (info->wSize==0);
}
return (done);
}
void DLS_StartChunk (infoDLS *info, int *chunkStart, int *chunkSize)
{
int tSize, tStart, worker;
int MsgInQueue; /* message came in */
int loc, maxRemaining; /* source of chunk to be migrated */
int i, j, NULLloc, chunkInfo[2];
double perfInfo[4];
MPI_Status mStatus, tStatus;
if (info->comm==MPI_COMM_NULL) { /* I'm just a simple worker */
MPI_Recv (chunkInfo, 2, MPI_INT, 0, WRK_TAG, info->crew, &tStatus);
*chunkStart = chunkInfo[0];
*chunkSize = chunkInfo[1];
}
else { /* I'm the coordinator, or a foreman */
if (info->wSize == 0) {
MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, info->comm, &mStatus);
MsgInQueue = 1; /*.true. */
}
else
MPI_Iprobe (MPI_ANY_SOURCE, MPI_ANY_TAG, info->comm, &MsgInQueue, &mStatus);
while (MsgInQueue) {
switch ( mStatus.MPI_TAG ) {
case (WRK_TAG):
MPI_Recv (chunkInfo, 2, MPI_INT, mStatus.MPI_SOURCE, WRK_TAG, info->comm, &tStatus);
if (info->wSize == 0) { /* no pending chunk */
info->t0 = MPI_Wtime(); /* elapsed time for chunk starts here */
info->wStart = chunkInfo[0];
info->wSize = chunkInfo[1];
info->rStart = info->wStart;
info->rSize = info->wSize;
info->req4WRKsent = 0; /* cancel request for work */
SetBreaks (info);
info->sumt1 = 0.0; /* for mu/wap */
info->sumt2 = 0.0; /* for sigma */
}
else { /* current chunk is not finished save as next chunk */
info->nextStart = chunkInfo[0];
info->nextSize = chunkInfo[1];
info->nextWRKrcvd = 1; /*.true. */
}
break;
case (REQ_TAG): /* received by foreman only */
worker = mStatus.MPI_SOURCE;
MPI_Recv (perfInfo, 4, MPI_DOUBLE, worker, REQ_TAG, info->comm, &tStatus);
/* printf("... recv REQ_TAG from %d\n", worker); */
if ( (info->method==AF) || (info->method==AWF_B) || (info->method==AWF_C) ||
(info->method==AWF_D) || (info->method==AWF_E) ) {
loc = perfInfo[2];
info->stats[3*loc+2] = info->stats[3*loc+2]+1.0;
/* adaptive methods */
info->stats[3*loc] = perfInfo[0];
info->stats[3*loc+1] = perfInfo[1];
if (info->finishedOne != info->commSize) {
/* workers that have not finished a first chunk */
/* assume the lowest performance */
j = loc;
for (i=info->firstRank;i<=info->lastRank;i++)
if ( (info->stats[3*i+2] > 0.0) &&
(info->stats[3*i] < info->stats[3*j]) ) j = i;
info->finishedOne = 0;
for (i=info->firstRank;i<=info->lastRank;i++)
if (info->stats[3*i+2] == 0.0) {
info->stats[3*i] = info->stats[3*j];
info->stats[3*i+1] = info->stats[3*j+1];
}
else
info->finishedOne = info->finishedOne + 1;
}
}
/* any remaining unscheduled iterates ? */
if (info->chunkStart <= info->lastIter)
SendChunk (info, worker);
else { /* all iterates scheduled */
info->numENDed = info->numENDed + 1;
if (worker != info->myRank) {
MPI_Send (&NULLloc, 0, MPI_INT, worker, END_TAG, info->comm);
/* printf("... sent END_TAG to %d\n", worker); */
}
info->gotWork = (info->numENDed!=info->commSize); /* foreman exits? */
}
break;
case (END_TAG): /* received by workers only */
MPI_Recv (&NULLloc, 0, MPI_INT, mStatus.MPI_SOURCE,
mStatus.MPI_TAG, info->comm, &tStatus);
info->gotWork = 0;
break;
} /* switch */
MPI_Iprobe (MPI_ANY_SOURCE, MPI_ANY_TAG, info->comm, &MsgInQueue, &mStatus);
} /* while (MsgInQueue) */
*chunkStart = info->wStart;
*chunkSize = min (info->wSize, info->probeFreq);
if (info->method == AF) *chunkSize = min(1, *chunkSize);
info->subChunkSize = *chunkSize;
if (info->subChunkSize!=0) info->t1 = MPI_Wtime();
/* relay chunkStart, chunkSize to icomm */
if (info->crew!=MPI_COMM_NULL) {
chunkInfo[0] = *chunkStart;
chunkInfo[1] = *chunkSize;
for (i=1;i<info->crewSize;i++)
MPI_Send (chunkInfo, 2, MPI_INT, i, WRK_TAG, info->crew);
}
} /* (info->comm!=MPI_COMM_NULL) { */
}
void DLS_EndChunk (infoDLS *info)
{
double tk, perfInfo[4];
int loc;
int i, j;
if (info->comm==MPI_COMM_NULL) return;
if (info->subChunkSize==0) return;
tk = MPI_Wtime();
info->t1 = tk - info->t1;
info->wStart = info->wStart + info->subChunkSize;
info->wSize = info->wSize - info->subChunkSize;
info->sumt1 = info->sumt1 + info->t1;
info->workTime = info->workTime + info->t1;
if (info->method == AF) info->sumt2 = info->sumt2 + info->t1*info->t1;
if (info->wSize == 0) { /* chunk finished */
if ( (info->method==AWF_B) || (info->method==AWF_C) ) {
/* adaptive weighted factoring, work time */
info->mySumTimes = info->mySumTimes + (1+info->myExecs)*info->sumt1;
info->mySumSizes = info->mySumSizes + (1.0+info->myExecs)*info->rSize;
}
else if ( (info->method==AWF_D) || (info->method==AWF_E) ) {
/* adaptive weighted factoring, elapsed time */
info->mySumTimes = info->mySumTimes + (1+info->myExecs)*(tk-info->t0);
info->mySumSizes = info->mySumSizes + (1.0+info->myExecs)*info->rSize;
}
if(info->method !=AF)
{
/* reset accumulators */
info->myIters = info->myIters + info->rSize;
info->myExecs = info->myExecs + 1;
info->sumt1 = 0.0; /* for mu */
info->sumt2 = 0.0; /* for sigma */
info->rSize = 0;
}
if (info->nextWRKrcvd) { /* foreman already responded to advance request */
info->t0 = MPI_Wtime(); /* elapsed time for chunk starts here */
info->wStart = info->nextStart;
info->wSize = info->nextSize;
info->rStart = info->wStart;
info->rSize = info->wSize;
SetBreaks (info);
info->nextSize = 0;
info->nextWRKrcvd = 0;
info->req4WRKsent = 0;
}
} /* if (info->wSize == 0) */
/* send request ? */
if ( (info->wSize<=info->sendRequest) && (info->req4WRKsent==0) ) {
switch (info->method) {
case STATIC:
case SS:
case FSC:
case mFSC:
case GSS:
case TSS:
case FAC:
case WF:
case AWF:
perfInfo[0] = 0.0;
perfInfo[1] = 0.0;
perfInfo[2] = 1.0*info->myRank;
break;
case AWF_B:
case AWF_C: /* mu = (chunk work time)/(chunk size) */
perfInfo[0] = ( info->mySumTimes + (info->myExecs+1)*info->sumt1 ) /
( info->mySumSizes + 1.0*(info->myExecs+1)*(info->rSize-info->wSize) );
perfInfo[1] = 0.0;
perfInfo[2] = 1.0*info->myRank;
break;
case AF:
perfInfo[0] = info->sumt1/(info->rSize-info->wSize); /* mu */
if ((info->rSize-info->wSize) > 1) {
perfInfo[1] = (info->sumt2 - perfInfo[0]*perfInfo[0]*(info->rSize-info->wSize)) /
(info->rSize-info->wSize-1); /* sigma */
if (perfInfo[1] < 0.0) perfInfo[1] = 0.0;
perfInfo[1] = sqrt(perfInfo[1]);
}
else perfInfo[1] = 0.0;
perfInfo[2] = 1.0*info->myRank;
if (info->wSize == 0)
{ // reset accumulators
info->myIters = info->myIters + info->rSize;
info->myExecs = info->myExecs + 1;
info->sumt1 = 0.0; // for mu
info->sumt2 = 0.0; // for sigma
info->rSize = 0;
}
break;
case AWF_D:
case AWF_E: /* mu = (chunk elapsed time)/(chunk size) */
perfInfo[0] = ( info->mySumTimes + (info->myExecs+1)*(tk-info->t0)) /
( info->mySumSizes + 1.0*(info->myExecs+1)*(info->rSize-info->wSize) );
perfInfo[1] = 0.0;
perfInfo[2] = 1.0*info->myRank;
break;
}
if(info->myRank == info->foreman)
{ //update performance data
if (info->method==AWF_B || info->method==AWF_C || info->method==AF || info->method==AWF_D || info->method==AWF_E)
{
loc = (int) perfInfo[2];
info->stats[3*loc+2] = info->stats[3*loc+2]+1.0;
//adaptive methods
info->stats[3*loc] = perfInfo[0];
info->stats[3*loc+1] = perfInfo[1];
if (info->finishedOne != info->commSize)
{
// workers that have not finished a first chunk
// assume the lowest performance
j = loc;
for(i=info->firstRank; i<=info->lastRank; i++)
{
if ( (info->stats[3*i+2] > 0.0) && (info->stats[3*i] < info->stats[3*j]) )
j = i;
}
info->finishedOne = 0;
for( i=info->firstRank; i<=info->lastRank;i++)
{
if (info->stats[3*i+2] == 0.0)
{
info->stats[3*i] = info->stats[3*j];
info->stats[3*i+1] = info->stats[3*j+1];
}
else
{
info->finishedOne = info->finishedOne + 1;
}
}
}
}
// get more work to myself
if (info->chunkStart < info->lastIter)
{
// printf("[end chunk] obtaining work ... current chunk start, %d \n", info->chunkStart);
info->req4WRKsent = 1;
info->nextWRKrcvd = 0;
SendChunk (info, info->myRank);
// printf("[end chunk] wsize: %d \n", info->wSize);
}
else if (info->wSize == 0 && info->chunkStart >= info->lastIter)
{
// all iterates scheduled
info->numENDed = info->numENDed + 1;
// printf( "[end chunk] ended ranks %d chunk %d \n, info->numENDed,info->subChunkSize);
info->gotWork = info->numENDed!=info->commSize; // foreman exits?
}
}
else