SST  8.0.0
StructuralSimulationToolkit
statengine.h
1 // Copyright 2009-2018 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-2018, 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_ENGINE
13 #define _H_SST_CORE_STATISTICS_ENGINE
14 
15 #include <sst/core/sst_types.h>
16 #include <sst/core/statapi/statfieldinfo.h>
17 #include <sst/core/statapi/statgroup.h>
18 #include <sst/core/unitAlgebra.h>
19 #include <sst/core/clock.h>
20 #include <sst/core/oneshot.h>
21 #include <sst/core/threadsafe.h>
22 
23 /* Forward declare for Friendship */
24 extern int main(int argc, char **argv);
25 extern void finalize_statEngineConfig(void);
26 
27 
28 namespace SST {
29 class BaseComponent;
30 class Simulation;
31 class ConfigGraph;
32 class ConfigStatGroup;
33 class ConfigStatOutput;
34 class Params;
35 
36 namespace Statistics {
37 
38 template<typename T> class Statistic;
39 class StatisticBase;
40 class StatisticOutput;
41 
42 /**
43  \class StatisticProcessingEngine
44 
45  An SST core component that handles timing and event processing informing
46  all registered Statistics to generate their outputs at desired rates.
47 */
48 
50 {
51  static StatisticProcessingEngine* instance;
52 
53 public:
54  static StatisticProcessingEngine* getInstance() { return instance; }
55 
56 
57  /** Called by the Components and Subcomponent to perform a statistic Output.
58  * @param stat - Pointer to the statistic.
59  * @param EndOfSimFlag - Indicates that the output is occurring at the end of simulation.
60  */
61  void performStatisticOutput(StatisticBase* stat, bool endOfSimFlag = false);
62 
63  /** Called by the Components and Subcomponent to perform a global statistic Output.
64  * This routine will force ALL Components and Subcomponents to output their statistic information.
65  * This may lead to unexpected results if the statistic counts or data is reset on output.
66  * @param endOfSimFlag - Indicates that the output is occurring at the end of simulation.
67  */
68  void performGlobalStatisticOutput(bool endOfSimFlag = false);
69 
70 
71  template<typename T>
72  Statistic<T>* createStatistic(BaseComponent *comp, const std::string &type, const std::string &statName, const std::string &statSubId, Params &params)
73  {
74  StatisticFieldInfo::fieldType_t fieldType = StatisticFieldInfo::getFieldTypeFromTemplate<T>();
75  return dynamic_cast<Statistic<T>*>(createStatistic(comp, type, statName, statSubId, params, fieldType));
76  }
77 
78  template<typename T>
79  bool registerStatisticWithEngine(StatisticBase* stat)
80  {
81  bool ok;
82  if ( true == (ok = registerStatisticCore(stat)) ) {
83  StatisticFieldInfo::fieldType_t fieldType = StatisticFieldInfo::getFieldTypeFromTemplate<T>();
84  addStatisticToCompStatMap(stat, fieldType);
85  }
86  return ok;
87  }
88 
89  template<typename T>
90  StatisticBase* isStatisticRegisteredWithEngine(const std::string& compName, const ComponentId_t& compId, std::string& statName, std::string& statSubId)
91  {
92  StatisticFieldInfo::fieldType_t fieldType = StatisticFieldInfo::getFieldTypeFromTemplate<T>();
93  return isStatisticInCompStatMap(compName, compId, statName, statSubId, fieldType);
94  }
95 
96 
97  const std::vector<StatisticOutput*>& getStatOutputs() const { return m_statOutputs; }
98 
99 private:
100  friend class SST::Simulation;
101  friend int ::main(int argc, char **argv);
102  friend void ::finalize_statEngineConfig(void);
103 
105  void setup(ConfigGraph *graph);
107 
108  static void init(ConfigGraph *graph);
109 
110  StatisticOutput* createStatisticOutput(const ConfigStatOutput &cfg);
111 
112  StatisticBase* createStatistic(BaseComponent* comp, const std::string &type,
113  const std::string &statName, const std::string &statSubId,
114  Params &params, StatisticFieldInfo::fieldType_t fieldType);
115  bool registerStatisticCore(StatisticBase* stat);
116 
117  StatisticOutput* getOutputForStatistic(const StatisticBase *stat) const;
118  StatisticGroup& getGroupForStatistic(const StatisticBase *stat) const;
119  bool addPeriodicBasedStatistic(const UnitAlgebra& freq, StatisticBase* Stat);
120  bool addEventBasedStatistic(const UnitAlgebra& count, StatisticBase* Stat);
121  UnitAlgebra getParamTime(StatisticBase *stat, const std::string& pName) const;
122  void setStatisticStartTime(StatisticBase* Stat);
123  void setStatisticStopTime(StatisticBase* Stat);
124 
125  void finalizeInitialization(); /* Called when performWireUp() finished */
126  void startOfSimulation();
127  void endOfSimulation();
128 
129 
130  void performStatisticOutputImpl(StatisticBase* stat, bool endOfSimFlag);
131  void performStatisticGroupOutputImpl(StatisticGroup& group, bool endOfSimFlag);
132 
133  bool handleStatisticEngineClockEvent(Cycle_t CycleNum, SimTime_t timeFactor);
134  bool handleGroupClockEvent(Cycle_t CycleNum, StatisticGroup* group);
135  void handleStatisticEngineStartTimeEvent(SimTime_t timeFactor);
136  void handleStatisticEngineStopTimeEvent(SimTime_t timeFactor);
137  StatisticBase* isStatisticInCompStatMap(const std::string& compName, const ComponentId_t& compId, std::string& statName, std::string& statSubId, StatisticFieldInfo::fieldType_t fieldType);
138  void addStatisticToCompStatMap(StatisticBase* Stat, StatisticFieldInfo::fieldType_t fieldType);
139 
140 private:
141  typedef std::vector<StatisticBase*> StatArray_t; /*!< Array of Statistics */
142  typedef std::map<SimTime_t, StatArray_t*> StatMap_t; /*!< Map of simtimes to Statistic Arrays */
143  typedef std::map<ComponentId_t, StatArray_t*> CompStatMap_t; /*!< Map of ComponentId's to StatInfo Arrays */
144 
145  StatArray_t m_EventStatisticArray; /*!< Array of Event Based Statistics */
146  StatMap_t m_PeriodicStatisticMap; /*!< Map of Array's of Periodic Based Statistics */
147  StatMap_t m_StartTimeMap; /*!< Map of Array's of Statistics that are started at a sim time */
148  StatMap_t m_StopTimeMap; /*!< Map of Array's of Statistics that are stopped at a sim time */
149  CompStatMap_t m_CompStatMap; /*!< Map of Arrays of Statistics tied to Component Id's */
150  bool m_SimulationStarted; /*!< Flag showing if Simulation has started */
151 
152  Output & m_output;
153  uint8_t m_statLoadLevel;
154  std::vector<StatisticOutput*> m_statOutputs;
155  StatisticGroup m_defaultGroup;
156  std::vector<StatisticGroup> m_statGroups;
157  Core::ThreadSafe::Barrier m_barrier;
158 
159 };
160 
161 } //namespace Statistics
162 } //namespace SST
163 
164 #endif
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:54
Main control class for a SST Simulation.
Definition: simulation.h:72
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:47
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:294
void performStatisticOutput(StatisticBase *stat, bool endOfSimFlag=false)
Called by the Components and Subcomponent to perform a statistic Output.
Definition: statengine.cc:405
Forms the base class for statistics gathering within SST.
Definition: statbase.h:61
Forms the template defined base class for statistics gathering within SST.
Definition: statbase.h:294
void setup()
Perform the setup() and run phases of the simulation.
Definition: simulation.cc:495
Main component object for the simulation.
Definition: baseComponent.h:104
Parameter store.
Definition: params.h:45
Definition: configGraph.h:170
void performGlobalStatisticOutput(bool endOfSimFlag=false)
Called by the Components and Subcomponent to perform a global statistic Output.
Definition: statengine.cc:469
An SST core component that handles timing and event processing informing all registered Statistics to...
Definition: statengine.h:49
Definition: statgroup.h:29
Performs Unit math in full precision.
Definition: unitAlgebra.h:104
Definition: threadsafe.h:45
fieldType_t
Supported Field Types.
Definition: statfieldinfo.h:36