SST  15.1.0
StructuralSimulationToolkit
statengine.h
1 // Copyright 2009-2025 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-2025, 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/serialization/serializable.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 #include <cstdint>
27 #include <map>
28 #include <string>
29 #include <vector>
30 
31 /* Forward declare for Friendship */
32 extern int main(int argc, char** argv);
33 extern void finalize_statEngineConfig();
34 
35 namespace SST {
36 class BaseComponent;
37 class Simulation_impl;
38 class ConfigGraph;
39 class ConfigStatGroup;
40 class ConfigStatOutput;
41 class Params;
42 struct StatsConfig;
43 
44 namespace Statistics {
45 
46 class StatisticOutput;
47 
48 /**
49  \class StatisticProcessingEngine
50 
51  An SST core component that handles timing and event processing informing
52  all registered Statistics to generate their outputs at desired rates.
53 */
55 {
56 
57 public:
58 
59  /** Called to create an enabled (non-null) statistic of the requested type
60  * This function also registers the statistic with the engine
61  * @param comp The statistic owner
62  * @param stat_name The name of the statistic
63  * @param stat_sub_id An optional id to differentiate multiple instances of the same statistic, "" if none
64  * @param params The parameters that should be used to configure the statistic
65  */
66  template <class T>
68  BaseComponent* comp, const std::string& stat_name, const std::string& stat_sub_id, Params& params)
69  {
70  // Create a new statistic
71  std::string type = params.find<std::string>("type", "sst.AccumulatorStatistic");
72  Statistic<T>* stat =
73  Factory::getFactory()->CreateWithParams<Statistic<T>>(type, params, comp, stat_name, stat_sub_id, params);
74 
75  // Register with engine
76  registerStatisticWithEngine(stat, params);
77  return stat;
78  }
79 
80  /** Called to create a disabled (null) statistic of the requested type
81  * @param comp The statistic owner
82  */
83  template <class T>
85  {
86  SST::Params empty {};
87  static Statistic<T>* stat =
88  Factory::getFactory()->CreateWithParams<Statistic<T>>("sst.NullStatistic", empty, nullptr, "", "", empty);
89  return stat;
90  // No need to register anything
91  }
92 
93  /** Re-registers a statistic with the engine during a restart run.
94  * Newly created statistics (not read from a checkpoint) use 'registerStatisticWithEngine' instead.
95  * @param stat The statistic to re-register
96  * @param start_at_time If the statistic should be enabled after some time, the time (in core cycles) else 0
97  * @param stop_at_time If the statistic should be disabled after some time, the time (in core cycles) else 0
98  * @param output_factor If the statistic should be output at a regular time interval, the interval (in core cycles)
99  * else 0
100  */
102  StatisticBase* stat, SimTime_t start_at_time, SimTime_t stop_at_time, SimTime_t output_factor);
103 
104  /** Called by the Components and Subcomponent to perform a statistic Output.
105  * @param stat - Pointer to the statistic.
106  * @param end_of_sim_flag - Indicates that the output is occurring at the end of simulation.
107  */
108  void performStatisticOutput(StatisticBase* stat, bool end_of_sim_flag = false);
109 
110  /** Called by the Components and Subcomponent to perform a global statistic Output.
111  * This routine will force ALL Components and Subcomponents to output their statistic information.
112  * This may lead to unexpected results if the statistic counts or data is reset on output.
113  * @param end_of_sim_flag - Indicates that the output is occurring at the end of simulation.
114  */
115  void performGlobalStatisticOutput(bool end_of_sim_flag = false);
116 
117  /** Return global statistic load level */
118  uint8_t getStatLoadLevel() const { return stat_load_level_; }
119 
120  /** Return the statistic output objects - static as they are per-MPI rank */
121  static const std::vector<StatisticOutput*>& getStatOutputs() { return stat_outputs_; }
122 
123  /** Called to setup the StatOutputs, which are shared across all
124  the StatEngines on the same MPI rank.
125  */
126  static void static_setup(StatsConfig* stats_config);
127 
128  /** Called to notify StatOutputs that simulation has started
129  */
130  static void stat_outputs_simulation_start();
131 
132  /** Called to notify StatOutputs that simulation has ended
133  */
134  static void stat_outputs_simulation_end();
135 
136  /** Get the start time factor belonging to 'stat'.
137  * Used during checkpoint. Linear search - could be slow.
138  * @param stat The statistic to lookup
139  */
141 
142  /** Get the stop time factor belonging to 'stat'.
143  * Used during checkpoint. Linear search - could be slow.
144  * @param stat The statistic to lookup
145  */
146  SimTime_t getStatisticStopTimeFactor(StatisticBase* stat);
147 
148  /** Serialization support for the engine. */
150  ImplementSerializable(SST::Statistics::StatisticProcessingEngine)
151 
152 private:
153  friend class SST::Simulation_impl;
154  friend int ::main(int argc, char** argv);
155  friend void ::finalize_statEngineConfig();
156 
157  /** Constructor */
159 
160  /** Destructor */
162 
163  void setup(StatsConfig* stats_config);
164  void restart();
165 
166  static StatisticOutput* createStatisticOutput(const ConfigStatOutput& cfg);
167 
168  /** Registers a newly-created statistic with the engine
169  * Statistics created during a restart run should use reregisterStatisticWithEngine instead.
170  */
171  bool registerStatisticWithEngine(StatisticBase* stat, Params& params);
172 
173  StatisticOutput* getOutputForStatistic(const StatisticBase* stat) const;
174  StatisticGroup& getGroupForStatistic(const StatisticBase* stat) const;
175  bool addPeriodicBasedStatistic(SimTime_t factor, StatisticBase* stat);
176  void addEventBasedStatistic(const UnitAlgebra& count, StatisticBase* stat);
177  void setStatisticStartTime(StatisticBase* stat, SimTime_t factor);
178  void setStatisticStopTime(StatisticBase* stat, SimTime_t factor);
179 
180  void finalizeInitialization(); /* Called when performWireUp() finished */
181  void startOfSimulation();
182  void endOfSimulation();
183 
184  void performStatisticOutputImpl(StatisticBase* stat, bool end_of_sim_flag);
185  void performStatisticGroupOutputImpl(StatisticGroup& group, bool end_of_sim_flag);
186 
187  bool handleStatisticEngineClockEvent(Cycle_t cycle_num, SimTime_t time_factor);
188  bool handleGroupClockEvent(Cycle_t cycle_num, StatisticGroup* group);
189  void handleStatisticEngineStartTimeEvent(SimTime_t time_factor);
190  void handleStatisticEngineStopTimeEvent(SimTime_t time_factor);
191 
192  [[noreturn]]
193  void castError(const std::string& type, const std::string& stat_name, const std::string& field_name);
194 
195  using StatArray_t = std::vector<StatisticBase*>; /*!< Array of Statistics */
196  using StatMap_t = std::map<SimTime_t, StatArray_t*>; /*!< Map of simtimes to Statistic Arrays */
197 
198  /* All non-null statistics are stored in *one* (only one) of the following data structures:
199  * - stat_groups_
200  * - stat_default_groups_
201  *
202  * Any stats with a start time are also stored in start_time_map_ until their start time occurs
203  * Any stats with a stop time are also stored in stop_time_map_ until their stop time occurs
204  *
205  * The stat engine creates a single instance of each null stat type which is shared across all null stats on the
206  * rank. These stats are not stored by the engine.
207  */
208  StatMap_t start_time_map_; /*!< Map of Array's of Statistics that are started at a sim time */
209  StatMap_t stop_time_map_; /*!< Map of Array's of Statistics that are stopped at a sim time */
210  bool simulation_started_; /*!< Flag showing if Simulation has started */
211 
212  Output& output_;
213  uint8_t stat_load_level_; /*!< The global statistic load level */
214  std::map<SimTime_t, StatisticGroup>
215  stat_default_groups_; /*!< Statistic groups for statistics that are not explicitly assigned to one */
216  std::vector<StatisticGroup> stat_groups_; /*!< A list of all statistic group objects */
217 
218  static std::vector<StatisticOutput*>
219  stat_outputs_; /*!< The statistic output objects that exist on this engine's rank */
220 };
221 
222 } // namespace Statistics
223 } // namespace SST
224 
225 #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:57
static std::vector< StatisticOutput * > stat_outputs_
Definition: statengine.h:219
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
Statistic< T > * createStatistic(BaseComponent *comp, const std::string &stat_name, const std::string &stat_sub_id, Params &params)
Called to create an enabled (non-null) statistic of the requested type This function also registers t...
Definition: statengine.h:67
uint8_t getStatLoadLevel() const
Return global statistic load level.
Definition: statengine.h:118
static void stat_outputs_simulation_start()
Called to notify StatOutputs that simulation has started.
Definition: statengine.cc:56
std::map< SimTime_t, StatArray_t * > StatMap_t
Definition: statengine.h:196
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:52
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization support for the engine.
Definition: statengine.cc:595
bool simulation_started_
Definition: statengine.h:210
Definition: configGraph.h:568
Forms the base class for statistics gathering within SST.
Definition: statbase.h:49
Definition: action.cc:18
void setup()
Perform the setup() and run phases of the simulation.
Definition: simulation.cc:885
Forms the template defined base class for statistics gathering within SST.
Definition: elementinfo.h:46
SimTime_t getStatisticStopTimeFactor(StatisticBase *stat)
Get the stop time factor belonging to &#39;stat&#39;.
Definition: statengine.cc:465
StatMap_t stop_time_map_
Definition: statengine.h:209
static const std::vector< StatisticOutput * > & getStatOutputs()
Return the statistic output objects - static as they are per-MPI rank.
Definition: statengine.h:121
std::vector< StatisticGroup > stat_groups_
Definition: statengine.h:216
Definition: serializable.h:23
~StatisticProcessingEngine()
Destructor.
Definition: statengine.cc:100
bool reregisterStatisticWithEngine(StatisticBase *stat, SimTime_t start_at_time, SimTime_t stop_at_time, SimTime_t output_factor)
Re-registers a statistic with the engine during a restart run.
Definition: statengine.cc:210
SimTime_t getStatisticStartTimeFactor(StatisticBase *stat)
Get the start time factor belonging to &#39;stat&#39;.
Definition: statengine.cc:425
std::map< SimTime_t, StatisticGroup > stat_default_groups_
Definition: statengine.h:215
static void static_setup(StatsConfig *stats_config)
Called to setup the StatOutputs, which are shared across all the StatEngines on the same MPI rank...
Definition: statengine.cc:47
uint8_t stat_load_level_
Definition: statengine.h:213
Statistic< T > * createDisabledStatistic()
Called to create a disabled (null) statistic of the requested type.
Definition: statengine.h:84
Main control class for a SST Simulation.
Definition: simulation_impl.h:122
StatMap_t start_time_map_
Definition: statengine.h:208
Main component object for the simulation.
Definition: baseComponent.h:64
std::vector< StatisticBase * > StatArray_t
Definition: statengine.h:195
std::enable_if_t<!std::is_same_v< std::string, T >, T > find(const std::string &k, T default_value, bool &found) const
Find a Parameter value in the set, and return its value as a type T.
Definition: params.h:333
static void stat_outputs_simulation_end()
Called to notify StatOutputs that simulation has ended.
Definition: statengine.cc:64
Parameter store.
Definition: params.h:63
Definition: configGraph.h:321
void performStatisticOutput(StatisticBase *stat, bool end_of_sim_flag=false)
Called by the Components and Subcomponent to perform a statistic Output.
Definition: statengine.cc:476
StatisticProcessingEngine()
Constructor.
Definition: statengine.cc:37
void performGlobalStatisticOutput(bool end_of_sim_flag=false)
Called by the Components and Subcomponent to perform a global statistic Output.
Definition: statengine.cc:534
bool registerStatisticWithEngine(StatisticBase *stat, Params &params)
Registers a newly-created statistic with the engine Statistics created during a restart run should us...
Definition: statengine.cc:108
An SST core component that handles timing and event processing informing all registered Statistics to...
Definition: statengine.h:54
Definition: statgroup.h:31
Performs Unit math in full precision.
Definition: unitAlgebra.h:105