-
Notifications
You must be signed in to change notification settings - Fork 0
/
logp_mpi.c
1588 lines (1352 loc) · 40.2 KB
/
logp_mpi.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
/*
* (c) copyright 1999-2003, by the Vrije Universiteit, The Netherlands.
* For full copyright and restrictions on use see the file COPYRIGHT.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "mpi.h"
#include "logp_mpi.h"
#include "logp_stats.h"
/*
* logp_mpi.c
*
* Interface to the network performance according to the
* "parametrized LogP" model, using MPI.
*
* The approach described in the paper is still followed, except
* that we now use confidence interval estimates to determine the
* stopping criteria.
*/
/* Some search values are made dependent on msg size to be able
* to speed things up a little. They're just heuristics.
*/
#define LOGP_SIZE_SMALL(s) ((s) <= 1024)
#define LOGP_SIZE_AVG(s) (!LOGP_SIZE_SMALL(s) && ((s) <= (64 * 1024)))
#define LOGP_SIZE_BIG(s) (!LOGP_SIZE_SMALL(s) && !LOGP_SIZE_AVG(s))
/* Extra sanity checks during development: */
/* #define DEBUG_TIMINGS */
#ifdef DEBUG_TIMINGS
#define debug_timings(list) printf list
#else
#define debug_timings(list) do {} while(0)
#endif
/* perform more tests, depending on msg size: */
#define LOGP_ITS_MULT_SMALL 4
#define LOGP_ITS_MULT_AVG 2
#define LOGP_ITS_MULT_MAX 16
#define LOGP_MAX_BUF_EXT 8 /* maxsize extension for g minimum search */
#define LOGP_T_EPS (1e-8) /* smallest measurable time */
typedef unsigned char byte_type;
#define sec_to_usec(s) (1E6 * (s))
static void
spin_wait(unsigned long usec)
{
double now, deadline;
now = MPI_Wtime();
deadline = now + ((double)usec) / 1E6;
while (MPI_Wtime() < deadline)
;
}
#define TIMING_LOOP 1000
static void
busy(void)
{
volatile int dummy;
volatile int i; /* Huh?!: gcc optimization bug/oddity otherwise?! */
dummy = 42;
for (i = 0; i < 300; i++) {
dummy++;
}
}
static double
measure_clock_ovhd(int diag, double confint)
{
double start, stop, loop0, loop1;
double t0[TIMING_LOOP], t1[TIMING_LOOP];
statistics loop0_stats, loop1_stats;
int j;
stats_init(&loop0_stats);
stats_init(&loop1_stats);
for (j = 0; j < 5; j++) {
int i;
/* first a loop without clock calls */
start = MPI_Wtime();
for (i = 0; i < TIMING_LOOP; i++) {
busy();
}
stop = MPI_Wtime();
loop0 = (stop - start) / TIMING_LOOP;
stats_update(&loop0_stats, loop0);
/* next, the same loop measured using two clock calls */
for (i = 0; i < TIMING_LOOP; i++) {
t0[i] = MPI_Wtime();
busy();
t1[i] = MPI_Wtime();
}
loop1 = 0.0;
for (i = 0; i < TIMING_LOOP; i++) {
loop1 += t1[i] - t0[i];
}
loop1 /= TIMING_LOOP;
stats_update(&loop1_stats, loop1);
debug_timings(("Busyloop takes %.3f usec,"
"measured with timing calls %.3f usec\n",
sec_to_usec(loop0), sec_to_usec(loop1)));
if (diag >= LOGP_DIAG_VERBOSE2) {
printf("clock: loop0 %.3f usec, loop1 %.3f usec\n",
sec_to_usec(loop0), sec_to_usec(loop1));
}
}
/* The clock overhead is used as a correction on other measurements,
* so we take the minima as not to overestimate it.
*/
loop0 = stats_min(&loop0_stats);
loop1 = stats_min(&loop1_stats);
if (diag >= LOGP_DIAG_VERBOSE2) {
printf("clock: loop0 avg %.3f +- %.3f us; loop1 avg %.3f +- %.3f us\n",
sec_to_usec(loop0),
sec_to_usec(stats_conf_int(&loop0_stats, 1, j, confint)),
sec_to_usec(loop1),
sec_to_usec(stats_conf_int(&loop1_stats, 1, j, confint)));
}
return loop1 - loop0;
}
/* File format: */
#define LOGP_FILE_HEADER "# LogP network performance data: %s\n"
#define LOGP_FILE_LATENCY_W "# Latency = %9.7f\n"
#define LOGP_FILE_LATENCY_R "# Latency = %lf\n"
#define LOGP_FILE_TABLE "# time bytes os os_min os_cnfint or or_min or_cnfint g\n"
#define LOGP_FILE_ENTRY_W "%9ld %7d %9.7f %9.7f %9.7f %9.7f %9.7f %9.7f %9.7f\n"
#define LOGP_FILE_ENTRY_R "%ld %d %lf %lf %lf %lf %lf %lf %lf\n"
#define LOGP_FILE_ITEMS 9
int
logp_save(logp_params *logp, char *filename)
{
FILE *file;
int i;
file = fopen(filename, "w");
if (file == NULL) {
fprintf(stderr, "LogP: cannot open '%s' for writing\n", filename);
return MPI_ERR_INTERN;
}
fprintf(file, LOGP_FILE_HEADER, logp->descr);
/* L in sec like the others */
fprintf(file, LOGP_FILE_LATENCY_W, logp->L);
fprintf(file, LOGP_FILE_TABLE);
for (i = 0; i < logp->nsizes; i++) {
logp_size_params *lsp = &logp->params[i];
fprintf(file, LOGP_FILE_ENTRY_W,
lsp->time, lsp->size, lsp->os, lsp->os_min, lsp->os_cnfint,
lsp->or, lsp->or_min, lsp->or_cnfint, lsp->g);
}
fclose(file);
return MPI_SUCCESS;
}
static void
read_err(char *filename, int line)
{
fprintf(stderr, "LogP: reading file '%s' failed in line %d\n",
filename, line);
}
static void
alloc_err(void)
{
fprintf(stderr, "LogP: out of memory\n");
}
#define LINESIZE 128
void
logp_free(logp_params *logp)
{
if (logp == NULL) {
return;
}
if (logp->descr != NULL) free(logp->descr);
if (logp->params != NULL) free(logp->params);
free(logp);
}
int
logp_load(logp_params **logpp, char *filename)
{
FILE *file;
int i, line, items;
char linebuffer[LINESIZE];
char tempdescr[LINESIZE];
logp_params *logp;
file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "LogP: cannot open '%s' for reading\n", filename);
return MPI_ERR_INTERN;
}
logp = NULL;
# define got_read_err(file, filename, line, logp) { \
read_err(filename, line); \
fclose(file); \
logp_free(logp); \
}
# define got_alloc_err(file, logp) { \
alloc_err(); \
fclose(file); \
logp_free(logp); \
}
logp = (logp_params *) malloc(sizeof(logp_params));
if (logp == NULL) {
got_alloc_err(file, logp);
return MPI_ERR_INTERN;
}
logp->descr = NULL;
logp->nsizes = 0;
logp->L = 0.0;
logp->params = NULL;
line = 0;
/* ignore first line: */
line++;
if (fgets(linebuffer, LINESIZE, file) == NULL) {
got_read_err(file, filename, line, logp);
return MPI_ERR_INTERN;
}
items = sscanf(linebuffer, LOGP_FILE_HEADER, &tempdescr[0]);
if (items != 1) {
got_read_err(file, filename, line, logp);
return MPI_ERR_INTERN;
}
logp->descr = strdup(tempdescr);
if (logp->descr == NULL) {
got_alloc_err(file, logp);
return MPI_ERR_INTERN;
}
line++;
if (fgets(linebuffer, LINESIZE, file) == NULL) {
got_read_err(file, filename, line, logp);
return MPI_ERR_INTERN;
}
items = sscanf(linebuffer, LOGP_FILE_LATENCY_R, &logp->L);
if (items != 1) {
got_read_err(file, filename, line, logp);
return MPI_ERR_INTERN;
}
/* ignore third line: */
line++;
if (fgets(linebuffer, LINESIZE, file) == NULL) {
got_read_err(file, filename, line, logp);
return MPI_ERR_INTERN;
}
for (i = 0; ; i++) {
logp_size_params *lsp;
int size;
line++;
if (fgets(linebuffer, LINESIZE, file) == NULL) {
/* EOF: no more lines */
break;
}
/* extend size_params in logp structure */
size = i + 1;
logp->params = (logp_size_params *)
realloc(logp->params, size * sizeof(logp_size_params));
if (logp->params == NULL) {
got_alloc_err(file, logp);
return MPI_ERR_INTERN;
}
logp->nsizes_max = size;
lsp = &logp->params[i];
items = sscanf(linebuffer, LOGP_FILE_ENTRY_R,
&lsp->time, &lsp->size,
&lsp->os, &lsp->os_cnfint, &lsp->os_min,
&lsp->or, &lsp->or_cnfint, &lsp->or_min, &lsp->g);
if (items != LOGP_FILE_ITEMS) {
got_read_err(file, filename, line, logp);
return MPI_ERR_INTERN;
}
/* RTT times currently not saved/restored: */
lsp->RTT = 0.0;
lsp->RTT_min = 0.0;
}
logp->nsizes = i;
fclose(file);
#if 0
/* Testing: write back to see if was read properly */
{
char copyfile[256];
int rank;
int size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
size = snprintf(copyfile, sizeof(copyfile),
"%s.%d", filename, rank);
if (size > 0) {
logp_save(logp, copyfile);
}
}
#endif
*logpp = logp;
return MPI_SUCCESS;
}
static int
sort_times(const void *v1, const void *v2)
{
double val1, val2;
val1 = * (double *) v1;
val2 = * (double *) v2;
if (val1 < val2) {
return -1;
} else if (val1 > val2) {
return 1;
} else {
return 0;
}
}
static void
logp_measure_or_os(logp_params *logp,
MPI_Comm comm, int rank, int peer,
int incr_iters, int max_iters, double confint, double eps,
double clock_ovhd,
logp_sendmode send_mode, logp_recvmode recv_mode,
byte_type *buffer,
double *times, double *times_os, int size_i, int diag)
{
int i, iters, busy;
double orig_start, start, stop, stop_os;
double clock_err;
long delta_theo, delta_theo_min;
MPI_Status mpi_stat;
MPI_Request mpi_req;
logp_size_params *lsp;
int cur_iters, cur_iters_max;
int size;
int ignore_first_runs;
statistics rtt_stats, os_stats, or_stats;
double rtt_conf_int, os_conf_int, or_conf_int;
stats_init(&rtt_stats);
stats_init(&os_stats);
/* or/os: delta round trips with varying message sizes
* we need two rounds here:
* first: round trips n bytes forward, 0 bytes back
* this is also used to determine os
* then: same roundtrips 0 bytes forward, n bytes back with a delta
* Finally we can compute L or g(msgsize) based using RTT's
* measured and g(0).
*
* Assumption: logp->g[0] is already measured.
*/
lsp = &logp->params[size_i];
size = lsp->size;
if (diag >= LOGP_DIAG_DEFAULT) {
printf("%d: %7d: ", rank, size);
fflush(stdout);
}
if (diag >= LOGP_DIAG_VERBOSE2) {
printf("\n");
fflush(stdout);
}
lsp->time = time((time_t *) NULL);
/* first do the round trips without delta, store this in RTT */
busy = 1;
iters = 0;
start = stop = stop_os = 0.0; /* just to avoid compiler warnings */
rtt_conf_int = os_conf_int = or_conf_int = 0.0; /* ditto */
/* we use twice the measured time; XXX why? */
clock_err = 2.0 * clock_ovhd;
orig_start = MPI_Wtime();
/* For "small" sizes (<= 1K) increase the amount of tests.
* This reduces the startup effect per round.
*/
if (LOGP_SIZE_BIG(size)) {
cur_iters_max = incr_iters;
} else {
cur_iters_max = incr_iters * LOGP_ITS_MULT_MAX;
if (LOGP_SIZE_SMALL(size)) {
max_iters *= LOGP_ITS_MULT_SMALL;
} else {
max_iters *= LOGP_ITS_MULT_AVG;
}
}
/* We don't want delta to be very close to the clock overhead: */
delta_theo_min = 100.0E6 * clock_ovhd;
/* The first few runs are usally highest because of cache interactions,
* especially for small message sizes.
* Just ignore them for the measurements.
*/
ignore_first_runs = !LOGP_SIZE_BIG(size);
if (ignore_first_runs) {
cur_iters = 2;
} else {
cur_iters = incr_iters;
}
while (busy) {
/* tell mirror the amount of tests we're going to do this round */
MPI_Send(&cur_iters, 1, MPI_INT, peer, 0, comm);
/* sync */
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
for (i = 0; i < cur_iters; i++) {
switch (send_mode) {
case LOGP_MPI_SEND:
start = MPI_Wtime();
MPI_Send(buffer, size, MPI_BYTE, peer, 0, comm);
stop_os = MPI_Wtime();
break;
case LOGP_MPI_ISEND:
start = MPI_Wtime();
MPI_Isend(buffer, size, MPI_BYTE, peer, 0, comm, &mpi_req);
stop_os = MPI_Wtime();
MPI_Wait(&mpi_req, &mpi_stat);
break;
case LOGP_MPI_SSEND:
start = MPI_Wtime();
MPI_Ssend(buffer, size, MPI_BYTE, peer, 0, comm);
stop_os = MPI_Wtime();
break;
}
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
stop = MPI_Wtime();
/* substract the clock_ovhd cost */
times [i] = stop - start - clock_ovhd;
if (times[i] < LOGP_T_EPS) { /* clock_ovhd overestimated? */
times[i] = LOGP_T_EPS;
}
times_os[i] = stop_os - start - clock_ovhd;
if (times_os[i] < LOGP_T_EPS) { /* clock_ovhd overestimated? */
times_os[i] = LOGP_T_EPS;
}
}
if (ignore_first_runs) {
/* this was just the first warming-up run */
ignore_first_runs = 0;
cur_iters = incr_iters;
continue;
}
iters += cur_iters;
debug_timings(("%d: os:", rank));
for (i = 0; i < cur_iters; i++) {
stats_update(&rtt_stats, times[i]);
stats_update(&os_stats, times_os[i]);
debug_timings(("%.3f ", sec_to_usec(times_os[i])));
}
/* I think we should be able to use the onesided tail
* since the values we are estimating have a lowerbound.
*/
rtt_conf_int = stats_conf_int(&rtt_stats, 1, iters, confint);
os_conf_int = stats_conf_int(&os_stats, 1, iters, confint);
if (diag >= LOGP_DIAG_VERBOSE2) {
printf("rtt %9.7f +- %9.7f (%3.1f%%); "
"os %9.7f +- %9.7f (%3.1f%%)\n",
stats_mean(&rtt_stats), rtt_conf_int,
100 * rtt_conf_int / stats_mean(&rtt_stats),
stats_mean(&os_stats), os_conf_int,
100 * os_conf_int / stats_mean(&os_stats));
}
/* TODO: take clock_err into account? */
busy = (iters < max_iters) &&
(((os_conf_int / stats_mean(&os_stats)) > 2 * eps) ||
((rtt_conf_int / stats_mean(&rtt_stats)) > 2 * eps));
}
/* tell mirror we're ready with round 1 */
MPI_Send(&busy, 1, MPI_INT, peer, 0, comm);
lsp->RTT = stats_mean(&rtt_stats);
lsp->RTT_min = stats_min(&rtt_stats);
lsp->os = stats_mean(&os_stats);
lsp->os_cnfint = os_conf_int;
lsp->os_min = stats_min(&os_stats);
if (diag >= LOGP_DIAG_DEFAULT) {
printf("os %9.7f", lsp->os);
if (diag >= LOGP_DIAG_VERBOSE) {
printf(" [%2d] ", iters);
} else {
printf(" ");
}
debug_timings((", i=%2d;", iters));
fflush(stdout);
}
if (diag >= LOGP_DIAG_VERBOSE2) {
printf("\n");
fflush(stdout);
}
/* second do the round trips with delta, store this in or */
busy = 1;
iters = 0;
stats_init(&or_stats);
/* See comments about first runs above */
ignore_first_runs = !LOGP_SIZE_BIG(size);
if (ignore_first_runs) {
cur_iters = 2;
} else {
cur_iters = incr_iters;
}
while (busy) {
/* tell mirror the amount of tests we're going to do this round */
MPI_Send(&cur_iters, 1, MPI_INT, peer, 0, comm);
/* sync */
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
/* compute a suitable delta for this data size:
* add 20% and translate to usec:
*/
delta_theo = sec_to_usec(1.2 * lsp->RTT);
if (delta_theo < delta_theo_min) {
delta_theo = delta_theo_min;
}
debug_timings((" delta=%ldus;", delta_theo));
for (i = 0; i < cur_iters; i++) {
switch (send_mode) {
case LOGP_MPI_SEND:
MPI_Send(buffer, 0, MPI_BYTE, peer, 0, comm);
break;
case LOGP_MPI_ISEND:
MPI_Isend(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_req);
MPI_Wait(&mpi_req, &mpi_stat);
break;
case LOGP_MPI_SSEND:
MPI_Ssend(buffer, 0, MPI_BYTE, peer, 0, comm);
break;
}
switch (recv_mode) {
case LOGP_MPI_RECV:
spin_wait(delta_theo);
start = MPI_Wtime();
MPI_Recv(buffer, size, MPI_BYTE, peer, 0, comm, &mpi_stat);
stop = MPI_Wtime();
break;
case LOGP_MPI_IRECV:
MPI_Irecv(buffer, size, MPI_BYTE, peer, 0, comm, &mpi_req);
spin_wait(delta_theo);
start = MPI_Wtime();
MPI_Wait(&mpi_req, &mpi_stat);
stop = MPI_Wtime();
break;
}
/* substract the clock_ovhd cost */
times[i] = stop - start - clock_ovhd;
if (times[i] < LOGP_T_EPS) {
times[i] = LOGP_T_EPS;
}
}
if (ignore_first_runs) {
/* this was just the first warming-up run */
ignore_first_runs = 0;
cur_iters = incr_iters;
continue;
}
iters += cur_iters;
debug_timings(("%d: or:", rank));
for (i = 0; i < cur_iters; i++) {
stats_update(&or_stats, times[i]);
debug_timings(("%.3f ", sec_to_usec(times[i])));
}
or_conf_int = stats_conf_int(&or_stats, 1, iters, confint);
if (diag >= LOGP_DIAG_VERBOSE2) {
printf("or %9.7f +- %9.7f (%3.1f%%)\n",
stats_mean(&or_stats), or_conf_int,
100.0 * or_conf_int / stats_mean(&or_stats));
}
busy = (iters < max_iters) &&
((or_conf_int / stats_mean(&or_stats)) > 2 * eps);
}
/* tell mirror we're ready with round 2 */
MPI_Send(&busy, 1, MPI_INT, peer, 0, comm);
lsp->or = stats_mean(&or_stats);
lsp->or_cnfint = or_conf_int;
lsp->or_min = stats_min(&or_stats);
if (diag >= LOGP_DIAG_DEFAULT) {
printf("or %9.7f", lsp->or);
if (diag >= LOGP_DIAG_VERBOSE) {
printf(" [%2d] ", iters);
} else {
printf(" ");
}
debug_timings(("(min %.3f us, max %.3f us)",
sec_to_usec(stats_min(&or_stats)),
sec_to_usec(stats_max(&or_stats))));
debug_timings((", i=%2d, %.3fs;", iters, MPI_Wtime() - orig_start));
}
if (size != 0) {
/* set g(m) */
logp_size_params *lsp0 = &logp->params[0];
if (lsp->RTT > lsp0->RTT) {
/* usually */
lsp->g = lsp->RTT - lsp0->RTT + lsp0->g;
} else {
/* Maybe there was a lot of variability while estimating RTT(0);
* just take the lowest one measured.
*/
lsp->g = lsp->RTT - lsp0->RTT_min + lsp0->g;
}
if (lsp->g <= 0) {
printf("cannot determine g(%d): RTT(%d) %9.7f; RTT(0) %9.7f\n",
size, size, lsp->RTT, lsp0->RTT);
lsp->g = LOGP_T_EPS;
}
}
if (diag >= LOGP_DIAG_DEFAULT) {
printf(" g %9.7f", lsp->g);
}
/* Check os/or/g discrepancies, due to measurement imprecision.
* For example, direct measurement of g may be smaller than
* suggested by derived os/or because pipelining messages
* gives different caching behaviour. This is hard to avoid..
*/
if (lsp->os > lsp->g) {
if (lsp->os > (1.0 + eps) * lsp->g) {
if (diag >= LOGP_DIAG_VERBOSE) {
printf(" os > g!");
}
#ifdef FIXUP_G
lsp->g = lsp->os;
#endif
}
}
if (lsp->or > lsp->g) {
if (lsp->or > (1.0 + eps) * lsp->g) {
if (diag >= LOGP_DIAG_VERBOSE) {
printf(" or > g!");
}
#ifdef FIXUP_G
lsp->g = lsp->or;
#endif
}
}
if (size == 0) {
/* set L */
logp_size_params *lsp0;
lsp0 = &logp->params[0];
/* L in sec like the others */
logp->L = (lsp0->RTT / 2.0 - lsp0->g);
if (logp->L <= 0.0) {
logp->L = 0.0;
if (diag >= LOGP_DIAG_DEFAULT) {
printf(" L unknown!");
}
} else {
if (diag >= LOGP_DIAG_DEFAULT) {
printf(" L = %9.2f", logp->L);
}
}
}
if (diag >= LOGP_DIAG_DEFAULT) {
printf("\n");
}
}
static void
logp_mirror_or_os(MPI_Comm comm, int rank, int peer,
byte_type *buffer, int size)
{
MPI_Status mpi_stat;
int incr_iters;
int i;
/* or/os, first loop: */
/* printf("%d: or/os with size %d\n", rank, size); */
for (;;) {
MPI_Recv(&incr_iters, 1, MPI_INT, peer, 0, comm, &mpi_stat);
if (incr_iters == 0) {
/* done with first loop */
break;
}
/* sync */
MPI_Send(buffer, 0, MPI_BYTE, peer, 0, comm);
for (i = 0; i < incr_iters; i++) {
MPI_Recv(buffer, size, MPI_BYTE, peer, 0, comm, &mpi_stat);
MPI_Send(buffer, 0, MPI_BYTE, peer, 0, comm);
}
}
/* or/os, second loop: */
for (;;) {
MPI_Recv(&incr_iters, 1, MPI_INT, peer, 0, comm, &mpi_stat);
if (incr_iters == 0) {
/* done with second loop */
break;
}
/* sync */
MPI_Send(buffer, 0, MPI_BYTE, peer, 0, comm);
for (i = 0; i < incr_iters; i++) {
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
MPI_Send(buffer, size, MPI_BYTE, peer, 0, comm);
}
}
}
#define START_RUNS 10
/* Measuring RTT(m) in g(m) quickly: */
#define GM_RTT_COUNT(size) (LOGP_SIZE_SMALL(size) ? 100 : 10)
static void
logp_measure_gm(logp_params *logp,
int size_i,
int size,
MPI_Comm comm, int rank, int peer,
int incr_iters, int max_iters, double eps,
double clock_ovhd, logp_sendmode send_mode,
logp_bufmode buf_mode, byte_type *buffer, int maxsize,
double *times, double *times_os, int diag)
{
int i, runs, minruns, maxruns;
double start, stop, stop_sync;
double rtt;
double tot_time, prev_tot_time;
double avg, prev_avg;
MPI_Status mpi_stat;
MPI_Request mpi_req;
logp_size_params *lsp;
int firstrun;
/*
* Measure g(m) by means of pipelining messages.
*/
lsp = &logp->params[size_i];
/* avoid compiler warnings: */
start = stop = 0.0;
avg = prev_avg = 0.0;
prev_tot_time = 0.0;
if (diag >= LOGP_DIAG_VERBOSE) {
printf("%d: g(%d)/pipeline\n", rank, size);
}
/* Start with a small number of runs. We will then check if
* we actually need more runs.
* Also limit the amount of iterations; especially needed on WAN links.
*/
runs = START_RUNS;
minruns = 2 * START_RUNS;
maxruns = 128 * minruns;
MPI_Send(&size, 1, MPI_INT, peer, 0, comm);
/* First measure RTT(m) quickly. */
/* sync */
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
start = MPI_Wtime();
for (i = 0; i < GM_RTT_COUNT(size); i++) {
MPI_Send(buffer, size, MPI_BYTE, peer, 0, comm);
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
}
stop = MPI_Wtime();
rtt = (stop - start) / GM_RTT_COUNT(size);
firstrun = 1;
for (;;) {
/* Notify other side about the next number of sends we'll be doing
* this round. By sending 0 we indicate end of this g(m) test.
*/
char *bufp, *endp;
MPI_Send(&runs, 1, MPI_INT, peer, 0, comm);
if (runs == 0) {
break;
}
/* sync */
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
switch (send_mode) {
case LOGP_MPI_SEND:
start = MPI_Wtime();
bufp = buffer;
endp = buffer + maxsize;
for (i = 0; i < runs; i++) {
MPI_Send(bufp, size, MPI_BYTE, peer, 0, comm);
if (buf_mode == LOGP_BUF_DYNAMIC) {
/* Sending from different place in the buffer may have
* noticable influence on performance due to cache.
*/
bufp += 4 * 1024 + (i % 4) * (1024 + 512);
if (bufp + size > endp) {
bufp = buffer;
}
}
}
stop = MPI_Wtime();
break;
case LOGP_MPI_ISEND:
start = MPI_Wtime();
for (i = 0; i < runs; i++) {
MPI_Isend(buffer, size, MPI_BYTE, peer, 0, comm, &mpi_req);
MPI_Wait(&mpi_req, &mpi_stat);
}
stop = MPI_Wtime();
break;
case LOGP_MPI_SSEND:
start = MPI_Wtime();
for (i = 0; i < runs; i++) {
MPI_Ssend(buffer, size, MPI_BYTE, peer, 0, comm);
}
stop = MPI_Wtime();
break;
}
avg = (stop - start) / runs;
MPI_Recv(buffer, 0, MPI_BYTE, peer, 0, comm, &mpi_stat);
stop_sync = MPI_Wtime();
tot_time = stop_sync - start;
if (diag >= LOGP_DIAG_VERBOSE) {
printf("%d: runs = %d: tot_time %.1f"
" incr %.1f%% RTT %.1f g_avg %.1f diff %.1f%%\n",
rank, runs, sec_to_usec(tot_time),
(prev_tot_time != 0.0) ?
100 * ((tot_time - prev_tot_time) / prev_tot_time) : 0.0,
sec_to_usec(rtt), sec_to_usec(avg),
(prev_avg != 0.0) ?
100.0 * ((avg - prev_avg) / prev_avg) : 0.0);
fflush(stdout);
}
if (firstrun) {
firstrun = 0;
runs *= 2;
} else {
double perc_diff, maxdiff;
perc_diff = 100.0 * ((avg - prev_avg) / prev_avg);
debug_timings(("%d: g(%d): %d took %.3f sec:"
"avg %9.9f, prev %9.9f, diff %.1f%%\n",
rank, size, runs, stop - start, avg,
prev_avg, perc_diff));
/* Be careful about when to stop:
* assume saturation is reached when the larger run gets
* (almost) the same per byte overhead as the previous one.
* Currently assume this if the difference is less than 1%
* (1.5% for "big" sizes, or larger even if things get really
* out of hand..).
*/
if (runs < maxruns / 4) {
maxdiff = (LOGP_SIZE_BIG(size) ? 1.5 : 1.0);
} else {
maxdiff = 8.0;
}
/*
* Stop if:
* (max number of runs has been reached)
* OR
* (we've done more than the minimum number of runs
* AND send overhead per byte is within eps of prev measurement
* AND sync'd total time is not dominated by latency
* AND sync'd total time is about two times the previous one)
*/
if ((runs >= maxruns) ||
((runs >= minruns) &&
(fabs(perc_diff) < maxdiff) &&
(rtt / tot_time < eps / 2) &&
(fabs((2*prev_tot_time - rtt/2) / tot_time) < (1.0 + eps))))
{
/* stop */
runs = 0;
} else {
/* TODO: doubling may be a bit too much if runs > 1000 */
runs *= 2;
}
}
prev_avg = avg;
prev_tot_time = tot_time;
}
if (lsp->g != 0.0) {
if (diag >= LOGP_DIAG_VERBOSE) {
printf("%d: overriding previous g(%d) %9.7f with %9.7f\n",
rank, size, lsp->g, avg);
fflush(stdout);
}
}
lsp->g = avg;
}
static void
logp_mirror_gm(MPI_Comm comm, int rank, int peer,
int incr_iters, logp_sendmode send_mode,
logp_bufmode buf_mode, byte_type *buffer, int maxsize)
{
int i, runs;
int size;
MPI_Status mpi_stat;
double start, stop;
/* g:
* pipeline g(m)
*/
/* printf("%d: g(m)/pipeline\n", rank); */
for (;;) {
char *bufp, *endp;
MPI_Recv(&size, 1, MPI_INT, peer, 0, comm, &mpi_stat);
if (size < 0) {
/* no more g(m) measurements */
break;
}
/* First measure RTT(m) quickly. */