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