-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpapi_wrap.cpp
1177 lines (1095 loc) · 38.7 KB
/
papi_wrap.cpp
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 <mpi.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <zconf.h>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <papi.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <string>
#include <cassert>
#include <algorithm>
#include <execinfo.h> // gcc-version backtrace
#include <json/json.h>
//#define UNW_LOCAL_ONLY
//#include <libunwind.h> // libunwind-version backtrace
#include "papi_wrap.h"
#include "clustering.h"
#include "utility.h"
#include <linux/perf_event.h>
// gettimeofday
//#include<sys/time.h>
#include <unistd.h>
#include <sstream>
#include <syscall.h>
#include <sys/ioctl.h>
#include "online_analyse.h"
#include "SocketClient.h"
// PMU, rdtsc
#include "pmu.h"
#include "pmc_wrapper.h"
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
using namespace std;
const long long Cpu_freq = CPU_FREQ;
// using DataType=std::vector<long long>;
// using GraphKey=std::pair<void*,void*>;
// using GraphValue=std::vector<DataType >;
int events[6] = {PAPI_TOT_INS, PAPI_L1_TCM, PAPI_L2_TCM, PAPI_BR_MSP, PAPI_VEC_DP}; //, PAPI_SR_INS}; //, PAPI_LD_INS, PAPI_L3_TCA};
const int J_MPI_INIT = 0;
const int J_MPI_BARRIER = 23;
const int J_MPI_WAIT = 317;
const int J_MPI_COMM_RANK = 61;
const int J_MPI_COMM_SIZE = 68;
const int J_MPI_WTIME = 359;
const int J_MPI_WAITANY = 319;
const int J_MPI_WAITALL = 318;
const int J_MPI_FILE_WRITE_AT_ALL = 138;
const int J_MPI_FILE_READ_AT_ALL = 118;
const int J_MPI_ALLREDUCE = 16;
const int J_MPI_FILE_READ_AT = 117;
const int J_MPI_FILE_WRITE_AT = 137;
const int J_MPI_Irecv = 4;
const int J_MPI_Iporbe = 208;
const int J_MPI_Testall = 271;
const int J_DYNINST = 999; // Dyninst instrumentation
const set<int> FILTER_OUT_MPI = {J_MPI_INIT, J_MPI_BARRIER, J_MPI_WAIT,
J_MPI_COMM_RANK, J_MPI_COMM_SIZE, J_MPI_WTIME,
J_MPI_WAITANY, J_MPI_WAITALL, J_MPI_Irecv,
J_MPI_Iporbe, J_MPI_Testall};
// const set<int> FILTER_OUT_MPI = {J_MPI_INIT, J_MPI_BARRIER, J_MPI_WAIT, J_MPI_COMM_RANK, J_MPI_COMM_SIZE, J_MPI_WTIME};
const set<int> IO_MPI = {J_MPI_FILE_READ_AT_ALL, J_MPI_FILE_WRITE_AT_ALL, J_MPI_FILE_READ_AT, J_MPI_FILE_WRITE_AT};
struct SummaryType
{
double total_run_time;
double covered_calc_time, covered_comm_time;
int edge_cnt, vertex_cnt;
/**
*
* @return String of a json with line feed
*/
string generate_summary_json()
{
Json::Value output;
ostringstream os;
output["total_run_time"] = total_run_time;
output["covered_calc_time"] = covered_calc_time;
output["covered_comm_time"] = covered_comm_time;
output["calc_covered_ratio"] = covered_calc_time / total_run_time;
output["comm_covered_ratio"] = covered_comm_time / total_run_time;
output["total_covered_ratio"] = (covered_calc_time + covered_comm_time) / total_run_time;
output["edge_cnt"] = edge_cnt;
output["vertex_cnt"] = vertex_cnt;
Json::FastWriter fast_writer;
os << fast_writer.write(output);
return os.str();
}
} summary;
static void *last_call_addr = nullptr;
int cnt_address;
map<void *, int> address_map;
map<void *, int> address_to_func;
map<void *, DataType> last_data_map;
Graph graph_calc, graph_comm, graph_io;
// counter of clustering result
map<GraphKey, int> cluster_cnt;
bool init_flag = false;
// finish flag is necessary because of the occurrence of hooked function in print_graph
bool finish_flag = false;
// ignore recursively invoked MPI function. For instance, PMPI_File_set_view
// invokes MPI_Type_size_x.
bool in_mpi_flag = false;
int recursive_depth = 0;
// switch for online analysis
constexpr bool online_analyze_flag = false;
// switch for analysis server
constexpr bool online_server_flag = false;
// switch for sampling
//#define ENABLE_SAMPLING
constexpr bool sampling_flag = false;
double sampling_rate = 0.01;
int mpi_rank = -1, mpi_size = -1;
VertexType vertex_type = VertexType::CallSite;
// online anaylsis
Graph graph_last_calc, graph_last_comm, graph_last_io;
int env_vapro_online_period = 5; // unit: second
// server analysis
int env_vapro_server_period = 15; // unit: second
SocketClient *socketClient;
char *server_addr;
int server_port;
unsigned long long init_time;
unsigned long long last_time;
// unsigned long long last_totcycle = 0;
vector<ULL> last_pmcs(CNT_PAPI_EVENTS, 0);
void pmc_enable()
{
init_time = rdtsc();
// wrapper for pmc API
pmc_enable_real();
// get VERTEX_GRAIN
char *vertex_path_str = getenv("VERTEX_GRAIN");
int vertex_path_int = (vertex_path_str == 0) ? VertexType::CallSite : atoi(vertex_path_str);
assert(!(vertex_path_int < 0 || vertex_path_int > 2));
if (vertex_path_int < 0 || vertex_path_int > 2)
{
fprintf(stderr, "vertex path environment variable error");
exit(-1);
}
else
vertex_type = (VertexType)vertex_path_int;
init_flag = true;
}
/**
* This function has side effect. It changes last_time and last_totcycle.
* @param cur_time
* @return
*/
inline DataType pmc_get_data(ULL cur_time)
{
auto new_pmcs = pmc_read_real();
ULL diff_pmcs[CNT_PAPI_EVENTS];
for (int i = 0; i < CNT_PAPI_EVENTS; ++i)
diff_pmcs[i] = new_pmcs[i] - last_pmcs[i];
DataType retv;
retv.set_papi_data(diff_pmcs);
retv.elapsed = cur_time - last_time; // append wall time to locate this record
retv.timestamp = cur_time;
last_time = cur_time;
last_pmcs = std::move(new_pmcs);
return retv;
}
void vapro_init()
{
PMPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
PMPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
// DEBUG
// char *env_vapro_online_period = getenv("VAPRO_ONLINE_PERIOD");
if (mpi_rank == 0)
{
fprintf(stderr, "Shared library loaded..\n");
fprintf(stderr, "INFO: vertex_path_int = %d\n", vertex_type);
}
if (online_analyze_flag)
{
if (mpi_rank == 0)
fprintf(stderr, "INFO: online mode ON. period=%d\n", env_vapro_online_period);
online_anaylze_init();
}
else if (online_server_flag)
{
server_addr = getenv("VAPRO_SERVER_IP");
if (!server_addr)
{
fprintf(stderr, "VAPRO_SERVER_IP is not defined.\n");
exit(-1);
}
const char *vapro_server_port = getenv("VAPRO_SERVER_PORT");
if (!vapro_server_port)
{
fprintf(stderr, "VAPRO_SERVER_PORT is not defined.\n");
exit(-1);
}
server_port = atoi(vapro_server_port);
if (mpi_rank == 0)
{
fprintf(stderr, "INFO: online server ON. period=%d. Server=%s:%d\n",
env_vapro_server_period, server_addr, server_port);
}
socketClient = new SocketClient(server_addr, server_port);
}
else
{
if (mpi_rank == 0)
fprintf(stderr, "INFO: online mode OFF.\n");
}
}
void insert_data(map<GraphKey, GraphValue> &graph, void *last_addr, void *current_addr, DataType &papi_data)
{
if (!address_map.count(current_addr))
{
address_map[current_addr] = cnt_address++;
address_to_func[current_addr] = papi_data.mpi_func;
}
if (!address_map.count(last_addr))
{
address_map[last_addr] = cnt_address++;
}
auto key = make_pair(last_addr, current_addr);
if (!graph.count(key))
graph[key] = GraphValue();
graph[key].push_back(papi_data);
// // differentiate to calculate self loop data
// for (int i = 0; i < CNT_PAPI_EVENTS+1; ++i)
// {
// accumulate_data[i] += papi_data[i];
// }
//
// // insert self loop value
// if (last_addr != current_addr && last_data_map.count(current_addr))
// {
// DataType self_loop_data{accumulate_data};
// for (int i = 0; i < CNT_PAPI_EVENTS+1; ++i)
// self_loop_data[i] -= last_data_map[current_addr][i];
// key = make_pair(current_addr, current_addr);
// if (!graph.count(key))
// graph[key] = GraphValue();
// self_loop_data.insert(self_loop_data.end(), mpi_data.begin(), mpi_data.end());
// if (self_loop_data.size()<all_data.size())
// self_loop_data.resize(all_data.size());
// graph[key].push_back(self_loop_data);
// }
//
// // update last call data for the current call point
// last_data_map[current_addr] = accumulate_data;
}
void *backtrace_buffer[10];
int trace_size;
void output_backtrace()
{
puts("=====");
backtrace_symbols_fd(backtrace_buffer, trace_size, 1);
}
__attribute_noinline__ void *get_invoke_point()
{
// This compiler function(MACRO?) have some strange problems
// return __builtin_return_address(2);
// constexpr int size = 5;
// todo: revert
static bool a = vertex_type == VertexType::CallSite;
// static int cnt=0; //Debug
if (likely(a))
// if (false)
{
// Upd1: UNROLL the loop. Loop may introduce 50% overhead. Unrolling can reduce it to 10%.
// // Irregualr stack
// for (int i = 1; i < trace_size; ++i)
// if (((unsigned long long)backtrace_buffer[i]) < 0x1d627d61)
// return backtrace_buffer[i];
return backtrace_buffer[1]; // for HPL and other apps
// return backtrace_buffer[2]; // for AMG
}
else if (vertex_type == VertexType::CallPath)
{
return (void *)hash_sequence(trace_size, backtrace_buffer);
}
// printf("==ERROR===\n");
// for (int j = 0; j < trace_size; ++j)
// {
// printf("d%d=%lx ch=%d\n", j, (unsigned long) buffer[j],
// ((unsigned long long) buffer[j] < 0x00645cbdeb8eLL));
// }
// printf("=====\n");
output_backtrace();
fprintf(stderr, "Error: Backtrace do not find EXE SEG call stack\n");
assert(0);
exit(1);
}
/**
* @param suffix should be 0 or 1. 0 is before MPI invocation and 1 is after.
*/
__attribute_noinline__ void papi_update(int suffix, int mpi_func, int count, int target, void *mpi_comm)
{
if (!init_flag)
{
// #ifdef USE_PAPI
// papi_init();
// #else
pmc_enable();
// #endif
// fprintf(stderr, "INFO: start online init, rank=%d\n", mpi_rank);
int inited = 0;
MPI_Initialized(&inited);
// if (inited && suffix==0)
// The wrapper of MPI_Init has only suffix 1
if (inited)
vapro_init();
else
return;
if (mpi_rank == 0)
fprintf(stderr, "INFO: Vapro init, rank=%d\n", mpi_rank);
}
// if finished
if (finish_flag)
return;
// Get PMU data ASAP to exclude Vapro noise
unsigned long long current_tsc = rdtsc();
auto papi_data = pmc_get_data(current_tsc);
#ifdef USE_RUSAGE
papi_data.set_rusage_data(get_rusage_data().data());
#endif
papi_data.mpi_func = mpi_func;
papi_data.mpi_count = count;
papi_data.target = target;
papi_data.mpi_comm = mpi_comm;
// skip in recursive mpi invocations
// There might be recursive in MPI libraries.
if (in_mpi_flag && suffix == 0)
{
recursive_depth++;
return;
}
if (recursive_depth > 0 && suffix == 1)
{
recursive_depth--;
return;
}
in_mpi_flag = (suffix == 0);
// online analysis
static unsigned long long last_online_tsc = current_tsc;
static int num_iters = 0;
if (online_analyze_flag && init_flag)
{
if (current_tsc > last_online_tsc +
(unsigned long)(env_vapro_online_period * CPU_FREQ))
{
if (num_iters)
{
online_analyze(current_tsc, last_online_tsc);
}
online_irecv_all();
online_isend_all(graph_calc, graph_comm, graph_io, last_online_tsc,
current_tsc);
graph_last_calc = std::move(graph_calc);
graph_last_comm = std::move(graph_comm);
graph_last_io = std::move(graph_io);
graph_calc.clear();
graph_comm.clear();
graph_io.clear();
last_online_tsc = current_tsc;
++num_iters;
}
}
// server analysis
if (online_server_flag && init_flag)
{
if (current_tsc > last_online_tsc +
(unsigned long)(env_vapro_server_period * CPU_FREQ))
{
socketClient->serialize(mpi_rank, last_online_tsc - init_time,
current_tsc - init_time, graph_calc,
graph_comm, graph_io);
graph_calc.clear();
graph_comm.clear();
graph_io.clear();
socketClient->send();
last_online_tsc = current_tsc;
}
}
long long real_addr;
if (suffix == 1 && last_call_addr)
{
real_addr = (long long)last_call_addr;
}
else
{
if (mpi_func == J_DYNINST)
real_addr = -target;
else
real_addr = (vertex_type == VertexType::Function) ? mpi_func : (long long)get_invoke_point();
}
void *current_call_addr = (void *)(real_addr);
#ifdef ENABLE_SAMPLING
if (sampling_flag && (double(rand_longlong()) > sampling_rate * ULLONG_MAX))
#endif
if (!(suffix == 1 && FILTER_OUT_MPI.count(mpi_func)))
{
if (suffix == 0)
{
if (!(last_call_addr == current_call_addr &&
FILTER_OUT_MPI.count(mpi_func)))
{
insert_data(graph_calc, last_call_addr, current_call_addr,
papi_data);
}
}
else
{
if ((IO_MPI.count(mpi_func)))
{
insert_data(graph_io,
last_call_addr, current_call_addr, papi_data);
}
else
{
// TODO: collective online
insert_data(graph_comm,
last_call_addr, current_call_addr, papi_data);
}
}
}
last_call_addr = current_call_addr;
// reset base counter
last_time = rdtsc();
// #ifdef USE_PAPI
// papi_get_data(last_time); // reset PAPI counter
// #else
// last_totcycle = rdpmc_instructions();
// #ifndef USE_ASSEMBLY_RDPMC
// pmc_reset();
// #endif
// #endif
last_pmcs = pmc_read_real();
last_time = rdtsc();
}
void *get_addr_from_mixture(void *p)
{
return (void *)(((long)p) >> 1);
}
int get_status_from_mixture(void *p)
{
return (int)(((long)p) & 1);
}
struct Interval
{
double l, r;
double perf;
};
typedef vector<Interval> PathPerformance;
/**
* Calculate relative performance from snippets records
* @param path_data
* @param smooth: enable smoothing or not
* @return
*/
PathPerformance calc_path(GraphValue &path_data, bool smooth)
{
unsigned long long min_elapsed = ULONG_LONG_MAX;
PathPerformance ret;
Interval interval;
const int window = 16; // window size of smoothing
vector<ULL> smoothed;
ULL sum = 0;
// if size is enough, smooth the results
if (path_data.size() > window && smooth)
{
for (int i = 0; i < window; ++i)
{
sum += path_data[i].elapsed;
}
smoothed.push_back(sum);
for (auto i = window; i < path_data.size(); ++i)
{
sum += path_data[i].elapsed - path_data[i - window].elapsed;
smoothed.push_back(sum);
}
for (auto &v : smoothed)
min_elapsed = min(v, min_elapsed);
for (int i = 0; i < path_data.size() - window; ++i)
{
const auto &value = path_data[i];
interval.perf = min_elapsed / double(smoothed[i]);
interval.l = double(value.timestamp - value.elapsed - init_time) /
CPU_FREQ;
interval.r = double(value.timestamp - init_time) / CPU_FREQ;
ret.push_back(interval);
}
}
else
{
for (const auto &value : path_data)
min_elapsed = (min_elapsed <= value.elapsed) ? min_elapsed : value.elapsed;
for (const auto &value : path_data)
{
interval.perf = min_elapsed / double(value.elapsed);
interval.l = double(value.timestamp - value.elapsed - init_time) / CPU_FREQ;
interval.r = double(value.timestamp - init_time) / CPU_FREQ;
// // debug
// if (interval.l<1&& interval.r>1)
// {
// for (auto t:path_data)
// printf("Warn: %d %d %lf %lf %d %d\n", t.mpi_func, t.mpi_count, interval.l, interval.r, t.src, t.dst);
// }
ret.push_back(interval);
}
}
return ret;
}
bool online_is_same_calc_cluster(double mean_cycles, double remote_mean_cycles)
{
return (1 - 0.02) * mean_cycles < remote_mean_cycles &&
remote_mean_cycles < (1 + 0.02) * mean_cycles;
}
bool online_is_same_comm_cluster(double mpi_count, double remote_mpi_count)
{
return fabs(remote_mpi_count - mpi_count) < eps;
}
PathPerformance online_calc_path(bool is_calc, GraphValue &path_data,
vector<pair<double, double>> &interprocess_min_time,
bool enable_output = false)
{
unsigned long long min_elapsed = LONG_LONG_MAX;
PathPerformance ret;
Interval interval;
double mean_cycles = 0;
int cnt_interprocess_make_effective = 0; // debug
for (const auto &value : path_data)
{
min_elapsed = (min_elapsed <= value.elapsed) ? min_elapsed
: value.elapsed;
mean_cycles += value.papi[I_PAPI_TOT_INS];
}
assert(path_data.size());
mean_cycles /= path_data.size();
ULL local_min_elapsed = min_elapsed;
// // debug
// if (mpi_rank == 1)
// {
// fprintf(stderr, "local path_mean=%.3lf min_time=%.3lf\n", mean_cycles,
// min_elapsed / CPU_FREQ);
// for (auto &tt:interprocess_min_time)
// {
// fprintf(stderr, "gloabl mean=%.3lf min_time=%.3lf\n", tt.first, tt.second);
// }
// }
for (const auto &record : interprocess_min_time)
{
if ((is_calc &&
online_is_same_calc_cluster(mean_cycles, record.first)) ||
(!is_calc &&
online_is_same_comm_cluster(path_data[0].mpi_count, record.first)))
{
if (min_elapsed > (unsigned long long)(record.second * CPU_FREQ))
{
++cnt_interprocess_make_effective;
min_elapsed = (unsigned long long)(record.second * CPU_FREQ);
}
}
}
// debug
if (mpi_rank == 0 && enable_output)
fprintf(stderr,
"DEBUG: among %lu cnt_interprocess_make_effective=%d lt=%llu gt=%llu\n",
interprocess_min_time.size(), cnt_interprocess_make_effective,
local_min_elapsed, min_elapsed);
for (const auto &value : path_data)
{
interval.perf = min_elapsed / double(value.elapsed);
interval.l =
double(value.timestamp - value.elapsed - init_time) / CPU_FREQ;
interval.r = double(value.timestamp - init_time) / CPU_FREQ;
// // debug
// if (interval.l<1&& interval.r>1)
// {
// for (auto t:path_data)
// printf("Warn: %d %d %lf %lf %d %d\n", t.mpi_func, t.mpi_count, interval.l, interval.r, t.src, t.dst);
// }
ret.push_back(interval);
}
return ret;
}
bool path_data_adequate(const GraphValue &path_data)
{
return path_data.size() >= 5;
}
bool path_stable(const GraphValue &path_data)
{
const double variance = 0.04;
long long inst_min = LONG_LONG_MAX, inst_max = 0;
for_each(path_data.begin(), path_data.end(),
[&inst_min, &inst_max](const DataType &data)
{
inst_min = min(inst_min, data.papi[I_PAPI_TOT_INS]);
inst_max = max(inst_max, data.papi[I_PAPI_TOT_INS]);
});
return (inst_max - inst_min) / (double(inst_max + inst_min) / 2) < variance;
}
vector<double> merge_interval_arithmetic_mean(vector<Interval> &intervals, int l, int r)
{
sort(intervals.begin(), intervals.end(), [](Interval a, Interval b)
{ return a.l < b.l; });
// debug
// printf("intervals len=%lu\n", intervals.size());
// for_each(intervals.begin(), intervals.end(), [](Interval interval) {
// printf("%lf %lf %lf\n", interval.l, interval.r, interval.perf);
// });
double sum = 0;
int cnt = 0;
auto rit = intervals.cbegin();
vector<double> ret;
vector<int> rank_r;
int lit = 0;
for (int i = 0; i < intervals.size(); ++i)
rank_r.push_back(i);
sort(ret.begin(), ret.end(), [&intervals](int a, int b)
{ return intervals[a].r < intervals[b].r; });
for (int i = l; i < r; ++i)
{
// // debug
// printf("=================i=%d========\n", i);
// rit is iterator of the interval whose l is the largest
while (rit != intervals.cend() && rit->l <= i + 1)
{
++cnt;
sum += rit->perf;
if (i == 0)
printf("+ %lf %lf\n", rit->l, rit->r);
++rit;
}
while (intervals[rank_r[lit]].r < i)
{
--cnt;
sum -= intervals[rank_r[lit]].perf;
if (i == 1)
printf("- %lf %lf\n", intervals[rank_r[lit]].l, intervals[rank_r[lit]].r);
++lit;
}
printf("i=%2d cnt=%d\n", i, cnt);
if (cnt)
ret.push_back(sum / cnt);
else // -1 means this interval is not covered
ret.push_back(-0.00001);
}
return ret;
}
vector<double> merge_interval_weighted_mean(vector<Interval> &intervals, int l, int r, double step = 1)
{
sort(intervals.begin(), intervals.end(), [](Interval a, Interval b)
{ return a.l < b.l; });
// // debug
// if (mpi_rank == 1)
// {
// printf("l=%d r=%d\n", l, r);
// printf("intervals len=%lu\n", intervals.size());
// for_each(intervals.begin(), intervals.end(), [](Interval interval) {
// printf("%lf %lf %lf\n", interval.l, interval.r, interval.perf);
// });
// }
double sum = 0, partial_sum = 0, partial_cnt = 0;
int cnt = 0;
auto rit = intervals.cbegin();
vector<double> ret;
vector<int> rank_r;
int lit = 0;
for (int i = 0; i < intervals.size(); ++i)
rank_r.push_back(i);
sort(ret.begin(), ret.end(), [&intervals](int a, int b)
{ return intervals[a].r < intervals[b].r; });
for (double i = l; i < r; i += step)
{
// // debug
// printf("=================i=%d========\n", i);
partial_cnt = partial_sum = 0;
// rit is iterator of the interval whose l is the largest
while (rit != intervals.cend() && rit->l <= i + step)
{
++cnt;
sum += rit->perf;
partial_sum -= rit->perf * (rit->l - i);
partial_cnt -= (rit->l - i);
// if (i == 0)
// printf("+ %lf %lf\n", rit->l, rit->r);
++rit;
}
while (lit < intervals.size() && intervals[rank_r[lit]].r < i + step)
{
--cnt;
sum -= intervals[rank_r[lit]].perf;
partial_sum += intervals[rank_r[lit]].perf * (intervals[rank_r[lit]].r - i);
partial_cnt += (intervals[rank_r[lit]].r - i);
// if (i == 1)
// printf("- %lf %lf\n", intervals[rank_r[lit]].l, intervals[rank_r[lit]].r);
++lit;
}
// printf("i=%2d cnt=%d\n", i, cnt);
if (fabs(cnt * step + partial_cnt) >= eps)
{
ret.push_back((sum * step + partial_sum) / (cnt * step + partial_cnt));
}
else
{ // -1 means this interval is not covered
ret.push_back(-0.005);
}
}
return ret;
}
/**
* Snippet graph to time-based process relative performance
* @param graph
* @param time_l
* @param time_r
* @return
*/
vector<double> calc_performance_process(Graph &graph, int time_l, int time_r)
{
vector<Interval> all_intervals;
vector<pair<double, double>> *interprocess_min_time = nullptr;
for (auto &kv : graph)
{
vector<vector<DataType>> classified = calc_classify(kv.second, 0.02);
int cnt_stable = 0, cnt_total = 0;
// vector<vector<DataType>> classified = calc_classify_fake(kv.second, 0.02);
if (online_analyze_flag && interprocess_info.count(kv.first))
interprocess_min_time = &interprocess_info[kv.first];
else
interprocess_min_time = nullptr;
for (auto &v : classified)
{
if (path_data_adequate(v) && path_stable(v))
{
cnt_stable++;
PathPerformance new_intervals;
if (interprocess_min_time)
new_intervals = online_calc_path(true, v, *interprocess_min_time);
else
new_intervals = calc_path(v, false);
// statistical data
for (auto &vv : new_intervals)
{
summary.covered_calc_time += vv.r - vv.l;
}
all_intervals.insert(all_intervals.end(), new_intervals.begin(), new_intervals.end());
}
cnt_total++;
}
// to output clustering counter
cluster_cnt[kv.first] = cnt_stable;
}
// printf("all_interval.size=%lu stable=%d total=%d\n", all_intervals.size(), cnt_stable, cnt_total);
auto ret = merge_interval_weighted_mean(all_intervals, time_l, time_r);
return ret;
}
Json::Value commbreakdownOutput;
void dump_addr_interval_map(
const std::map<pair<void *, void *>, vector<PathPerformance>> &data)
{
Json::Value output;
for (const auto &kv : data)
{
Json::Value key;
Json::Value value;
key.append((Json::UInt64)kv.first.first);
key.append((Json::UInt64)kv.first.second);
for (const auto &v : kv.second)
{
Json::Value pathPerformance;
for (auto vv : v)
{
Json::Value interval;
interval.append(vv.l);
interval.append(vv.r);
interval.append(vv.perf);
pathPerformance.append(interval);
}
value.append(pathPerformance);
}
output.append(key);
output.append(value);
}
commbreakdownOutput.append(output);
}
/**
* Output commbreakdownOutput to file
* @param filename_prefix
*/
void dump_addr_interval_map_to_file(string filename_prefix)
{
string filename = filename_prefix + std::to_string((long long)mpi_rank) +
string(".txt");
ofstream os;
os.open(filename.c_str());
Json::FastWriter fast_writer;
os << fast_writer.write(commbreakdownOutput);
commbreakdownOutput.clear();
os.close();
}
vector<double> comm_performance_process(Graph &graph, int time_l, int time_r,
bool enable_output = false)
{
vector<Interval> all_intervals;
vector<pair<double, double>> *interprocess_min_time = nullptr;
std::map<pair<void *, void *>, vector<PathPerformance>> debug_data;
for (auto &kv : graph)
{
vector<vector<DataType>> classified = comm_classify(kv.second);
// vector<vector<DataType>> classified = comm_classify_fake(kv.second);
unsigned long newlen = 0;
int cnt_stable = 0, cnt_total = 0;
debug_data[kv.first] = vector<PathPerformance>();
if (online_analyze_flag && interprocess_info.count(kv.first))
interprocess_min_time = &interprocess_info[kv.first];
else
interprocess_min_time = nullptr;
// debug
if (mpi_rank == 0 && enable_output)
{
printf("IO %lu classes, total interprocess size=%lu, this snippet %lu, key=%p,%p\n",
classified.size(),
interprocess_info.size(),
interprocess_min_time ? interprocess_min_time->size() : -1,
kv.first.first, kv.first.second);
}
for (auto &v : classified)
{
if (mpi_rank == 0 && enable_output)
{
printf("IO class has %lu eles\n", v.size());
}
if (path_data_adequate(v))
{
// debug: print timing to be calced in python
// if (mpi_rank==0) print_timing_for_python(v);
cnt_stable++;
PathPerformance new_intervals;
if (interprocess_min_time)
new_intervals = online_calc_path(false, v, *interprocess_min_time,
enable_output);
else
new_intervals = calc_path(v, true);
// statistical data
for (auto &vv : new_intervals)
{
summary.covered_comm_time += vv.r - vv.l;
newlen++;
}
all_intervals.insert(all_intervals.end(), new_intervals.begin(),
new_intervals.end());
// TODO: DEBUG dump {kv:interval}
debug_data[kv.first].push_back(new_intervals);
// printf("BD: %d")
}
cnt_total++;
}
assert(kv.second.size() >= newlen);
cluster_cnt[kv.first] = cnt_stable;
}
auto ret = merge_interval_weighted_mean(all_intervals, time_l, time_r);
return ret;
}
void print_graph_json(const Graph &graph, int rank, const string &filename_prefix)
{
// debug
double sum_comm_time = 0;
finish_flag = true;
Json::Value output;
string filename = filename_prefix + std::to_string((long long)rank) + string(".txt");
ofstream os;
os.open(filename.c_str());
for (const auto &item : graph)
{
Json::Value list;
// cout << get_addr_from_mixture(item.first.first) << " " <<
// get_status_from_mixture(item.first.first) << " " <<
// get_addr_from_mixture(item.first.second) << " " <<
// get_status_from_mixture(item.first.second) << " " <<
list["info"].append((Json::UInt64)item.first.first);
list["info"].append((Json::UInt64)item.first.second);
// number of clusters with corresponding snippet
list["ccnt"] = cluster_cnt[item.first];
vector<uint64_t> temp_sum(6, 0);
for (const auto &data : item.second)
{
// for (auto value:data)
// one_call.append((uint64_t)value);
Json::Value one_call;
// only place TOT_INST at the beginning
one_call.append((Json::UInt64)data.papi[0]);
one_call.append(data.mpi_func);
one_call.append(data.mpi_count);
one_call.append(data.target);
// debug
one_call.append("T");
one_call.append(double(data.timestamp - init_time) / CPU_FREQ);
if (data.mpi_func == J_MPI_INIT)
one_call.append(-0.00001);
else
{
one_call.append(double(data.elapsed) / CPU_FREQ);
sum_comm_time += double(data.elapsed) / CPU_FREQ;
}
// append other PMU data to the end
for (int i = 1; i < CNT_TOTAL_EVENTS; ++i)
one_call.append((Json::UInt64)data.papi[i]);
list["value"].append(one_call);
}
output["edge"].append(list);
}
Json::FastWriter fast_writer;
os << fast_writer.write(output);
os.close();
}
void print_function_cnt_to_file(FILE *f)
{
map<void *, int> cnt_map;
for (auto kv : graph_comm)
{
// CallPath address(hash value) must be different
if (vertex_type != VertexType::CallPath &&
!(kv.first.first == 0 ||
((long long)kv.first.first) == ((long long)kv.first.second)))
{