Block-Structured AMR Software Framework
AMReX_BLProfStats.H
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 // BLProfStats.H
3 // ----------------------------------------------------------------------
4 #ifndef BL_BLPROFSTATS_H
5 #define BL_BLPROFSTATS_H
6 #include <AMReX_Config.H>
7 
8 #include <AMReX_BLProfiler.H>
9 #include <AMReX_SPACE.H>
10 #include <AMReX_IntVect.H>
11 #include <AMReX_Box.H>
12 #include <AMReX_FArrayBox.H>
13 #include <AMReX_MultiFab.H>
14 #include <cstdio>
15 #include <iostream>
16 #include <fstream>
17 #include <string>
18 #include <set>
19 #include <map>
20 #include <sstream>
21 
22 namespace {
23  static std::stringstream hs;
24 }
25 
26 using amrex::Real;
27 
28 
29 // ----------------------------------------------------------------------
30 class BLProfStats {
31  public:
32 
34 
35  struct BLPDataBlock {
37  : proc(0), fileName(""), seekpos(0), readoffset(0),
38  streamIndex(-1)
39  { }
40  BLPDataBlock(int p, const std::string &fn, long sp, int si)
41  : proc(p), fileName(fn), seekpos(sp),
42  readoffset(0), streamIndex(si)
43  { }
44 
45  int proc;
46  std::string fileName;
47  long seekpos;
48  long readoffset;
50 
53  };
54 
55  struct FuncStat {
57  : nCalls(0), totalTime(0.0)
58  { }
59 
60  FuncStat(long ncalls, Real tt)
61  : nCalls(ncalls), totalTime(tt)
62  { }
63 
64  long nCalls;
65  Real totalTime;
66  };
67 
68  struct CallTreeNode {
70  : fnameNumber(-1), stackDepth(-1), nCalls(0), totalTime(0.0), stackTime(0.0)
71  { }
72 
74  long nCalls;
76  std::map<int, CallTreeNode> calledFunctions; // [fnn, ctn]
77  };
78 
79  struct TimeRange {
80  TimeRange() : startTime(0.0), stopTime(0.0) { }
81  TimeRange(Real startT, Real stopT) : startTime(startT), stopTime(stopT) { }
82 
84  bool Contains(Real t) const {
85  return(t >= startTime && t <= stopTime);
86  }
87  bool operator==(const TimeRange &rhs) const {
88  return((startTime == rhs.startTime) && (stopTime == rhs.stopTime));
89  }
90  bool operator!=(const TimeRange &rhs) const {
91  return((startTime != rhs.startTime) || (stopTime != rhs.stopTime));
92  }
94  if (rhs != *this)
95  {
96  startTime = rhs.startTime;
97  stopTime = rhs.stopTime;
98  }
99  return *this;
100  }
101  std::ostream& operator<<(std::ostream& os) {
102  os << startTime << " - " << stopTime;
103  if (os.fail()) { amrex::Error("output for TimeRange failed"); }
104  return os;
105  }
106  };
107 
109  bool operator()(const TimeRange &lhs, const TimeRange &rhs) const {
110  return lhs.startTime < rhs.startTime;
111  }
112  };
113 
114  BLProfStats();
115  ~BLProfStats();
116  static int Verbose() { return verbose; }
117  static void SetVerbose(int vlevel = 0) { verbose = vlevel; }
118  static void SetDirName(const std::string &dirname) { dirName = dirname; }
119  static void SetNOutFiles(const int nof) { nOutFiles = nof; }
120  static int GetNOutFiles() { return nOutFiles; }
121 
122  void AddRegionName(const std::string &rname, int rnumber);
123  std::set<int> WhichRegions(int proc, Real t);
124 
125  void SetProc(int p) { currentProc = p; }
126  int GetProc() const { return currentProc; }
127 
128  static void SetNProcs(int p) { dataNProcs = p; }
129  static int GetNProcs() { return dataNProcs; }
130 
131  void SetMaxRNumber(int n) { maxRNumber = n; }
132  int GetMaxRNumber() const { return maxRNumber; }
133 
134  void InitFilterTimeRanges();
135  void SetFilterTimeRanges(const amrex::Vector<std::list<TimeRange> > &ftr);
137  { return filterTimeRanges; }
138 
140  { regionTimeRanges = rtr; }
141  void SetRegionTimeRanges(const TimeRange &tr, const int n, const int r, const int t)
142  { regionTimeRanges[n][r][t] = tr; }
144  { return regionTimeRanges; }
145 
146  const std::map<std::string, int> &RegionNames() { return regionNames; }
147  const std::map<int, std::string> &RegionNumbers() { return regionNumbers; }
148 
149  //const std::map<std::string, int> &BarrierNamesToNumbers() { return barrierNamesToNumbers; }
150  const std::map<int, std::string> &BarrierNumbersToNames() { return barrierNumbersToNames; }
151 
152  void MakeFilterFile(const std::string &ffname);
153  void SetFilter(FilterStatus fs, const std::string &rname, int rnumber);
154  void SetFilter(FilterStatus fs);
155 
156  static bool AddPiece(std::list<TimeRange> &addToHere,
157  const TimeRange &pieceToAdd);
158  static std::list<TimeRange> RangeIntersection(std::list<TimeRange> &rangeList,
159  const TimeRange &pieceToIntersect);
160  static bool RemovePiece(std::list<TimeRange> &removeFromHere,
161  const TimeRange &pieceToRemove);
162 
163  virtual void SetBLPVersion(const int blpv) { blProfVersion = blpv; }
164  virtual void AddFunctionName(const std::string &fname);
165  virtual void InitBLProfDataBlock(const int proc, const std::string &filename,
166  const long seekpos);
167  virtual void CollectFuncStats(amrex::Vector<amrex::Vector<FuncStat> > &funcStats);
168  virtual void WriteSummary(std::ostream &ios, bool bwriteavg = false, int whichProc = 0,
169  bool graphTopPct = true);
170  virtual void AddCalcEndTime(double cet) { calcEndTime = cet; }
171 
172  virtual void SetCPVersion(const int /*cpv*/) { }
173  virtual void SetCSSize(const int /*css*/) { }
174  virtual void InitCommDataBlock(const int /*proc*/, const long /*ncommstats*/,
175  const std::string &/*filename*/, const long /*seekpos*/,
176  const std::string &/*nodename*/ = "",
177  const int /*nodenumber*/ = -1) { }
178  virtual void AddBarrier(long /*bnum*/, const std::string &/*bname*/, long /*index*/) { }
179  virtual void AddReduction(const long /*rnum*/, const long /*index*/) { }
180  virtual void AddTimeMinMax(const double /*tmin*/, const double /*tmax*/) { }
181  virtual void AddTimerTime(const double /*tt*/) { }
182  virtual void AddNameTag(const long /*ntnindex*/, const long /*seekindex*/) { }
183  virtual void AddNameTagName(const std::string &/*name*/) { }
184  virtual void AddTagRange(const long /*tmin*/, const long /*tmax*/) { }
185  virtual void AddGridLevel(const int /*level*/, const int /*ngrids*/) { }
186  virtual void AddGrid3D(int /*level*/, int /*xlo*/, int /*ylo*/, int /*zlo*/,
187  int /*xhi*/, int /*yhi*/, int /*zhi*/,
188  int /*xc*/, int /*yc*/, int /*zc*/,
189  int /*xn*/, int /*yn*/, int /*zn*/, int /*proc*/) { }
190  virtual void AddFinestLevel(const int /*lev*/) { }
191  virtual void AddMaxLevel(const int /*lev*/) { }
192  virtual void AddRefRatio(const int /*lev*/, const amrex::IntVect &/*rr*/) { }
193  virtual void AddTopoCoord(const int /*nid*/, const int /*node*/,
194  const int /*tx*/, const int /*ty*/, const int /*tz*/,
195  const bool /*servicenode*/ = false) { }
196 
197  virtual void AddCommHeaderFileName(const std::string &/*hfn*/) { }
198  virtual void SetCSVersion(int /*csv*/) { }
199  virtual void AddCStatsHeaderFileName(const std::string &/*hfn*/) { }
200  virtual void AddFunctionName(const std::string &/*fname*/, int /*fnumber*/) { }
201  virtual void InitCStatsDataBlock(int /*proc*/, long /*nrss*/, long /*ntracestats*/,
202  const std::string &/*filename*/, long /*seekpos*/) { }
203  static void SetInitDataBlocks(bool b) { bInitDataBlocks = b; }
204  static bool InitDBlocks() { return bInitDataBlocks; }
205 
206  static void SetGPercent(Real p) { gPercent = p/100.0; }
207 
208  virtual void AddProbDomain(const int /*lev*/, const amrex::Box &/*pd*/) { }
209  virtual TimeRange MakeRegionPlt(amrex::FArrayBox &/*rFab*/, int /*noregionnumber*/,
210  int /*width*/, int /*height*/,
211  amrex::Vector<amrex::Vector<amrex::Box>> &/*regionBoxes*/)
212  { return TimeRange(); }
213 
214  static void OpenAllStreams(const std::string &dirname);
215  static void CloseAllStreams();
216 
217  virtual void AddEdisonPID(int /*X*/, int /*Y*/, int /*Z*/,
218  int /*col*/, int /*row*/, int /*cage*/, int /*slot*/,
219  int /*cpu*/, int /*pid*/) { }
220 
221  static bool TimeRangeInitialized() { return bTimeRangeInitialized; }
222 
223  void CheckData();
224 
226  void SetFNames(const amrex::Vector<std::string> &fNames) { blpFNames = fNames; }
227 
228  protected:
229  static int verbose;
234  static std::string dirName;
235  static bool bInitDataBlocks;
236 
238  bool InTimeRange(int proc, Real calltime);
239 
240  std::map<std::string, int> regionNames; // [rname, rnumber]
241  std::map<int, std::string> regionNumbers; // [rnumber, rname]
242 
243  std::map<int, std::string> barrierNumbersToNames; // [bnumber, bname]
244 
245  std::set<int> includeSet, excludeSet;
247 
250 
251  static std::map<std::string, int> blpDataFileNames; // [filename, stream index]
252 
255 
256  static Real gPercent;
257 
259 
260  private:
261 
262  void ReadBlock(BLPDataBlock &dBlock); // reads whole block
263  void ReadBlockNoOpen(BLPDataBlock &dBlock); // files must be open already
264  void ClearBlock(BLPDataBlock &dBlock);
265 
266  friend int yyparse(void *);
267 };
268 
269 std::ostream &operator<< (std::ostream &os, const BLProfStats::TimeRange &tr);
270 
271 #endif
272 // ----------------------------------------------------------------------
273 // ----------------------------------------------------------------------
std::ostream & operator<<(std::ostream &os, const BLProfStats::TimeRange &tr)
Definition: AMReX_BLProfStats.cpp:344
Definition: AMReX_BLProfStats.H:30
static int nOutFiles
Definition: AMReX_BLProfStats.H:230
virtual void SetBLPVersion(const int blpv)
Definition: AMReX_BLProfStats.H:163
void InitFilterTimeRanges()
Definition: AMReX_BLProfStats.cpp:276
int maxRNumber
Definition: AMReX_BLProfStats.H:231
int GetMaxRNumber() const
Definition: AMReX_BLProfStats.H:132
static amrex::Vector< std::ifstream * > blpDataStreams
Definition: AMReX_BLProfStats.H:258
std::map< int, std::string > regionNumbers
Definition: AMReX_BLProfStats.H:241
virtual void WriteSummary(std::ostream &ios, bool bwriteavg=false, int whichProc=0, bool graphTopPct=true)
Definition: AMReX_BLProfStats.cpp:516
virtual void InitCStatsDataBlock(int, long, long, const std::string &, long)
Definition: AMReX_BLProfStats.H:201
virtual void AddCalcEndTime(double cet)
Definition: AMReX_BLProfStats.H:170
static int GetNOutFiles()
Definition: AMReX_BLProfStats.H:120
int currentDataBlock
Definition: AMReX_BLProfStats.H:231
static void SetNOutFiles(const int nof)
Definition: AMReX_BLProfStats.H:119
bool InTimeRange(int proc, Real calltime)
Definition: AMReX_BLProfStats.cpp:319
void AddRegionName(const std::string &rname, int rnumber)
Definition: AMReX_BLProfStats.cpp:200
virtual void AddCommHeaderFileName(const std::string &)
Definition: AMReX_BLProfStats.H:197
amrex::Vector< std::list< TimeRange > > filterTimeRanges
Definition: AMReX_BLProfStats.H:249
static std::list< TimeRange > RangeIntersection(std::list< TimeRange > &rangeList, const TimeRange &pieceToIntersect)
Definition: AMReX_BLProfStats.cpp:107
virtual void SetCSVersion(int)
Definition: AMReX_BLProfStats.H:198
static void SetGPercent(Real p)
Definition: AMReX_BLProfStats.H:206
std::set< int > includeSet
Definition: AMReX_BLProfStats.H:245
friend int yyparse(void *)
void ClearBlock(BLPDataBlock &dBlock)
Definition: AMReX_BLProfStats.cpp:483
virtual void InitBLProfDataBlock(const int proc, const std::string &filename, const long seekpos)
Definition: AMReX_BLProfStats.cpp:363
virtual void AddTimerTime(const double)
Definition: AMReX_BLProfStats.H:181
virtual void AddTagRange(const long, const long)
Definition: AMReX_BLProfStats.H:184
void ReadBlock(BLPDataBlock &dBlock)
Definition: AMReX_BLProfStats.cpp:446
const std::map< std::string, int > & RegionNames()
Definition: AMReX_BLProfStats.H:146
virtual void AddBarrier(long, const std::string &, long)
Definition: AMReX_BLProfStats.H:178
static int GetNProcs()
Definition: AMReX_BLProfStats.H:129
static void SetDirName(const std::string &dirname)
Definition: AMReX_BLProfStats.H:118
virtual void AddFunctionName(const std::string &, int)
Definition: AMReX_BLProfStats.H:200
static void SetVerbose(int vlevel=0)
Definition: AMReX_BLProfStats.H:117
virtual void AddNameTag(const long, const long)
Definition: AMReX_BLProfStats.H:182
int currentProc
Definition: AMReX_BLProfStats.H:231
static std::string dirName
Definition: AMReX_BLProfStats.H:234
Real maxRegionTime
Definition: AMReX_BLProfStats.H:233
static void SetNProcs(int p)
Definition: AMReX_BLProfStats.H:128
virtual void AddMaxLevel(const int)
Definition: AMReX_BLProfStats.H:191
amrex::Vector< amrex::Vector< amrex::Vector< TimeRange > > > regionTimeRanges
Definition: AMReX_BLProfStats.H:248
static bool bTimeRangeInitialized
Definition: AMReX_BLProfStats.H:237
void SetMaxRNumber(int n)
Definition: AMReX_BLProfStats.H:131
virtual void SetCSSize(const int)
Definition: AMReX_BLProfStats.H:173
bool bDefaultInclude
Definition: AMReX_BLProfStats.H:246
static bool bInitDataBlocks
Definition: AMReX_BLProfStats.H:235
std::set< int > WhichRegions(int proc, Real t)
Definition: AMReX_BLProfStats.cpp:208
virtual void AddCStatsHeaderFileName(const std::string &)
Definition: AMReX_BLProfStats.H:199
static bool InitDBlocks()
Definition: AMReX_BLProfStats.H:204
static void SetInitDataBlocks(bool b)
Definition: AMReX_BLProfStats.H:203
virtual TimeRange MakeRegionPlt(amrex::FArrayBox &, int, int, int, amrex::Vector< amrex::Vector< amrex::Box >> &)
Definition: AMReX_BLProfStats.H:209
static std::map< std::string, int > blpDataFileNames
Definition: AMReX_BLProfStats.H:251
~BLProfStats()
Definition: AMReX_BLProfStats.cpp:195
const std::map< int, std::string > & RegionNumbers()
Definition: AMReX_BLProfStats.H:147
static void CloseAllStreams()
Definition: AMReX_BLProfStats.cpp:399
void SetFilterTimeRanges(const amrex::Vector< std::list< TimeRange > > &ftr)
Definition: AMReX_BLProfStats.cpp:269
static void OpenAllStreams(const std::string &dirname)
Definition: AMReX_BLProfStats.cpp:383
amrex::Vector< std::string > blpFNames
Definition: AMReX_BLProfStats.H:253
virtual void InitCommDataBlock(const int, const long, const std::string &, const long, const std::string &="", const int=-1)
Definition: AMReX_BLProfStats.H:174
virtual void AddGridLevel(const int, const int)
Definition: AMReX_BLProfStats.H:185
virtual void AddFunctionName(const std::string &fname)
Definition: AMReX_BLProfStats.cpp:352
static bool RemovePiece(std::list< TimeRange > &removeFromHere, const TimeRange &pieceToRemove)
Definition: AMReX_BLProfStats.cpp:127
virtual void AddFinestLevel(const int)
Definition: AMReX_BLProfStats.H:190
virtual void AddProbDomain(const int, const amrex::Box &)
Definition: AMReX_BLProfStats.H:208
virtual void SetCPVersion(const int)
Definition: AMReX_BLProfStats.H:172
void ReadBlockNoOpen(BLPDataBlock &dBlock)
Definition: AMReX_BLProfStats.cpp:476
virtual void AddNameTagName(const std::string &)
Definition: AMReX_BLProfStats.H:183
std::map< int, std::string > barrierNumbersToNames
Definition: AMReX_BLProfStats.H:243
virtual void AddEdisonPID(int, int, int, int, int, int, int, int, int)
Definition: AMReX_BLProfStats.H:217
void CheckData()
Definition: AMReX_BLProfStats.cpp:410
int GetProc() const
Definition: AMReX_BLProfStats.H:126
void SetFilter(FilterStatus fs, const std::string &rname, int rnumber)
Definition: AMReX_BLProfStats.cpp:245
static Real gPercent
Definition: AMReX_BLProfStats.H:256
static bool AddPiece(std::list< TimeRange > &addToHere, const TimeRange &pieceToAdd)
Definition: AMReX_BLProfStats.cpp:76
const amrex::Vector< std::list< TimeRange > > & GetFilterTimeRanges() const
Definition: AMReX_BLProfStats.H:136
virtual void AddReduction(const long, const long)
Definition: AMReX_BLProfStats.H:179
virtual void AddTimeMinMax(const double, const double)
Definition: AMReX_BLProfStats.H:180
void SetRegionTimeRanges(const TimeRange &tr, const int n, const int r, const int t)
Definition: AMReX_BLProfStats.H:141
FilterStatus
Definition: AMReX_BLProfStats.H:33
@ OFF
Definition: AMReX_BLProfStats.H:33
@ INCLUDENONE
Definition: AMReX_BLProfStats.H:33
@ INCLUDEALL
Definition: AMReX_BLProfStats.H:33
@ ON
Definition: AMReX_BLProfStats.H:33
@ UNDEFINED
Definition: AMReX_BLProfStats.H:33
amrex::Vector< BLPDataBlock > blpDataBlocks
Definition: AMReX_BLProfStats.H:254
static int Verbose()
Definition: AMReX_BLProfStats.H:116
virtual void CollectFuncStats(amrex::Vector< amrex::Vector< FuncStat > > &funcStats)
Definition: AMReX_BLProfStats.cpp:492
void SetFNames(const amrex::Vector< std::string > &fNames)
Definition: AMReX_BLProfStats.H:226
static int verbose
Definition: AMReX_BLProfStats.H:229
virtual void AddGrid3D(int, int, int, int, int, int, int, int, int, int, int, int, int, int)
Definition: AMReX_BLProfStats.H:186
static int dataNProcs
Definition: AMReX_BLProfStats.H:230
const std::map< int, std::string > & BarrierNumbersToNames()
Definition: AMReX_BLProfStats.H:150
void SetProc(int p)
Definition: AMReX_BLProfStats.H:125
virtual void AddRefRatio(const int, const amrex::IntVect &)
Definition: AMReX_BLProfStats.H:192
std::map< std::string, int > regionNames
Definition: AMReX_BLProfStats.H:240
void MakeFilterFile(const std::string &ffname)
Definition: AMReX_BLProfStats.cpp:227
const amrex::Vector< amrex::Vector< amrex::Vector< TimeRange > > > & GetRegionTimeRanges()
Definition: AMReX_BLProfStats.H:143
void SetRegionTimeRanges(const amrex::Vector< amrex::Vector< amrex::Vector< TimeRange > > > &rtr)
Definition: AMReX_BLProfStats.H:139
Real minRegionTime
Definition: AMReX_BLProfStats.H:233
BLProfStats()
Definition: AMReX_BLProfStats.cpp:184
const amrex::Vector< std::string > & BLPFNames()
Definition: AMReX_BLProfStats.H:225
std::set< int > excludeSet
Definition: AMReX_BLProfStats.H:245
Real calcEndTime
Definition: AMReX_BLProfStats.H:232
static bool TimeRangeInitialized()
Definition: AMReX_BLProfStats.H:221
virtual void AddTopoCoord(const int, const int, const int, const int, const int, const bool=false)
Definition: AMReX_BLProfStats.H:193
static int blProfVersion
Definition: AMReX_BLProfStats.H:230
A Fortran Array of REALs.
Definition: AMReX_FArrayBox.H:229
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition: AMReX.cpp:219
Definition: AMReX_BLProfStats.H:35
amrex::Vector< long > nCalls
Definition: AMReX_BLProfStats.H:51
long readoffset
Definition: AMReX_BLProfStats.H:48
std::string fileName
Definition: AMReX_BLProfStats.H:46
int streamIndex
Definition: AMReX_BLProfStats.H:49
BLPDataBlock()
Definition: AMReX_BLProfStats.H:36
amrex::Vector< Real > totalTime
Definition: AMReX_BLProfStats.H:52
int proc
Definition: AMReX_BLProfStats.H:45
long seekpos
Definition: AMReX_BLProfStats.H:47
BLPDataBlock(int p, const std::string &fn, long sp, int si)
Definition: AMReX_BLProfStats.H:40
Definition: AMReX_BLProfStats.H:68
int stackDepth
Definition: AMReX_BLProfStats.H:73
Real totalTime
Definition: AMReX_BLProfStats.H:75
CallTreeNode()
Definition: AMReX_BLProfStats.H:69
std::map< int, CallTreeNode > calledFunctions
Definition: AMReX_BLProfStats.H:76
long nCalls
Definition: AMReX_BLProfStats.H:74
Real stackTime
Definition: AMReX_BLProfStats.H:75
int fnameNumber
Definition: AMReX_BLProfStats.H:73
Definition: AMReX_BLProfStats.H:55
Real totalTime
Definition: AMReX_BLProfStats.H:65
FuncStat()
Definition: AMReX_BLProfStats.H:56
FuncStat(long ncalls, Real tt)
Definition: AMReX_BLProfStats.H:60
long nCalls
Definition: AMReX_BLProfStats.H:64
Definition: AMReX_BLProfStats.H:108
bool operator()(const TimeRange &lhs, const TimeRange &rhs) const
Definition: AMReX_BLProfStats.H:109
Definition: AMReX_BLProfStats.H:79
bool operator!=(const TimeRange &rhs) const
Definition: AMReX_BLProfStats.H:90
TimeRange()
Definition: AMReX_BLProfStats.H:80
TimeRange operator=(const TimeRange &rhs)
Definition: AMReX_BLProfStats.H:93
std::ostream & operator<<(std::ostream &os)
Definition: AMReX_BLProfStats.H:101
Real stopTime
Definition: AMReX_BLProfStats.H:83
TimeRange(Real startT, Real stopT)
Definition: AMReX_BLProfStats.H:81
Real startTime
Definition: AMReX_BLProfStats.H:83
bool Contains(Real t) const
Definition: AMReX_BLProfStats.H:84
bool operator==(const TimeRange &rhs) const
Definition: AMReX_BLProfStats.H:87