forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAMReX_BLProfiler.cpp
More file actions
1784 lines (1549 loc) · 57.6 KB
/
Copy pathAMReX_BLProfiler.cpp
File metadata and controls
1784 lines (1549 loc) · 57.6 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 <AMReX_BLFort.H>
#ifdef BL_PROFILING
#include <AMReX_BLProfiler.H>
#include <AMReX_REAL.H>
#include <AMReX_Utility.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_Array.H>
#include <AMReX_Vector.H>
#include <AMReX_NFiles.H>
#include <AMReX_Print.H>
#include <AMReX_ParmParse.H>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <limits>
#include <cstdlib>
#include <cmath>
namespace amrex {
bool BLProfiler::bWriteAll = true;
bool BLProfiler::bNoOutput = false;
bool BLProfiler::bWriteFabs = true;
bool BLProfiler::groupSets = false;
bool BLProfiler::bFirstCommWrite = true; // header
bool BLProfiler::bInitialized = false;
bool BLProfiler::bFlushPrint = true;
const int defaultFlushSize = 8192000;
const int defaultReserveSize = 8192000;
int BLProfiler::currentStep = 0;
int BLProfiler::baseFlushSize = defaultFlushSize;
int BLProfiler::csFlushSize = defaultFlushSize;
int BLProfiler::traceFlushSize = defaultFlushSize;
int BLProfiler::baseFlushCount = 0;
int BLProfiler::csFlushCount = 0;
int BLProfiler::traceFlushCount = 0;
int BLProfiler::flushInterval = -1;
int BLProfiler::nProfFiles = 256;
int BLProfiler::finestLevel = -1;
int BLProfiler::maxLevel = -1;
Real BLProfiler::flushTimeInterval = -1.0;
Real BLProfiler::pctTimeLimit = 5.0;
Real BLProfiler::calcRunTime = 0.0;
Real BLProfiler::startTime = 0.0;
Real BLProfiler::timerTime = 0.0;
#ifndef BL_AMRPROF
Vector<IntVect> BLProfiler::refRatio;
Vector<Box> BLProfiler::probDomain;
#endif
std::stack<Real> BLProfiler::nestedTimeStack;
std::map<int, Real> BLProfiler::mStepMap;
std::map<std::string, BLProfiler::ProfStats> BLProfiler::mProfStats;
Vector<BLProfiler::CommStats> BLProfiler::vCommStats;
std::map<std::string, BLProfiler *> BLProfiler::mFortProfs;
Vector<std::string> BLProfiler::mFortProfsErrors;
const int mFortProfMaxErrors(32);
Vector<BLProfiler *> BLProfiler::mFortProfsInt;
Vector<std::string> BLProfiler::mFortProfsIntNames;
const int mFortProfsIntMaxFuncs(32);
std::map<std::string, BLProfiler::CommFuncType> BLProfiler::CommStats::cftNames;
std::set<BLProfiler::CommFuncType> BLProfiler::CommStats::cftExclude;
int BLProfiler::CommStats::barrierNumber(0);
int BLProfiler::CommStats::reductionNumber(0);
int BLProfiler::CommStats::tagWrapNumber(0);
int BLProfiler::CommStats::tagMin(0);
int BLProfiler::CommStats::tagMax(0);
int BLProfiler::CommStats::csVersion(1);
Vector<std::pair<std::string,int> > BLProfiler::CommStats::barrierNames;
Vector<std::pair<int,int> > BLProfiler::CommStats::nameTags;
Vector<std::string> BLProfiler::CommStats::nameTagNames;
Vector<int> BLProfiler::CommStats::tagWraps;
std::string BLProfiler::procName("NoProcName");
int BLProfiler::procNumber(-1);
bool BLProfiler::blProfDirCreated(false);
std::string BLProfiler::blProfDirName("bl_prof");
int BLProfiler::BLProfVersion(1);
std::map<std::string, int> BLProfiler::mFNameNumbers;
Vector<BLProfiler::CallStats> BLProfiler::vCallTrace;
// Region support
std::map<std::string, int> BLProfiler::mRegionNameNumbers;
int BLProfiler::inNRegions(0);
Vector<BLProfiler::RStartStop> BLProfiler::rStartStop;
const std::string BLProfiler::noRegionName("__NoRegion__");
bool BLProfiler::bFirstTraceWrite(true);
int BLProfiler::CallStats::cstatsVersion(1);
Vector<BLProfiler::CallStatsStack> BLProfiler::callIndexStack;
Vector<BLProfiler::CallStatsPatch> BLProfiler::callIndexPatch;
#ifdef BL_TRACE_PROFILING
int BLProfiler::callStackDepth(-1);
int BLProfiler::prevCallStackDepth(0);
Real BLProfiler::CallStats::minCallTime(std::numeric_limits<Real>::max());
Real BLProfiler::CallStats::maxCallTime(-1.0);
#endif
BLProfiler::BLProfiler(const std::string &funcname)
: bltstart(0.0), bltelapsed(0.0)
, fname(funcname)
, bRunning(false)
{
start();
}
BLProfiler::BLProfiler(const std::string &funcname, bool bstart)
: bltstart(0.0), bltelapsed(0.0)
, fname(funcname)
, bRunning(false)
{
if(bstart) {
start();
}
}
BLProfiler::~BLProfiler() {
if(bRunning) {
stop();
}
}
void BLProfiler::Initialize() {
if(bInitialized) {
return;
}
startTime = amrex::second();
int resultLen(-1);
char cProcName[MPI_MAX_PROCESSOR_NAME + 11];
#ifdef BL_USE_MPI
MPI_Get_processor_name(cProcName, &resultLen);
#endif
if(resultLen < 1) {
procName = "NoProcName";
procNumber = ParallelDescriptor::MyProc();
} else {
procName = cProcName;
procNumber = ParallelDescriptor::MyProc();
}
//amrex::AllPrint() << myProc << ":::: " << procName << " len = " << resultLen << '\n';
Real t0, t1;
int nTimerTimes(1000);
for(int i(0); i < nTimerTimes; ++i) { // ---- time the timer
t0 = amrex::second();
t1 = amrex::second();
timerTime += t1 - t0;
}
timerTime /= static_cast<Real> (nTimerTimes);
#ifdef BL_COMM_PROFILING
vCommStats.reserve(std::max(csFlushSize, defaultReserveSize));
#endif
#ifdef BL_TRACE_PROFILING
vCallTrace.reserve(std::max(traceFlushSize, defaultReserveSize));
// ---- make sure there is always at least one so we dont need to check in start()
CallStats unusedCS(-1, -1, -1, -1.1, -1.2, -1.3);
vCallTrace.push_back(unusedCS);
#endif
BL_PROFILE_REGION_START(noRegionName);
CommStats::cftExclude.insert(AllCFTypes); // temporarily
CommStats::cftNames["InvalidCFT"] = InvalidCFT;
CommStats::cftNames["AllReduceT"] = AllReduceT;
CommStats::cftNames["AllReduceR"] = AllReduceR;
CommStats::cftNames["AllReduceL"] = AllReduceL;
CommStats::cftNames["AllReduceI"] = AllReduceI;
CommStats::cftNames["AsendTsii"] = AsendTsii;
CommStats::cftNames["AsendTsiiM"] = AsendTsiiM;
CommStats::cftNames["AsendvTii"] = AsendvTii;
CommStats::cftNames["SendTsii"] = SendTsii;
CommStats::cftNames["SendvTii"] = SendvTii;
CommStats::cftNames["ArecvTsii"] = ArecvTsii;
CommStats::cftNames["ArecvTsiiM"] = ArecvTsiiM;
CommStats::cftNames["ArecvTii"] = ArecvTii;
CommStats::cftNames["ArecvvTii"] = ArecvvTii;
CommStats::cftNames["RecvTsii"] = RecvTsii;
CommStats::cftNames["RecvvTii"] = RecvvTii;
CommStats::cftNames["ReduceT"] = ReduceT;
CommStats::cftNames["ReduceR"] = ReduceR;
CommStats::cftNames["ReduceL"] = ReduceL;
CommStats::cftNames["ReduceI"] = ReduceI;
CommStats::cftNames["BCastTsi"] = BCastTsi;
CommStats::cftNames["GatherTsT1Si"] = GatherTsT1Si;
CommStats::cftNames["GatherTi"] = GatherTi;
CommStats::cftNames["GatherRiRi"] = GatherRiRi;
CommStats::cftNames["ScatterTsT1si"] = ScatterTsT1si;
CommStats::cftNames["Barrier"] = Barrier;
CommStats::cftNames["Waitsome"] = Waitsome;
CommStats::cftNames["NameTag"] = NameTag;
CommStats::cftNames["AllCFTypes"] = AllCFTypes;
CommStats::cftNames["NoCFTypes"] = NoCFTypes;
CommStats::cftNames["IOStart"] = IOStart;
CommStats::cftNames["IOEnd"] = IOEnd;
CommStats::cftNames["TagWrap"] = TagWrap;
CommStats::cftNames["Allgather"] = Allgather;
CommStats::cftNames["Alltoall"] = Alltoall;
CommStats::cftNames["Alltoallv"] = Alltoallv;
CommStats::cftNames["Gatherv"] = Gatherv;
CommStats::cftNames["Get_count"] = Get_count;
CommStats::cftNames["Iprobe"] = Iprobe;
CommStats::cftNames["Test"] = Test;
CommStats::cftNames["Wait"] = Wait;
CommStats::cftNames["Waitall"] = Waitall;
CommStats::cftNames["Waitany"] = Waitany;
// check for exclude file
std::string exFile("CommFuncExclude.txt");
Vector<CommFuncType> vEx;
Vector<char> fileCharPtr;
bool bExitOnError(false); // in case the file does not exist
ParallelDescriptor::ReadAndBcastFile(exFile, fileCharPtr, bExitOnError);
CommStats::cftExclude.erase(AllCFTypes);
if(fileCharPtr.size() > 0) {
std::string fileCharPtrString(fileCharPtr.dataPtr());
std::istringstream cfex(fileCharPtrString, std::istringstream::in);
while( ! cfex.eof()) {
std::string cft;
cfex >> cft;
if( ! cfex.eof()) {
vEx.push_back(CommStats::StringToCFT(cft));
}
}
for(int i(0); i < vEx.size(); ++i) {
CommStats::cftExclude.insert(vEx[i]);
}
}
// initialize fort int profilers
mFortProfsInt.resize(mFortProfsIntMaxFuncs + 1); // use 0 for undefined
mFortProfsIntNames.resize(mFortProfsIntMaxFuncs + 1); // use 0 for undefined
for(int i(0); i < mFortProfsInt.size(); ++i) {
std::ostringstream fname;
fname << "FORTFUNC_" << i;
mFortProfsIntNames[i] = fname.str();
#ifdef AMREX_DEBUG
mFortProfsInt[i] = 0;
#else
mFortProfsInt[i] = new BLProfiler(mFortProfsIntNames[i], false); // dont start
#endif
}
bInitialized = true;
}
void BLProfiler::InitParams() {
ParmParse pParse("blprofiler");
pParse.queryAdd("prof_nfiles", nProfFiles);
pParse.queryAdd("prof_csflushsize", csFlushSize);
pParse.queryAdd("prof_traceflushsize", traceFlushSize);
pParse.queryAdd("prof_flushinterval", flushInterval);
pParse.queryAdd("prof_flushtimeinterval", flushTimeInterval);
pParse.queryAdd("prof_flushprint", bFlushPrint);
}
void BLProfiler::ChangeFortIntName(const std::string &fname, int intname) {
#ifdef AMREX_DEBUG
mFortProfsIntNames[intname] = fname;
#else
delete mFortProfsInt[intname];
mFortProfsInt[intname] = new BLProfiler(fname, false); // dont start
#endif
}
void BLProfiler::PStart() {
bltelapsed = 0.0;
start();
}
void BLProfiler::PStop() {
if(bRunning) {
stop();
}
}
void BLProfiler::start() {
#ifdef AMREX_USE_OMP
#pragma omp master
#endif
{
bltelapsed = 0.0;
bltstart = amrex::second();
++mProfStats[fname].nCalls;
bRunning = true;
nestedTimeStack.push(0.0);
#ifdef BL_TRACE_PROFILING
int fnameNumber;
std::map<std::string, int>::iterator it = BLProfiler::mFNameNumbers.find(fname);
if(it == BLProfiler::mFNameNumbers.end()) {
fnameNumber = BLProfiler::mFNameNumbers.size();
BLProfiler::mFNameNumbers.insert(std::pair<std::string, int>(fname, fnameNumber));
} else {
fnameNumber = it->second;
}
++callStackDepth;
BL_ASSERT(vCallTrace.size() > 0);
Real calltime(bltstart - startTime);
vCallTrace.push_back(CallStats(callStackDepth, fnameNumber, 1, 0.0, 0.0, calltime));
CallStats::minCallTime = std::min(CallStats::minCallTime, calltime);
CallStats::maxCallTime = std::max(CallStats::maxCallTime, calltime);
callIndexStack.push_back(CallStatsStack(vCallTrace.size() - 1));
prevCallStackDepth = callStackDepth;
#endif
}
}
void BLProfiler::stop() {
#ifdef AMREX_USE_OMP
#pragma omp master
#endif
{
double tDiff(amrex::second() - bltstart);
double nestedTime(0.0);
bltelapsed += tDiff;
bRunning = false;
Real thisFuncTime(bltelapsed);
if( ! nestedTimeStack.empty()) {
nestedTime = nestedTimeStack.top();
thisFuncTime -= nestedTime;
nestedTimeStack.pop();
}
if( ! nestedTimeStack.empty()) {
nestedTimeStack.top() += bltelapsed;
}
mProfStats[fname].totalTime += thisFuncTime;
#ifdef BL_TRACE_PROFILING
prevCallStackDepth = callStackDepth;
--callStackDepth;
BL_ASSERT(vCallTrace.size() > 0);
if(vCallTrace.back().csFNameNumber == mFNameNumbers[fname]) {
vCallTrace.back().totalTime = thisFuncTime + nestedTime;
vCallTrace.back().stackTime = thisFuncTime;
}
if( ! callIndexStack.empty()) {
CallStatsStack &cis(callIndexStack.back());
if(cis.bFlushed) {
callIndexPatch[cis.index].callStats.totalTime = thisFuncTime + nestedTime;
callIndexPatch[cis.index].callStats.stackTime = thisFuncTime;
} else {
vCallTrace[cis.index].totalTime = thisFuncTime + nestedTime;
vCallTrace[cis.index].stackTime = thisFuncTime;
}
callIndexStack.pop_back();
}
#endif
}
}
void BLProfiler::InitParams(const Real ptl, const bool writeall, const bool writefabs) {
pctTimeLimit = ptl;
bWriteAll = writeall;
bWriteFabs = writefabs;
}
#ifndef BL_AMRPROF
void BLProfiler::InitAMR(const int flev, const int mlev, const Vector<IntVect> &rr,
const Vector<Box> pd)
{
finestLevel = flev;
maxLevel = mlev;
refRatio.resize(rr.size());
probDomain.resize(pd.size());
for(int i(0); i < rr.size(); ++i) {
refRatio[i] = rr[i];
}
for(int i(0); i < pd.size(); ++i) {
probDomain[i] = pd[i];
}
}
#endif
void BLProfiler::AddStep(const int snum) {
currentStep = snum;
mStepMap.insert(std::map<int, Real>::value_type(currentStep,
amrex::second()));
}
void BLProfiler::RegionStart(const std::string &rname) {
Real rsTime(amrex::second() - startTime);
if(rname != noRegionName) {
++inNRegions;
}
if(inNRegions == 1) {
RegionStop(noRegionName);
}
int rnameNumber;
std::map<std::string, int>::iterator it = BLProfiler::mRegionNameNumbers.find(rname);
if(it == BLProfiler::mRegionNameNumbers.end()) {
rnameNumber = BLProfiler::mRegionNameNumbers.size();
BLProfiler::mRegionNameNumbers.insert(std::pair<std::string, int>(rname, rnameNumber));
} else {
rnameNumber = it->second;
}
rStartStop.push_back(RStartStop(rsTime, rnameNumber, true));
}
void BLProfiler::RegionStop(const std::string &rname) {
Real rsTime(amrex::second() - startTime);
int rnameNumber;
std::map<std::string, int>::iterator it = BLProfiler::mRegionNameNumbers.find(rname);
if(it == BLProfiler::mRegionNameNumbers.end()) { // ---- error
// amrex::Print() << "-------- error in RegionStop: region " << rname
// << " never started.\n";
rnameNumber = BLProfiler::mRegionNameNumbers.size();
BLProfiler::mRegionNameNumbers.insert(std::pair<std::string, int>(rname, rnameNumber));
} else {
rnameNumber = it->second;
}
rStartStop.push_back(RStartStop(rsTime, rnameNumber, false));
if(rname != noRegionName) {
--inNRegions;
}
if(inNRegions == 0) {
RegionStart(noRegionName);
}
}
void BLProfiler::Finalize(bool bFlushing, bool memCheck) {
if( ! bInitialized) {
return;
}
if(bNoOutput) {
bInitialized = false;
return;
}
WriteBaseProfile(bFlushing);
BL_PROFILE_REGION_STOP(noRegionName);
#ifdef BL_TRACE_PROFILING
WriteCallTrace(bFlushing, memCheck);
#endif
#ifdef BL_COMM_PROFILING
// filter out profiler communications.
CommStats::cftExclude.insert(AllCFTypes);
WriteCommStats(bFlushing, memCheck);
#endif
WriteFortProfErrors();
#ifdef AMREX_DEBUG
#else
if (!bFlushing)
{
for(int i(0); i < mFortProfsInt.size(); ++i) {
delete mFortProfsInt[i];
}
bInitialized = false;
}
#endif
}
namespace BLProfilerUtils {
void WriteHeader(std::ostream &ios, const int colWidth,
const Real maxlen, const bool bwriteavg)
{
int maxlenI = int(maxlen);
if(bwriteavg) {
ios << std::setfill('-') << std::setw(maxlenI+4 + 7 * (colWidth+2))
<< std::left << "Total times " << '\n';
ios << std::right << std::setfill(' ');
ios << std::setw(maxlenI + 2) << "Function Name"
<< std::setw(colWidth + 2) << "NCalls"
<< std::setw(colWidth + 2) << "Min"
<< std::setw(colWidth + 2) << "Avg"
<< std::setw(colWidth + 2) << "Max"
<< std::setw(colWidth + 2) << "StdDev"
<< std::setw(colWidth + 2) << "CoeffVar"
<< std::setw(colWidth + 4) << "Percent %"
<< '\n';
} else {
ios << std::setfill('-') << std::setw(maxlenI+4 + 3 * (colWidth+2))
<< std::left << "Total times " << '\n';
ios << std::right << std::setfill(' ');
ios << std::setw(maxlenI + 2) << "Function Name"
<< std::setw(colWidth + 2) << "NCalls"
<< std::setw(colWidth + 2) << "Time"
<< std::setw(colWidth + 4) << "Percent %"
<< '\n';
}
}
void WriteRow(std::ostream &ios, const std::string &fname,
const BLProfiler::ProfStats &pstats, const Real percent,
const int colWidth, const Real maxlen,
const bool bwriteavg)
{
int maxlenI = int(maxlen);
int numPrec(4), pctPrec(2);
Real stdDev(0.0), coeffVariation(0.0);
if(pstats.variance > 0.0) {
stdDev = std::sqrt(pstats.variance);
}
if(pstats.avgTime > 0.0) {
coeffVariation = 100.0 * (stdDev / pstats.avgTime); // ---- percent
}
if(bwriteavg) {
ios << std::right;
ios << std::setw(maxlenI + 2) << fname << " "
<< std::setw(colWidth) << pstats.nCalls << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< pstats.minTime << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< pstats.avgTime << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< pstats.maxTime << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< stdDev << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< coeffVariation << " "
<< std::setprecision(pctPrec) << std::fixed << std::setw(colWidth)
<< percent << " %" << '\n';
} else {
ios << std::setw(maxlenI + 2) << fname << " "
<< std::setw(colWidth) << pstats.nCalls << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< pstats.totalTime << " "
<< std::setprecision(pctPrec) << std::fixed << std::setw(colWidth)
<< percent << " %" << '\n';
}
}
void WriteStats(std::ostream &ios,
const std::map<std::string, BLProfiler::ProfStats> &mpStats,
const std::map<std::string, int> &fnameNumbers,
const Vector<BLProfiler::CallStats> &callTraces,
bool bwriteavg, bool bwriteinclusivetimes)
{
const int myProc(ParallelDescriptor::MyProc());
const int colWidth(10);
const Real calcRunTime(BLProfiler::GetRunTime());
std::map<Real, std::string, std::greater<Real> > mTimersTotalsSorted;
Real totalTimers(0.0), percent(0.0);
int maxlen(0);
for(std::map<std::string, BLProfiler::ProfStats>::const_iterator it = mpStats.begin();
it != mpStats.end(); ++it)
{
std::string profName(it->first);
int pnLen(profName.size());
maxlen = std::max(maxlen, pnLen);
if(bwriteavg) {
totalTimers += it->second.avgTime;
} else {
totalTimers += it->second.totalTime;
}
}
Real pTimeTotal(totalTimers);
if(calcRunTime > 0.0 && bwriteavg == false) {
pTimeTotal = calcRunTime;
}
ios << '\n' << '\n';
if( ! bwriteavg) {
ios << std::setfill('*')
<< std::setw(maxlen + 2 + 3 * (colWidth + 2) - (colWidth+12)) << "";
ios << std::setfill(' ');
ios << " Processor: " << std::setw(colWidth) << myProc << '\n';
}
// -------- write timers sorted by name
BLProfilerUtils::WriteHeader(ios, colWidth, maxlen, bwriteavg);
for(std::map<std::string, BLProfiler::ProfStats>::const_iterator it = mpStats.begin();
it != mpStats.end(); ++it)
{
if(pTimeTotal > 0.0) {
if(bwriteavg) {
percent = 100.0 * (it->second.avgTime / pTimeTotal);
} else {
percent = 100.0 * (it->second.totalTime / pTimeTotal);
}
} else {
percent = 100.0;
}
std::string fname(it->first);
const BLProfiler::ProfStats &pstats = it->second;
BLProfilerUtils::WriteRow(ios, fname, pstats, percent, colWidth, maxlen, bwriteavg);
}
ios << '\n';
ios << "Total Timers = " << std::setw(colWidth) << totalTimers
<< " seconds." << '\n';
if(calcRunTime > 0.0) {
percent = 100.0 * totalTimers / calcRunTime;
ios << "Calc Run Time = " << std::setw(colWidth) << calcRunTime
<< " seconds." << '\n';
ios << "Percent Coverage = " << std::setw(colWidth) << percent << " %" << '\n';
}
// -------- write timers sorted by percent
ios << '\n' << '\n';
BLProfilerUtils::WriteHeader(ios, colWidth, maxlen, bwriteavg);
for(std::map<std::string, BLProfiler::ProfStats>::const_iterator it = mpStats.begin();
it != mpStats.end(); ++it)
{
Real dsec;
if(bwriteavg) {
dsec = it->second.avgTime;
} else {
dsec = it->second.totalTime;
}
std::string sfir(it->first);
mTimersTotalsSorted.insert(std::make_pair(dsec, sfir));
}
for(std::map<Real, std::string>::const_iterator it = mTimersTotalsSorted.begin();
it != mTimersTotalsSorted.end(); ++it)
{
if(pTimeTotal > 0.0) {
percent = 100.0 * (it->first / pTimeTotal);
} else {
percent = 100.0;
}
std::string fname(it->second);
std::map<std::string, BLProfiler::ProfStats>::const_iterator mpsit = mpStats.find(fname);
if(mpsit != mpStats.end()) {
const BLProfiler::ProfStats &pstats = mpsit->second;
BLProfilerUtils::WriteRow(ios, fname, pstats, percent, colWidth, maxlen, bwriteavg);
} else {
// error: should not be able to get here if names are synced
}
}
if(bwriteavg) {
ios << std::setfill('=') << std::setw(maxlen+4 + 7 * (colWidth+2)) << ""
<< '\n';
} else {
ios << std::setfill('=') << std::setw(maxlen+4 + 3 * (colWidth+2)) << ""
<< '\n';
}
ios << std::setfill(' ');
ios << '\n';
#ifdef BL_TRACE_PROFILING
// -------- write timers sorted by inclusive times
Vector<std::string> fNumberNames(fnameNumbers.size());
for(std::map<std::string, int>::const_iterator it = fnameNumbers.begin();
it != fnameNumbers.end(); ++it)
{
fNumberNames[it->second] = it->first;
}
// sort by total time
Vector<BLProfiler::RIpair> funcTotalTimes(fnameNumbers.size());
for(int i(0); i < funcTotalTimes.size(); ++i) {
funcTotalTimes[i].first = 0.0;
funcTotalTimes[i].second = i;
}
Vector<int> callStack(64, -1);
int maxCSD(0);
std::set<int> recursiveFuncs;
for(int i(0); i < callTraces.size(); ++i) {
const BLProfiler::CallStats &cs = callTraces[i];
if(cs.csFNameNumber < 0) { // ---- an unused cs
continue;
}
int depth(cs.callStackDepth);
maxCSD = std::max(maxCSD, depth);
if(depth >= callStack.size()) {
callStack.resize(depth + 1);
}
callStack[depth] = cs.csFNameNumber;
bool recursiveCall(false);
for(int d(0); d < depth; ++d) {
if(cs.csFNameNumber == callStack[d]) {
recursiveFuncs.insert(cs.csFNameNumber);
recursiveCall = true;
}
}
if( ! recursiveCall) {
funcTotalTimes[cs.csFNameNumber].first += cs.totalTime;
}
}
ios << " MaxCallStackDepth = " << maxCSD << '\n';
for(std::set<int>::iterator rfi = recursiveFuncs.begin(); rfi != recursiveFuncs.end(); ++rfi) {
ios << " Recursive function: " << fNumberNames[*rfi] << '\n';
}
ios << '\n';
if(bwriteinclusivetimes) {
std::sort(funcTotalTimes.begin(), funcTotalTimes.end(), BLProfiler::fTTComp());
int numPrec(4);
ios << '\n' << '\n';
ios << std::setfill('-') << std::setw(maxlen+4 + 1 * (colWidth+2))
<< std::left << "Inclusive times " << '\n';
ios << std::right << std::setfill(' ');
ios << std::setw(maxlen + 2) << "Function Name"
<< std::setw(colWidth + 4) << "Time s"
<< '\n';
for(int i(0); i < funcTotalTimes.size(); ++i) {
ios << std::setw(maxlen + 2) << fNumberNames[funcTotalTimes[i].second] << " "
<< std::setprecision(numPrec) << std::fixed << std::setw(colWidth)
<< funcTotalTimes[i].first << " s"
<< '\n';
}
ios << std::setfill('=') << std::setw(maxlen+4 + 1 * (colWidth+2)) << ""
<< '\n';
ios << std::setfill(' ');
ios << '\n';
}
#endif
}
} // end namespace BLProfilerUtils
std::ostream &operator<< (std::ostream &os, const BLProfiler::CommStats &cs) {
os << BLProfiler::CommStats::CFTToString(cs.cfType) << " " << cs.size
<< " " << cs.commpid << " " << cs.tag << " " << cs.timeStamp;
return os;
}
void BLProfiler::WriteBaseProfile(bool bFlushing, bool memCheck) { // ---- write basic profiling data
amrex::ignore_unused(memCheck);
// --------------------------------------- gather global stats
Real baseProfStart(amrex::second()); // time the timer
const int nProcs(ParallelDescriptor::NProcs());
//const int myProc(ParallelDescriptor::MyProc());
const int iopNum(ParallelDescriptor::IOProcessorNumber());
// -------- make sure the set of profiled functions is the same on all processors
Vector<std::string> localStrings, syncedStrings;
bool alreadySynced;
for(std::map<std::string, ProfStats>::const_iterator it = mProfStats.begin();
it != mProfStats.end(); ++it)
{
localStrings.push_back(it->first);
}
amrex::SyncStrings(localStrings, syncedStrings, alreadySynced);
if( ! alreadySynced) { // ---- add the new name
for(int i(0); i < syncedStrings.size(); ++i) {
std::map<std::string, ProfStats>::const_iterator it =
mProfStats.find(syncedStrings[i]);
if(it == mProfStats.end()) {
ProfStats ps;
mProfStats.insert(std::pair<std::string, ProfStats>(syncedStrings[i], ps));
}
}
}
// ---- add the following names if they have not been called already
// ---- they will be called below to write the database and the names
// ---- need to be in the database before it is written
Vector<std::string> addNames;
addNames.push_back("ParallelDescriptor::Send(Tsii)i");
addNames.push_back("ParallelDescriptor::Recv(Tsii)i");
addNames.push_back("ParallelDescriptor::Gather(TsT1si)d");
addNames.push_back("ParallelDescriptor::Gather(TsT1si)l");
for(int iname(0); iname < addNames.size(); ++iname) {
std::map<std::string, ProfStats>::iterator it = mProfStats.find(addNames[iname]);
if(it == mProfStats.end()) {
// amrex::Print() << "BLProfiler::Finalize: adding name: " << addNames[iname] << "\n";
ProfStats ps;
mProfStats.insert(std::pair<std::string, ProfStats>(addNames[iname], ps));
}
}
// Print to std::out if this is a Finalize call
// or if user sets print on flushes.
// Should generally be turned off as this requires synchronization.
if ((!bFlushing) || (bFlushPrint)) {
// ---------------------------------- now collect global data onto the ioproc
int maxlen(0);
Vector<Real> gtimes(1);
Vector<Long> ncalls(1);
if(ParallelDescriptor::IOProcessor()) {
gtimes.resize(nProcs);
ncalls.resize(nProcs);
}
for(std::map<std::string, ProfStats>::const_iterator it = mProfStats.begin();
it != mProfStats.end(); ++it)
{
std::string profName(it->first);
int pnLen(profName.size());
maxlen = std::max(maxlen, pnLen);
ProfStats &pstats = mProfStats[profName];
if(nProcs == 1) {
gtimes[0] = pstats.totalTime;
ncalls[0] = pstats.nCalls;
} else {
ParallelDescriptor::Gather(&pstats.totalTime, 1, gtimes.dataPtr(), 1, iopNum);
ParallelDescriptor::Gather(&pstats.nCalls, 1, ncalls.dataPtr(), 1, iopNum);
}
Real tsum(0.0), tmin(gtimes[0]), tmax(gtimes[0]), tavg(0.0), variance(0.0);
Long ncsum(0);
if(ParallelDescriptor::IOProcessor()) {
for(int i(0); i < gtimes.size(); ++i) {
tsum += gtimes[i];
tmin = std::min(tmin, gtimes[i]);
tmax = std::max(tmax, gtimes[i]);
}
tavg = tsum / static_cast<Real> (gtimes.size());
for(int i(0); i < gtimes.size(); ++i) {
variance += (gtimes[i] - tavg) * (gtimes[i] - tavg);
}
pstats.minTime = tmin;
pstats.maxTime = tmax;
pstats.avgTime = tavg;
pstats.variance = variance / static_cast<Real> (gtimes.size()); // n - 1 for sample
for(int i(0); i < ncalls.size(); ++i) {
ncsum += ncalls[i];
}
// uncomment for reporting total calls summed over all procs
//pstats.nCalls = ncsum;
}
}
// --------------------------------------- print global stats to cout
if(ParallelDescriptor::IOProcessor()) {
bool bWriteAvg(true);
if(nProcs == 1) {
bWriteAvg = false;
}
BLProfilerUtils::WriteStats(amrex::OutStream(), mProfStats, mFNameNumbers, vCallTrace, bWriteAvg);
}
}
// --------------------------------------- print all procs stats to a file
if(bWriteAll) {
// ----
// ---- if we use an unordered_map for mProfStats, copy to a sorted container
// ----
ParallelDescriptor::Barrier(); // ---- wait for everyone (remove after adding filters)
Vector<Long> nCallsOut(mProfStats.size(), 0);
Vector<Real> totalTimesOut(mProfStats.size(), 0.0);
int count(0);
for(std::map<std::string, ProfStats>::const_iterator phit = mProfStats.begin();
phit != mProfStats.end(); ++phit)
{
nCallsOut[count] = phit->second.nCalls;
totalTimesOut[count] = phit->second.totalTime;
++count;
}
std::string cdir(blProfDirName);
if( ! blProfDirCreated) {
amrex::UtilCreateCleanDirectory(cdir);
blProfDirCreated = true;
}
const int nOutFiles = std::max(1, std::min(nProcs, nProfFiles));
std::string phFilePrefix("bl_prof");
std::string cFileName(cdir + '/' + phFilePrefix + "_D_");
Long seekPos(0);
bool setBuf(true);
NFilesIter nfi(nOutFiles, cFileName, groupSets, setBuf);
for( ; nfi.ReadyToWrite(); ++nfi) {
seekPos = nfi.SeekPos();
if(nCallsOut.size() > 0) {
nfi.Stream().write((char *) nCallsOut.dataPtr(),
nCallsOut.size() * sizeof(Long));
}
if(totalTimesOut.size() > 0) {
nfi.Stream().write((char *) totalTimesOut.dataPtr(),
totalTimesOut.size() * sizeof(Real));
}
}
Vector<Long> seekPosOut(1);
if(ParallelDescriptor::IOProcessor()) {
seekPosOut.resize(nProcs, 0);
}
ParallelDescriptor::Gather(&seekPos, 1, seekPosOut.dataPtr(), 1, iopNum);
if(ParallelDescriptor::IOProcessor()) {
std::string phFileName(cdir + '/' + phFilePrefix + "_H");
std::ofstream phHeaderFile;
phHeaderFile.open(phFileName.c_str(), std::ios::out | std::ios::trunc);
phHeaderFile << "BLProfVersion " << BLProfVersion << '\n';
phHeaderFile << "NProcs " << nProcs << '\n';
phHeaderFile << "NOutFiles " << nOutFiles << '\n';
for(std::map<std::string, ProfStats>::const_iterator phit = mProfStats.begin();
phit != mProfStats.end(); ++phit)
{
phHeaderFile << "phFName " << '"' << phit->first << '"' << '\n';
}
std::string dFileName(phFilePrefix + "_D_");
for(int p(0); p < nProcs; ++p) {
std::string dFullName(NFilesIter::FileName(nOutFiles, dFileName, p, groupSets));
phHeaderFile << "BLProfProc " << p << " datafile " << dFullName
<< " seekpos " << seekPosOut[p] << '\n';
}
phHeaderFile << "calcEndTime " << std::setprecision(16)
<< amrex::second() - startTime << '\n';
phHeaderFile.close();
}
BL_PROFILE_REGION_STOP(noRegionName);
ParallelDescriptor::Barrier("BLProfiler::Finalize");
}
amrex::Print() << "BLProfiler::Finalize(): time: " // time the timer
<< amrex::second() - baseProfStart << "\n";
}
void BLProfiler::WriteCallTrace(bool bFlushing, bool memCheck) { // ---- write call trace data
if(memCheck) {
int nCT(vCallTrace.size());
ParallelDescriptor::ReduceIntMax(nCT);
bool doFlush(nCT > traceFlushSize);
if(doFlush) {
amrex::Print() << "Flushing call traces: nCT traceFlushSize = " << nCT
<< " " << traceFlushSize << "\n";
} else {
amrex::Print() << "Bypassing call trace flush, nCT < traceFlushSize: " << nCT
<< " " << traceFlushSize << "\n";
return;
}
}
Real wctStart(amrex::second()); // time the timer
std::string cdir(blProfDirName);
const int myProc = ParallelDescriptor::MyProc();
const int nProcs = ParallelDescriptor::NProcs();
const int nOutFiles = std::max(1, std::min(nProcs, nProfFiles));
std::string cFilePrefix("bl_call_stats");
std::string cFileName(cdir + '/' + cFilePrefix + "_D_");
if( ! blProfDirCreated) {
amrex::UtilCreateCleanDirectory(cdir);
blProfDirCreated = true;
}
// -------- make sure the set of region names is the same on all processors
Vector<std::string> localStrings, syncedStrings;
bool alreadySynced;
for(std::map<std::string, int>::iterator it = mRegionNameNumbers.begin();
it != mRegionNameNumbers.end(); ++it)
{
localStrings.push_back(it->first);