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