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