1 #ifndef UTOPIA_DATAIO_HDFDATASPACE_HH
2 #define UTOPIA_DATAIO_HDFDATASPACE_HH
9 #include "../core/logging.hh"
10 #include "../core/type_traits.hh"
11 #include "../core/ostream.hh"
50 return H5Sget_simple_extent_ndims(
get_C_id());
54 throw std::runtime_error(
55 "Error, trying to get rank of invalid dataspace");
66 std::pair< arma::Row< hsize_t >, arma::Row< hsize_t > >
71 throw std::runtime_error(
72 "Error, trying to get properties of invalid dataspace," +
76 arma::Row< hsize_t >
size;
120 this->
_log->debug(
"Opening dataspace, setting it to H5S_ALL");
122 _id.
open(H5S_ALL, [](hid_t) -> herr_t {
return 1; });
123 _path =
"Dataspace_all";
132 template <
typename Object >
148 _log = spdlog::get(
"data_io");
164 arma::Row< hsize_t > extent,
168 this->
_log->debug(
"Opening dataspace from scratch with rank {}, extent "
169 "{} and capacity {}",
176 H5Screate_simple(
rank, extent.memptr(), NULL), &H5Sclose, name);
185 _log = spdlog::get(
"data_io");
194 std::pair< arma::Row< hsize_t >, arma::Row< hsize_t > >
197 arma::Row< hsize_t > start;
198 arma::Row< hsize_t >
end;
206 if (H5Sget_select_bounds(
get_C_id(), start.memptr(),
end.memptr()) <
209 throw std::runtime_error(
210 "Error, cannot get selection bounds of invalid dataspace");
213 return std::make_pair(start,
end);
226 arma::Row< hsize_t >
end,
227 arma::Row< hsize_t > stride)
231 "Selecting slice in dataspace with start={}, end={}, stride={}",
238 throw std::runtime_error(
239 "Error, trying to select a slice in an invalid dataspace");
244 if ((start.n_elem != r) or (
end.n_elem != r))
246 throw std::runtime_error(
247 "Error, dimensionality of start and end has to be the same as "
248 "the dataspace's rank");
254 arma::Row< hsize_t > count;
255 hsize_t* strideptr =
nullptr;
256 if (stride.size() == 0)
258 count = ((
end - start)).as_row();
262 count = ((
end - start) / stride).as_row();
263 strideptr = stride.memptr();
266 auto err = H5Sselect_hyperslab(
get_C_id(),
274 throw std::runtime_error(
275 "Error when trying to select slice in dataspace");
286 this->
_log->debug(
"Selecting everything in dataspace");
290 throw std::runtime_error(
291 "Error, trying to select everything of an invalid dataspace");
294 herr_t err = H5Sselect_all(
get_C_id());
297 throw std::runtime_error(
298 "Error when trying to select entire dataspace");
313 this->
_log->debug(
"Resizing dataset from {} to {}",
319 throw std::runtime_error(
320 "Error, trying to resize an invalid dataspace");
326 auto new_capacity = arma::max(current_capacity, new_size);
329 herr_t err = H5Sset_extent_simple(
get_C_id(),
332 current_capacity.memptr());
336 throw std::runtime_error(
"Error in resizing dataspace");
347 this->
_log->debug(
"Releasing selection");
351 throw std::runtime_error(
352 "Cannot reset selection, dataspace is invalid");
405 std::vector< hsize_t > extent,
418 template <
typename Object >
421 not std::is_same_v< std::decay_t< Object >,
HDFDataspace >,
424 open(std::forward< Object >(
object));
443 swap(
static_cast< Base&
>(*
this),
static_cast< Base&
>(other));
Class that wraps an HDF5 dataspace and takes care of managing its resources.
Definition: hdfdataspace.hh:37
void open(hid_t id, std::function< herr_t(hid_t) > closing_func)
Open the object and bind it to another C-Level id.
Definition: hdfidentifier.hh:156
Common base class for all HDF5 classes in the DATAIO Module i.e., for all classes that wrap HDF5-C-Li...
Definition: hdfobject.hh:37
std::string _path
Name of the object.
Definition: hdfobject.hh:50
std::string get_path() const
Get the name or path object.
Definition: hdfobject.hh:88
virtual bool is_valid() const
Check if the object is still valid.
Definition: hdfobject.hh:143
std::shared_ptr< spdlog::logger > _log
pointer to the logger for dataio
Definition: hdfobject.hh:56
HDFIdentifier _id
Identifier object that binds an instance of this class to an HDF5 object.
Definition: hdfobject.hh:44
hid_t get_C_id() const
Get the C id object.
Definition: hdfobject.hh:120
void bind_to(hid_t id, std::function< herr_t(hid_t) > closing_func, std::string path={})
Open the object and bind it to a HDF5 object identified by 'id' with name 'path'. Object should be cr...
Definition: hdfobject.hh:186
std::string to_string(const Config &node)
Given a config node, returns a string representation of it.
Definition: cfg_utils.hh:110
void swap(WriteTask< BGB, DW, DB, AWG, AWD > &lhs, WriteTask< BGB, DW, DB, AWG, AWD > &rhs)
Swaps the state of lhs and rhs.
Definition: write_task.hh:240
hid_t open_dataspace(Object &&object)
Depending on object category, opens a dataset or attribute dataspace.
Definition: hdfutilities.hh:235
arma::Row< hsize_t > size()
Get the current size of the dataspace in each dimension.
Definition: hdfdataspace.hh:94
void open(std::string name, hsize_t rank, arma::Row< hsize_t > extent, arma::Row< hsize_t > capacity)
Open a new dataset of type 'simple', which is equivalent to a N-dimensional array of dimension N = 'r...
Definition: hdfdataspace.hh:162
void swap(HDFDataspace &other)
Swap state with argument.
Definition: hdfdataspace.hh:439
HDFDataspace(HDFDataspace &&)=default
Move constructor.
void release_selection()
Release a previously defined selection.
Definition: hdfdataspace.hh:345
std::string generate_object_name(const Object &object)
Use category and path variable of object to make a string that identifies the object it is applied to...
Definition: hdfutilities.hh:220
virtual ~HDFDataspace()=default
Destroy the HDFDataspace object.
void select_slice(arma::Row< hsize_t > start, arma::Row< hsize_t > end, arma::Row< hsize_t > stride)
Select a slice in the dataspace defined by [start, end, stride] in the manner of numpy....
Definition: hdfdataspace.hh:225
arma::Row< hsize_t > capacity()
Get the capacity of the dataspace in each dimension.
Definition: hdfdataspace.hh:106
HDFDataspace(const HDFDataspace &)=default
Copy constructor.
HDFDataspace(Object &&object, std::enable_if_t< not std::is_same_v< std::decay_t< Object >, HDFDataspace >, int >=0)
Construct a new HDFDataspace object from an HDFDataset or HDFAttribute. This loads the file-dataspace...
Definition: hdfdataspace.hh:419
void open(Object &&object)
Open the dataspace with an HDF5 object, i.e., dataset or attribute.
Definition: hdfdataspace.hh:134
HDFDataspace(std::string name, hsize_t rank, std::vector< hsize_t > extent, std::vector< hsize_t > capacity)
Construct HDFDataspace from the given arguments.
Definition: hdfdataspace.hh:403
void select_all()
Select the entire dataspace as hyperslap to be read/written to.
Definition: hdfdataspace.hh:284
std::pair< arma::Row< hsize_t >, arma::Row< hsize_t > > get_selection_bounds()
Get the selection bounding box, i.e., the start and end vector of the currently selected subset of th...
Definition: hdfdataspace.hh:195
HDFDataspace & operator=(HDFDataspace &&)=default
Move assign dataspace.
HDFDataspace & operator=(const HDFDataspace &)=default
Copy assign dataspace.
HDFDataspace()
Construct HDFDataspace per default. Equivalent to using H5S_ALL when employing the pure C interface.
Definition: hdfdataspace.hh:363
hsize_t rank()
Get thet dataspace's rank, i.e., number of dimensions.
Definition: hdfdataspace.hh:46
void open()
Open the dataspace - set it to be equivalent to any data that later will be used to write or read.
Definition: hdfdataspace.hh:117
std::pair< arma::Row< hsize_t >, arma::Row< hsize_t > > get_properties()
Get the properties object: size and capacity. @notice The dimensions can be infered from the size of ...
Definition: hdfdataspace.hh:67
void swap(HDFDataspace &lhs, HDFDataspace &rhs)
Swap states of lhs and rhs.
Definition: hdfdataspace.hh:454
void resize(arma::Row< hsize_t > new_size)
Resize the dataspace. The new size needs to fit into the dataspaces capacity.
Definition: hdfdataspace.hh:311
This file provides metafunctions for automatically determining the nature of a C/C++ types at compile...
std::string str(T &&t)
Turn any object for which operator<< exists into a string. Mostly useful for logging data via spdlog ...
Definition: ostream.hh:164