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