SST  14.1.0
StructuralSimulationToolkit
statengine.h
1 // Copyright 2009-2024 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-2024, 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/serialization/serializable.h"
19 #include "sst/core/sst_types.h"
20 #include "sst/core/statapi/statbase.h"
21 #include "sst/core/statapi/statfieldinfo.h"
22 #include "sst/core/statapi/statgroup.h"
23 #include "sst/core/statapi/statnull.h"
24 #include "sst/core/threadsafe.h"
25 #include "sst/core/unitAlgebra.h"
26 
27 /* Forward declare for Friendship */
28 extern int main(int argc, char** argv);
29 extern void finalize_statEngineConfig(void);
30 
31 namespace SST {
32 class BaseComponent;
33 class Simulation_impl;
34 class ConfigGraph;
35 class ConfigStatGroup;
36 class ConfigStatOutput;
37 class Params;
38 
39 namespace Statistics {
40 
41 // template<typename T> class Statistic;
42 // class StatisticBase;
43 class StatisticOutput;
44 
45 /**
46  \class StatisticProcessingEngine
47 
48  An SST core component that handles timing and event processing informing
49  all registered Statistics to generate their outputs at desired rates.
50 */
51 
53 {
54 
55 public:
56  /** Called by the Components and Subcomponent to perform a statistic Output.
57  * @param stat - Pointer to the statistic.
58  * @param EndOfSimFlag - Indicates that the output is occurring at the end of simulation.
59  */
60  void performStatisticOutput(StatisticBase* stat, bool endOfSimFlag = false);
61 
62  /** Called by the Components and Subcomponent to perform a global statistic Output.
63  * This routine will force ALL Components and Subcomponents to output their statistic information.
64  * This may lead to unexpected results if the statistic counts or data is reset on output.
65  * @param endOfSimFlag - Indicates that the output is occurring at the end of simulation.
66  */
67  void performGlobalStatisticOutput(bool endOfSimFlag = false);
68 
69  template <class T>
70  Statistic<T>* createStatistic(
71  BaseComponent* comp, const std::string& type, const std::string& statName, const std::string& statSubId,
72  Params& params)
73  {
74 
75  return Factory::getFactory()->CreateWithParams<Statistic<T>>(type, params, comp, statName, statSubId, params);
76  }
77 
78  bool registerStatisticWithEngine(StatisticBase* stat) { return registerStatisticCore(stat); }
79 
80  uint8_t statLoadLevel() const { return m_statLoadLevel; }
81 
82  // Outputs are per MPI rank, so have to be static data
83  static const std::vector<StatisticOutput*>& getStatOutputs() { return m_statOutputs; }
84 
85  /** Called to setup the StatOutputs, which are shared across all
86  the StatEngines on the same MPI rank.
87  */
88  static void static_setup(ConfigGraph* graph);
89 
90  /** Called to notify StatOutputs that simulation has started
91  */
92  static void stat_outputs_simulation_start();
93 
94  /** Called to notify StatOutputs that simulation has ended
95  */
96  static void stat_outputs_simulation_end();
97 
98  void serialize_order(SST::Core::Serialization::serializer& ser) override;
99  ImplementSerializable(SST::Statistics::StatisticProcessingEngine)
100 private:
101  friend class SST::Simulation_impl;
102  friend int ::main(int argc, char** argv);
103  friend void ::finalize_statEngineConfig(void);
104 
106  void setup(Simulation_impl* sim, ConfigGraph* graph);
107  void restart(Simulation_impl* sim);
109 
110  static StatisticOutput* createStatisticOutput(const ConfigStatOutput& cfg);
111 
112  bool registerStatisticCore(StatisticBase* stat);
113 
114  StatisticOutput* getOutputForStatistic(const StatisticBase* stat) const;
115  StatisticGroup& getGroupForStatistic(const StatisticBase* stat) const;
116  bool addPeriodicBasedStatistic(const UnitAlgebra& freq, StatisticBase* Stat);
117  bool addEventBasedStatistic(const UnitAlgebra& count, StatisticBase* Stat);
118  bool addEndOfSimStatistic(StatisticBase* Stat);
119  UnitAlgebra getParamTime(StatisticBase* stat, const std::string& pName) const;
120  void setStatisticStartTime(StatisticBase* Stat);
121  void setStatisticStopTime(StatisticBase* Stat);
122 
123  void finalizeInitialization(); /* Called when performWireUp() finished */
124  void startOfSimulation();
125  void endOfSimulation();
126 
127  void performStatisticOutputImpl(StatisticBase* stat, bool endOfSimFlag);
128  void performStatisticGroupOutputImpl(StatisticGroup& group, bool endOfSimFlag);
129 
130  bool handleStatisticEngineClockEvent(Cycle_t CycleNum, SimTime_t timeFactor);
131  bool handleGroupClockEvent(Cycle_t CycleNum, StatisticGroup* group);
132  void handleStatisticEngineStartTimeEvent(SimTime_t timeFactor);
133  void handleStatisticEngineStopTimeEvent(SimTime_t timeFactor);
134  StatisticBase* isStatisticInCompStatMap(
135  const std::string& compName, const ComponentId_t& compId, const std::string& statName,
136  const std::string& statSubId, StatisticFieldInfo::fieldType_t fieldType);
137  void addStatisticToCompStatMap(StatisticBase* Stat, StatisticFieldInfo::fieldType_t fieldType);
138  void castError(const std::string& type, const std::string& statName, const std::string& fieldName);
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  Simulation_impl* m_sim;
153  Output& m_output;
154  uint8_t m_statLoadLevel;
155  StatisticGroup m_defaultGroup;
156  std::vector<StatisticGroup> m_statGroups;
157 
158  // Outputs are per MPI rank, so have to be static data
159  static std::vector<StatisticOutput*> m_statOutputs;
160 };
161 
162 } // namespace Statistics
163 } // namespace SST
164 
165 #endif // SST_CORE_STATAPI_STATENGINE_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:53
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:43
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
static void stat_outputs_simulation_start()
Called to notify StatOutputs that simulation has started.
Definition: statengine.cc:49
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:50
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:389
void performStatisticOutput(StatisticBase *stat, bool endOfSimFlag=false)
Called by the Components and Subcomponent to perform a statistic Output.
Definition: statengine.cc:441
Forms the base class for statistics gathering within SST.
Definition: statbase.h:45
Definition: action.cc:18
void setup()
Perform the setup() and run phases of the simulation.
Definition: simulation.cc:748
Forms the template defined base class for statistics gathering within SST.
Definition: elementinfo.h:45
Definition: serializable.h:24
Main control class for a SST Simulation.
Definition: simulation_impl.h:76
Main component object for the simulation.
Definition: baseComponent.h:62
static void stat_outputs_simulation_end()
Called to notify StatOutputs that simulation has ended.
Definition: statengine.cc:57
Parameter store.
Definition: params.h:55
Definition: configGraph.h:194
void performGlobalStatisticOutput(bool endOfSimFlag=false)
Called by the Components and Subcomponent to perform a global statistic Output.
Definition: statengine.cc:494
An SST core component that handles timing and event processing informing all registered Statistics to...
Definition: statengine.h:52
Definition: statgroup.h:29
Performs Unit math in full precision.
Definition: unitAlgebra.h:106