SST  15.1.0
StructuralSimulationToolkit
basicPerf.h
1 // Copyright 2009-2025 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2025, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_UTIL_BASIC_PERF_H
13 #define SST_CORE_UTIL_BASIC_PERF_H
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include <map>
18 #include <stack>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 namespace SST {
24 class Output;
25 
26 namespace Util {
27 
29 {
30  double begin_time = 0.0;
31  double end_time = 0.0;
32  uint64_t begin_mem = 0;
33  uint64_t end_mem = 0;
34  size_t level = 0;
35  bool last_of_level = true;
36  bool has_child = false;
37  std::string tag;
38 
39  // Roll-up data
40 
41  // First entry will be the max duration. If detailed is turned on
42  // (i.e. report all rank data), then all the rank data will come
43  // after, with index 1 being rank 0, etc.
44  std::vector<double> rollup_duration;
45 
46  // Rank with the max duration
47  int rollup_max_duration_rank = -1;
48 
49  // This tracks the begin memory. Index 0 will be the total
50  // global memory, index 1 will be the max memory on any one
51  // rank. If detailed is turned on (i.e. report all rank data),
52  // then all the rank data will come after, with index 2 being rank
53  // 0, etc.
54  std::vector<uint64_t> rollup_begin_mem;
55 
56  // Rank with the max memory
57  int rollup_begin_mem_max_rank = -1;
58 
59  // This tracks the end memory. Index 0 will be the total
60  // global memory, index 1 will be the max memory on any one
61  // rank. If detailed is turned on (i.e. report all rank data),
62  // then all the rank data will come after, with index 2 being rank
63  // 0, etc.
64  std::vector<uint64_t> rollup_end_mem;
65 
66  // Rank with the max memory
67  int rollup_end_mem_max_rank = -1;
68 
69  RegionPerfInfo(const std::string& tag, size_t level) :
70  level(level),
71  tag(tag)
72  {}
73 };
74 
75 /**
76  Class used to track various performance data during simulation
77  execution.
78 
79  Regions:
80 
81  Regions are tracked hierarchically and you can output various
82  levels of data based on the verbose value supplied to the output
83  functions. Each region is denoted using beginRegion() and
84  endRegion(). Each region can be contained with another region, but
85  it must be wholly contained within that region. For output, the
86  verbose value can be used to indicate how much detail to output.
87  The verbose value indicates how deep to print in the region
88  hierarchy. A verbose value of 0 indicates no output, 1 will output
89  only the top level regions, etc. The default verbose level is 2.
90 
91  Scalars:
92 
93  Scalars are tracked using the addMetric<>() function and are
94  retrieved by using the getMetric<>() function. These can only
95  track signed and unsigned ints and floating point numbers. All
96  types are stored as their 64-bit versions.
97  */
99 {
100 public:
101 
102  void initialize(int rank, int num_ranks);
103 
104  RegionPerfInfo getRegionPerfInfo(const std::string& name);
105 
106  /**
107  Begin a new code region
108  */
109  void beginRegion(const std::string& tag);
110 
111  /**
112  End the named region. This will cause a series of collectives
113  to gather the total and max resource utilizations for the
114  region. This data will be kept on all ranks.
115  */
116  void endRegion(const std::string& tag);
117 
118  ///// Functions to get the local and global information about the execution /////
119 
120  /**
121  Get the local begin time for the specified region
122  */
123  double getRegionBeginTime(const std::string& tag);
124 
125  /**
126  Get the local ending time for the specified region
127  */
128  double getRegionEndTime(const std::string& tag);
129 
130  /**
131  Get the local duration for the specified region
132  */
133  double getRegionDuration(const std::string& tag);
134 
135  /**
136  Get the global duration for the specified region
137  */
138  double getRegionGlobalDuration(const std::string& tag);
139 
140  ///// Functions to get the local and global information about the memory usage /////
141 
142  /**
143  Get the local memory size at begin for the specified region
144 
145  @return local memory usage in bytes
146  */
147  uint64_t getLocalRegionBeginMemSize(const std::string& tag);
148 
149  /**
150  Get the global total memory size at begin for the specified region
151 
152  @return global total memory usage in bytes
153  */
154  uint64_t getGlobalTotalRegionBeginMemSize(const std::string& tag);
155 
156  /**
157  Get the global max memory size at begin for the specified region
158 
159  @return pair of max memory usage in bytes and the rank that
160  usage was from
161  */
162  std::pair<uint64_t, int> getGlobalMaxRegionBeginMemSize(const std::string& tag);
163 
164 
165  /**
166  Get the local memory size at end for the specified region
167 
168  @return local memory usage in bytes
169  */
170  uint64_t getLocalRegionEndMemSize(const std::string& tag);
171 
172  /**
173  Get the global total memory size at end for the specified region
174 
175  @return global total memory usage in bytes
176  */
177  uint64_t getGlobalTotalRegionEndMemSize(const std::string& tag);
178 
179  /**
180  Get the global max memory size at end for the specified region
181 
182  @return pair of max memory usage in bytes and the rank that
183  usage was from
184  */
185  std::pair<uint64_t, int> getGlobalMaxRegionEndMemSize(const std::string& tag);
186 
187 
188  void addMetric(const std::string& name, uint64_t value);
189  void addMetric(const std::string& name, int64_t value);
190  void addMetric(const std::string& name, double value);
191 
192  uint64_t getMetricUnsigned(const std::string& name);
193  int64_t getMetricSigned(const std::string& name);
194  double getMetricFloat(const std::string& name);
195 
196  void outputRegionData(Output& out, size_t verbose);
197 
198 private:
199 
200  /**
201  Stores the regions in the order they are created
202  */
203  std::vector<RegionPerfInfo> regions_;
204 
205  /**
206  Stack of currently active regions
207  */
208  std::stack<size_t> current_regions_;
209 
210  /**
211  Gets the specified region from the regions_ vector. If it's
212  not there, prints an error message that references the passed
213  in function name and calls SST_Exit().
214  */
215  RegionPerfInfo& getRegion(const std::string& tag, const std::string& function_name, bool must_be_ended = true);
216 
217 
218  // Scalar storage
219  std::map<std::string, uint64_t> scalars_unsigned;
220  std::map<std::string, int64_t> scalars_signed;
221  std::map<std::string, double> scalars_float;
222 
223  int rank_;
224  int num_ranks_;
225  // TEMP
226  static int level;
227 };
228 
229 
230 } // namespace Util
231 } // namespace SST
232 
233 #endif // SST_CORE_UTIL_BASIC_PERF_H
uint64_t getGlobalTotalRegionBeginMemSize(const std::string &tag)
Get the global total memory size at begin for the specified region.
Definition: basicPerf.cc:220
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:57
double getRegionBeginTime(const std::string &tag)
Get the local begin time for the specified region.
Definition: basicPerf.cc:182
double getRegionGlobalDuration(const std::string &tag)
Get the global duration for the specified region.
Definition: basicPerf.cc:204
double getRegionDuration(const std::string &tag)
Get the local duration for the specified region.
Definition: basicPerf.cc:196
Definition: action.cc:18
uint64_t getGlobalTotalRegionEndMemSize(const std::string &tag)
Get the global total memory size at end for the specified region.
Definition: basicPerf.cc:242
Definition: basicPerf.h:28
std::pair< uint64_t, int > getGlobalMaxRegionBeginMemSize(const std::string &tag)
Get the global max memory size at begin for the specified region.
Definition: basicPerf.cc:227
uint64_t getLocalRegionBeginMemSize(const std::string &tag)
Get the local memory size at begin for the specified region.
Definition: basicPerf.cc:213
std::pair< uint64_t, int > getGlobalMaxRegionEndMemSize(const std::string &tag)
Get the global max memory size at end for the specified region.
Definition: basicPerf.cc:249
double getRegionEndTime(const std::string &tag)
Get the local ending time for the specified region.
Definition: basicPerf.cc:189
void beginRegion(const std::string &tag)
Begin a new code region.
Definition: basicPerf.cc:64
void endRegion(const std::string &tag)
End the named region.
Definition: basicPerf.cc:110
uint64_t getLocalRegionEndMemSize(const std::string &tag)
Get the local memory size at end for the specified region.
Definition: basicPerf.cc:235
Class used to track various performance data during simulation execution.
Definition: basicPerf.h:98