SST  10.1.0
StructuralSimulationToolkit
statoutput.h
1 // Copyright 2009-2020 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-2020, 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 _H_SST_CORE_STATISTICS_OUTPUT
13 #define _H_SST_CORE_STATISTICS_OUTPUT
14 
15 #include "sst/core/sst_types.h"
16 #include "sst/core/warnmacros.h"
17 #include "sst/core/module.h"
18 #include "sst/core/params.h"
19 #include "sst/core/statapi/statfieldinfo.h"
20 #include "sst/core/statapi/statbase.h"
21 #include "sst/core/eli/elementinfo.h"
22 #include <unordered_map>
23 
24 #include <mutex>
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 {
76  return false;
77  }
78 
79 /////////////////
80 // Methods for Registering Fields (Called by Statistic Objects)
81 public:
82  //by default, no params to return
83  static const std::vector<SST::ElementInfoParam>& ELI_getParams() {
84  static std::vector<SST::ElementInfoParam> var{};
85  return var;
86  }
87 
88 protected:
89  friend class SST::Simulation;
91 
92  // Routine to have Output Check its options for validity
93  /** Have the Statistic Output check its parameters
94  * @return True if all parameters are ok; False if a parameter is missing or incorrect.
95  */
96  virtual bool checkOutputParameters() = 0;
97 
98  /** Have Statistic Object print out its usage and parameter info.
99  * Called when checkOutputParameters() returns false */
100  virtual void printUsage() = 0;
101 
102  // Simulation Events
103  /** Indicate to Statistic Output that simulation has started.
104  * Allows object to perform any setup required. */
105  virtual void startOfSimulation() = 0;
106 
107  /** Indicate to Statistic Output that simulation has ended.
108  * Allows object to perform any shutdown required. */
109  virtual void endOfSimulation() = 0;
110 
111 private:
112  // Start / Stop of register Fields
113  virtual void registerStatistic(StatisticBase *stat) = 0;
114 
115  void registerGroup(StatisticGroup *group);
116  void outputGroup(StatisticGroup* group, bool endOfSimFlag);
117 
118  virtual void startOutputGroup(StatisticGroup* group) = 0;
119  virtual void stopOutputGroup() = 0;
120 
121  virtual void startRegisterGroup(StatisticGroup* group) = 0;
122  virtual void stopRegisterGroup() = 0;
123 
124  void castError();
125 
126 protected:
127  /** Construct a base StatisticOutput
128  * @param outputParameters - The parameters for the statistic Output.
129  */
130  StatisticOutput(Params& outputParameters);
131  StatisticOutput() {;} // For serialization only
132  void setStatisticOutputName(const std::string& name) {m_statOutputName = name;}
133 
134  void lock() { m_lock.lock(); }
135  void unlock() { m_lock.unlock(); }
136 
137 private:
138  std::string m_statOutputName;
139  Params m_outputParameters;
140  std::recursive_mutex m_lock;
141 
142 };
143 
145 {
146  public:
147  void registerStatistic(StatisticBase *stat) override;
148 
149  // Start / Stop of output
150  void output(StatisticBase* statistic, bool endOfSimFlag) override;
151 
152  void startOutputGroup(StatisticGroup *group) override;
153  void stopOutputGroup() override;
154 
155  void startRegisterGroup(StatisticGroup *group) override;
156  void stopRegisterGroup() override;
157 
158  // Start / Stop of output
159  /** Indicate to Statistic Output that a statistic is about to send data to be output
160  * Allows object to perform any initialization before output. */
161  virtual void implStartOutputEntries(StatisticBase* statistic) = 0;
162 
163  /** Indicate to Statistic Output that a statistic is finished sending data to be output
164  * Allows object to perform any cleanup. */
165  virtual void implStopOutputEntries() = 0;
166 
167 
168 // /** Adjust the hierarchy of the fields (FUTURE SUPPORT)
169 // * @param fieldHandle - The handle of the field to adjust.
170 // * @param Level - The level of the field.
171 // * @param parent - The parent field of the field.
172 // */
173 // void setFieldHierarchy(fieldHandle_t fieldHandle, uint32_t Level, fieldHandle_t parent);
174 
175  /** Return the information on a registered field via the field handle.
176  * @param fieldHandle - The handle of the registered field.
177  * @return Pointer to the registered field info.
178  */
179  // Get the Field Information object, NULL is returned if not found
180  StatisticFieldInfo* getRegisteredField(fieldHandle_t fieldHandle);
181 
182  /** Return the information on a registered field via known names.
183  * @param componentName - The name of the component.
184  * @param statisticName - The name of the statistic.
185  * @param fieldName - The name of the field .
186  * @return Pointer to the registered field info.
187  */
188  // Get Registered Fields
189  // ONLY SUPPORTED TYPES ARE int32_t, uint32_t, int64_t, uint64_t, float, double
190  template<typename T>
191  StatisticFieldInfo* getRegisteredField(const char* statisticName, const char* fieldName)
192  {
193  StatisticFieldInfo* NewStatFieldInfo;
194  StatisticFieldInfo* ExistingStatFieldInfo;
195  StatisticFieldInfo::fieldType_t FieldType = StatisticFieldInfo::StatisticFieldInfo::getFieldTypeFromTemplate<T>();
196 
197  NewStatFieldInfo = new StatisticFieldInfo(statisticName, fieldName, FieldType);
198 
199  // Now search the FieldNameMap_t of type for a matching entry
200  FieldNameMap_t::const_iterator found = m_outputFieldNameMap.find(NewStatFieldInfo->getFieldUniqueName());
201  if (found != m_outputFieldNameMap.end()) {
202  // We found a map entry, now get the StatFieldInfo from the m_outputFieldInfoArray at the index given by the map
203  // and then delete the NewStatFieldInfo to prevent a leak
204  ExistingStatFieldInfo = m_outputFieldInfoArray[found->second];
205  delete NewStatFieldInfo;
206  return ExistingStatFieldInfo;
207  }
208 
209  delete NewStatFieldInfo;
210  return nullptr;
211  }
212 
213  /** Return the array of registered field infos. */
214  FieldInfoArray_t& getFieldInfoArray() {return m_outputFieldInfoArray;}
215 
216 /////////////////
217  // Methods for Outputting Fields (Called by Statistic Objects)
218  // Output fields (will call virtual functions of Derived Output classes)
219  // These aren't really part of a generic interface - optimization purposes only
220  /** Output field data.
221  * @param fieldHandle - The handle of the registered field.
222  * @param data - The data to be output.
223  */
224  virtual void outputField(fieldHandle_t fieldHandle, int32_t data);
225  virtual void outputField(fieldHandle_t fieldHandle, uint32_t data);
226  virtual void outputField(fieldHandle_t fieldHandle, int64_t data);
227  virtual void outputField(fieldHandle_t fieldHandle, uint64_t data);
228  virtual void outputField(fieldHandle_t fieldHandle, float data);
229  virtual void outputField(fieldHandle_t fieldHandle, double data);
230 
231  /** Register a field to be output (templated function)
232  * @param fieldName - The name of the field.
233  * @return The handle of the registered field or -1 if type is not supported.
234  * Note: Any field names (of the same data type) that are previously
235  * registered by a statistic will return the previously
236  * handle.
237  */
238  // Field Registration
239  // ONLY SUPPORTED TYPES ARE int32_t, uint32_t, int64_t, uint64_t, float, double
240  template<typename T>
241  fieldHandle_t registerField(const char* fieldName)
242  {
243  StatisticFieldInfo::fieldType_t FieldType =
244  StatisticFieldInfo::StatisticFieldInfo::getFieldTypeFromTemplate<T>();
245 
246  auto res = generateFieldHandle(addFieldToLists(fieldName, FieldType));
247  implRegisteredField(res);
248  return res;
249  }
250 
251  /** Output field data.
252  * @param type - The field type to get name of.
253  * @return String name of the field type.
254  */
255  const char* getFieldTypeShortName(fieldType_t type);
256 
257  protected:
258  /** Construct a base StatisticOutput
259  * @param outputParameters - The parameters for the statistic Output.
260  */
261  StatisticFieldsOutput(Params& outputParameters);
262 
263  //For Serialization
265 
266  private:
267  // Other support functions
268  StatisticFieldInfo* addFieldToLists(const char* fieldName, fieldType_t fieldType);
269  fieldHandle_t generateFieldHandle(StatisticFieldInfo* FieldInfo);
270  virtual void implRegisteredField(fieldHandle_t UNUSED(fieldHandle)){}
271 
272  FieldInfoArray_t m_outputFieldInfoArray;
273  FieldNameMap_t m_outputFieldNameMap;
274  fieldHandle_t m_highestFieldHandle;
275  std::string m_currentFieldStatName;
276 
277  protected:
278  /** These can be overriden, if necessary, but must be callable
279  * by the derived class */
280  virtual void startRegisterFields(StatisticBase *statistic);
281  virtual void stopRegisterFields();
282 
283  virtual void startOutputEntries(StatisticBase* statistic);
284  virtual void stopOutputEntries();
285 
286 
287 };
288 
289 
290 } //namespace Statistics
291 } //namespace SST
292 
293 #endif
Main control class for a SST Simulation.
Definition: simulation.h:73
std::string & getStatisticOutputName()
Return the Statistic Output name.
Definition: statoutput.h:65
The class for representing Statistic Output Fields.
Definition: statfieldinfo.h:99
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
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:214
virtual bool checkOutputParameters()=0
Have the Statistic Output check its parameters.
const char * getFieldTypeShortName(fieldType_t type)
Output field data.
Definition: statoutput.cc:212
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function)
Definition: statoutput.h:241
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.
virtual void printUsage()=0
Have Statistic Object print out its usage and parameter info.
virtual bool acceptsGroups() const
True if this StatOutput can handle StatisticGroups.
Definition: statoutput.h:71
Definition: statoutput.h:144
StatisticOutput(Params &outputParameters)
Construct a base StatisticOutput.
Definition: statoutput.cc:25
Params & getOutputParameters()
Return the parameters for the StatisticOutput.
Definition: statoutput.h:68
Parameter store.
Definition: params.h:44
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:105
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:226
StatisticFieldInfo * getRegisteredField(const char *statisticName, const char *fieldName)
Return the information on a registered field via known names.
Definition: statoutput.h:191
virtual void endOfSimulation()=0
Indicate to Statistic Output that simulation has ended.