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