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