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