]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Constructor for opening a dataset
authorDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Tue, 7 Aug 2018 20:06:37 +0000 (22:06 +0200)
committerDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Wed, 9 Jan 2019 17:22:55 +0000 (18:22 +0100)
include/deal.II/base/hdf5.h
source/base/hdf5.cc

index 96b94caf2e08798b78d32eb57977239a8882ed20..c396d105f35881eba35f0368152e70d3ced00857 100644 (file)
@@ -166,7 +166,9 @@ namespace HDF5
      */
     template <typename T>
     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<hsize_t>   dimensions,
             std::shared_ptr<hid_t> 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<double>.
@@ -301,10 +304,9 @@ namespace HDF5
     void
     write_data_none() const;
 
-    const unsigned int         rank;
-    const std::vector<hsize_t> dimensions;
-
   private:
+    unsigned int           rank;
+    std::vector<hsize_t>   dimensions;
     std::shared_ptr<hid_t> 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<double>.
index 6c0c5e5ece1057ab8fdd067cfbf9cff40353075b..27086bd846b5cae83dce62dbbfe13a9cf06d4488 100644 (file)
@@ -239,7 +239,7 @@ namespace HDF5
 
   template <typename T>
   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<hid_t>(new hid_t, [](auto pointer) {
+      // Relase the HDF5 resource
+      H5Dclose(*pointer);
+      delete pointer;
+    });
+    dataspace      = std::shared_ptr<hid_t>(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<int>(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<hsize_t>   dimensions,
                    std::shared_ptr<hid_t> 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 <typename T>
@@ -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 <typename T>
   DataSet
   Group::create_dataset(const std::string          name,
                         const std::vector<hsize_t> dimensions) const
   {
     std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<T>();
-    return DataSet(
-      name, *hdf5_reference, dimensions, t_type, mpi, DataSet::Mode::create);
+    return DataSet(name, *hdf5_reference, dimensions, t_type, mpi);
   }
 
   template <typename T>

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.