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