SST  11.0.0
StructuralSimulationToolkit
statoutputhdf5.h
1 // Copyright 2009-2021 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-2021, 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 _H_SST_CORE_STATISTICS_OUTPUTHDF5
13 #define _H_SST_CORE_STATISTICS_OUTPUTHDF5
14 
15 #include "sst/core/sst_types.h"
16 
17 #include "sst/core/statapi/statoutput.h"
18 #include "sst/core/warnmacros.h"
19 
20 DISABLE_WARN_MISSING_OVERRIDE
21 #include "H5Cpp.h"
22 REENABLE_WARNING
23 
24 #include <map>
25 #include <string>
26 
27 namespace SST {
28 namespace Statistics {
29 
30 /**
31  \class StatisticOutputHDF5
32 
33  The class for statistics output to a comma separated file.
34 */
36 {
37 public:
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  StatisticOutputHDF5(Params& outputParameters);
50 
51  bool acceptsGroups() const override { return true; }
52 private:
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 
109 protected:
110  StatisticOutputHDF5() {;} // For serialization
111 
112 private:
113 
114  typedef union {
115  int32_t i32;
116  uint32_t u32;
117  int64_t i64;
118  uint64_t u64;
119  float f;
120  double d;
121  } StatData_u;
122 
123 
124  class DataSet {
125  public:
126  DataSet(H5::H5File *file) : file(file) { }
127  virtual ~DataSet() { }
128  H5::H5File* getFile() { return file; }
129  virtual bool isGroup() const = 0;
130 
131  virtual void setCurrentStatistic(StatisticBase *UNUSED(stat)) { }
132  virtual void registerField(StatisticFieldInfo *fi) = 0;
133  virtual void finalizeCurrentStatistic() = 0;
134 
135  virtual void beginGroupRegistration(StatisticGroup *UNUSED(group)) { }
136  virtual void finalizeGroupRegistration() { }
137 
138 
139  virtual void startNewGroupEntry() {}
140  virtual void finishGroupEntry() {}
141 
142  virtual void startNewEntry(StatisticBase *stat) = 0;
143  virtual StatData_u& getFieldLoc(fieldHandle_t fieldHandle) = 0;
144  virtual void finishEntry() = 0;
145 
146  protected:
147  H5::H5File *file;
148  };
149 
150  class StatisticInfo : public DataSet {
151  StatisticBase *statistic;
152  std::vector<fieldHandle_t> indexMap;
153  std::vector<StatData_u> currentData;
154  std::vector<fieldType_t> typeList;
155  std::vector<std::string> fieldNames;
156 
157  H5::DataSet *dataset;
158  H5::CompType *memType;
159 
160  hsize_t nEntries;
161 
162  public:
163  StatisticInfo(StatisticBase *stat, H5::H5File *file) :
164  DataSet(file), statistic(stat), nEntries(0)
165  {
166  typeList.push_back(StatisticFieldType<uint64_t>::id());
167  indexMap.push_back(-1);
168  fieldNames.push_back("SimTime");
169  }
170  ~StatisticInfo() {
171  if ( dataset ) delete dataset;
172  if ( memType ) delete memType;
173  }
174  void registerField(StatisticFieldInfo *fi) override;
175  void finalizeCurrentStatistic() override;
176 
177  bool isGroup() const override { return false; }
178  void startNewEntry(StatisticBase *stat) override;
179  StatData_u& getFieldLoc(fieldHandle_t fieldHandle) override;
180  void finishEntry() override;
181  };
182 
183  class GroupInfo : public DataSet {
184  struct GroupStat {
185  GroupInfo *gi;
186  std::string statPath;
187 
188  H5::DataSet *dataset;
189  H5::CompType *memType;
190 
191  hsize_t nEntries;
192 
193  std::vector<std::string> registeredFields; /* fi->uniqueName */
194  std::vector<fieldType_t> typeList;
195  std::map<fieldHandle_t, size_t> handleIndexMap;
196 
197  std::vector<StatData_u> currentData;
198  size_t currentCompOffset;
199 
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 
215 
216  hsize_t nEntries;
217  std::map<std::string, GroupStat> m_statGroups;
218  GroupStat *m_currentStat;
219  StatisticGroup *m_statGroup;
220  std::vector<BaseComponent*> m_components;
221  H5::DataSet *timeDataSet;
222 
223  public:
224  GroupInfo(StatisticGroup *group, H5::H5File *file);
225  void beginGroupRegistration(StatisticGroup *UNUSED(group)) override { }
226  void setCurrentStatistic(StatisticBase *stat) override;
227  void registerField(StatisticFieldInfo *fi) override;
228  void finalizeCurrentStatistic() override;
229  void finalizeGroupRegistration() override;
230 
231  bool isGroup() const override { return true; }
232  void startNewEntry(StatisticBase *stat) override;
233  StatData_u& getFieldLoc(fieldHandle_t fieldHandle) override { return m_currentStat->getFieldLoc(fieldHandle); }
234  void finishEntry() override;
235 
236  void startNewGroupEntry() override;
237  void finishGroupEntry() override;
238  size_t getNumComponents() const { return m_components.size(); }
239 
240  const std::string& getName() const;
241  };
242 
243 
244  H5::H5File* m_hFile;
245  DataSet* m_currentDataSet;
246  std::map<StatisticBase*, StatisticInfo*> m_statistics;
247  std::map<std::string, GroupInfo> m_statGroups;
248 
249 
250  StatisticInfo* initStatistic(StatisticBase* statistic);
251  StatisticInfo* getStatisticInfo(StatisticBase* statistic);
252 };
253 
254 } //namespace Statistics
255 } //namespace SST
256 
257 #endif
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:49
Forms the base class for statistics gathering within SST.
Definition: statbase.h:64
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function)
Definition: statoutput.h:241
bool acceptsGroups() const override
True if this StatOutput can handle StatisticGroups.
Definition: statoutputhdf5.h:51
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.
Definition: statoutput.h:144
The class for statistics output to a comma separated file.
Definition: statoutputhdf5.h:35
Parameter store.
Definition: params.h:44
Definition: statgroup.h:29