SST 15.0
Structural Simulation Toolkit
statoutputhdf5.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_STATOUTPUTHDF5_H
13#define SST_CORE_STATAPI_STATOUTPUTHDF5_H
14
15#include "sst/core/sst_types.h"
16#include "sst/core/statapi/statoutput.h"
17#include "sst/core/warnmacros.h"
18
19DISABLE_WARN_MISSING_OVERRIDE
20#include "H5Cpp.h"
21REENABLE_WARNING
22
23#include <cstddef>
24#include <map>
25#include <string>
26#include <vector>
27
28namespace SST::Statistics {
29
30/**
31 \class StatisticOutputHDF5
32
33 The class for statistics output to a comma separated file.
34*/
35class StatisticOutputHDF5 : public StatisticFieldsOutput
36{
37public:
40 StatisticOutputHDF5,
41 "sst",
42 "statoutputhdf5",
43 SST_ELI_ELEMENT_VERSION(1,0,0),
44 "Output to an HDF5 file")
45
46 /** Construct a StatOutputHDF5
47 * @param outputParameters - Parameters used for this Statistic Output
48 */
49 explicit StatisticOutputHDF5(Params& outputParameters);
50
51 bool acceptsGroups() const override { return true; }
52
53 void serialize_order(SST::Core::Serialization::serializer& ser) override;
54
55 ImplementSerializable(SST::Statistics::StatisticOutputHDF5)
56
57private:
58 /** Perform a check of provided parameters
59 * @return True if all required parameters and options are acceptable
60 */
61 bool checkOutputParameters() override;
62
63 /** Print out usage for this Statistic Output */
64 void printUsage() override;
65
66 void startRegisterFields(StatisticBase* stat) override;
67 void implRegisteredField(fieldHandle_t fieldHandle) override;
68 void stopRegisterFields() override;
69
70 void startRegisterGroup(StatisticGroup* group) override;
71 void stopRegisterGroup() override;
72
73 /** Indicate to Statistic Output that simulation started.
74 * Statistic output may perform any startup code here as necessary.
75 */
76 void startOfSimulation() override;
77
78 /** Indicate to Statistic Output that simulation ended.
79 * Statistic output may perform any shutdown code here as necessary.
80 */
81 void endOfSimulation() override;
82
83 /** Implementation function for the start of output.
84 * This will be called by the Statistic Processing Engine to indicate that
85 * a Statistic is about to send data to the Statistic Output for processing.
86 * @param statistic - Pointer to the statistic object than the output can
87 * retrieve data from.
88 */
89 void implStartOutputEntries(StatisticBase* statistic) override;
90
91 /** Implementation function for the end of output.
92 * This will be called by the Statistic Processing Engine to indicate that
93 * a Statistic is finished sending data to the Statistic Output for processing.
94 * The Statistic Output can perform any output related functions here.
95 */
96 void implStopOutputEntries() override;
97
98 void startOutputGroup(StatisticGroup* group) override;
99 void stopOutputGroup() override;
100
101 /** Implementation functions for output.
102 * These will be called by the statistic to provide Statistic defined
103 * data to be output.
104 * @param fieldHandle - The handle to the registered statistic field.
105 * @param data - The data related to the registered field to be output.
106 */
107 void outputField(fieldHandle_t fieldHandle, int32_t data) override;
108 void outputField(fieldHandle_t fieldHandle, uint32_t data) override;
109 void outputField(fieldHandle_t fieldHandle, int64_t data) override;
110 void outputField(fieldHandle_t fieldHandle, uint64_t data) override;
111 void outputField(fieldHandle_t fieldHandle, float data) override;
112 void outputField(fieldHandle_t fieldHandle, double data) override;
113
114protected:
115 StatisticOutputHDF5(); // For serialization
116
117private:
118 union StatData_u {
119 int32_t i32;
120 uint32_t u32;
121 int64_t i64;
122 uint64_t u64;
123 float f;
124 double d;
125 };
126
127 class DataSet
128 {
129 public:
130 explicit DataSet(H5::H5File* file) :
131 file(file)
132 {}
133 virtual ~DataSet() {}
134 H5::H5File* getFile() { return file; }
135 virtual bool isGroup() const = 0;
136
137 virtual void setCurrentStatistic(StatisticBase* UNUSED(stat)) {}
138 virtual void registerField(StatisticFieldInfo* fi) = 0;
139 virtual void finalizeCurrentStatistic() = 0;
140
141 virtual void beginGroupRegistration(StatisticGroup* UNUSED(group)) {}
142 virtual void finalizeGroupRegistration() {}
143
144 virtual void startNewGroupEntry(Cycle_t UNUSED(cycle)) {}
145 virtual void finishGroupEntry() {}
146
147 virtual void startNewEntry(StatisticBase* stat, Cycle_t cycle) = 0;
148 virtual StatData_u& getFieldLoc(fieldHandle_t fieldHandle) = 0;
149 virtual void finishEntry() = 0;
150
151 protected:
152 H5::H5File* file;
153 };
154
155 class StatisticInfo : public DataSet
156 {
157 StatisticBase* statistic;
158 std::vector<fieldHandle_t> indexMap;
159 std::vector<StatData_u> currentData;
160 std::vector<fieldType_t> typeList;
161 std::vector<std::string> fieldNames;
162
163 H5::DataSet* dataset;
164 H5::CompType* memType;
165
166 hsize_t nEntries;
167
168 public:
169 StatisticInfo(StatisticBase* stat, H5::H5File* file) :
170 DataSet(file),
171 statistic(stat),
172 nEntries(0)
173 {
174 typeList.push_back(StatisticFieldType<uint64_t>::id());
175 indexMap.push_back(-1);
176 fieldNames.push_back("SimTime");
177 }
178 ~StatisticInfo()
179 {
180 if ( dataset ) delete dataset;
181 if ( memType ) delete memType;
182 }
183 void registerField(StatisticFieldInfo* fi) override;
184 void finalizeCurrentStatistic() override;
185
186 bool isGroup() const override { return false; }
187 void startNewEntry(StatisticBase* stat, Cycle_t cycle) override;
188 StatData_u& getFieldLoc(fieldHandle_t fieldHandle) override;
189 void finishEntry() override;
190 };
191
192 class GroupInfo : public DataSet
193 {
194 struct GroupStat
195 {
196 GroupInfo* gi;
197 std::string statPath;
198
199 H5::DataSet* dataset;
200 H5::CompType* memType;
201
202 hsize_t nEntries;
203
204 std::vector<std::string> registeredFields; /* fi->uniqueName */
205 std::vector<fieldType_t> typeList;
206 std::map<fieldHandle_t, size_t> handleIndexMap;
207
208 std::vector<StatData_u> currentData;
209 size_t currentCompOffset;
210
211 GroupStat(GroupInfo* group, StatisticBase* stat);
212 void finalizeRegistration();
213 static std::string getStatName(StatisticBase* stat);
214
215 void startNewGroupEntry();
216
217 void startNewEntry(size_t componentIndex, StatisticBase* stat);
218 StatData_u& getFieldLoc(fieldHandle_t fieldHandle);
219 void finishEntry();
220
221 void finishGroupEntry();
222 };
223
224 hsize_t nEntries;
225 std::map<std::string, GroupStat> m_statGroups;
226 GroupStat* m_currentStat;
227 StatisticGroup* m_statGroup;
228 std::vector<BaseComponent*> m_components;
229 H5::DataSet* timeDataSet;
230
231 public:
232 GroupInfo(StatisticGroup* group, H5::H5File* file);
233 void beginGroupRegistration(StatisticGroup* UNUSED(group)) override {}
234 void setCurrentStatistic(StatisticBase* stat) override;
235 void registerField(StatisticFieldInfo* fi) override;
236 void finalizeCurrentStatistic() override;
237 void finalizeGroupRegistration() override;
238
239 bool isGroup() const override { return true; }
240 void startNewEntry(StatisticBase* stat, Cycle_t cycle) override;
241 StatData_u& getFieldLoc(fieldHandle_t fieldHandle) override { return m_currentStat->getFieldLoc(fieldHandle); }
242 void finishEntry() override;
243
244 void startNewGroupEntry(Cycle_t cycle) override;
245 void finishGroupEntry() override;
246 size_t getNumComponents() const { return m_components.size(); }
247
248 const std::string& getName() const;
249 };
250
251 H5::H5File* m_hFile;
252 DataSet* m_currentDataSet;
253 std::map<StatisticBase*, StatisticInfo*> m_statistics;
254 std::map<std::string, GroupInfo> m_statGroups;
255
256 StatisticInfo* initStatistic(StatisticBase* statistic);
257 StatisticInfo* getStatisticInfo(StatisticBase* statistic);
258};
259
260} // namespace SST::Statistics
261
262#endif // SST_CORE_STATAPI_STATOUTPUTHDF5_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
Parameter store.
Definition params.h:58
Forms the base class for statistics gathering within SST.
Definition statbase.h:49
ImplementVirtualSerializable(SST::Statistics::StatisticFieldsOutput) protected StatisticFieldsOutput()
Construct a base StatisticOutput.
Definition statoutput.h:292
Definition statgroup.h:31
The class for statistics output to a comma separated file.
Definition statoutputhdf5.h:36
ImplementSerializable(SST::Statistics::StatisticOutputHDF5) private void printUsage() override
Perform a check of provided parameters.
Definition statoutputhdf5.cc:75
void startRegisterFields(StatisticBase *stat) override
These can be overriden, if necessary, but must be callable by the derived class.
Definition statoutputhdf5.cc:86
void endOfSimulation() override
Indicate to Statistic Output that simulation ended.
Definition statoutputhdf5.cc:135
void startOfSimulation() override
Indicate to Statistic Output that simulation started.
Definition statoutputhdf5.cc:131
void implStartOutputEntries(StatisticBase *statistic) override
Implementation function for the start of output.
Definition statoutputhdf5.cc:144
void outputField(fieldHandle_t fieldHandle, int32_t data) override
Implementation functions for output.
Definition statoutputhdf5.cc:174
bool acceptsGroups() const override
True if this StatOutput can handle StatisticGroups.
Definition statoutputhdf5.h:51
void implStopOutputEntries() override
Implementation function for the end of output.
Definition statoutputhdf5.cc:151
SST_ELI_REGISTER_DERIVED(StatisticOutput, StatisticOutputHDF5, "sst", "statoutputhdf5", SST_ELI_ELEMENT_VERSION(1, 0, 0), "Output to an HDF5 file") explicit StatisticOutputHDF5(Params &outputParameters)
Construct a StatOutputHDF5.
Forms the base class for statistics output generation within the SST core.
Definition statoutput.h:52
virtual bool checkOutputParameters()=0
Have the Statistic Output check its parameters.