-
Notifications
You must be signed in to change notification settings - Fork 3
/
liblsb.cpp
1220 lines (1071 loc) · 35.6 KB
/
liblsb.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
/*
* Copyright (c) 2015 ETH-Zurich. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <cerrno>
#include "config.h"
#include "liblsb_internal.hpp"
#ifdef HAVE_MPI
#include <mpi.h>
#endif
#ifdef HAVE_HRTIMER
#include "hrtimer/hrtimer.h"
#endif
#include <algorithm>
LSB_Data *lsb_data;
#ifdef HAVE_LIKWID
std::vector<long long> PMvalues(10);
#endif
#ifdef HAVE_HRTIMER
uint64_t liblsb_g_timerfreq;
double liblsb_g_hrtimer_startvalue;
#endif
/**
* end the current epoch and start a new one
* same as LSB_Rec() but doesn't record anything -> all measurements will be lost
*/
void LSB_Res() {
CHK_DISABLED;
#ifdef _OPENMP
#pragma omp master
{
#endif
#if defined HAVE_PAPI
std::vector<long long> values(2);
PAPI_stop(lsb_data->eventset, &values[0]);
lsb_data->tlast = PAPI_get_real_usec();
PAPI_start(lsb_data->eventset);
#elif defined HAVE_LIKWID
int cpu_id;
cpu_id = likwid_getProcessorId();
PMvalues[0] = msr_read(cpu_id, MSR_AMD15_PMC0);
PMvalues[1] = msr_read(cpu_id, MSR_AMD15_PMC1);
PMvalues[2] = msr_read(cpu_id, MSR_AMD15_PMC2);
PMvalues[3] = msr_read(cpu_id, MSR_AMD15_PMC3);
PMvalues[4] = msr_read(cpu_id, MSR_AMD15_PMC4);
PMvalues[5] = msr_read(cpu_id, MSR_AMD15_PMC5);
PMvalues[6] = msr_read(cpu_id, MSR_AMD15_NB_PMC0);
PMvalues[7] = msr_read(cpu_id, MSR_AMD15_NB_PMC1);
PMvalues[8] = msr_read(cpu_id, MSR_AMD15_NB_PMC2);
PMvalues[9] = msr_read(cpu_id, MSR_AMD15_NB_PMC3);
unsigned long long ticks;
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
lsb_data->tlast = HRT_GET_USEC(ticks);
#elif defined HAVE_HRTIMER
unsigned long long ticks;
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
lsb_data->tlast = HRT_GET_USEC(ticks);
#else
#error "No possibility to do accurate timing!\n"
#endif
#ifdef _OPENMP
}
#endif
}
double LSB_Wait(double microseconds){
double start, current;
#ifdef _OPENMP
#pragma omp master
{
#endif
unsigned long long ticks;
#if defined HAVE_PAPI
start = PAPI_get_real_usec();
#else
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
start = HRT_GET_USEC(ticks);
#endif
current = start;
while (current-start<microseconds){
#if defined HAVE_PAPI
current = PAPI_get_real_usec();
#else
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
current = HRT_GET_USEC(ticks);
#endif
}
#ifdef _OPENMP
}
#endif
return current - start;
}
/*
void LSB_Rec_ints(unsigned int id, int int1, int int2) {
CHK_DISABLED;
#ifdef _OPENMP
#pragma omp master
{
#endif
t_recint rec = {id,int1,int2};
if(lsb_data->rec_enabled) lsb_data->recints.push_back(rec);
#ifdef _OPENMP
}
#endif
}
void LSB_Rec_intdbl(unsigned int id, int int1, double dbl) {
CHK_DISABLED;
#ifdef _OPENMP
#pragma omp master
{
#endif
t_recintdbl rec = {id,int1,dbl};
if(lsb_data->rec_enabled) lsb_data->recintdbl.push_back(rec);
#ifdef _OPENMP
}
#endif
}
*/
void LSB_Fold(unsigned int id, lsb_op_t op, double * result){
#ifdef _OPENMP
#pragma omp master
{
#endif
double res=-1;
std::vector<double> measures;
int j=0;
for (int i=0; i<lsb_data->recs.size(); i++){
if (lsb_data->recs[i].id != id) continue;
switch (op){
case LSB_SUM:
res+=lsb_data->recs[i].tduration;
break;
case LSB_COUNT:
res++;
break;
case LSB_MEDIAN:
measures.push_back(lsb_data->recs[i].tduration);
break;
case LSB_MAX:
if (i==0 || res<lsb_data->recs[i].tduration)
res = lsb_data->recs[i].tduration;
break;
case LSB_MIN:
if (i==0 || res>lsb_data->recs[i].tduration)
res = lsb_data->recs[i].tduration;
break;
}
}
if (op==LSB_MEDIAN && measures.size()>0){
std::sort(measures.begin(), measures.end());
//for (int j=0; j<measures.size(); j++) printf("%i: %f\n", j, measures[j]);
res = measures[measures.size()/2];
//printf("returning res: %lf; size: %i; measures[0]: %lf; size/2: %i; measures[size-1]: %lf\n", res, measures.size(), measures[0], measures.size()/2, measures[measures.size()-1]);
}
*result = res;
#ifdef _OPENMP
}
#endif
}
double LSB_Rec(unsigned int id){
double res=0;
#ifdef _OPENMP
#pragma omp master
{
#endif
res = LSB_Stop(id, 1);
lsb_data->next = lsb_data->recs.size();
#ifdef _OPENMP
}
#endif
return res;
}
void LSB_Next(){
#ifdef _OPENMP
#pragma omp master
{
#endif
lsb_data->next = lsb_data->recs.size();
#ifdef _OPENMP
}
#endif
}
double LSB_Check(unsigned int id){
double res=0;
#ifdef _OPENMP
#pragma omp master
{
#endif
res = LSB_Stop(id, 0);
lsb_data->next = lsb_data->recs.size();
#ifdef _OPENMP
}
#endif
return res;
}
/**
* write a tracing record
* this ends the epoch with the specified id and opens a new epoch
* @param id user-defined kernel id
*/
double LSB_Stop(unsigned int id, unsigned int reset) {
double measure=0;
if (lsb_data->lsb_disabled) return -1;
//CHK_DISABLED;
#ifdef _OPENMP
#pragma omp master
{
#endif
double tstart, tlast; // epoch duration and function start time
#if defined HAVE_PAPI
tstart = PAPI_get_real_usec();
std::vector<long long> values(2);
PAPI_stop(lsb_data->eventset, &values[0]);
#elif defined HAVE_LIKWID
//std::vector<long long> values(10);
int cpu_id;
cpu_id = likwid_getProcessorId();
PMvalues[0] = msr_read(cpu_id, MSR_AMD15_PMC0) - PMvalues[0];
PMvalues[1] = msr_read(cpu_id, MSR_AMD15_PMC1) - PMvalues[1];
PMvalues[2] = msr_read(cpu_id, MSR_AMD15_PMC2) - PMvalues[2];
PMvalues[3] = msr_read(cpu_id, MSR_AMD15_PMC3) - PMvalues[3];
PMvalues[4] = msr_read(cpu_id, MSR_AMD15_PMC4) - PMvalues[4];
PMvalues[5] = msr_read(cpu_id, MSR_AMD15_PMC5) - PMvalues[5];
PMvalues[6] = msr_read(cpu_id, MSR_AMD15_NB_PMC0) - PMvalues[6];
PMvalues[7] = msr_read(cpu_id, MSR_AMD15_NB_PMC1) - PMvalues[7];
PMvalues[8] = msr_read(cpu_id, MSR_AMD15_NB_PMC2) - PMvalues[8];
PMvalues[9] = msr_read(cpu_id, MSR_AMD15_NB_PMC3) - PMvalues[9];
unsigned long long ticks;
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
tstart = HRT_GET_USEC(ticks);
#elif defined HAVE_HRTIMER
unsigned long long ticks;
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
tstart = HRT_GET_USEC(ticks);
#else
#error "No possibility to do accurate timing!\n"
#endif
measure = tstart-lsb_data->tlast;
#if defined HAVE_PAPI
t_rec rec = {values[0], values[1], measure, 0, id};
#elif defined HAVE_LIKWID
t_rec rec = {PMvalues[0], PMvalues[1], PMvalues[2], PMvalues[3], PMvalues[4], \
PMvalues[5], PMvalues[6], PMvalues[7], PMvalues[8], PMvalues[9], \
measure, 0, id};
#else
t_rec rec = {measure, 0, id};
#endif
if(lsb_data->rec_enabled) lsb_data->recs.push_back(rec);
#ifdef HAVE_UNWIND
unw_cursor_t cursor; unw_context_t uc;
unw_word_t ip;
unw_getcontext(&uc);
if(lsb_data->rec_enabled) {
lsb_data->iptrace.resize(lsb_data->iptrace.size()+1);
unw_init_local(&cursor, &uc);
while (unw_step(&cursor) > 0) {
unw_get_reg(&cursor, UNW_REG_IP, &ip);
//unw_get_reg(&cursor, UNW_REG_SP, &sp);
lsb_data->iptrace[lsb_data->iptrace.size()-1].push_back(ip);
//printf ("ip = %lx, sp = %lx\n", (long) ip, (long) sp);
}
}
#endif
#if defined HAVE_PAPI
PAPI_start(lsb_data->eventset);
tlast = PAPI_get_real_usec();
#elif HAVE_LIKWID
PMvalues[0] = msr_read(cpu_id, MSR_AMD15_PMC0);
PMvalues[1] = msr_read(cpu_id, MSR_AMD15_PMC1);
PMvalues[2] = msr_read(cpu_id, MSR_AMD15_PMC2);
PMvalues[3] = msr_read(cpu_id, MSR_AMD15_PMC3);
PMvalues[4] = msr_read(cpu_id, MSR_AMD15_PMC4);
PMvalues[5] = msr_read(cpu_id, MSR_AMD15_PMC5);
PMvalues[6] = msr_read(cpu_id, MSR_AMD15_NB_PMC0);
PMvalues[7] = msr_read(cpu_id, MSR_AMD15_NB_PMC1);
PMvalues[8] = msr_read(cpu_id, MSR_AMD15_NB_PMC2);
PMvalues[9] = msr_read(cpu_id, MSR_AMD15_NB_PMC3);
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
tlast = HRT_GET_USEC(ticks);
#elif defined HAVE_HRTIMER
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
tlast = HRT_GET_USEC(ticks);
#else
#error "No possibility to do accurate timing!\n"
#endif
if(lsb_data->rec_enabled) lsb_data->recs.back().toverhead = tlast-tstart;
if (reset) lsb_data->tlast = tlast;
#ifdef _OPENMP
}
#endif
return measure;
}
/**
* simple handler for SIGALRM callback
* simply calls LSB_Rec to record an epoch
* @param x epoch identifier
*/
void LSB_Handler(int x) {
LSB_Rec(0);
}
/**
* flush data to disk and end current measurement
* this is only used if an application is started multiple times with different parameters
*/
void LSB_Flush() {
CHK_DISABLED;
#ifdef _OPENMP
#pragma omp master
{
#endif
//printf("starting flushing\n");
double tend;
#if defined HAVE_PAPI
tend = PAPI_get_real_usec();
#elif defined HAVE_HRTIMER
unsigned long long ticks;
HRT_TIMESTAMP_T t;
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
tend = HRT_GET_USEC(ticks);
#else
#error "No possibility to do accurate timing!\n"
#endif
FILE *fp=lsb_data->outfd;
enum {PRETTY, EFFICIENT, ACCUMULATED} style = PRETTY; // printing style
char *env = getenv("LSB_OUTPUT_FORMAT"); // print format
if(env != NULL) {
if(strstr(env, "efficient")) style = EFFICIENT;
if(strstr(env, "accumulated")) style = ACCUMULATED;
}
//printf("flushing: style: %i\n", style);
/* Create hashtable for the parameters */
t_hparam * ptable = NULL;
t_hparam * found;
t_hparam * current;
t_hparam * toadd;
int pnum=0;
//printf("creating hashtable\n");
for (int i=0; i<lsb_data->recparams.size(); i++){
HASH_FIND_STR(ptable, lsb_data->recparams[i].pindex, found);
if (found==NULL) {
toadd = (t_hparam *) malloc(sizeof(t_hparam));
//printf("hashtable add: %s\n", lsb_data->recparams[i].pindex);
toadd->pname = lsb_data->recparams[i].pindex;
toadd->value = pnum++;
HASH_ADD_KEYPTR(hh, ptable, toadd->pname, strlen(toadd->pname), toadd);
}
}
int rparams_ptr = 0;
t_recparam * rparams = (t_recparam *) malloc(sizeof(t_recparam)*(pnum));
for (int i=0; i<pnum; i++) rparams[i].start_idx=-1;
const char ** pnames = (const char **) malloc(sizeof(char *)*(pnum));
for (current=ptable; current!=NULL; current= (t_hparam *) current->hh.next){
pnames[current->value] = current->pname;
}
if (lsb_data->write_header){
#ifdef HAVE_MPI
if(lsb_data->write_file==1) fprintf(fp, "# MPI execution on rank %i with %i processes in world\n", lsb_data->r, lsb_data->p);
#endif
fprintf(fp, "# Reported time measurements are in microseconds\n");
}
unsigned long long dur = 0; for(unsigned int i=0; i<lsb_data->recs.size(); ++i) dur += lsb_data->recs[i].toverhead;
//printf("tend: %lf; tstart: %lf\n", tend, lsb_data->tstart);
//if(lsb_data->write_file==1) fprintf(fp, "# Runtime: %lf s (overhead: %lf %%) %i records\n", (double)(tend-lsb_data->tstart)/1e6, (double)dur/(tend-lsb_data->tstart)*100, (int)lsb_data->recs.size());
if((style == PRETTY) && (lsb_data->write_file==1)) {
//printf("pretty printing\n");
if (lsb_data->write_header) {
fprintf(fp, "# pretty output format\n");
for (int j=0; j<pnum; j++) fprintf(fp, " %12s", pnames[j]);
fprintf(fp, " %8s %12s %12s ", "id", "time", "overhead");
#ifdef HAVE_PAPI
char p1[1024], p2[1024];
PAPI_event_code_to_name(lsb_data->papi1, p1);
PAPI_event_code_to_name(lsb_data->papi2, p2);
fprintf(fp, "%16s %16s", p1, p2);
#endif
#ifdef HAVE_UNWIND
fprintf(fp, " #ips ip-list");
#endif
fprintf(fp, "\n");
}
// print all records
for(unsigned int i=0; i < lsb_data->recs.size(); ++i) {
//printf("aa\n");
//printf("printing records; rparams_ptr: %i; recparams.size(): %i; i: %i; \n", rparams_ptr, lsb_data->recparams.size(), i);
//printf("printing records; rparams_ptr: %i; recparams.size(): %i; i: %i; start_idx: %i; \n", rparams_ptr, lsb_data->recparams.size(), i, lsb_data->recparams[rparams_ptr].start_idx);
/* search for rparam update */
while (rparams_ptr < lsb_data->recparams.size() && lsb_data->recparams[rparams_ptr].start_idx <= i){
//printf("advancing values\n");
HASH_FIND_STR(ptable, lsb_data->recparams[rparams_ptr].pindex, current);
rparams[current->value] = (lsb_data->recparams[rparams_ptr]);
rparams_ptr++;
}
//printf("printing parameters\n");
for (int j=0; j<pnum; j++) {
if (rparams[j].start_idx==-1) fprintf(fp, "NA");
else if (rparams[j].type==LSB_STR) fprintf(fp, " %12s ", rparams[j].str);
else if (rparams[j].type==LSB_INT) fprintf(fp, " %12i ", rparams[j].value);
else if (rparams[j].type==LSB_DBL) fprintf(fp, " %12f ", rparams[j].dbl);
else if (rparams[j].type==LSB_LNG) fprintf(fp, " %12ld ", rparams[j].lng);
}
//printf("end printing paramaters\nstart printing data\n");
fprintf(fp, " %8i ", lsb_data->recs[i].id);
fprintf(fp, "%f ", lsb_data->recs[i].tduration);
fprintf(fp, "%12lu ", lsb_data->recs[i].toverhead);
//printf("end printing data111\n");
#ifdef HAVE_PAPI
//printf("have papi\n");
fprintf(fp, "%16lli ", lsb_data->recs[i].papictr1);
fprintf(fp, "%16lli ", lsb_data->recs[i].papictr2);
#endif
#ifdef HAVE_LIKWID
//printf("have likwid\n");
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter0);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter1);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter2);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter3);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter4);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter5);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter6);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter7);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter8);
fprintf(fp, "%16lli ", lsb_data->recs[i].PMcounter9);
#endif
#ifdef HAVE_UNWIND
//printf("have unwind\n");
fprintf(fp, "%i ", (int)lsb_data->iptrace[i].size());
for(unsigned int j=0; j<lsb_data->iptrace[i].size(); ++j) fprintf(fp, "%lx ", lsb_data->iptrace[i][j]);
#endif
fprintf(fp, "\n");
}
//printf("printing ints\n");
if(lsb_data->recints.size()) {
// print all ints
fprintf(fp, "# printing integer list, %i records\n %8s %8s %8s\n", (int)lsb_data->recints.size(), "id", "int1", "int2");
for(unsigned int i=0; i < lsb_data->recints.size(); ++i) {
fprintf(fp, " %8i ", lsb_data->recints[i].id);
fprintf(fp, "%8i ", lsb_data->recints[i].int1);
fprintf(fp, "%8i ", lsb_data->recints[i].int2);
fprintf(fp, "\n");
}
}
//printf("end printing ints\nprinting dbls");
if(lsb_data->recintdbl.size()) {
// print all ints
fprintf(fp, "# printing integer/double list, %i records\n %8s %8s %16s\n", (int)lsb_data->recintdbl.size(), "id", "int1", "double");
for(unsigned int i=0; i < lsb_data->recintdbl.size(); ++i) {
fprintf(fp, " %8i ", lsb_data->recintdbl[i].id);
fprintf(fp, "%8i ", lsb_data->recintdbl[i].int1);
fprintf(fp, "%16f ", lsb_data->recintdbl[i].dbl);
fprintf(fp, "\n");
}
}
//printf("end pretty printing\n");
} else if ((style == EFFICIENT) && (lsb_data->write_file==1)) {
if (lsb_data->write_header){
fprintf(fp, "# efficient output format\n");
for (int j=0; j<pnum; j++) fprintf(fp, " P%i", j);
fprintf(fp, " %s %s %s ", "id", "time", "overhead");
}
#ifdef HAVE_PAPI
char p1[1024], p2[1024];
PAPI_event_code_to_name(lsb_data->papi1, p1);
PAPI_event_code_to_name(lsb_data->papi2, p2);
fprintf(fp, "%s %s", p1, p2);
#endif
#ifdef HAVE_UNWIND
fprintf(fp, " #ips ip-list");
#endif
fprintf(fp, "\n");
// print all records
for(unsigned int i=0; i < lsb_data->recs.size(); ++i) {
while (rparams_ptr < lsb_data->recparams.size() && lsb_data->recparams[rparams_ptr].start_idx <= i){
HASH_FIND_STR(ptable, lsb_data->recparams[rparams_ptr].pindex, current);
rparams[current->value] = (lsb_data->recparams[rparams_ptr]);
rparams_ptr++;
}
for (int j=0; j<pnum; j++) {
if (rparams[j].start_idx==-1) fprintf(fp, "NA");
else if (rparams[j].type==LSB_STR) fprintf(fp, " %12s ", rparams[j].str);
else if (rparams[j].type==LSB_INT) fprintf(fp, " %12i ", rparams[j].value);
else if (rparams[j].type==LSB_DBL) fprintf(fp, " %12f ", rparams[j].dbl);
else if (rparams[j].type==LSB_LNG) fprintf(fp, " %12ld ", rparams[j].lng);
}
fprintf(fp, "%i ", lsb_data->recs[i].id);
fprintf(fp, "%f ", lsb_data->recs[i].tduration);
fprintf(fp, "%lu ", lsb_data->recs[i].toverhead);
#ifdef HAVE_PAPI
fprintf(fp, "%lli ", lsb_data->recs[i].papictr1);
fprintf(fp, "%lli ", lsb_data->recs[i].papictr2);
#endif
#ifdef HAVE_LIKWID
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter0);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter1);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter2);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter3);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter4);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter5);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter6);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter7);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter8);
fprintf(fp, "%lli ", lsb_data->recs[i].PMcounter9);
#endif
#ifdef HAVE_UNWIND
fprintf(fp, "%i ", (int)lsb_data->iptrace[i].size());
for(unsigned int j=0; j<lsb_data->iptrace[i].size(); ++j) fprintf(fp, "%lx ", lsb_data->iptrace[i][j]);
#endif
fprintf(fp, "\n");
}
if(lsb_data->recints.size()) {
// print all ints
fprintf(fp, "# printing integer list, %i records\n %8s %8s %8s\n", (int)lsb_data->recints.size(), "id", "int1", "int2");
for(unsigned int i=0; i < lsb_data->recints.size(); ++i) {
fprintf(fp, "%i ", lsb_data->recints[i].id);
fprintf(fp, "%i ", lsb_data->recints[i].int1);
fprintf(fp, "%i ", lsb_data->recints[i].int2);
fprintf(fp, "\n");
}
}
if(lsb_data->recintdbl.size()) {
// print all ints
fprintf(fp, "# printing integer/double list, %i records\n %8s %8s %16s\n", (int)lsb_data->recintdbl.size(), "id", "int1", "double");
for(unsigned int i=0; i < lsb_data->recintdbl.size(); ++i) {
fprintf(fp, "%i ", lsb_data->recintdbl[i].id);
fprintf(fp, "%i ", lsb_data->recintdbl[i].int1);
fprintf(fp, "%f ", lsb_data->recintdbl[i].dbl);
fprintf(fp, "\n");
}
}
} else if (style == ACCUMULATED) { /* TODO: extend with rparams */
#ifdef HAVE_MPI
MPI_Comm comm = MPI_COMM_WORLD;
int size = lsb_data->recs.size();
int max, min;
MPI_Allreduce(&size,&max,1,MPI_INT,MPI_MAX,comm);
MPI_Allreduce(&size,&min,1,MPI_INT,MPI_MIN,comm);
if(max != min) {
if(lsb_data->r == 0) printf("not the same number of records on all processes (min: %i, max: %i)! No output is generated!", min, max);
} else {
std::vector<long long> my_duration(size); // convert to long long for MPI :-(
std::vector<long> my_overhead(size); // convert to long for MPI :-(
#ifdef HAVE_PAPI
std::vector<long long> my_papictr1(size), my_papictr2(size); // convert to long long for MPI :-(
#endif
for(int i=0; i<size; ++i) {
my_duration[i] = lsb_data->recs[i].tduration;
my_overhead[i] = lsb_data->recs[i].toverhead;
#ifdef HAVE_PAPI
my_papictr1[i] = lsb_data->recs[i].papictr1;
my_papictr2[i] = lsb_data->recs[i].papictr2;
#endif
}
std::vector<long long> max_duration(size), min_duration(size), avg_duration(size);
std::vector<long> max_overhead(size), min_overhead(size), avg_overhead(size);
#ifdef HAVE_PAPI
std::vector<long long> max_papictr1(size), max_papictr2(size), min_papictr1(size),
min_papictr2(size), avg_papictr1(size), avg_papictr2(size);
#endif
MPI_Allreduce(&my_overhead[0],&min_overhead[0],size,MPI_LONG,MPI_MIN,comm);
MPI_Allreduce(&my_overhead[0],&max_overhead[0],size,MPI_LONG,MPI_MAX,comm);
MPI_Allreduce(&my_overhead[0],&avg_overhead[0],size,MPI_LONG,MPI_SUM,comm);
for(int i=0; i<size; ++i) avg_overhead[i]/= lsb_data->p;
MPI_Allreduce(&my_duration[0],&min_duration[0],size,MPI_LONG_LONG,MPI_MIN,comm);
MPI_Allreduce(&my_duration[0],&max_duration[0],size,MPI_LONG_LONG,MPI_MAX,comm);
MPI_Allreduce(&my_duration[0],&avg_duration[0],size,MPI_LONG_LONG,MPI_SUM,comm);
for(int i=0; i<size; ++i) avg_duration[i]/= lsb_data->p;
#ifdef HAVE_PAPI
MPI_Allreduce(&my_papictr1[0],&min_papictr1[0],size,MPI_LONG_LONG,MPI_MIN,comm);
MPI_Allreduce(&my_papictr1[0],&max_papictr1[0],size,MPI_LONG_LONG,MPI_MAX,comm);
MPI_Allreduce(&my_papictr1[0],&avg_papictr1[0],size,MPI_LONG_LONG,MPI_SUM,comm);
for(int i=0; i<size; ++i) avg_papictr1[i]/= lsb_data->p;
MPI_Allreduce(&my_papictr2[0],&min_papictr2[0],size,MPI_LONG_LONG,MPI_MIN,comm);
MPI_Allreduce(&my_papictr2[0],&max_papictr2[0],size,MPI_LONG_LONG,MPI_MAX,comm);
MPI_Allreduce(&my_papictr2[0],&avg_papictr2[0],size,MPI_LONG_LONG,MPI_SUM,comm);
for(int i=0; i<size; ++i) avg_papictr2[i]/= lsb_data->p;
#endif
if(lsb_data->write_file==1) {
fprintf(fp, "# accumulated output format\n");
fprintf(fp, "%s %s - %s - ", "id", "time (min, avg, max)", "overhead (min, avg, max)");
#ifdef HAVE_PAPI
char p1[1024], p2[1024];
PAPI_event_code_to_name(lsb_data->papi1, p1);
PAPI_event_code_to_name(lsb_data->papi2, p2);
fprintf(fp, "%s - %s", p1, p2);
#endif
#ifdef HAVE_UNWIND
fprintf(fp, " #ips ip-list");
#endif
fprintf(fp, "\n");
}
// print all records
if(lsb_data->write_file==1) for(unsigned int i=0; i < size; ++i) {
fprintf(fp, "%i ", lsb_data->recs[i].id);
fprintf(fp, "%lli %lli %lli - ", min_duration[i], avg_duration[i], max_duration[i] );
fprintf(fp, "%li %li %li - ", min_overhead[i], avg_overhead[i], max_overhead[i]);
#ifdef HAVE_PAPI
fprintf(fp, "%lli %lli %lli - ", min_papictr1[i], avg_papictr1[i], max_papictr1[i]);
fprintf(fp, "%lli %lli %lli - ", min_papictr2[i], avg_papictr2[i], max_papictr2[i]);
#endif
#ifdef HAVE_UNWIND
fprintf(fp, "%i ", (int)lsb_data->iptrace[i].size());
for(unsigned int j=0; j<lsb_data->iptrace[i].size(); ++j) fprintf(fp, "%lx ", lsb_data->iptrace[i][j]);
#endif
fprintf(fp, "\n");
}
}
#else
printf("MPI support required for the ACCUMULATED output format\n");
#endif
}
if(lsb_data->write_file==1) fprintf(fp, "# Runtime: %lf s (overhead: %lf %%) %i records\n", (double)(tend-lsb_data->tstart)/1e6, (double)dur/(tend-lsb_data->tstart)*100, (int)lsb_data->recs.size());
/* Update rparams to the last values. It covers the corner case in which there are
parameters but not measures. */
while (rparams_ptr < lsb_data->recparams.size()){
//printf("advancing values\n");
HASH_FIND_STR(ptable, lsb_data->recparams[rparams_ptr].pindex, current);
rparams[current->value] = (lsb_data->recparams[rparams_ptr]);
rparams_ptr++;
}
lsb_data->next=0;
//printf("flush: start clear\n");
lsb_data->recs.clear();
lsb_data->recints.clear();
lsb_data->recintdbl.clear();
lsb_data->recparams.clear();
/* Add last known value of the parameters */
for (int i=0; i<pnum; i++){
rparams[i].start_idx=0;
lsb_data->recparams.push_back(rparams[i]);
}
/******/
//printf("flush: end clear\n");
free(rparams);
lsb_data->write_header = 0;
#ifdef HAVE_UNWIND
lsb_data->ipctrace.clear();
#endif
#if defined HAVE_PAPI
//lsb_data->tstart = PAPI_get_real_usec();
#elif defined HAVE_LIKWID
int cpu_id;
cpu_id = likwid_getProcessorId();
PMvalues[0] = msr_read(cpu_id, MSR_AMD15_PMC0);
PMvalues[1] = msr_read(cpu_id, MSR_AMD15_PMC1);
PMvalues[2] = msr_read(cpu_id, MSR_AMD15_PMC2);
PMvalues[3] = msr_read(cpu_id, MSR_AMD15_PMC3);
PMvalues[4] = msr_read(cpu_id, MSR_AMD15_PMC4);
PMvalues[5] = msr_read(cpu_id, MSR_AMD15_PMC5);
PMvalues[6] = msr_read(cpu_id, MSR_AMD15_NB_PMC0);
PMvalues[7] = msr_read(cpu_id, MSR_AMD15_NB_PMC1);
PMvalues[8] = msr_read(cpu_id, MSR_AMD15_NB_PMC2);
PMvalues[9] = msr_read(cpu_id, MSR_AMD15_NB_PMC3);
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
//lsb_data->tstart = HRT_GET_USEC(ticks);
#elif defined HAVE_HRTIMER
HRT_GET_TIMESTAMP(t);
HRT_GET_TIME(t, ticks);
//lsb_data->tstart = HRT_GET_USEC(ticks);
#else
#error "No possibility to do accurate timing!\n"
#endif
#ifdef _OPENMP
}
#endif
}
/**
* ends profiling
* last function to be called in a profiling session
*/
static int LSB_finalized;
void LSB_Finalize() {
CHK_DISABLED;
if(LSB_finalized) return; // so that it can be called before atexit() if atexit doesn't work (e.g., BG/P)
#ifdef _OPENMP
#pragma omp master
{
#endif
LSB_finalized = 1;
if(lsb_data->print) printf("******* LSB_Finalize *******\n");
if(lsb_data->recs.size() > 0 || lsb_data->recints.size() || lsb_data->recintdbl.size()) LSB_Flush();
if(lsb_data->write_file==1) fclose(lsb_data->outfd);
#ifdef _OPENMP
}
#endif
#ifdef HAVE_LIKWID
perfmon_stopCounters();
msr_finalize();
#endif
}
/**
* internal helper function to write benchmark information in header
* @param fd file descriptor to write to
*/
static void write_host_information(FILE *fd) {
#ifdef _OPENMP
#pragma omp master
{
#endif
struct utsname uninfo;
if (uname (&uninfo) >= 0) {
fprintf(fd, "# Sysname : %s\n", uninfo.sysname);
fprintf(fd, "# Nodename: %s\n", uninfo.nodename);
fprintf(fd, "# Release : %s\n", uninfo.release);
fprintf(fd, "# Version : %s\n", uninfo.version);
fprintf(fd, "# Machine : %s\n", uninfo.machine);
}
time_t t = time(NULL);
struct tm *local = gmtime(&t);
fprintf(fd, "# Execution time and date (UTC): %s", asctime(local));
local = localtime(&t);
fprintf(fd, "# Execution time and date (local): %s", asctime(local));
#ifdef _OPENMP
}
#endif
}
/**
* first function to be called to open a tracing session
* @param projname name of the project
* @param autoprof_interval automatic (SIGALRM) profiling interval, set 0 to disable automatic profiling
*/
void LSB_Init(const char* projname, int autoprof_interval /* in ms, off if 0 */) {
const char *fname = projname, *dir;
char name[1024];
int rev;
struct stat st;
char *env;
#ifdef _OPENMP
#pragma omp master
{
#endif
lsb_data = new(LSB_Data);
lsb_data->rec_enabled = 1;
lsb_data->lsb_disabled = 0;
lsb_data->print=1;
lsb_data->next = 0;
dir = getenv("LSB_OUTDIR");
if(dir == NULL) dir = ".";
env = getenv("LSB_OUTFILE");
if(env != NULL) fname = env;
snprintf(name, 1023, "%s/lsb.%s", dir, fname);
int r;
#ifdef HAVE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &r);
MPI_Comm_size(MPI_COMM_WORLD, &lsb_data->p);
#else
r=0;
lsb_data->p = 1;
#endif
lsb_data->r=r;
lsb_data->write_header=1;
if(r > 0) lsb_data->print = 0;
snprintf(name, 1023, "%s/lsb.%s.r%i", dir, fname, r);
rev = 0;
while(stat(name, &st) != -1) { // path exists!
snprintf(name, 1023, "%s/lsb.%s.r%i-%i", dir, fname, r, ++rev);
}
lsb_data->write_file=1;
env = getenv("LSB_OUTPUT_FORMAT"); // if accumulated is specified, then all processes but rank 0 are disabled!
if((env != NULL) && (strstr(env, "accumulated") != NULL)) {
#ifndef HAVE_MPI
printf("Compiled without MPI: accumulated format disabled (check LSB_OUTPUT_FORMAT envirorment variable)!\n");
exit(-1);
#else
lsb_data->write_file=0;
if(lsb_data->r == 0) lsb_data->write_file=1;
#endif
}
env = getenv("LSB_ENABLE_PROCMASK"); // list of enabled processes
if(env != NULL) {
lsb_data->lsb_disabled=1;
// parse comma-separated list of ints
for( char *s2 = env; s2; ){
while( *s2 == ' ' || *s2 == '\t' ) s2++;
char *s1 = strsep( &s2, "," );
if( !*s1 ){
//printf("val: (empty)\n" );
} else {
int val;
char ch;
int ret = sscanf( s1, " %i %c", &val, &ch );
if( ret != 1 ){
//printf("val: (syntax error)\n" );
}
else{
//printf("val: %i\n", val );
if(r==val) lsb_data->lsb_disabled=0;
}
}
}
}
#ifdef _OPENMP
}
#pragma omp barrier
#endif
CHK_DISABLED;
#ifdef _OPENMP
#pragma omp master
{
#endif
//printf("printing enabled: %i\n", lsb_data->write_file);
if(lsb_data->write_file==1) {
lsb_data->outfd = fopen(name, "w");
if (lsb_data->outfd == NULL)
{
printf("LibLSB: Error opening output file! (filename: %s)\n", name);
perror("LibLSB: I/O error");
}
write_host_information(lsb_data->outfd);
}
#ifdef HAVE_PAPI
if(PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT ) {
printf("PAPI initialization error! \n");
exit(1);
}
lsb_data->eventset = PAPI_NULL;
assert(PAPI_create_eventset(&lsb_data->eventset) == PAPI_OK);
// define what to count! -- check "papi_event_chooser PRESET event1 event2"
env = getenv("LSB_PAPI1");
if(env != NULL) PAPI_event_name_to_code(env, &lsb_data->papi1);
else lsb_data->papi1 = PAPI_TOT_INS;
env = getenv("LSB_PAPI2");
if(env != NULL) PAPI_event_name_to_code(env, &lsb_data->papi2);
else lsb_data->papi2 = PAPI_TOT_CYC;
if(PAPI_add_event(lsb_data->eventset, lsb_data->papi1) != PAPI_OK) {
char p1[1024];
PAPI_event_code_to_name(lsb_data->papi1, p1);
if(lsb_data->print) printf("Adding PAPI counter 1 (%s) failed!\n", p1);
exit(11);
}
if(PAPI_add_event(lsb_data->eventset, lsb_data->papi2) != PAPI_OK) {
char p1[1024];
PAPI_event_code_to_name(lsb_data->papi1, p1);
if(lsb_data->print) printf("Adding PAPI counter 2 (%s) failed!\n", p1);
exit(11);
}
#endif
#ifdef HAVE_LIKWID
// init likwid using the low level API
FILE *OUTSTREAM = stdout;
int numThreads=1;
int threads[1];
char *myeventstring;
// start likwid things
cpuid_init();
// printf("cpuid_topology.numHWThreads %d\n",cpuid_topology.numHWThreads);
// numa_init(); // do we need this one ?
// affinity_init(); // skip this, let aprun pin
msr_init();
threads[0]=likwid_getProcessorId();
perfmon_init(numThreads,threads,OUTSTREAM);
timer_init();
// Set the event set string
myeventstring=(char *)malloc(60*sizeof(char));
sprintf(myeventstring,"UNC_L3_CACHE_MISS_CORE_%d:UPMC%d",likwid_getProcessorId()%8,lsb_data->r%4);
printf("LIKWID: rank %d event %s\n",lsb_data->r,myeventstring);
bstring eventString = bfromcstr(myeventstring);
perfmon_setupEventSet(eventString);
perfmon_startCounters();
free(myeventstring);
#endif
if(lsb_data->print) {