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