From: Daniel Garcia-Sanchez Date: Sun, 5 Aug 2018 15:24:33 +0000 (+0200) Subject: Use templates for the function members of DataSet X-Git-Tag: v9.1.0-rc1~453^2~73 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db9fc3dd9dbce87e9802fdabed5fa13096ad4a68;p=dealii.git Use templates for the function members of DataSet --- diff --git a/include/deal.II/base/hdf5.h b/include/deal.II/base/hdf5.h index 26d1884403..96b94caf2e 100644 --- a/include/deal.II/base/hdf5.h +++ b/include/deal.II/base/hdf5.h @@ -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("frequency", std::vector{nb_frequency_points}); + * auto frequency_dataset = data.create_dataset("frequency", std::vector{nb_frequency_points}); * - * auto displacement = data.template create_dataset>("displacement", + * auto displacement = data.create_dataset>("displacement", * std::vector{nb_slice_points, nb_frequency_points})); * * // ... do some calculations ... @@ -180,17 +180,17 @@ namespace HDF5 * * @author Daniel Garcia-Sanchez, 2018 */ - template class DataSet : public HDF5Object { friend class Group; protected: - DataSet(const std::string name, - const hid_t & parent_group_id, - std::vector dimensions, - bool mpi, - const Mode mode); + DataSet(const std::string name, + const hid_t & parent_group_id, + std::vector dimensions, + std::shared_ptr t_type, + bool mpi, + const Mode mode); public: ~DataSet(); @@ -205,6 +205,7 @@ namespace HDF5 * Transfer: Datatype Conversion and Selection" section in the HDF5 * User's Guide. */ + template void write_data(const std::vector &data) const; @@ -218,6 +219,7 @@ namespace HDF5 * Transfer: Datatype Conversion and Selection" section in the HDF5 * User's Guide. */ + template void write_data(const FullMatrix &data) const; @@ -243,6 +245,7 @@ namespace HDF5 * Transfer: Datatype Conversion and Selection" section in the HDF5 * User's Guide. */ + template void write_data_selection(const std::vector & data, const std::vector coordinates) const; @@ -259,6 +262,7 @@ namespace HDF5 * Transfer: Datatype Conversion and Selection" section in the HDF5 * User's Guide. */ + template void write_data_hyperslab(const std::vector & data, const std::vector offset, @@ -276,18 +280,32 @@ namespace HDF5 * Transfer: Datatype Conversion and Selection" section in the HDF5 * User's Guide. */ + template void write_data_hyperslab(const FullMatrix & data, const std::vector offset, const std::vector 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. + * + * Datatype conversion takes place at the time of a read or write and is + * automatic. See the "Data + * Transfer: Datatype Conversion and Selection" section in the HDF5 + * User's Guide. + */ + template void - write_data_none() const; + write_data_none() const; + const unsigned int rank; const std::vector dimensions; private: std::shared_ptr dataspace; - std::shared_ptr 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. * - * 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 "Data @@ -330,7 +344,7 @@ namespace HDF5 * User's Guide. */ template - DataSet + DataSet create_dataset(const std::string name, const std::vector dimensions) const; diff --git a/source/base/hdf5.cc b/source/base/hdf5.cc index dfe415927c..dd38f8b34a 100644 --- a/source/base/hdf5.cc +++ b/source/base/hdf5.cc @@ -266,16 +266,16 @@ namespace HDF5 H5Aclose(attr); } - template - DataSet::DataSet(const std::string name, - const hid_t & parent_group_id, - std::vector dimensions, - const bool mpi, - const Mode mode) + + 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) : HDF5Object(name, mpi) , rank(dimensions.size()) , dimensions(dimensions) - , t_type(internal::get_hdf5_datatype()) { hdf5_reference = std::shared_ptr(new hid_t, [](auto pointer) { // Relase the HDF5 resource @@ -318,8 +318,8 @@ namespace HDF5 } } - template - DataSet::~DataSet() + + DataSet::~DataSet() { // Close first the dataset hdf5_reference.reset(); @@ -329,10 +329,11 @@ namespace HDF5 template void - DataSet::write_data(const std::vector &data) const + DataSet::write_data(const std::vector &data) const { AssertDimension(total_size, data.size()); - hid_t plist; + std::shared_ptr t_type = internal::get_hdf5_datatype(); + hid_t plist; if (mpi) { @@ -354,10 +355,11 @@ namespace HDF5 template void - DataSet::write_data(const FullMatrix &data) const + DataSet::write_data(const FullMatrix &data) const { AssertDimension(total_size, data.m() * data.n()); - hid_t plist; + std::shared_ptr t_type = internal::get_hdf5_datatype(); + hid_t plist; if (mpi) { @@ -381,13 +383,16 @@ namespace HDF5 template void - DataSet::write_data_selection(const std::vector & data, - const std::vector coordinates) const + DataSet::write_data_selection(const std::vector & data, + const std::vector coordinates) const { AssertDimension(coordinates.size(), data.size() * rank); - hid_t memory_dataspace; - hid_t plist; - std::vector data_dimensions = {data.size()}; + std::shared_ptr t_type = internal::get_hdf5_datatype(); + std::vector 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 void - DataSet::write_data_hyperslab(const std::vector & data, - const std::vector offset, - const std::vector count) const + DataSet::write_data_hyperslab(const std::vector & data, + const std::vector offset, + const std::vector count) const { AssertDimension(std::accumulate(count.begin(), count.end(), 1, std::multiplies()), data.size()); - hid_t memory_dataspace; - hid_t plist; - std::vector data_dimensions = {data.size()}; + std::shared_ptr t_type = internal::get_hdf5_datatype(); + std::vector 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 void - DataSet::write_data_hyperslab(const FullMatrix & data, - const std::vector offset, - const std::vector count) const + DataSet::write_data_hyperslab(const FullMatrix & data, + const std::vector offset, + const std::vector count) const { AssertDimension(std::accumulate(count.begin(), count.end(), 1, std::multiplies()), data.m() * data.n()); - hid_t memory_dataspace; - hid_t plist; - std::vector data_dimensions = {data.m(), data.n()}; + std::shared_ptr t_type = internal::get_hdf5_datatype(); + std::vector 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 void - DataSet::write_data_none() const + DataSet::write_data_none() const { - hid_t memory_dataspace; - hid_t plist; - std::vector data_dimensions = {0}; + std::shared_ptr t_type = internal::get_hdf5_datatype(); + std::vector 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 - DataSet + DataSet Group::create_dataset(const std::string name, const std::vector dimensions) const { - return DataSet( - name, *hdf5_reference, dimensions, mpi, DataSet::Mode::create); + std::shared_ptr t_type = internal::get_hdf5_datatype(); + return DataSet( + name, *hdf5_reference, dimensions, t_type, mpi, DataSet::Mode::create); } template @@ -674,17 +687,6 @@ namespace HDF5 {} - // explicit instantiations of classes - template class DataSet; - template class DataSet; - template class DataSet; - template class DataSet>; - template class DataSet>; - template class DataSet>; - template class DataSet; - template class DataSet; - - // explicit instantiations of functions template float HDF5Object::attr(const std::string attr_name) const; @@ -730,33 +732,82 @@ namespace HDF5 HDF5Object::write_attr(const std::string attr_name, unsigned int value) const; + template void + DataSet::write_data_selection( + const std::vector & data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection( + const std::vector &data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection( + const std::vector &data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection>( + const std::vector> &data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection>( + const std::vector> &data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection>( + const std::vector> &data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection( + const std::vector & data, + const std::vector coordinates) const; + template void + DataSet::write_data_selection( + const std::vector &data, + const std::vector coordinates) const; + + template void + DataSet::write_data_none() const; + template void + DataSet::write_data_none() const; + template void + DataSet::write_data_none() const; + template void + DataSet::write_data_none>() const; + template void + DataSet::write_data_none>() const; + template void + DataSet::write_data_none>() const; + template void + DataSet::write_data_none() const; + template void + DataSet::write_data_none() const; - template DataSet + template DataSet Group::create_dataset(const std::string name, const std::vector dimensions) const; - template DataSet + template DataSet Group::create_dataset(const std::string name, const std::vector dimensions) const; - template DataSet + template DataSet Group::create_dataset( const std::string name, const std::vector dimensions) const; - template DataSet> + template DataSet Group::create_dataset>( const std::string name, const std::vector dimensions) const; - template DataSet> + template DataSet Group::create_dataset>( const std::string name, const std::vector dimensions) const; - template DataSet> + template DataSet Group::create_dataset>( const std::string name, const std::vector dimensions) const; - template DataSet + template DataSet Group::create_dataset(const std::string name, const std::vector dimensions) const; - template DataSet + template DataSet Group::create_dataset( const std::string name, const std::vector dimensions) const;