SST  6.0.0
StructuralSimulationToolkit
statoutput.h
1 // Copyright 2009-2016 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2016, 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/module.h>
17 #include <sst/core/params.h>
18 #include <sst/core/statapi/statfieldinfo.h>
19 #include <sst/core/statapi/statbase.h>
20 #include <unordered_map>
21 
22 #include <mutex>
23 
24 // Default Settings for Statistic Output and Load Level
25 #define STATISTICSDEFAULTOUTPUTNAME "sst.statOutputConsole"
26 #define STATISTICSDEFAULTLOADLEVEL 0
27 
28 extern int main(int argc, char **argv);
29 
30 namespace SST {
31 class Component;
32 class Simulation;
33 namespace Statistics {
34 class StatisticProcessingEngine;
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 statistics load level for the system */
66  uint8_t getStatisticLoadLevel() {return m_statLoadLevel;}
67 
68  /** Return the parameters for the StatisticOutput */
69  Params& getOutputParameters() {return m_outputParameters;}
70 
71 /////////////////
72 // Methods for Registering Fields (Called by Statistic Objects)
73 public:
74  /** Register a field to be output (templated function)
75  * @param fieldName - The name of the field.
76  * @return The handle of the registerd field or -1 if type is not supported.
77  * Note: Any field names (of the same data type) that are previously
78  * registered by a statistic will return the previously
79  * handle.
80  */
81  // Field Registration
82  // ONLY SUPPORTED TYPES ARE int32_t, uint32_t, int64_t, uint64_t, float, double
83  template<typename T>
84  fieldHandle_t registerField(const char* fieldName)
85  {
86  if (is_type_same<T, int32_t >::value){return generateFileHandle(addFieldToLists(fieldName, StatisticFieldInfo::INT32)); }
87  if (is_type_same<T, uint32_t >::value){return generateFileHandle(addFieldToLists(fieldName, StatisticFieldInfo::UINT32));}
88  if (is_type_same<T, int64_t >::value){return generateFileHandle(addFieldToLists(fieldName, StatisticFieldInfo::INT64)); }
89  if (is_type_same<T, uint64_t >::value){return generateFileHandle(addFieldToLists(fieldName, StatisticFieldInfo::UINT64));}
90  if (is_type_same<T, float >::value){return generateFileHandle(addFieldToLists(fieldName, StatisticFieldInfo::FLOAT)); }
91  if (is_type_same<T, double >::value){return generateFileHandle(addFieldToLists(fieldName, StatisticFieldInfo::DOUBLE));}
92 
93  //TODO: IF WE GET THERE, GENERATE AN ERROR AS THIS IS AN UNSUPPORTED TYPE
94  return -1;
95  }
96 
97 // /** Adjust the heirarchy of the fields (FUTURE SUPPORT)
98 // * @param fieldHandle - The handle of the field to adjust.
99 // * @param Level - The level of the field.
100 // * @param parent - The parent field of the field.
101 // */
102 // void setFieldHierarchy(fieldHandle_t fieldHandle, uint32_t Level, fieldHandle_t parent);
103 
104  /** Return the information on a registered field via the field handle.
105  * @param fieldHandle - The handle of the registered field.
106  * @return Pointer to the registered field info.
107  */
108  // Get the Field Information object, NULL is returned if not found
109  StatisticFieldInfo* getRegisteredField(fieldHandle_t fieldHandle);
110 
111  /** Return the information on a registered field via known names.
112  * @param componentName - The name of the component.
113  * @param statisticName - The name of the statistic.
114  * @param fieldName - The name of the field .
115  * @return Pointer to the registered field info.
116  */
117  // Get Registerd Fields
118  // ONLY SUPPORTED TYPES ARE int32_t, uint32_t, int64_t, uint64_t, float, double
119  template<typename T>
120  StatisticFieldInfo* getRegisteredField(const char* statisticName, const char* fieldName)
121  {
122  StatisticFieldInfo* NewStatFieldInfo;
123  StatisticFieldInfo* ExistingStatFieldInfo;
124  StatisticFieldInfo::fieldType_t FieldType = StatisticFieldInfo::UNDEFINED;
125 
126  // Figure out the Field Type
127  if (is_type_same<T, int32_t >::value) {FieldType = StatisticFieldInfo::INT32; }
128  if (is_type_same<T, uint32_t >::value) {FieldType = StatisticFieldInfo::UINT32;}
129  if (is_type_same<T, int64_t >::value) {FieldType = StatisticFieldInfo::INT64; }
130  if (is_type_same<T, uint64_t >::value) {FieldType = StatisticFieldInfo::UINT64;}
131  if (is_type_same<T, float >::value) {FieldType = StatisticFieldInfo::FLOAT; }
132  if (is_type_same<T, double >::value) {FieldType = StatisticFieldInfo::DOUBLE;}
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  /** Output field data.
155  * @param fieldHandle - The handle of the registered field.
156  * @param data - The data to be output.
157  */
158  // Methods for Outputting Fields (Called by Statistic Objects)
159  // Output fields (will call virtual functions of Derived Output classes)
160  void outputField(fieldHandle_t fieldHandle, int32_t data);
161  void outputField(fieldHandle_t fieldHandle, uint32_t data);
162  void outputField(fieldHandle_t fieldHandle, int64_t data);
163  void outputField(fieldHandle_t fieldHandle, uint64_t data);
164  void outputField(fieldHandle_t fieldHandle, float data);
165  void outputField(fieldHandle_t fieldHandle, double data);
166 
167  /** Output field data.
168  * @param type - The field type to get name of.
169  * @return String name of the field type.
170  */
171  const char* getFieldTypeShortName(fieldType_t type);
172 
173 protected:
174  friend int ::main(int argc, char **argv);
175  friend class SST::Component;
176  friend class SST::Simulation;
178 
179  // Routine to have Output Check its options for validity
180  /** Have the Statistic Output check its parameters
181  * @return True if all parameters are ok; False if a parameter is missing or incorrect.
182  */
183  virtual bool checkOutputParameters() = 0;
184 
185  /** Have Statistic Object print out its usage and parameter info.
186  * Called when checkOutputParameters() returns false */
187  virtual void printUsage() = 0;
188 
189  // Simulation Events
190  /** Indicate to Statistic Output that simulation has started.
191  * Allows object to perform any setup required. */
192  virtual void startOfSimulation() = 0;
193 
194  /** Indicate to Statistic Output that simulation has ended.
195  * Allows object to perform any shutdown required. */
196  virtual void endOfSimulation() = 0;
197 
198  // Start / Stop of output
199  /** Indicate to Statistic Output that a statistic is about to send data to be output
200  * Allows object to perform any initialization before output. */
201  virtual void implStartOutputEntries(StatisticBase* statistic) = 0;
202 
203  /** Indicate to Statistic Output that a statistic is finished sending data to be output
204  * Allows object to perform any cleanup. */
205  virtual void implStopOutputEntries() = 0;
206 
207  // Field Outputs
208  /** Implementation of outputField() for derived classes.
209  * Perform the actual implementation of the output. */
210  virtual void implOutputField(fieldHandle_t fieldHandle, int32_t data) = 0;
211  virtual void implOutputField(fieldHandle_t fieldHandle, uint32_t data) = 0;
212  virtual void implOutputField(fieldHandle_t fieldHandle, int64_t data) = 0;
213  virtual void implOutputField(fieldHandle_t fieldHandle, uint64_t data) = 0;
214  virtual void implOutputField(fieldHandle_t fieldHandle, float data) = 0;
215  virtual void implOutputField(fieldHandle_t fieldHandle, double data) = 0;
216 
217 private:
218  // Start / Stop of register Fields
219  void startRegisterFields(const char* componentName, const char* statisticName);
220  void stopRegisterFields();
221 
222  // Set the Statistic Load Level
223  void setStatisticLoadLevel(uint8_t loadLevel) {m_statLoadLevel = loadLevel;}
224 
225  // Start / Stop of output
226  void startOutputEntries(StatisticBase* statistic);
227  void stopOutputEntries();
228 
229  // Other support functions
230  StatisticFieldInfo* addFieldToLists(const char* fieldName, fieldType_t fieldType);
231  fieldHandle_t generateFileHandle(StatisticFieldInfo* FieldInfo);
232 
233 
234 protected:
235  StatisticOutput() {;} // For serialization only
236  void setStatisticOutputName(std::string name) {m_statOutputName = name;}
237 
238  void lock() { m_lock.lock(); }
239  void unlock() { m_lock.unlock(); }
240 
241 private:
242  std::string m_statOutputName;
243  Params m_outputParameters;
244  FieldInfoArray_t m_outputFieldInfoArray;
245  FieldNameMap_t m_outputFieldNameMap;
246  fieldHandle_t m_highestFieldHandle;
247  std::string m_currentFieldCompName;
248  std::string m_currentFieldStatName;
249  uint8_t m_statLoadLevel;
250  std::recursive_mutex m_lock;
251 
252 };
253 
254 } //namespace Statistics
255 } //namespace SST
256 
257 #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:75
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
Definition: statfieldinfo.h:22
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function)
Definition: statoutput.h:84
Main component object for the simulation.
Definition: component.h:56
Forms the base class for statistics gathering within SST.
Definition: statbase.h:36
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:120
Params & getOutputParameters()
Return the parameters for the StatisticOutput.
Definition: statoutput.h:69
Parameter store.
Definition: params.h:46
const char * getFieldTypeShortName(fieldType_t type)
Output field data.
Definition: statoutput.cc:153
StatisticFieldInfo * getRegisteredField(fieldHandle_t fieldHandle)
Adjust the heirarchy of the fields (FUTURE SUPPORT)
Definition: statoutput.cc:93
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:151
An SST core component that handles timing and event processing informing all registered Statistics to...
Definition: statengine.h:33
std::string getFieldUniqueName() const
Return the field type related to this field info.
Definition: statfieldinfo.cc:35
void outputField(fieldHandle_t fieldHandle, int32_t data)
Output field data.
Definition: statoutput.cc:117
fieldType_t
Supported Field Types.
Definition: statfieldinfo.h:36
uint8_t getStatisticLoadLevel()
Return the statistics load level for the system.
Definition: statoutput.h:66
virtual void endOfSimulation()=0
Indicate to Statistic Output that simulation has ended.