SST 16.0.0
Structural Simulation Toolkit
statoutput.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_STATAPI_STATOUTPUT_H
13#define SST_CORE_STATAPI_STATOUTPUT_H
14
15#include "sst/core/eli/elementinfo.h"
16#include "sst/core/module.h"
17#include "sst/core/params.h"
18#include "sst/core/serialization/serializable.h"
19#include "sst/core/sst_types.h"
20#include "sst/core/statapi/statbase.h"
21#include "sst/core/statapi/statfieldinfo.h"
22#include "sst/core/warnmacros.h"
23
24#include <cstdint>
25#include <mutex>
26#include <string>
27#include <unordered_map>
28#include <vector>
29
30// Default Settings for Statistic Output and Load Level
31#define STATISTICSDEFAULTOUTPUTNAME "sst.statOutputConsole"
32#define STATISTICSDEFAULTLOADLEVEL 0
33#define STATISTICLOADLEVELUNINITIALIZED 0xff
34
35namespace SST {
36class BaseComponent;
37class Simulation;
38namespace Statistics {
40class StatisticGroup;
41
42////////////////////////////////////////////////////////////////////////////////
43
44/**
45 \class StatisticOutput
46
47 Forms the base class for statistics output generation within the SST core.
48 Statistics are gathered by the statistic objects and then processed sent to
49 the derived output object either periodically or by event and/or also at
50 the end of the simulation. A single statistic output will be created by the
51 simulation (per node) and will collect the data per its design.
52*/
54{
55public:
56 SST_ELI_DECLARE_BASE(StatisticOutput)
57 SST_ELI_DECLARE_INFO_EXTERN(ELI::ProvidesParams)
58 SST_ELI_DECLARE_CTOR_EXTERN(SST::Params&)
59
60 using fieldType_t = StatisticFieldInfo::fieldType_t;
61 using fieldHandle_t = StatisticFieldInfo::fieldHandle_t;
62 using FieldInfoArray_t = std::vector<StatisticFieldInfo*>;
63 using FieldNameMap_t = std::unordered_map<std::string, fieldHandle_t>;
64
65public:
66 ~StatisticOutput() = default;
67
68 /** Return the Statistic Output name */
69 std::string& getStatisticOutputName() { return m_statOutputName; }
70
71 /** Return the parameters for the StatisticOutput */
72 Params& getOutputParameters() { return m_outputParameters; }
73
74 /** True if this StatOutput can handle StatisticGroups */
75 virtual bool acceptsGroups() const { return false; }
76
77 virtual void output(StatisticBase* statistic, bool endOfSimFlag) = 0;
78
79 virtual bool supportsDynamicRegistration() const { return false; }
80
81 void serialize_order(SST::Core::Serialization::serializer& ser) override;
82
83ImplementVirtualSerializable(SST::Statistics::StatisticOutput)
84
85 /////////////////
86 // Methods for Registering Fields (Called by Statistic Objects)
87 public :
88 // by default, no params to return
89 static const std::vector<SST::ElementInfoParam>& ELI_getParams()
90 {
91 static std::vector<SST::ElementInfoParam> var {};
92 return var;
93 }
94
95protected:
96 friend class SST::Simulation;
97 friend class SST::Statistics::StatisticProcessingEngine;
98
99 // Routine to have Output Check its options for validity
100 /** Have the Statistic Output check its parameters
101 * @return True if all parameters are ok; False if a parameter is missing or incorrect.
102 */
103 virtual bool checkOutputParameters() = 0;
104
105 // Simulation Events
106 /** Indicate to Statistic Output that simulation has started.
107 * Allows object to perform any setup required. */
108 virtual void startOfSimulation() = 0;
109
110 /** Indicate to Statistic Output that simulation has ended.
111 * Allows object to perform any shutdown required. */
112 virtual void endOfSimulation() = 0;
113
114 /** Have Statistic Object print out its usage and parameter info.
115 * Called when checkOutputParameters() returns false */
116 virtual void printUsage();
117
118 /** Gets the Output object for the Simulation object associeted
119 * with this StatOutput. */
121
122 /**
123 Gets the number of ranks for the simulation */
125
126 /** Gets the Rank (MPI rank and thread) that this StatisticOutput
127 is associated with. */
129
130 /** Gets the current simulstion cycle */
131 SimTime_t getCurrentSimCycle();
132
133 /**
134 Get the absolute path for the specified file taking into
135 account the specified output directory
136 */
137 std::string getAbsolutePathForOutputFile(const std::string& filename);
138
139private:
140 // Start / Stop of register Fields
141 virtual void registerStatistic(StatisticBase* stat) = 0;
142
143 void registerGroup(StatisticGroup* group);
144 void outputGroup(StatisticGroup* group, bool endOfSimFlag);
145
146 virtual void startOutputGroup(StatisticGroup* group) = 0;
147 virtual void stopOutputGroup() = 0;
148
149 virtual void startRegisterGroup(StatisticGroup* group) = 0;
150 virtual void stopRegisterGroup() = 0;
151
152 void castError();
153
154protected:
155 /** Construct a base StatisticOutput
156 * @param outputParameters - The parameters for the statistic Output.
157 */
158 explicit StatisticOutput(Params& outputParameters);
159 StatisticOutput() { ; } // For serialization only
160 void setStatisticOutputName(const std::string& name) { m_statOutputName = name; }
161
162 void lock() { m_lock.lock(); }
163 void unlock() { m_lock.unlock(); }
164
165private:
166 std::string m_statOutputName;
167 Params m_outputParameters;
168 std::recursive_mutex m_lock;
169};
170
172{
173public:
174 void registerStatistic(StatisticBase* stat) override;
175
176 // Start / Stop of output
177 void output(StatisticBase* statistic, bool endOfSimFlag) override;
178
179 void startOutputGroup(StatisticGroup* group) override;
180 void stopOutputGroup() override;
181
182 void startRegisterGroup(StatisticGroup* group) override;
183 void stopRegisterGroup() override;
184
185 // Start / Stop of output
186 /** Indicate to Statistic Output that a statistic is about to send data to be output
187 * Allows object to perform any initialization before output. */
188 virtual void implStartOutputEntries(StatisticBase* statistic) = 0;
189
190 /** Indicate to Statistic Output that a statistic is finished sending data to be output
191 * Allows object to perform any cleanup. */
192 virtual void implStopOutputEntries() = 0;
193
194 // /** Adjust the hierarchy of the fields (FUTURE SUPPORT)
195 // * @param fieldHandle - The handle of the field to adjust.
196 // * @param Level - The level of the field.
197 // * @param parent - The parent field of the field.
198 // */
199 // void setFieldHierarchy(fieldHandle_t fieldHandle, uint32_t Level, fieldHandle_t parent);
200
201 /** Return the information on a registered field via the field handle.
202 * @param fieldHandle - The handle of the registered field.
203 * @return Pointer to the registered field info.
204 */
205 // Get the Field Information object, NULL is returned if not found
206 StatisticFieldInfo* getRegisteredField(fieldHandle_t fieldHandle);
207
208 /** Return the information on a registered field via known names.
209 * @param componentName - The name of the component.
210 * @param statisticName - The name of the statistic.
211 * @param fieldName - The name of the field .
212 * @return Pointer to the registered field info.
213 */
214 // Get Registered Fields
215 // ONLY SUPPORTED TYPES ARE int32_t, uint32_t, int64_t, uint64_t, float, double
216 template <typename T>
217 StatisticFieldInfo* getRegisteredField(const char* statisticName, const char* fieldName)
218 {
219 StatisticFieldInfo* NewStatFieldInfo;
220 StatisticFieldInfo* ExistingStatFieldInfo;
221 StatisticFieldInfo::fieldType_t FieldType =
222 StatisticFieldInfo::StatisticFieldInfo::getFieldTypeFromTemplate<T>();
223
224 NewStatFieldInfo = new StatisticFieldInfo(statisticName, fieldName, FieldType);
225
226 // Now search the FieldNameMap_t of type for a matching entry
227 FieldNameMap_t::const_iterator found = m_outputFieldNameMap.find(NewStatFieldInfo->getFieldUniqueName());
228 if ( found != m_outputFieldNameMap.end() ) {
229 // We found a map entry, now get the StatFieldInfo from the m_outputFieldInfoArray at the index given by the
230 // map and then delete the NewStatFieldInfo to prevent a leak
231 ExistingStatFieldInfo = m_outputFieldInfoArray[found->second];
232 delete NewStatFieldInfo;
233 return ExistingStatFieldInfo;
234 }
235
236 delete NewStatFieldInfo;
237 return nullptr;
238 }
239
240 /** Return the array of registered field infos. */
241 FieldInfoArray_t& getFieldInfoArray() { return m_outputFieldInfoArray; }
242
243 /////////////////
244 // Methods for Outputting Fields (Called by Statistic Objects)
245 // Output fields (will call virtual functions of Derived Output classes)
246 // These aren't really part of a generic interface - optimization purposes only
247 /** Output field data.
248 * @param fieldHandle - The handle of the registered field.
249 * @param data - The data to be output.
250 */
251 virtual void outputField(fieldHandle_t fieldHandle, int32_t data);
252 virtual void outputField(fieldHandle_t fieldHandle, uint32_t data);
253 virtual void outputField(fieldHandle_t fieldHandle, int64_t data);
254 virtual void outputField(fieldHandle_t fieldHandle, uint64_t data);
255 virtual void outputField(fieldHandle_t fieldHandle, float data);
256 virtual void outputField(fieldHandle_t fieldHandle, double data);
257
258 /** Register a field to be output (templated function)
259 * @param fieldName - The name of the field.
260 * @return The handle of the registered field or -1 if type is not supported.
261 * Note: Any field names (of the same data type) that are previously
262 * registered by a statistic will return the previously
263 * handle.
264 */
265 // Field Registration
266 // ONLY SUPPORTED TYPES ARE int32_t, uint32_t, int64_t, uint64_t, float, double
267 template <typename T>
268 fieldHandle_t registerField(const char* fieldName)
269 {
270 StatisticFieldInfo::fieldType_t FieldType =
271 StatisticFieldInfo::StatisticFieldInfo::getFieldTypeFromTemplate<T>();
272
273 auto res = generateFieldHandle(addFieldToLists(fieldName, FieldType));
274 implRegisteredField(res);
275 return res;
276 }
277
278 /** Output field data.
279 * @param type - The field type to get name of.
280 * @return String name of the field type.
281 */
282 const char* getFieldTypeShortName(fieldType_t type);
283
284 void serialize_order(SST::Core::Serialization::serializer& ser) override;
285ImplementVirtualSerializable(SST::Statistics::StatisticFieldsOutput)
286
287 protected :
288 /** Construct a base StatisticOutput
289 * @param outputParameters - The parameters for the statistic Output.
290 */
291 explicit StatisticFieldsOutput(Params& outputParameters);
292
293 // For Serialization
295 m_highestFieldHandle(0),
296 m_currentFieldStatName("")
297 {}
298
299private:
300 // Other support functions
301 StatisticFieldInfo* addFieldToLists(const char* fieldName, fieldType_t fieldType);
302 fieldHandle_t generateFieldHandle(StatisticFieldInfo* FieldInfo);
303 virtual void implRegisteredField(fieldHandle_t UNUSED(fieldHandle)) {}
304
305 FieldInfoArray_t m_outputFieldInfoArray;
306 FieldNameMap_t m_outputFieldNameMap;
307 fieldHandle_t m_highestFieldHandle;
308 std::string m_currentFieldStatName;
309
310protected:
311 /** These can be overriden, if necessary, but must be callable
312 * by the derived class */
313 virtual void startRegisterFields(StatisticBase* statistic);
314 virtual void stopRegisterFields();
315
316 virtual void startOutputEntries(StatisticBase* statistic);
317 virtual void stopOutputEntries();
318};
319
320} // namespace Statistics
321} // namespace SST
322
323#endif // SST_CORE_STATAPI_STATOUTPUT_H
Main component object for the simulation.
Definition baseComponent.h:67
Definition serializable.h:25
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
Definition paramsInfo.h:41
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:58
Parameter store.
Definition params.h:65
Definition rankInfo.h:24
Main control class for a SST Simulation.
Definition simulation.h:121
Forms the base class for statistics gathering within SST.
Definition statbase.h:51
The class for representing Statistic Output Fields.
Definition statfieldinfo.h:99
std::string getFieldUniqueName() const
Return the field type related to this field info.
Definition statfieldinfo.cc:42
Definition statoutput.h:172
FieldInfoArray_t & getFieldInfoArray()
Return the array of registered field infos.
Definition statoutput.h:241
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function).
Definition statoutput.h:268
ImplementVirtualSerializable(SST::Statistics::StatisticFieldsOutput) protected StatisticFieldsOutput()
Construct a base StatisticOutput.
Definition statoutput.h:294
const char * getFieldTypeShortName(fieldType_t type)
Output field data.
Definition statoutput.cc:262
StatisticFieldInfo * getRegisteredField(fieldHandle_t fieldHandle)
Adjust the hierarchy of the fields (FUTURE SUPPORT).
Definition statoutput.cc:159
virtual void implStopOutputEntries()=0
Indicate to Statistic Output that a statistic is finished sending data to be output Allows object to ...
virtual void implStartOutputEntries(StatisticBase *statistic)=0
Indicate to Statistic Output that a statistic is about to send data to be output Allows object to per...
StatisticFieldInfo * getRegisteredField(const char *statisticName, const char *fieldName)
Return the information on a registered field via known names.
Definition statoutput.h:217
virtual void outputField(fieldHandle_t fieldHandle, int32_t data)
Output field data.
virtual void startRegisterFields(StatisticBase *statistic)
These can be overriden, if necessary, but must be callable by the derived class.
Definition statoutput.cc:279
Definition statgroup.h:32
Forms the base class for statistics output generation within the SST core.
Definition statoutput.h:54
virtual void printUsage()
Have Statistic Object print out its usage and parameter info.
Definition statoutput.cc:78
Output & getSimulationOutput()
Gets the Output object for the Simulation object associeted with this StatOutput.
Definition statoutput.cc:84
virtual bool checkOutputParameters()=0
Have the Statistic Output check its parameters.
std::string & getStatisticOutputName()
Return the Statistic Output name.
Definition statoutput.h:69
RankInfo getNumRanks()
Gets the number of ranks for the simulation.
Definition statoutput.cc:90
virtual void startOfSimulation()=0
Indicate to Statistic Output that simulation has started.
virtual bool acceptsGroups() const
True if this StatOutput can handle StatisticGroups.
Definition statoutput.h:75
SimTime_t getCurrentSimCycle()
Gets the current simulstion cycle.
Definition statoutput.cc:102
RankInfo getRank()
Gets the Rank (MPI rank and thread) that this StatisticOutput is associated with.
Definition statoutput.cc:96
StatisticOutput(Params &outputParameters)
Construct a base StatisticOutput.
Definition statoutput.cc:29
std::string getAbsolutePathForOutputFile(const std::string &filename)
Get the absolute path for the specified file taking into account the specified output directory.
Definition statoutput.cc:108
virtual void endOfSimulation()=0
Indicate to Statistic Output that simulation has ended.
Params & getOutputParameters()
Return the parameters for the StatisticOutput.
Definition statoutput.h:72
An SST core component that handles timing and event processing informing all registered Statistics to...
Definition statengine.h:55