gem5  v20.1.0.0
hdf5.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 Arm Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "base/stats/hdf5.hh"
39 
40 #include "base/logging.hh"
41 #include "base/stats/info.hh"
42 
46 template<typename T>
47 bool emptyStrings(const T &labels)
48 {
49  for (const auto &s : labels) {
50  if (!s.empty())
51  return false;
52  }
53  return true;
54 }
55 
56 
57 namespace Stats {
58 
59 Hdf5::Hdf5(const std::string &file, unsigned chunking,
60  bool desc, bool formulas)
61  : fname(file), timeChunk(chunking),
62  enableDescriptions(desc), enableFormula(formulas),
63  dumpCount(0)
64 {
65  // Tell the library not to print exceptions by default. There are
66  // cases where we rely on exceptions to determine if we need to
67  // create a node or if we can just open it.
68  H5::Exception::dontPrint();
69 }
70 
72 {
73 }
74 
75 
76 void
78 {
79  h5File = H5::H5File(fname,
80  // Truncate the file if this is the first dump
81  dumpCount > 0 ? H5F_ACC_RDWR : H5F_ACC_TRUNC);
82  path.push(h5File.openGroup("/"));
83 }
84 
85 void
87 {
88  assert(valid());
89 
90  dumpCount++;
91 }
92 
93 bool
94 Hdf5::valid() const
95 {
96  return true;
97 }
98 
99 
100 void
101 Hdf5::beginGroup(const char *name)
102 {
103  auto base = path.top();
104 
105  // Try to open an existing stat group corresponding to the
106  // name. Create it if it doesn't exist.
107  H5::Group group;
108  try {
109  group = base.openGroup(name);
110  } catch (const H5::FileIException& e) {
111  group = base.createGroup(name);
112  } catch (const H5::GroupIException& e) {
113  group = base.createGroup(name);
114  }
115 
116  path.push(group);
117 }
118 
119 void
121 {
122  assert(!path.empty());
123  path.pop();
124 }
125 
126 void
128 {
129  // Since this stat is a scalar, we need 1-dimensional value in the
130  // stat file. The Hdf5::appendStat helper will populate the size
131  // of the first dimension (time).
132  hsize_t fdims[1] = { 0, };
133  double data[1] = { info.result(), };
134 
135  appendStat(info, 1, fdims, data);
136 }
137 
138 void
140 {
141  appendVectorInfo(info);
142 }
143 
144 void
145 Hdf5::visit(const DistInfo &info)
146 {
147  warn_once("HDF5 stat files don't support distributions.\n");
148 }
149 
150 void
152 {
153  warn_once("HDF5 stat files don't support vector distributions.\n");
154 }
155 
156 void
158 {
159  // Request a 3-dimensional stat, the first dimension will be
160  // populated by the Hdf5::appendStat() helper. The remaining two
161  // dimensions correspond to the stat instance.
162  hsize_t fdims[3] = { 0, info.x, info.y };
163  H5::DataSet data_set = appendStat(info, 3, fdims, info.cvec.data());
164 
165  if (dumpCount == 0) {
166  if (!info.subnames.empty() && !emptyStrings(info.subnames))
167  addMetaData(data_set, "subnames", info.subnames);
168 
169  if (!info.y_subnames.empty() && !emptyStrings(info.y_subnames))
170  addMetaData(data_set, "y_subnames", info.y_subnames);
171 
172  if (!info.subdescs.empty() && !emptyStrings(info.subdescs))
173  addMetaData(data_set, "subdescs", info.subdescs);
174  }
175 }
176 
177 void
179 {
180  if (!enableFormula)
181  return;
182 
183  H5::DataSet data_set = appendVectorInfo(info);
184 
185  if (dumpCount == 0)
186  addMetaData(data_set, "equation", info.str());
187 }
188 
189 void
191 {
192  warn_once("HDF5 stat files don't support sparse histograms.\n");
193 }
194 
195 H5::DataSet
197 {
198  const VResult &vr(info.result());
199  // Request a 2-dimensional stat, the first dimension will be
200  // populated by the Hdf5::appendStat() helper. The remaining
201  // dimension correspond to the stat instance.
202  hsize_t fdims[2] = { 0, vr.size() };
203  H5::DataSet data_set = appendStat(info, 2, fdims, vr.data());
204 
205  if (dumpCount == 0) {
206  if (!info.subnames.empty() && !emptyStrings(info.subnames))
207  addMetaData(data_set, "subnames", info.subnames);
208 
209  if (!info.subdescs.empty() && !emptyStrings(info.subdescs))
210  addMetaData(data_set, "subdescs", info.subdescs);
211  }
212 
213  return data_set;
214 }
215 
216 H5::DataSet
217 Hdf5::appendStat(const Info &info, int rank, hsize_t *dims, const double *data)
218 {
219  H5::Group group = path.top();
220  H5::DataSet data_set;
221  H5::DataSpace fspace;
222 
223  dims[0] = dumpCount + 1;
224 
225  if (dumpCount > 0) {
226  // Get the existing stat if we have already dumped this stat
227  // before.
228  data_set = group.openDataSet(info.name);
229  data_set.extend(dims);
230  fspace = data_set.getSpace();
231  } else {
232  // We don't have the stat already, create it.
233 
234  H5::DSetCreatPropList props;
235 
236  // Setup max dimensions based on the requested file dimensions
237  std::vector<hsize_t> max_dims(rank);
238  std::copy(dims, dims + rank, max_dims.begin());
239  max_dims[0] = H5S_UNLIMITED;
240 
241  // Setup chunking
242  std::vector<hsize_t> chunk_dims(rank);
243  std::copy(dims, dims + rank, chunk_dims.begin());
244  chunk_dims[0] = timeChunk;
245  props.setChunk(rank, chunk_dims.data());
246 
247  // Enable compression
248  props.setDeflate(1);
249 
250  fspace = H5::DataSpace(rank, dims, max_dims.data());
251  data_set = group.createDataSet(info.name, H5::PredType::NATIVE_DOUBLE,
252  fspace, props);
253 
254  if (enableDescriptions && !info.desc.empty()) {
255  addMetaData(data_set, "description", info.desc);
256  }
257  }
258 
259  // The first dimension is time which isn't included in data.
260  dims[0] = 1;
261  H5::DataSpace mspace(rank, dims);
262  std::vector<hsize_t> foffset(rank, 0);
263  foffset[0] = dumpCount;
264 
265  fspace.selectHyperslab(H5S_SELECT_SET, dims, foffset.data());
266  data_set.write(data, H5::PredType::NATIVE_DOUBLE, mspace, fspace);
267 
268  return data_set;
269 }
270 
271 void
272 Hdf5::addMetaData(H5::DataSet &loc, const char *name,
273  const std::vector<const char *> &values)
274 {
275  H5::StrType type(H5::PredType::C_S1, H5T_VARIABLE);
276  hsize_t dims[1] = { values.size(), };
277  H5::DataSpace space(1, dims);
278  H5::Attribute attribute = loc.createAttribute(name, type, space);
279  attribute.write(type, values.data());
280 }
281 
282 void
283 Hdf5::addMetaData(H5::DataSet &loc, const char *name,
284  const std::vector<std::string> &values)
285 {
286  std::vector<const char *> cstrs(values.size());
287  for (int i = 0; i < values.size(); ++i)
288  cstrs[i] = values[i].c_str();
289 
290  addMetaData(loc, name, cstrs);
291 }
292 
293 void
294 Hdf5::addMetaData(H5::DataSet &loc, const char *name,
295  const std::string &value)
296 {
297  H5::StrType type(H5::PredType::C_S1, value.length() + 1);
298  hsize_t dims[1] = { 1, };
299  H5::DataSpace space(1, dims);
300  H5::Attribute attribute = loc.createAttribute(name, type, space);
301  attribute.write(type, value.c_str());
302 }
303 
304 void
305 Hdf5::addMetaData(H5::DataSet &loc, const char *name, double value)
306 {
307  hsize_t dims[1] = { 1, };
308  H5::DataSpace space(1, dims);
309  H5::Attribute attribute = loc.createAttribute(
310  name, H5::PredType::NATIVE_DOUBLE, space);
311  attribute.write(H5::PredType::NATIVE_DOUBLE, &value);
312 }
313 
314 
315 std::unique_ptr<Output>
316 initHDF5(const std::string &filename, unsigned chunking,
317  bool desc, bool formulas)
318 {
319  return std::unique_ptr<Output>(
320  new Hdf5(simout.resolve(filename), chunking, desc, formulas));
321 }
322 
323 }; // namespace Stats
Stats::Vector2dInfo::subdescs
std::vector< std::string > subdescs
Definition: info.hh:223
Stats::FormulaInfo::str
virtual std::string str() const =0
data
const char data[]
Definition: circlebuf.test.cc:42
Stats::VectorInfo
Definition: info.hh:156
warn_once
#define warn_once(...)
Definition: logging.hh:243
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Stats::Vector2dInfo
Definition: info.hh:218
type
uint8_t type
Definition: inet.hh:421
Stats::Vector2dInfo::cvec
VCounter cvec
Local storage for the entry values, used for printing.
Definition: info.hh:230
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
Stats::Vector2dInfo::subnames
std::vector< std::string > subnames
Names and descriptions of subfields.
Definition: info.hh:222
Stats::Vector2dInfo::y_subnames
std::vector< std::string > y_subnames
Definition: info.hh:224
std::vector< Result >
Stats::Hdf5::endGroup
void endGroup() override
Definition: hdf5.cc:120
Stats::Hdf5::~Hdf5
~Hdf5()
Definition: hdf5.cc:71
OutputDirectory::resolve
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:203
Stats::Hdf5
Definition: hdf5.hh:54
Stats::ScalarInfo::result
virtual Result result() const =0
Stats::Hdf5::dumpCount
unsigned dumpCount
Definition: hdf5.hh:147
Stats::VectorDistInfo
Definition: info.hh:200
info.hh
Stats::DistInfo
Definition: info.hh:193
Stats::Hdf5::appendVectorInfo
H5::DataSet appendVectorInfo(const VectorInfo &info)
Helper function to append vector stats and set their metadata.
Definition: hdf5.cc:196
Stats::Hdf5::begin
void begin() override
Definition: hdf5.cc:77
Stats::Hdf5::appendStat
H5::DataSet appendStat(const Info &info, int rank, hsize_t *dims, const double *data)
Helper function to append an n-dimensional double stat to the file.
Definition: hdf5.cc:217
Stats::VectorInfo::result
virtual const VResult & result() const =0
Stats::Hdf5::Hdf5
Hdf5()=delete
Stats::Hdf5::end
void end() override
Definition: hdf5.cc:86
Stats::Hdf5::enableDescriptions
const bool enableDescriptions
Definition: hdf5.hh:142
Stats::VectorInfo::subdescs
std::vector< std::string > subdescs
Definition: info.hh:161
Stats::Vector2dInfo::x
size_type x
Definition: info.hh:226
Stats::SparseHistInfo
Definition: info.hh:251
Stats::Hdf5::fname
const std::string fname
Definition: hdf5.hh:140
Stats::Hdf5::path
std::stack< H5::Group > path
Definition: hdf5.hh:145
name
const std::string & name()
Definition: trace.cc:50
Stats::Info
Definition: info.hh:69
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
Stats::VectorInfo::subnames
std::vector< std::string > subnames
Names and descriptions of subfields.
Definition: info.hh:160
Stats::FormulaInfo
Definition: info.hh:237
Stats::Hdf5::visit
void visit(const ScalarInfo &info) override
Definition: hdf5.cc:127
Stats::Vector2dInfo::y
size_type y
Definition: info.hh:227
Stats::Hdf5::valid
bool valid() const override
Definition: hdf5.cc:94
Stats::Info::name
std::string name
The name of the stat.
Definition: info.hh:73
Stats::initHDF5
std::unique_ptr< Output > initHDF5(const std::string &filename, unsigned chunking, bool desc, bool formulas)
Definition: hdf5.cc:316
logging.hh
Stats::ScalarInfo
Definition: info.hh:148
Stats::Info::desc
std::string desc
The description of the stat.
Definition: info.hh:77
Stats
Definition: statistics.cc:61
Stats::Hdf5::timeChunk
const hsize_t timeChunk
Definition: hdf5.hh:141
emptyStrings
bool emptyStrings(const T &labels)
Check if all strings in a container are empty.
Definition: hdf5.cc:47
Stats::Hdf5::enableFormula
const bool enableFormula
Definition: hdf5.hh:143
simout
OutputDirectory simout
Definition: output.cc:61
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
Stats::Hdf5::h5File
H5::H5File h5File
Definition: hdf5.hh:148
hdf5.hh
Stats::Hdf5::beginGroup
void beginGroup(const char *name) override
Definition: hdf5.cc:101
Stats::Hdf5::addMetaData
void addMetaData(H5::DataSet &loc, const char *name, const std::vector< const char * > &values)
Helper function to add a string vector attribute to a stat.
Definition: hdf5.cc:272

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17