hops
H5Easy_scalar.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #ifndef H5EASY_BITS_SCALAR_HPP
10 #define H5EASY_BITS_SCALAR_HPP
11 
12 #include "../H5Easy.hpp"
13 #include "H5Easy_misc.hpp"
14 
15 namespace H5Easy {
16 
17 namespace detail {
18 
19 /*
20 Base template for partial specialization: the fallback if specialized templates don't match.
21 Used e.g. for scalars.
22 */
23 template <typename T, typename = void>
24 struct io_impl {
25 
26  inline static DataSet dump(File& file,
27  const std::string& path,
28  const T& data,
29  const DumpOptions& options) {
30  DataSet dataset = initScalarDataset(file, path, data, options);
31  dataset.write(data);
32  if (options.flush()) {
33  file.flush();
34  }
35  return dataset;
36  }
37 
38  inline static T load(const File& file, const std::string& path) {
39  DataSet dataset = file.getDataSet(path);
40  T data;
41  dataset.read(data);
42  return data;
43  }
44 
45  inline static Attribute dumpAttribute(File& file,
46  const std::string& path,
47  const std::string& key,
48  const T& data,
49  const DumpOptions& options) {
50  Attribute attribute = initScalarAttribute(file, path, key, data, options);
51  attribute.write(data);
52  if (options.flush()) {
53  file.flush();
54  }
55  return attribute;
56  }
57 
58  inline static T loadAttribute(const File& file,
59  const std::string& path,
60  const std::string& key) {
61  DataSet dataset = file.getDataSet(path);
62  Attribute attribute = dataset.getAttribute(key);
63  T data;
64  attribute.read(data);
65  return data;
66  }
67 
68  inline static DataSet dump_extend(File& file,
69  const std::string& path,
70  const T& data,
71  const std::vector<size_t>& idx,
72  const DumpOptions& options) {
73  std::vector<size_t> ones(idx.size(), 1);
74 
75  if (file.exist(path)) {
76  DataSet dataset = file.getDataSet(path);
77  std::vector<size_t> dims = dataset.getDimensions();
78  std::vector<size_t> shape = dims;
79  if (dims.size() != idx.size()) {
80  throw detail::error(file, path,
81  "H5Easy::dump: Dimension of the index and the existing field do not match");
82  }
83  for (size_t i = 0; i < dims.size(); ++i) {
84  shape[i] = std::max(dims[i], idx[i] + 1);
85  }
86  if (shape != dims) {
87  dataset.resize(shape);
88  }
89  dataset.select(idx, ones).write(data);
90  if (options.flush()) {
91  file.flush();
92  }
93  return dataset;
94  }
95 
97  std::vector<size_t> shape = idx;
98  const size_t unlim = DataSpace::UNLIMITED;
99  std::vector<size_t> unlim_shape(idx.size(), unlim);
100  std::vector<hsize_t> chunks(idx.size(), 10);
101  if (options.isChunked()) {
102  chunks = options.getChunkSize();
103  if (chunks.size() != idx.size()) {
104  throw error(file, path, "H5Easy::dump: Incorrect dimension ChunkSize");
105  }
106  }
107  for (size_t& i : shape) {
108  i++;
109  }
110  DataSpace dataspace = DataSpace(shape, unlim_shape);
111  DataSetCreateProps props;
112  props.add(Chunking(chunks));
113  DataSet dataset = file.createDataSet(path, dataspace, AtomicType<T>(), props);
114  dataset.select(idx, ones).write(data);
115  if (options.flush()) {
116  file.flush();
117  }
118  return dataset;
119  }
120 
121  inline static T load_part(const File& file,
122  const std::string& path,
123  const std::vector<size_t>& idx) {
124  std::vector<size_t> ones(idx.size(), 1);
125  DataSet dataset = file.getDataSet(path);
126  T data;
127  dataset.select(idx, ones).read(data);
128  return data;
129  }
130 };
131 
132 } // namespace detail
133 } // namespace H5Easy
134 
135 #endif // H5EASY_BITS_SCALAR_HPP
H5Easy::detail::initScalarDataset
DataSet initScalarDataset(File &file, const std::string &path, const T &data, const DumpOptions &options)
Definition: H5Easy_misc.hpp:114
HighFive::DataSpace::UNLIMITED
static const size_t UNLIMITED
Definition: H5DataSpace.hpp:42
HighFive::Attribute::read
void read(T &array) const
Definition: H5Attribute_misc.hpp:58
H5Easy::detail::io_impl
Definition: H5Easy_scalar.hpp:24
HighFive::NodeTraits::getDataSet
DataSet getDataSet(const std::string &dataset_name, const DataSetAccessProps &accessProps=DataSetAccessProps()) const
get an existing dataset in the current file
Definition: H5Node_traits_misc.hpp:95
H5Easy_misc.hpp
H5Easy::DumpOptions::getChunkSize
std::vector< hsize_t > getChunkSize() const
Get chunk size.
Definition: H5Easy_public.hpp:93
HighFive::SliceTraits::read
void read(T &array) const
Definition: H5Slice_traits_misc.hpp:158
HighFive::DataSetCreateProps
PropertyList< PropertyType::DATASET_CREATE > DataSetCreateProps
Definition: H5PropertyList.hpp:79
H5Easy::detail::io_impl::dumpAttribute
static Attribute dumpAttribute(File &file, const std::string &path, const std::string &key, const T &data, const DumpOptions &options)
Definition: H5Easy_scalar.hpp:45
H5Easy::DumpOptions
Options for dumping data.
Definition: H5Easy.hpp:112
H5Easy::detail::createGroupsToDataSet
void createGroupsToDataSet(File &file, const std::string &path)
Recursively create groups in an open HDF5 file such that a DataSet can be created (see getParentName)...
Definition: H5Easy_misc.hpp:44
H5Easy::detail::io_impl::load_part
static T load_part(const File &file, const std::string &path, const std::vector< size_t > &idx)
Definition: H5Easy_scalar.hpp:121
HighFive::SliceTraits::select
Selection select(const std::vector< size_t > &offset, const std::vector< size_t > &count, const std::vector< size_t > &stride=std::vector< size_t >()) const
Select a region in the current Slice/Dataset of count points at offset separated by stride....
Definition: H5Slice_traits_misc.hpp:75
HighFive::SliceTraits::write
void write(const T &buffer)
Definition: H5Slice_traits_misc.hpp:200
HighFive::DataSet::getDimensions
std::vector< size_t > getDimensions() const
Get the dimensions of the whole DataSet. This is a shorthand for getSpace().getDimensions()
Definition: H5DataSet.hpp:83
H5Easy::detail::initScalarAttribute
Attribute initScalarAttribute(File &file, const std::string &path, const std::string &key, const T &data, const DumpOptions &options)
Definition: H5Easy_misc.hpp:163
HighFive::Chunking
Definition: H5PropertyList.hpp:95
H5Easy
Definition: H5Easy.hpp:51
H5Easy::DumpOptions::isChunked
bool isChunked() const
Check if chunk-size is manually set (or should be computed automatically).
Definition: H5Easy_public.hpp:88
HighFive::File
File class.
Definition: H5File.hpp:24
HighFive::AtomicType
create an HDF5 DataType from a C++ type
Definition: H5_definitions.hpp:36
HighFive::Attribute
Class representing an attribute of a dataset or group.
Definition: H5Attribute.hpp:23
HighFive::AnnotateTraits::getAttribute
Attribute getAttribute(const std::string &attribute_name) const
open an existing attribute with the name attribute_name
Definition: H5Annotate_traits_misc.hpp:69
HighFive::DataSet::resize
void resize(const std::vector< size_t > &dims)
Change the size of the dataset.
Definition: H5DataSet_misc.hpp:65
H5Easy::detail::io_impl::loadAttribute
static T loadAttribute(const File &file, const std::string &path, const std::string &key)
Definition: H5Easy_scalar.hpp:58
HighFive::DataSpace
Class representing the space (dimensions) of a dataset.
Definition: H5DataSpace.hpp:37
H5Easy::detail::error
Exception error(const File &file, const std::string &path, const std::string &message)
Definition: H5Easy_misc.hpp:52
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
HighFive::File::flush
void flush()
flush
Definition: H5File_misc.hpp:83
HighFive::NodeTraits::exist
bool exist(const std::string &node_name) const
check a dataset or group exists in the current node / group
Definition: H5Node_traits_misc.hpp:218
H5Easy::detail::io_impl::load
static T load(const File &file, const std::string &path)
Definition: H5Easy_scalar.hpp:38
H5Easy::DumpOptions::flush
bool flush() const
Check to flush.
Definition: H5Easy_public.hpp:73
HighFive::NodeTraits::createDataSet
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space
Definition: H5Node_traits_misc.hpp:36
H5Easy::detail::io_impl::dump
static DataSet dump(File &file, const std::string &path, const T &data, const DumpOptions &options)
Definition: H5Easy_scalar.hpp:26
HighFive::DataSet
Class representing a dataset.
Definition: H5DataSet.hpp:27
HighFive::Attribute::write
void write(const T &buffer)
Definition: H5Attribute_misc.hpp:106
H5Easy::detail::io_impl::dump_extend
static DataSet dump_extend(File &file, const std::string &path, const T &data, const std::vector< size_t > &idx, const DumpOptions &options)
Definition: H5Easy_scalar.hpp:68