From 010ddd86d2f6ea4dd25ad1a97d5cd2f34b6f9ae0 Mon Sep 17 00:00:00 2001 From: Daniel Garcia-Sanchez Date: Tue, 7 Aug 2018 22:06:37 +0200 Subject: [PATCH] Constructor for opening a dataset --- include/deal.II/base/hdf5.h | 27 +++++++---- source/base/hdf5.cc | 92 ++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 45 deletions(-) diff --git a/include/deal.II/base/hdf5.h b/include/deal.II/base/hdf5.h index 96b94caf2e..c396d105f3 100644 --- a/include/deal.II/base/hdf5.h +++ b/include/deal.II/base/hdf5.h @@ -166,7 +166,9 @@ namespace HDF5 */ template void - write_attr(const std::string attr_name, T value) const; + write_attr(const std::string attr_name, const T value) const; + + const std::string name; protected: @@ -185,16 +187,17 @@ namespace HDF5 friend class Group; protected: + // Open dataset + DataSet(const std::string name, const hid_t &parent_group_id, bool mpi); + + // Create dataset DataSet(const std::string name, const hid_t & parent_group_id, std::vector dimensions, std::shared_ptr t_type, - bool mpi, - const Mode mode); + bool mpi); public: - ~DataSet(); - /** * Writes data in the dataset. T can be double, int, unsigned int, bool * or std::complex. @@ -301,10 +304,9 @@ namespace HDF5 void write_data_none() const; - const unsigned int rank; - const std::vector dimensions; - private: + unsigned int rank; + std::vector dimensions; std::shared_ptr dataspace; unsigned int total_size; }; @@ -324,6 +326,9 @@ namespace HDF5 Group(const std::string name, const bool mpi); public: + /** + * Opens a group. + */ Group group(const std::string name); @@ -333,6 +338,12 @@ namespace HDF5 Group create_group(const std::string name); + /** + * Opens a dataset. + */ + DataSet + dataset(const std::string name); + /** * Creates a dataset. T can be double, int, unsigned int, bool or * std::complex. diff --git a/source/base/hdf5.cc b/source/base/hdf5.cc index 6c0c5e5ece..27086bd846 100644 --- a/source/base/hdf5.cc +++ b/source/base/hdf5.cc @@ -239,7 +239,7 @@ namespace HDF5 template void - HDF5Object::write_attr(const std::string attr_name, T value) const + HDF5Object::write_attr(const std::string attr_name, const T value) const { hid_t attr; hid_t aid; @@ -268,7 +268,8 @@ namespace HDF5 template <> void - HDF5Object::write_attr(const std::string attr_name, std::string value) const + HDF5Object::write_attr(const std::string attr_name, + const std::string value) const { // Writes a UTF8 variable string // @@ -317,14 +318,48 @@ namespace HDF5 H5Aclose(attr); } + DataSet::DataSet(const std::string name, + const hid_t & parent_group_id, + const bool mpi) + : HDF5Object(name, mpi) + { + hdf5_reference = std::shared_ptr(new hid_t, [](auto pointer) { + // Relase the HDF5 resource + H5Dclose(*pointer); + delete pointer; + }); + dataspace = std::shared_ptr(new hid_t, [](auto pointer) { + // Relase the HDF5 resource + H5Sclose(*pointer); + delete pointer; + }); + *hdf5_reference = H5Dopen2(parent_group_id, name.data(), H5P_DEFAULT); + *dataspace = H5Dget_space(*hdf5_reference); + auto rank_ret = H5Sget_simple_extent_ndims(*dataspace); + // rank_ret can take a negative value if the function fails. rank is + // unsigned int, that is way rank_ret is used to store the return + // value of H5Sget_simple_extent_ndims + Assert(rank_ret >= 0, ExcInternalError()); + rank = rank_ret; + hsize_t *dims = (hsize_t *)malloc(rank * sizeof(hsize_t)); + rank_ret = H5Sget_simple_extent_dims(*dataspace, dims, NULL); + Assert(rank_ret == static_cast(rank), ExcInternalError()); + dimensions.assign(dims, dims + rank); + free(dims); + + total_size = 1; + for (auto &&dimension : dimensions) + { + total_size *= dimension; + } + } DataSet::DataSet(const std::string name, const hid_t & parent_group_id, std::vector dimensions, std::shared_ptr t_type, - const bool mpi, - const Mode mode) + const bool mpi) : HDF5Object(name, mpi) , rank(dimensions.size()) , dimensions(dimensions) @@ -340,43 +375,21 @@ namespace HDF5 delete pointer; }); + *dataspace = H5Screate_simple(rank, dimensions.data(), NULL); + + *hdf5_reference = H5Dcreate2(parent_group_id, + name.data(), + *t_type, + *dataspace, + H5P_DEFAULT, + H5P_DEFAULT, + H5P_DEFAULT); total_size = 1; for (auto &&dimension : dimensions) { total_size *= dimension; } - - switch (mode) - { - case (Mode::create): - *dataspace = H5Screate_simple(rank, dimensions.data(), NULL); - - *hdf5_reference = H5Dcreate2(parent_group_id, - name.data(), - *t_type, - *dataspace, - H5P_DEFAULT, - H5P_DEFAULT, - H5P_DEFAULT); - break; - case (Mode::open): - // Write the code for open - *hdf5_reference = H5Gopen2(parent_group_id, name.data(), H5P_DEFAULT); - break; - default: - Assert(false, ExcInternalError()); - break; - } - } - - - DataSet::~DataSet() - { - // Close first the dataset - hdf5_reference.reset(); - // Then close the dataspace - dataspace.reset(); } template @@ -647,14 +660,19 @@ namespace HDF5 return Group(name, *this, mpi, Mode::create); } + DataSet + Group::dataset(const std::string name) + { + return DataSet(name, *hdf5_reference, mpi); + } + template DataSet Group::create_dataset(const std::string name, const std::vector dimensions) const { std::shared_ptr t_type = internal::get_hdf5_datatype(); - return DataSet( - name, *hdf5_reference, dimensions, t_type, mpi, DataSet::Mode::create); + return DataSet(name, *hdf5_reference, dimensions, t_type, mpi); } template -- 2.39.5