-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
1067 lines (807 loc) · 32.4 KB
/
Main.cpp
File metadata and controls
1067 lines (807 loc) · 32.4 KB
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
#include <vector>
#include <iostream>
#include <iomanip>
#include <chrono>
#include "mpi.h"
#include <math.h>
#include "cuda.h"
#include <list>
#include <cstring> /* memset & co. */
#include <ctime>
#include <cassert>
//#include <cuda.h>
#include <cuda_runtime.h>
//#include "Kokkos_Core.hpp"
#include <fstream>
#include <iomanip>
#include <ostream>
#include <sstream>
#include <iostream>
#include <numeric>
int n = 10;
#include<unistd.h>
int start =1;
bool server = false;
int testcount = 5;
int NG_START_PACKET_SIZE = 1028;
int max_datasize = 10000000 * 2;
int maxbufersize = 100000000;
bool selfPack;
MPI_Datatype datatype = MPI_FLOAT;
float *d_data1;
float *h_data;
int total_size;
int rank, num_procs;
template<typename T>
T variance(const std::list<T> &vec) {
const size_t sz = vec.size();
if (sz <= 1) {
return 0.0;
}
// Calculate the mean
const T mean = std::accumulate(vec.begin(), vec.end(), 0.0) / sz;
// Now calculate the variance
auto variance_func = [&mean, &sz](T accumulator, const T &val) {
return accumulator + ((val - mean) * (val - mean) / (sz - 1));
};
return std::accumulate(vec.begin(), vec.end(), 0.0, variance_func);
}
void pingpong(void *buffer , int i) {
MPI_Status status;
int size_s;
//abslute modle times
//retal error.
MPI_Type_size(datatype, &size_s);
for (int j = 0; j < testcount; ++j) {
if (rank == 0) {
MPI_Send(buffer, i
, MPI_FLOAT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(buffer, i
, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &status);
}
if (rank == 1) {
MPI_Recv(buffer, i, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(buffer, i, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
}
}
fflush(stdout);
int x = 3;
for (int j = 0; j < 10; ++j) {
if (rank == 0) {
MPI_Send(buffer, i, datatype, 1, 0, MPI_COMM_WORLD);
MPI_Recv(buffer, i, datatype, 1, 0, MPI_COMM_WORLD, &status);
}
if (rank == 1) {
MPI_Recv(buffer, i, datatype, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(buffer, i, datatype, 0, 0, MPI_COMM_WORLD);
}
}
std::list<double> times0, times1;
for (int k = 0; k < 10; ++k) {
double t1 = MPI_Wtime();
// for (int j = 0; j < testcount; ++j) {
// if (rank == 0) {
// MPI_Send(((int *) buffer) + j + k * testcount, i, datatype, 1, 0, MPI_COMM_WORLD);
// MPI_Recv(((int *) buffer) + j + 1 + k * testcount, i, datatype, 1, 0, MPI_COMM_WORLD, &status);
// }
// if (rank == 1) {
// MPI_Recv(((int *) buffer) + j + 1 + k * testcount, i, datatype, 0, 0, MPI_COMM_WORLD, &status);
// MPI_Send(((int *) buffer) + j + k * testcount, i, datatype, 0, 0, MPI_COMM_WORLD);
// }
// }
double tfinal2 = (MPI_Wtime() - t1) / ( testcount);
times1.push_back(tfinal2);
double t0 = MPI_Wtime();
for (int j = 0; j < testcount; ++j) {
if (rank == 0) {
MPI_Send(buffer, i, datatype, 1, 0, MPI_COMM_WORLD);
MPI_Recv(buffer, i, datatype, 1, 0, MPI_COMM_WORLD, &status);
}
if (rank == 1) {
MPI_Recv(buffer, i, datatype, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(buffer, i, datatype, 0, 0, MPI_COMM_WORLD);
}
}
double tfinal1 = (MPI_Wtime() - t0) / (testcount);
times0.push_back(tfinal1);
}
times0.sort();
times1.sort();
int count = 0;
double x00 = 0;
double x50 = 0;
double x90 = 0;
for (const auto &item: times0) {
if (count == 0) {
x00 = item;
}
if (count == 5) {
x50 = item;
}
if (count == 9) {
x90 = item;
}
count++;
}
count = 0;
double x01 = 0;
double x51 = 0;
double x91 = 0;
for (const auto &item: times1) {
if (count == 0) {
x01 = item;
}
if (count == 5) {
x51 = item;
}
if (count == 9) {
x91 = item;
}
count++;
}
double mean1 = std::accumulate(times1.begin(), times1.end(), 0.0) / times1.size();
double mean0 = std::accumulate(times0.begin(), times0.end(), 0.0) / times0.size();
printf("%i,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f\n", i * size_s, mean1, x01, x51, x91,mean0,x00, x50, x90);
fflush(stdout);
}
void pingpong(void *buffer) {
MPI_Status status;
int size_s;
MPI_Type_size(datatype, &size_s);
int i = 55;
for (int j = 0; j < testcount; ++j) {
if (rank == 0) {
MPI_Send(buffer, i
, MPI_FLOAT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(buffer, i
, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &status);
}
if (rank == 1) {
MPI_Recv(buffer, i, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(buffer, i, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
}
}
fflush(stdout);
int x = 3;
for (i = start/size_s; i < 150000; i = i * 2) {
for (int j = 0; j < 10; ++j) {
if (rank == 0) {
MPI_Send(buffer, i, datatype, 1, 0, MPI_COMM_WORLD);
MPI_Recv(buffer, i, datatype, 1, 0, MPI_COMM_WORLD, &status);
}
if (rank == 1) {
MPI_Recv(buffer, i, datatype, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(buffer, i, datatype, 0, 0, MPI_COMM_WORLD);
}
}
std::list<double> times0, times1;
for (int k = 0; k < 10; ++k) {
double t1 = MPI_Wtime();
// for (int j = 0; j < testcount; ++j) {
// if (rank == 0) {
// MPI_Send(((int *) buffer) + j + k * testcount, i, datatype, 1, 0, MPI_COMM_WORLD);
// MPI_Recv(((int *) buffer) + j + 1 + k * testcount, i, datatype, 1, 0, MPI_COMM_WORLD, &status);
// }
// if (rank == 1) {
// MPI_Recv(((int *) buffer) + j + 1 + k * testcount, i, datatype, 0, 0, MPI_COMM_WORLD, &status);
// MPI_Send(((int *) buffer) + j + k * testcount, i, datatype, 0, 0, MPI_COMM_WORLD);
// }
// }
double tfinal2 = (MPI_Wtime() - t1) / ( testcount);
times1.push_back(tfinal2);
double t0 = MPI_Wtime();
for (int j = 0; j < testcount; ++j) {
if (rank == 0) {
MPI_Send(buffer, i, datatype, 1, 0, MPI_COMM_WORLD);
MPI_Recv(buffer, i, datatype, 1, 0, MPI_COMM_WORLD, &status);
}
if (rank == 1) {
MPI_Recv(buffer, i, datatype, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(buffer, i, datatype, 0, 0, MPI_COMM_WORLD);
}
}
double tfinal1 = (MPI_Wtime() - t0) / (testcount);
times0.push_back(tfinal1);
}
times0.sort();
times1.sort();
int count = 0;
double x00 = 0;
double x50 = 0;
double x90 = 0;
for (const auto &item: times0) {
if (count == 0) {
x00 = item;
}
if (count == 5) {
x50 = item;
}
if (count == 9) {
x90 = item;
}
count++;
}
count = 0;
double x01 = 0;
double x51 = 0;
double x91 = 0;
for (const auto &item: times1) {
if (count == 0) {
x01 = item;
}
if (count == 5) {
x51 = item;
}
if (count == 9) {
x91 = item;
}
count++;
}
double mean1 = std::accumulate(times1.begin(), times1.end(), 0.0) / times1.size();
double mean0 = std::accumulate(times0.begin(), times0.end(), 0.0) / times0.size();
printf("%i,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f,%15.9f\n", i * size_s, mean1, x01, x51, x91,mean0,x00, x50, x90);
fflush(stdout);
}
}
static void *mpi_cuda_malloc(size_t size) {
cudaMalloc((void **) &d_data1, size * sizeof(float));
// cudaMalloc((void **) &d_data0, data_size * sizeof(float));
h_data = (float *) malloc(size * sizeof(float));
for (int i = 0; i < size; ++i) {
h_data[i] = i * 1.0f;
}
// cudaMemcpy(h_data, d_data0, data_size * sizeof(float), cudaMemcpyDeviceToHost);
printf("%p\n",d_data1);
fflush(stdout);
cudaMemcpy(h_data, d_data1, size * sizeof(float), cudaMemcpyDeviceToHost);
return d_data1;
}
struct ng_loggp_tests_val;
int loggp_do_benchmarks();
int loggp_prepare_benchmarks();
static int be_a_server(void *buffer, int size, int n, double d, char o_r,
MPI_Datatype dt_r, MPI_Datatype dt_s, int size_r,
int size_s);
static int be_a_client(void *buffer, int size, struct ng_loggp_tests_val *values,
MPI_Datatype dt_r, MPI_Datatype dt_s, int size_r,
int size_s);
/* a is object1 that holds all the tests for a specific PRTT(n,d,s)
* in microseconds */
struct ng_loggp_tests_val {
double *data; /* data array */
int n, s; /* n,s values s in bytes*/
double d; /* d (delay time) in usec */
int testc, itestc; /* maximal and actual testcount */
char o_r; /* should we measure o_r or not? - if yes, measure o_r at server and communicate it to client */
void (*constructor)(struct ng_loggp_tests_val *a, int testc, int n, double d, int s);
void (*destructor)(struct ng_loggp_tests_val *a);
void (*addval)(struct ng_loggp_tests_val *a, double val);
double (*getmed)(struct ng_loggp_tests_val *a);
};
/* object function definitions :) */
/* constructor for ng_loggp_tests_val class */
static void ng_loggp_tests_val_constr(struct ng_loggp_tests_val *a, int testc, int n, double d, int s) {
a->n = n;
a->d = d;
a->s = s;
a->testc = testc;
a->o_r = 0;
a->itestc = 0; /* number of tests in array */
a->data = (double *) malloc(testc * sizeof(double));
{
int itestc;
for (itestc = 0; itestc < testc; itestc++) {
a->data[itestc] = 0.0;
}
}
}
/* destructor for ng_loggp_tests_val */
static void ng_loggp_tests_val_destr(struct ng_loggp_tests_val *a) {
if (a->data != NULL) free(a->data);
a->data = NULL;
}
/* add a measurement value */
static void ng_loggp_tests_val_addval(struct ng_loggp_tests_val *a, double val) {
if (a->itestc < a->testc) {
a->data[(a->itestc)++] = val;
} else {
// ng_error("too many tests (a should not happen!)\n");
}
}
/* get median of all mesurements */
static double ng_loggp_tests_val_getmed(struct ng_loggp_tests_val *a) {
/* bubble-sort data */
int x, y;
double holder;
for (x = 0; x < a->itestc; x++)
for (y = 0; y < a->itestc - 1; y++)
if (a->data[y] > a->data[y + 1]) {
holder = a->data[y + 1];
a->data[y + 1] = a->data[y];
a->data[y] = holder;
}
/* return median */
y = (a->itestc + 1) / 2;
return a->data[y];
}
/* a object holds a full PRTT(n,d,s) for a fixed n and d */
typedef struct {
int size;
double value;
} t_sizevalue;
struct ng_loggp_prtt_val {
t_sizevalue *data; /* data array */
int n; /* n values */
double d; /* d value in microseconds */
int elems; /* # of elements in the data array */
double a, b, lsquares; /* curve parameters, y=ax+b */
void (*constructor)(struct ng_loggp_prtt_val *a, int n, double d);
void (*destructor)(struct ng_loggp_prtt_val *a);
void (*addval)(struct ng_loggp_prtt_val *a, int s, double val);
void (*getfit)(struct ng_loggp_prtt_val *a, int lower, int upper);
void (*remove)(struct ng_loggp_prtt_val *a, int item);
};
/* constructor for ng_loggp_prtt_val class */
static void ng_loggp_prtt_val_constr(struct ng_loggp_prtt_val *a, int n, double d) {
a->n = n;
a->d = d;
a->elems = 0;
a->data = NULL;
}
/* destructor for ng_loggp_prtt_val class */
static void ng_loggp_prtt_val_destr(struct ng_loggp_prtt_val *a) {
if (a->data != NULL) free(a->data);
a->data = NULL;
}
/* calculate the parameters for y = ax + b in the interval [lower,upper] elements
* if lower == upper == 0 -> fit all values */
static void ng_loggp_prtt_getfit(struct ng_loggp_prtt_val *a, int lower, int upper) {
long double
x_mean = 0,
y_mean = 0,
h = 0, j = 0;
int iterator;
int count = upper - lower;
/* solve the linear least squares problem directly, see
* http://de.wikipedia.org/wiki/Kleinste-Quadrate-Methode (sorry, it's
* missing in the english variant) for details.
*/
for (iterator = lower; iterator < upper; iterator++) {
// printf("fit: %i - %f (%Lf, %Lf)\n", a->data[iterator].size, a->data[iterator].value, x_mean, y_mean);
x_mean += a->data[iterator].size;
y_mean += a->data[iterator].value;
}
x_mean /= count;
y_mean /= count;
for (iterator = lower; iterator < upper; iterator++) {
h += (a->data[iterator].size - x_mean) * (a->data[iterator].value - y_mean);
j += (a->data[iterator].size - x_mean) * (a->data[iterator].size - x_mean);
}
a->a = h / j;
a->b = y_mean - a->a * x_mean;
//printf("params: %lf, %lf (%i) (%Lf, %Lf, %Lf, %Lf)\n", a->a, a->b, count, x_mean, y_mean, h, j);
/* calculate the least squares difference for a fixed msg-size
* (x-axis) */
a->lsquares = 0;
for (iterator = lower; iterator < upper; iterator++) {
double sq;
sq = a->a * a->data[iterator].size + a->b - a->data[iterator].value;
a->lsquares += sq * sq;
}
a->lsquares /= (count - 2);
a->lsquares = sqrt(a->lsquares);
}
/* addval for ng_loggp_prtt_val class */
static void ng_loggp_prtt_addval(struct ng_loggp_prtt_val *a, int s, double val) {
(a->elems)++;
a->data = static_cast<t_sizevalue *>(realloc(a->data, a->elems * sizeof(t_sizevalue)));
assert(a->data != NULL);
a->data[a->elems - 1].size = s;
a->data[a->elems - 1].value = val;
}
/* remove for ng_loggp_prtt_val class */
static void ng_loggp_prtt_remove(struct ng_loggp_prtt_val *a, int item) {
t_sizevalue *tmp;
int i, ind;
(a->elems)--;
tmp = static_cast<t_sizevalue *>(malloc(a->elems * sizeof(t_sizevalue)));
assert(tmp != NULL);
ind = 0;
for (i = 0; i < a->elems + 1; i++) {
if (i == item) continue;
tmp[ind] = a->data[i];
ind++;
}
free(a->data);
a->data = tmp;
}
static void printparams(struct ng_loggp_prtt_val *gresults,
struct ng_loggp_prtt_val *results_1_0,
struct ng_loggp_prtt_val *results_n_d,
struct ng_loggp_prtt_val *results_n_0,
struct ng_loggp_prtt_val *results_o_r,
unsigned long data_size, FILE *out, int n, int lower, int upper) {
double g, G, o_s, o_r, L;
int ielem;
g = gresults->b;
G = gresults->a;
ielem = results_n_d->elems - 1;
o_s = (results_n_d->data[ielem].value - results_1_0->data[ielem].value) / (results_n_d->n - 1) -
results_1_0->data[ielem].value /* =d */;
o_r = results_o_r->data[ielem].value;
L = results_1_0->data[0].value / 2;
printf("L=%lf ", L);
printf(" s=%i ", results_1_0->data[ielem].size);
printf(" o_s=%lf ", o_s);
printf(" o_r=%lf ", o_r);
printf(" g=%lf ", g);
printf(" G=%lf (%lf GiB/s)", G, 1 / G * 8.0 / 1024);
printf(" lsqu(g,G)=%lf ", gresults->lsquares);
if (results_n_d->d < g + G * data_size)
printf("!!! d (%lf) is smaller than g+size*G (%lf) !!!\n", results_n_d->d, g + G * data_size);
printf("\n");
}
/* a is the inner test loop from do_benchmarks ... we have to put
* a in an extra function because we need to do the whole
* benchmarkset more than once */
static int prtt_do_benchmarks(unsigned long data_size,
struct ng_loggp_tests_val *values, struct ng_loggp_prtt_val *results, char *buffer,
char o_r, MPI_Datatype dt_s, MPI_Datatype dt_c) {
/** number of times to test the current datasize */
unsigned long test_count = testcount;
/** how long does the test run? */
time_t test_time, cur_test_time;
/** number of tests run */
int test, ovr_tests, ovr_bytes;
int size_s;
int size_c;
MPI_Type_size(dt_s, &size_s);
MPI_Type_size(dt_c, &size_c);
/* initialize tests object */
values->n = results->n;
values->d = results->d;
if (!server) {
values->constructor(values, /* testcount = */ test_count, /* n =*/ values->n,
/* d = */ values->d, /* s = */ 0);
}
values->o_r = o_r;
test_time = 0;
for (test = 0; test < test_count; test++) {
if (server) {
/* execute server mode function */
be_a_server(buffer, data_size, values->n, (o_r ? 30000 : 0.0), o_r, dt_s, dt_c, size_s, size_c);
} else {
/* wait some time for the server to get ready */
usleep(10);
/* execute client mode function */
be_a_client(buffer, data_size, values, dt_s, dt_c, size_s, size_c);
}
}
if (!server) {
double res;
res = values->getmed(values);
results->addval(results, data_size, res);
values->destructor(values);
}
return 0;
}
/* the REAL benchmark loop - loops over all sizes for all three
* benchmarks (PRTT(1,0,s), PRTT(n,0,s), PRTT(n,d,s) where d=PRTT(1,0,s)
* and n is defined as const */
int loggp_do_benchmarks() {
/** size of the buffer used for transmission tests */
/** Output File */
FILE *out = NULL;
/* initialize the statistics */
int res;
/** to store the temporary results and define test parameters */
struct ng_loggp_tests_val values = {
.data= nullptr, /* data array */
.n=0, /* n values */
.s=0, /* n values */
.d=0,/* d value in microseconds */
.testc=0,
.itestc=0, /* maximal and actual testcount */
.o_r=0, /* should we measure o_r or not? - if yes, measure o_r at server and communicate it to client */
.constructor = ng_loggp_tests_val_constr,
.destructor = ng_loggp_tests_val_destr,
.addval = ng_loggp_tests_val_addval,
.getmed = ng_loggp_tests_val_getmed
};
/* stores the final results (median of tests) for n=1 and d=0 */
struct ng_loggp_prtt_val results_1_0 = {
.data= nullptr, /* data array */
.n=0, /* n values */
.d=0,/* d value in microseconds */
.elems=0,/* # of elements in the data array */
.a=0, .b=0, .lsquares=0,
.constructor = ng_loggp_prtt_val_constr,
.destructor = ng_loggp_prtt_val_destr,
.addval = ng_loggp_prtt_addval,
.getfit = ng_loggp_prtt_getfit,
.remove = ng_loggp_prtt_remove,
};
/* stores the final results (median of tests) for arbitrary n and d=0 */
struct ng_loggp_prtt_val results_n_0 = {
.data= nullptr, /* data array */
.n=0, /* n values */
.d=0,/* d value in microseconds */
.elems=0,/* # of elements in the data array */
.a=0, .b=0, .lsquares=0,
.constructor = ng_loggp_prtt_val_constr,
.destructor = ng_loggp_prtt_val_destr,
.addval = ng_loggp_prtt_addval,
.getfit = ng_loggp_prtt_getfit,
.remove = ng_loggp_prtt_remove,
};
/* stores the final results (median of tests) for arbitrary a and d */
struct ng_loggp_prtt_val results_n_d = {
.data= nullptr, /* data array */
.n=0, /* n values */
.d=0,/* d value in microseconds */
.elems=0,/* # of elements in the data array */
.a=0, .b=0, .lsquares=0,
.constructor = ng_loggp_prtt_val_constr,
.destructor = ng_loggp_prtt_val_destr,
.addval = ng_loggp_prtt_addval,
.getfit = ng_loggp_prtt_getfit,
.remove = ng_loggp_prtt_remove,
};
/* stores the o_r results - it's a bit an abuse of a data structure
* but it works conveniently */
struct ng_loggp_prtt_val results_o_r = {
.data= nullptr, /* data array */
.n=0, /* n values */
.d=0,/* d value in microseconds */
.elems=0,/* # of elements in the data array */
.a=0, .b=0, .lsquares=0,
.constructor = ng_loggp_prtt_val_constr,
.destructor = ng_loggp_prtt_val_destr,
.addval = ng_loggp_prtt_addval,
.getfit = ng_loggp_prtt_getfit,
.remove = ng_loggp_prtt_remove,
};
/* a is just a temp. object to store
* (PRTT(size,n,0)-PRTT(size,0,0))/(n-1) to fit g and G to a
* values */
struct ng_loggp_prtt_val gresults = {
.data= nullptr, /* data array */
.n=0, /* n values */
.d=0,/* d value in microseconds */
.elems=0,/* # of elements in the data array */
.a=0, .b=0, .lsquares=0,
.constructor = ng_loggp_prtt_val_constr,
.destructor = ng_loggp_prtt_val_destr,
.addval = ng_loggp_prtt_addval,
.getfit = ng_loggp_prtt_getfit,
.remove = ng_loggp_prtt_remove,
};
/* the famous n of PRTT(n,d,s) */
/** currently tested packet size */
unsigned long data_size;
/** number of times to test the current datasize */
unsigned long test_count = testcount;
/* element of last protocol change */
int lastchange = 0 /* last protocol change */;
if (loggp_prepare_benchmarks()) return 1;
char *buffer = (char *) mpi_cuda_malloc(maxbufersize);
MPI_Type_size(datatype, &total_size);
results_1_0.constructor(&results_1_0, 1, 0);
results_n_0.constructor(&results_n_0, n, 0);
results_n_d.constructor(&results_n_d, n, results_n_d.d);
results_o_r.constructor(&results_o_r, n, 0);
gresults.constructor(&gresults, 1, 0);
// pingpong(buffer);
for (int i = start/total_size; i < 150000; i = i * 2) {
fflush(stdout);
pingpong(buffer ,i);
data_size = i * total_size;
res = prtt_do_benchmarks(data_size, &values, &results_1_0, buffer, 0, datatype, datatype);
res = prtt_do_benchmarks(data_size, &values, &results_n_0, buffer, 0, datatype, datatype);
/* g needs to be fitted to: (PRTT(size,n,0)-PRTT(size,0,0))/(n-1) */
if (!server) {
/* add last measurement value to gresults */
gresults.addval(&gresults,
results_n_0.data[results_1_0.elems - 1].size,
(results_n_0.data[results_1_0.elems - 1].value -
results_1_0.data[results_1_0.elems - 1].value) / (results_n_0.n - 1));
//gresults.getfit(&gresults, lastchange, gresults.elems);
/* take the PRTT(1,0,s) as delay - a is bigger than g+G*size :) */
results_o_r.d = results_n_d.d = results_1_0.data[results_1_0.elems - 1].value;
}
/* results_o_r.d must be valid on client and server! */
MPI_Bcast(&results_o_r.d, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
/* only set a once */
res = prtt_do_benchmarks(data_size, &values, &results_n_d, buffer, 0, datatype, datatype);
/* only set a once ,datatype,MPI_CHAR);
,MPI_CHAR,datatype);*/
res = prtt_do_benchmarks(data_size, &values, &results_o_r, buffer, 1, datatype, datatype);
/* evaluate the measurement results */
if (!server) {
int ielem;
/* if lsquares-deviation of fit too high:
* remove all extreme outliers from gresults -
* an outlier is a value that is more than 2*lsquares(g,G) away from the fitted function
*/
if (gresults.lsquares > 100) {
for (ielem = 0; ielem < gresults.elems; ielem++) {
double lsquares = gresults.lsquares;
/* if a point is more than 2*lsquares above the line, it's
* probably an outlier */
if (gresults.data[ielem].value >
gresults.a * gresults.data[ielem].size + gresults.b + 2 * lsquares) {
double value = gresults.data[ielem].value;
unsigned int size = gresults.data[ielem].size;
/* remove value from elements */
gresults.remove(&gresults, ielem);
/* TODO: should we also remove it from other prtt_results ? */
gresults.getfit(&gresults, lastchange, gresults.elems);
printf("**** removed value %lf for size %u from gresults, lsquares was: %lf, new lsquares: %lf\n",
value, size, lsquares, gresults.lsquares);
}
}
}
{
const int x = 5; /* number of points to look ahead (+1) */
//for(ielem = lastchange+3 /* we need 2 elements for a fit */; ielem<gresults.elems-x-1; ielem++) {
if (lastchange + x /* look-ahead x elems */ + 2 /* we need 2 elems for fit */ <= gresults.elems) {
int ix /* runner */;
int flag = 1; /* are all bigger than f(x) + 2*lsquares ? -> 1 = yes */
double lsquares;
const double pfact = 2.0; /* wurschtel-factor */
ielem = gresults.elems - x;
/* get fit for lastchange up to current item */
//printf("getfit: %i, %i\n", lastchange, ielem);
gresults.getfit(&gresults, lastchange, ielem);
lsquares = gresults.lsquares;
/* look x elements ahead */
for (ix = ielem + 1; ix < ielem + x; ix++) {
gresults.getfit(&gresults, lastchange, ix);
/* only if all lsquares have at least doubled
* ... alles scheisse, wenn lsquares mal wirklich klein ist
* haben wir viele Protokollwechsel :-( */
if ((gresults.lsquares < pfact * lsquares) || isnan(lsquares) ||
(lsquares < 0.15) /* lower bound to prevent flapping */)
flag = 0;
}
/* if all x points are > f(x) + 2*lsquares, we have a protocol
* change, if only b < x points are larger, they are
* outliers and are removed in the next loop ... */
if (flag) {
/* we have a protocol change and the current element
* (ielem) is the last element in the old protocol */
printf("we detected a protocol change at %i bytes:\n", gresults.data[ielem].size);
gresults.getfit(&gresults, lastchange, ielem);
printparams(&gresults, &results_1_0, &results_n_d, &results_n_0, &results_o_r, data_size, out,
n, lastchange, ielem);
lastchange = ielem + 1;
/* ok, we have now a new protocol beginning at ielem + 1,
* and we need to fit the new line to the next x elements */
//ielem += x; /* ATTENTION: we change the loop-runner here */
gresults.getfit(&gresults, lastchange, gresults.elems);
}
} /* for(ielem = 0; ielem<gresults.elems ... */
gresults.getfit(&gresults, lastchange, gresults.elems);
printparams(&gresults, &results_1_0, &results_n_d, &results_n_0, &results_o_r, data_size, out, n,
lastchange, results_n_0.elems);
}
}
}
for (int j = 0; j < results_1_0.elems; ++j) {
printf("n=%i,size-%i,1_0 %15.9f,n_0 %15.9f,n_d %15.9f,o_r %15.9f,gresults %15.9f;",n,
results_1_0.data[j].size,
results_n_0.data[j].value,
results_n_d.data[j].value,
results_o_r.data[j].value, gresults.data[j].value);
}
return 0;
}
int loggp_prepare_benchmarks() {
/* only if we've got MPI */
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
server = rank == 0;
return 0;
}
static void my_wait(double d) {
auto start = std::chrono::high_resolution_clock::now();
while (d > std::chrono::duration<double, std::micro>(std::chrono::high_resolution_clock::now() - start).count());
}
int sendto(int dst, void *buffer, int size, MPI_Datatype datatype1) {
MPI_Send(buffer, size, datatype1, dst, 13, MPI_COMM_WORLD);
return 0;
}
int recvfrom(int src, void *buffer, int size, MPI_Datatype datatype1) {
MPI_Recv(buffer, size, datatype1, src, 13, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
return 0;
}
static int
be_a_server(void *buffer, int size, int n, double d, char o_r /* measure o_r? */, MPI_Datatype dt_r, MPI_Datatype dt_s,
int size_r,
int size_s) {
int in;
const int partner = 1;
if (o_r) {
recvfrom(partner, buffer, size / size_r, dt_r);
/* get start time */
auto start = std::chrono::high_resolution_clock::now();
/* Phase 1: receive data */
for (in = 0; in < n - 1; in++) {
my_wait(d);
recvfrom(partner, buffer, size / size_r, dt_r);
}
auto stop = std::chrono::high_resolution_clock::now();
sendto(partner, buffer, size / size_s, dt_s);
double val;
double duration = std::chrono::duration<double, std::micro>(stop - start).count();
val = (duration - d * (n - 1)) / (n - 1);
MPI_Send(&val, 1, MPI_DOUBLE, partner, 11, MPI_COMM_WORLD);
} else {
recvfrom(partner, buffer, size / size_r, dt_r);
for (in = 0; in < n - 1; in++) {
recvfrom(partner, buffer, size / size_r, dt_r);
}
sendto(partner, buffer, size / size_s, dt_s);
}
return 0;
}
static int
be_a_client(void *buffer, int size, struct ng_loggp_tests_val *values, MPI_Datatype dt_r, MPI_Datatype dt_s, int size_r,
int size_s) {
int in;
const int partner = 0;
auto start = std::chrono::high_resolution_clock::now();
sendto(partner, buffer, size / size_s, dt_s);
for (in = 0; in < values->n - 1; in++) {
my_wait(values->d);
sendto(partner, buffer, size / size_s, dt_s);
}
/* Phase 2: receive returned data */
recvfrom(partner, buffer, size / size_r, dt_r);
/* get after-receiving time */
auto stop = std::chrono::high_resolution_clock::now();
/* calculate results */
double duration = std::chrono::duration<double, std::micro>(stop - start).count();
if (values->o_r) { /* benchmark o_r */
double val;
MPI_Recv(&val, 1, MPI_DOUBLE, partner, 11, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
values->addval(values, val);
} else {
values->addval(values, duration);
}