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