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