]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use templates for the function members of DataSet
authorDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Sun, 5 Aug 2018 15:24:33 +0000 (17:24 +0200)
committerDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Wed, 9 Jan 2019 17:22:43 +0000 (18:22 +0100)
include/deal.II/base/hdf5.h
source/base/hdf5.cc

index 26d18844035bb2495f595eaeb95cba999e8905f0..96b94caf2e08798b78d32eb57977239a8882ed20 100644 (file)
@@ -62,9 +62,9 @@ DEAL_II_NAMESPACE_OPEN
  * // Create some distributed datasets.
  * // Note that the keyword template is used between data and create because
  * // the function create_dataset returns a dependent template.
- * auto frequency_dataset = data.template create_dataset<double>("frequency", std::vector<hsize_t>{nb_frequency_points});
+ * auto frequency_dataset = data.create_dataset<double>("frequency", std::vector<hsize_t>{nb_frequency_points});
  *
- * auto displacement = data.template create_dataset<std::complex<double>>("displacement",
+ * auto displacement = data.create_dataset<std::complex<double>>("displacement",
  *                                                           std::vector<hsize_t>{nb_slice_points, nb_frequency_points}));
  *
  * // ... do some calculations ...
@@ -180,17 +180,17 @@ namespace HDF5
    *
    * @author Daniel Garcia-Sanchez, 2018
    */
-  template <typename T>
   class DataSet : public HDF5Object
   {
     friend class Group;
 
   protected:
-    DataSet(const std::string    name,
-            const hid_t &        parent_group_id,
-            std::vector<hsize_t> dimensions,
-            bool                 mpi,
-            const Mode           mode);
+    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);
 
   public:
     ~DataSet();
@@ -205,6 +205,7 @@ namespace HDF5
      * Transfer: Datatype Conversion and Selection"</a>  section in the HDF5
      * User's Guide.
      */
+    template <typename T>
     void
     write_data(const std::vector<T> &data) const;
 
@@ -218,6 +219,7 @@ namespace HDF5
      * Transfer: Datatype Conversion and Selection"</a>  section in the HDF5
      * User's Guide.
      */
+    template <typename T>
     void
     write_data(const FullMatrix<T> &data) const;
 
@@ -243,6 +245,7 @@ namespace HDF5
      * Transfer: Datatype Conversion and Selection"</a>  section in the HDF5
      * User's Guide.
      */
+    template <typename T>
     void
     write_data_selection(const std::vector<T> &     data,
                          const std::vector<hsize_t> coordinates) const;
@@ -259,6 +262,7 @@ namespace HDF5
      * Transfer: Datatype Conversion and Selection"</a>  section in the HDF5
      * User's Guide.
      */
+    template <typename T>
     void
     write_data_hyperslab(const std::vector<T> &     data,
                          const std::vector<hsize_t> offset,
@@ -276,18 +280,32 @@ namespace HDF5
      * Transfer: Datatype Conversion and Selection"</a>  section in the HDF5
      * User's Guide.
      */
+    template <typename T>
     void
     write_data_hyperslab(const FullMatrix<T> &      data,
                          const std::vector<hsize_t> offset,
                          const std::vector<hsize_t> count) const;
+
+    /**
+     * This function does not write any data, but can contribute to a collective
+     * write call. T can be double, int, unsigned int, bool or
+     * std::complex<double>.
+     *
+     * Datatype conversion takes place at the time of a read or write and is
+     * automatic. See the <a
+     * href="https://support.hdfgroup.org/HDF5/doc/UG/HDF5_Users_Guide-Responsive%20HTML5/index.html#t=HDF5_Users_Guide%2FDatatypes%2FHDF5_Datatypes.htm%23TOC_6_10_Data_Transferbc-26&rhtocid=6.5_2">"Data
+     * Transfer: Datatype Conversion and Selection"</a>  section in the HDF5
+     * User's Guide.
+     */
+    template <typename T>
     void
-                               write_data_none() const;
+    write_data_none() const;
+
     const unsigned int         rank;
     const std::vector<hsize_t> dimensions;
 
   private:
     std::shared_ptr<hid_t> dataspace;
-    std::shared_ptr<hid_t> t_type;
     unsigned int           total_size;
   };
 
@@ -319,10 +337,6 @@ namespace HDF5
      * Creates a dataset. T can be double, int, unsigned int, bool or
      * std::complex<double>.
      *
-     * The keyword template has to be used between create_dataset and the name
-     * of the object because the function create_dataset returns a dependent
-     * template. See the example in HDF5.
-     *
      * Datatype conversion takes place at the time of a read or write and is
      * automatic. See the <a
      * href="https://support.hdfgroup.org/HDF5/doc/UG/HDF5_Users_Guide-Responsive%20HTML5/index.html#t=HDF5_Users_Guide%2FDatatypes%2FHDF5_Datatypes.htm%23TOC_6_10_Data_Transferbc-26&rhtocid=6.5_2">"Data
@@ -330,7 +344,7 @@ namespace HDF5
      * User's Guide.
      */
     template <typename T>
-    DataSet<T>
+    DataSet
     create_dataset(const std::string          name,
                    const std::vector<hsize_t> dimensions) const;
 
index dfe415927c1cc3ed1ef6e5382b3112074db52c9e..dd38f8b34a4c6ff6d135850ef399e0bc7198cbac 100644 (file)
@@ -266,16 +266,16 @@ namespace HDF5
     H5Aclose(attr);
   }
 
-  template <typename T>
-  DataSet<T>::DataSet(const std::string    name,
-                      const hid_t &        parent_group_id,
-                      std::vector<hsize_t> dimensions,
-                      const bool           mpi,
-                      const Mode           mode)
+
+  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)
     : HDF5Object(name, mpi)
     , rank(dimensions.size())
     , dimensions(dimensions)
-    , t_type(internal::get_hdf5_datatype<T>())
   {
     hdf5_reference = std::shared_ptr<hid_t>(new hid_t, [](auto pointer) {
       // Relase the HDF5 resource
@@ -318,8 +318,8 @@ namespace HDF5
       }
   }
 
-  template <typename T>
-  DataSet<T>::~DataSet()
+
+  DataSet::~DataSet()
   {
     // Close first the dataset
     hdf5_reference.reset();
@@ -329,10 +329,11 @@ namespace HDF5
 
   template <typename T>
   void
-  DataSet<T>::write_data(const std::vector<T> &data) const
+  DataSet::write_data(const std::vector<T> &data) const
   {
     AssertDimension(total_size, data.size());
-    hid_t plist;
+    std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<T>();
+    hid_t                  plist;
 
     if (mpi)
       {
@@ -354,10 +355,11 @@ namespace HDF5
 
   template <typename T>
   void
-  DataSet<T>::write_data(const FullMatrix<T> &data) const
+  DataSet::write_data(const FullMatrix<T> &data) const
   {
     AssertDimension(total_size, data.m() * data.n());
-    hid_t plist;
+    std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<T>();
+    hid_t                  plist;
 
     if (mpi)
       {
@@ -381,13 +383,16 @@ namespace HDF5
 
   template <typename T>
   void
-  DataSet<T>::write_data_selection(const std::vector<T> &     data,
-                                   const std::vector<hsize_t> coordinates) const
+  DataSet::write_data_selection(const std::vector<T> &     data,
+                                const std::vector<hsize_t> coordinates) const
   {
     AssertDimension(coordinates.size(), data.size() * rank);
-    hid_t                memory_dataspace;
-    hid_t                plist;
-    std::vector<hsize_t> data_dimensions = {data.size()};
+    std::shared_ptr<hid_t> t_type          = internal::get_hdf5_datatype<T>();
+    std::vector<hsize_t>   data_dimensions = {data.size()};
+
+    hid_t memory_dataspace;
+    hid_t plist;
+
 
     memory_dataspace = H5Screate_simple(1, data_dimensions.data(), NULL);
     H5Sselect_elements(*dataspace,
@@ -421,18 +426,21 @@ namespace HDF5
 
   template <typename T>
   void
-  DataSet<T>::write_data_hyperslab(const std::vector<T> &     data,
-                                   const std::vector<hsize_t> offset,
-                                   const std::vector<hsize_t> count) const
+  DataSet::write_data_hyperslab(const std::vector<T> &     data,
+                                const std::vector<hsize_t> offset,
+                                const std::vector<hsize_t> count) const
   {
     AssertDimension(std::accumulate(count.begin(),
                                     count.end(),
                                     1,
                                     std::multiplies<unsigned int>()),
                     data.size());
-    hid_t                memory_dataspace;
-    hid_t                plist;
-    std::vector<hsize_t> data_dimensions = {data.size()};
+    std::shared_ptr<hid_t> t_type          = internal::get_hdf5_datatype<T>();
+    std::vector<hsize_t>   data_dimensions = {data.size()};
+
+    hid_t memory_dataspace;
+    hid_t plist;
+
 
     memory_dataspace = H5Screate_simple(1, data_dimensions.data(), NULL);
     H5Sselect_hyperslab(
@@ -463,18 +471,20 @@ namespace HDF5
 
   template <typename T>
   void
-  DataSet<T>::write_data_hyperslab(const FullMatrix<T> &      data,
-                                   const std::vector<hsize_t> offset,
-                                   const std::vector<hsize_t> count) const
+  DataSet::write_data_hyperslab(const FullMatrix<T> &      data,
+                                const std::vector<hsize_t> offset,
+                                const std::vector<hsize_t> count) const
   {
     AssertDimension(std::accumulate(count.begin(),
                                     count.end(),
                                     1,
                                     std::multiplies<unsigned int>()),
                     data.m() * data.n());
-    hid_t                memory_dataspace;
-    hid_t                plist;
-    std::vector<hsize_t> data_dimensions = {data.m(), data.n()};
+    std::shared_ptr<hid_t> t_type          = internal::get_hdf5_datatype<T>();
+    std::vector<hsize_t>   data_dimensions = {data.m(), data.n()};
+
+    hid_t memory_dataspace;
+    hid_t plist;
 
     memory_dataspace = H5Screate_simple(2, data_dimensions.data(), NULL);
     H5Sselect_hyperslab(
@@ -505,11 +515,13 @@ namespace HDF5
 
   template <typename T>
   void
-  DataSet<T>::write_data_none() const
+  DataSet::write_data_none() const
   {
-    hid_t                memory_dataspace;
-    hid_t                plist;
-    std::vector<hsize_t> data_dimensions = {0};
+    std::shared_ptr<hid_t> t_type          = internal::get_hdf5_datatype<T>();
+    std::vector<hsize_t>   data_dimensions = {0};
+
+    hid_t memory_dataspace;
+    hid_t plist;
 
     memory_dataspace = H5Screate_simple(1, data_dimensions.data(), NULL);
     H5Sselect_none(*dataspace);
@@ -584,12 +596,13 @@ namespace HDF5
   }
 
   template <typename T>
-  DataSet<T>
+  DataSet
   Group::create_dataset(const std::string          name,
                         const std::vector<hsize_t> dimensions) const
   {
-    return DataSet<T>(
-      name, *hdf5_reference, dimensions, mpi, DataSet<T>::Mode::create);
+    std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<T>();
+    return DataSet(
+      name, *hdf5_reference, dimensions, t_type, mpi, DataSet::Mode::create);
   }
 
   template <typename T>
@@ -674,17 +687,6 @@ namespace HDF5
   {}
 
 
-  // explicit instantiations of classes
-  template class DataSet<float>;
-  template class DataSet<double>;
-  template class DataSet<long double>;
-  template class DataSet<std::complex<float>>;
-  template class DataSet<std::complex<double>>;
-  template class DataSet<std::complex<long double>>;
-  template class DataSet<int>;
-  template class DataSet<unsigned int>;
-
-
   // explicit instantiations of functions
   template float
   HDF5Object::attr<float>(const std::string attr_name) const;
@@ -730,33 +732,82 @@ namespace HDF5
   HDF5Object::write_attr<unsigned int>(const std::string attr_name,
                                        unsigned int      value) const;
 
+  template void
+  DataSet::write_data_selection<float>(
+    const std::vector<float> & data,
+    const std::vector<hsize_t> coordinates) const;
+  template void
+  DataSet::write_data_selection<double>(
+    const std::vector<double> &data,
+    const std::vector<hsize_t> coordinates) const;
+  template void
+  DataSet::write_data_selection<long double>(
+    const std::vector<long double> &data,
+    const std::vector<hsize_t>      coordinates) const;
+  template void
+  DataSet::write_data_selection<std::complex<float>>(
+    const std::vector<std::complex<float>> &data,
+    const std::vector<hsize_t>              coordinates) const;
+  template void
+  DataSet::write_data_selection<std::complex<double>>(
+    const std::vector<std::complex<double>> &data,
+    const std::vector<hsize_t>               coordinates) const;
+  template void
+  DataSet::write_data_selection<std::complex<long double>>(
+    const std::vector<std::complex<long double>> &data,
+    const std::vector<hsize_t>                    coordinates) const;
+  template void
+  DataSet::write_data_selection<int>(
+    const std::vector<int> &   data,
+    const std::vector<hsize_t> coordinates) const;
+  template void
+  DataSet::write_data_selection<unsigned int>(
+    const std::vector<unsigned int> &data,
+    const std::vector<hsize_t>       coordinates) const;
+
+  template void
+  DataSet::write_data_none<float>() const;
+  template void
+  DataSet::write_data_none<double>() const;
+  template void
+  DataSet::write_data_none<long double>() const;
+  template void
+  DataSet::write_data_none<std::complex<float>>() const;
+  template void
+  DataSet::write_data_none<std::complex<double>>() const;
+  template void
+  DataSet::write_data_none<std::complex<long double>>() const;
+  template void
+  DataSet::write_data_none<int>() const;
+  template void
+  DataSet::write_data_none<unsigned int>() const;
 
-  template DataSet<float>
+  template DataSet
   Group::create_dataset<float>(const std::string          name,
                                const std::vector<hsize_t> dimensions) const;
-  template DataSet<double>
+  template DataSet
   Group::create_dataset<double>(const std::string          name,
                                 const std::vector<hsize_t> dimensions) const;
-  template DataSet<long double>
+  template DataSet
   Group::create_dataset<long double>(
     const std::string          name,
     const std::vector<hsize_t> dimensions) const;
-  template DataSet<std::complex<float>>
+  template DataSet
   Group::create_dataset<std::complex<float>>(
     const std::string          name,
     const std::vector<hsize_t> dimensions) const;
-  template DataSet<std::complex<double>>
+  template DataSet
   Group::create_dataset<std::complex<double>>(
     const std::string          name,
     const std::vector<hsize_t> dimensions) const;
-  template DataSet<std::complex<long double>>
+  template DataSet
   Group::create_dataset<std::complex<long double>>(
     const std::string          name,
     const std::vector<hsize_t> dimensions) const;
-  template DataSet<int>
+  template DataSet
   Group::create_dataset<int>(const std::string          name,
                              const std::vector<hsize_t> dimensions) const;
-  template DataSet<unsigned int>
+  template DataSet
   Group::create_dataset<unsigned int>(
     const std::string          name,
     const std::vector<hsize_t> dimensions) const;

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.