25#ifndef __HDF5_TREE_HPP__
26#define __HDF5_TREE_HPP__
32#include <initializer_list>
38enum class hdf5_access_t
51 operator hid_t()
const noexcept
53 return H5T_NATIVE_FLOAT;
60 operator hid_t()
const noexcept
62 return H5T_NATIVE_DOUBLE;
69 operator hid_t()
const noexcept
71 return H5T_NATIVE_INT;
78 operator hid_t()
const noexcept
80 return H5T_NATIVE_UCHAR;
111 if ((
id_ = H5Gopen(file_id, path.c_str(), H5P_DEFAULT)) < 0) {
113 s <<
"error in H5Gopen()" << std::endl <<
"path : " << path;
121 if ((
id_ = H5Gcreate(g.
id(), name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
123 s <<
"error in H5Gcreate()" << std::endl <<
"name : " << name;
131 if (H5Gclose(
id_) < 0) {
132 RTE_THROW(
"error in H5Gclose()");
137 inline hid_t
id()
const
154 std::vector<hsize_t> current_dims(dims.size());
155 for (
int i = 0; i < (int)dims.size(); i++) {
156 current_dims[dims.size() - i - 1] = dims[i];
159 if ((
id_ = H5Screate_simple((
int)dims.size(), ¤t_dims[0], NULL)) < 0) {
160 RTE_THROW(
"error in H5Screate_simple()");
167 if (H5Sclose(
id_) < 0) {
168 RTE_THROW(
"error in H5Sclose()");
173 inline hid_t
id()
const
190 if ((
id_ = H5Dopen(group_id, name.c_str(), H5P_DEFAULT)) < 0) {
191 RTE_THROW(
"error in H5Dopen()");
198 if ((
id_ = H5Dcreate(group.
id(), name.c_str(), type_id, dataspace.
id(), H5P_DEFAULT, H5P_DEFAULT,
200 RTE_THROW(
"error in H5Dcreate()");
207 if (H5Dclose(
id_) < 0) {
208 RTE_THROW(
"error in H5Dclose()");
213 inline hid_t
id()
const
232 if ((
id_ = H5Aopen(attribute_id, name.c_str(), H5P_DEFAULT)) < 0)
233 RTE_THROW(
"error in H5Aopen()");
248 if ((dataspace_id = H5Screate(H5S_SCALAR)) < 0) {
249 RTE_THROW(
"error in H5Screate()");
252 if ((
id_ = H5Acreate(parent_id, name.c_str(), type_id, dataspace_id, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
253 RTE_THROW(
"error in H5Acreate()");
260 if (H5Aclose(
id_) < 0) {
261 RTE_THROW(
"error in H5Aclose()");
266 inline hid_t
id()
const
281 hid_t
string_type(
const std::string& data, hid_t base_string_type)
const
285 if (H5Tset_size(
string_type, std::size(data)) < 0) {
286 RTE_THROW(
"error in H5Tset_size()");
292 template <
typename T>
295 hsize_t data_size = std::size(data);
300 template <
typename T>
301 void write(
const std::string& name, T
const* data, std::vector<int>
const& dims)
314 RTE_THROW(
"error in H5Dwrite()");
323 template <
typename T>
324 void write_attribute(
const std::string& name,
const T*
const data, hid_t type_id)
const
330 if (H5Awrite(attribute.
id(), type_id, data) < 0) {
331 RTE_THROW(
"error in H5Awrite()");
341 template <
typename T>
343 const std::string& dataset_name)
const
351 if (H5Awrite(attribute.
id(), type_id, data) < 0) {
352 RTE_THROW(
"error in H5Awrite()");
357 template <
typename T>
358 void read(
const std::string& name, T* data, std::vector<int>
const& dims)
367 RTE_THROW(
"error in H5Dread()");
377 HDF5_tree(
const std::string& file_name__, hdf5_access_t access__)
381 RTE_THROW(
"error in H5open()");
385 H5Eset_auto(H5E_DEFAULT, NULL, NULL);
389 case hdf5_access_t::truncate: {
392 RTE_THROW(
"error in H5Fcreate()");
396 case hdf5_access_t::read_write: {
400 case hdf5_access_t::read_only: {
406 RTE_THROW(
"H5Fopen() failed");
417 RTE_THROW(
"error in H5Fclose()");
438 return (*
this)[name];
441 template <
typename T,
int N>
444 std::vector<int> dims(N + 1);
446 for (
int i = 0; i < N; i++) {
447 dims[i + 1] = (int)data.size(i);
449 write(name, (T*)data.at(sddk::memory_t::host), dims);
453 template <
typename T,
int N>
456 std::vector<int> dims(N);
457 for (
int i = 0; i < N; i++) {
458 dims[i] =
static_cast<int>(data__.
size(i));
460 write(name__, data__.at(sddk::memory_t::host), dims);
464 template <
typename T,
int N>
467 std::string name = std::to_string(name_id);
472 template <
typename T>
473 void write(
const std::string& name, T
const* data,
int size)
475 std::vector<int> dims(1);
477 write(name, data, dims);
481 template <
typename T>
482 void write(
const std::string& name, T data)
484 std::vector<int> dims(1);
486 write(name, &data, dims);
490 template <
typename T>
491 void write(
int name_id, std::vector<T>
const& vec)
493 std::string name = std::to_string(name_id);
494 write(name, &vec[0], (
int)vec.size());
498 template <
typename T>
499 void write(
const std::string& name, std::vector<T>
const& vec)
501 write(name, &vec[0], (
int)vec.size());
505 template <
typename T>
512 template <
typename T>
513 void write_attribute(
const std::string& name,
const T& data,
const std::string& dataset_name)
const
520 hid_t base_string_type = H5T_FORTRAN_S1)
const
526 void write_attribute(
const std::string& name,
const std::string& data,
const std::string& dataset_name,
527 hid_t base_string_type = H5T_FORTRAN_S1)
const
533 template <std::
size_t N>
534 void write_attribute(
const std::string& name,
const char (&data)[N], hid_t base_string_type = H5T_FORTRAN_S1)
const
540 template <std::
size_t N>
541 void write_attribute(
const std::string& name,
const char (&data)[N],
const std::string& dataset_name,
542 hid_t base_string_type = H5T_FORTRAN_S1)
const
544 write_attribute(name, std::string(data), dataset_name, base_string_type);
548 template <
typename T>
555 template <
typename T>
556 void write_attribute(
const std::string& name,
const std::vector<T>& data,
const std::string& dataset_name)
const
562 template <
typename T>
563 void write_attribute(
const std::string& name,
const std::initializer_list<T>& data)
const
569 template <
typename T>
571 const std::string& dataset_name)
const
579 std::vector<int> dims(N + 1);
581 for (
int i = 0; i < N; i++) {
582 dims[i + 1] = (int)data.size(i);
584 read(name, (
double*)data.at(sddk::memory_t::host), dims);
587 template <
typename T,
int N>
588 void read(
const std::string& name, sddk::mdarray<T, N>& data)
590 std::vector<int> dims(N);
591 for (
int i = 0; i < N; i++) {
592 dims[i] = (int)data.size(i);
594 read(name, data.at(sddk::memory_t::host), dims);
597 template <
typename T,
int N>
598 void read(
int name_id, sddk::mdarray<T, N>& data)
600 std::string name = std::to_string(name_id);
605 template <
typename T>
606 void read(std::string
const& name, T* data,
int size)
608 std::vector<int> dims(1);
610 read(name, data, dims);
613 template <
typename T>
614 void read(
int name_id, std::vector<T>& vec)
616 read(std::to_string(name_id), &vec[0], (
int)vec.size());
619 template <
typename T>
620 void read(std::string
const& name, std::vector<T>& vec)
622 read(name, &vec[0], (
int)vec.size());
625 HDF5_tree operator[](std::string
const& path__)
627 auto new_path =
path_ + path__ +
"/";
633 auto new_path =
path_ + std::to_string(idx__) +
"/";
HDF5_attribute(hid_t parent_id, const std::string &name, hid_t type_id)
hid_t id_
HDF5 id of the current object.
hid_t id() const
Return HDF5 id of the current object.
HDF5_attribute(hid_t attribute_id, const std::string &name)
Constructor which opens the existing attribute object.
~HDF5_attribute()
Destructor.
Auxiliary class to handle HDF5 Dataset object.
~HDF5_dataset()
Destructor.
HDF5_dataset(HDF5_group &group, HDF5_dataspace &dataspace, const std::string &name, hid_t type_id)
Constructor which creates the new dataset object.
HDF5_dataset(hid_t group_id, const std::string &name)
Constructor which openes the existing dataset object.
hid_t id_
HDF5 id of the current object.
hid_t id() const
Return HDF5 id of the current object.
Auxiliary class to handle HDF5 Dataspace object.
HDF5_dataspace(const std::vector< int > dims)
Constructor which creates the new dataspace object.
hid_t id_
HDF5 id of the current object.
hid_t id() const
Return HDF5 id of the current object.
~HDF5_dataspace()
Destructor.
Auxiliary class to handle HDF5 Group object.
hid_t id() const
Return HDF5 id of the current object.
HDF5_group(hid_t file_id, const std::string &path)
Constructor which openes the existing group.
hid_t id_
HDF5 id of the current object.
HDF5_group(HDF5_group const &g, const std::string &name)
Constructor which creates the new group.
Interface to the HDF5 library.
void write(const std::string &name, T data)
Write a scalar.
hid_t file_id_
HDF5 file handler.
void write_attribute(const std::string &name, const std::string &data, const std::string &dataset_name, hid_t base_string_type=H5T_FORTRAN_S1) const
Write string attribute.
void write_attribute(const std::string &name, const char(&data)[N], hid_t base_string_type=H5T_FORTRAN_S1) const
Write string attribute.
void write_attribute(const std::string &name, const char(&data)[N], const std::string &dataset_name, hid_t base_string_type=H5T_FORTRAN_S1) const
Write string attribute.
std::string file_name_
HDF5 file name.
HDF5_tree(const std::string &file_name__, hdf5_access_t access__)
Constructor to create the HDF5 tree.
void write_attribute(const std::string &name, const std::vector< T > &data) const
Write array attribute.
HDF5_tree create_node(int idx)
Create node by integer index.
void read(const std::string &name, T *data, std::vector< int > const &dims)
Read a multidimensional array.
void write(int name_id, sddk::mdarray< T, N > const &data)
Write a multidimensional array by integer index.
bool root_node_
True if this is a root node.
hid_t string_type(const std::string &data, hid_t base_string_type) const
Create HDF5 string type from std::string.
void read(std::string const &name, T *data, int size)
Read a vector or a scalar.
hid_t array_type(const std::vector< T > &data) const
Create HDF5 array type from std::vector.
HDF5_tree(hid_t file_id__, const std::string &path__)
Constructor to create branches of the HDF5 tree.
void write(const std::string &name, T const *data, std::vector< int > const &dims)
Write a multidimensional array.
void write_attribute(const std::string &name, const std::initializer_list< T > &data, const std::string &dataset_name) const
Write array attribute.
void write_attribute(const std::string &name, const std::initializer_list< T > &data) const
Write array attribute.
void write(int name_id, std::vector< T > const &vec)
Write a vector by id.
HDF5_tree create_node(std::string const &name)
Create node by name.
void write_attribute(const std::string &name, const T &data, const std::string &dataset_name) const
Write attribute.
void write(std::string const &name__, sddk::mdarray< T, N > const &data__)
Write a multidimensional array by name.
void write_attribute(const std::string &name, const T *const data, hid_t type_id) const
void write(const std::string &name, T const *data, int size)
Write a buffer.
std::string path_
path inside HDF5 file
void write_attribute(const std::string &name, const T &data) const
Write attribute.
void write_attribute(const std::string &name, const std::vector< T > &data, const std::string &dataset_name) const
Write array attribute.
void write_attribute(const std::string &name, const std::string &data, hid_t base_string_type=H5T_FORTRAN_S1) const
Write string attribute.
void write(const std::string &name, std::vector< T > const &vec)
Write a vector by name.
void write_attribute(const std::string &name, const T *const data, hid_t type_id, const std::string &dataset_name) const
Multidimensional array with the column-major (Fortran) order.
size_t size() const
Return total size (number of elements) of the array.
Memory management functions and classes.
Namespace of the SIRIUS library.
Eror and warning handling during run-time execution.