diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/Writer.h b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/Writer.h index 2b4bdd3af56f44d670a82c85ad5606fbddebccfb..86555879520345bd6b5f0d4a267071c0e3668ce1 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/Writer.h +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/Writer.h @@ -1,6 +1,6 @@ // this is -*- C++ -*- /* - Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration */ #ifndef HDF_TUPLE_HH #define HDF_TUPLE_HH @@ -13,9 +13,11 @@ * **/ +#include "WriterConfiguration.h" #include "H5Traits.h" #include "CompressedTypes.h" #include "common.h" +#include "defaults.h" #include "H5Cpp.h" @@ -345,7 +347,10 @@ namespace H5Utils { Writer(H5::Group& group, const std::string& name, const Consumers<I>& consumers, const std::array<hsize_t, N>& extent = internal::uniform<N>(5), - hsize_t batch_size = 2048); + hsize_t batch_size = defaults::batch_size); + Writer(H5::Group& group, + const Consumers<I>& consumers, + const WriterConfiguration<N>& = WriterConfiguration<N>()); Writer(const Writer&) = delete; Writer(Writer&&) = default; Writer& operator=(Writer&) = delete; @@ -358,6 +363,7 @@ namespace H5Utils { using input_type = I; template <typename T> using function_type = typename consumer_type::template function_type<T>; + using configuration_type = WriterConfiguration<N>; private: const internal::DSParameters<I,N> m_par; hsize_t m_offset; @@ -373,30 +379,45 @@ namespace H5Utils { const Consumers<I>& consumers, const std::array<hsize_t,N>& extent, hsize_t batch_size): - m_par(consumers.getConsumers(), extent, batch_size), + Writer<N,I>( + group, consumers, WriterConfiguration<N>{ + name, // name + extent, // extent + batch_size, // batch_size + extent, // chunks + defaults::deflate // deflate + }) + {} + + template <size_t N, typename I> + Writer<N, I>::Writer(H5::Group& group, + const Consumers<I>& consumers, + const WriterConfiguration<N>& cfg): + m_par(consumers.getConsumers(), cfg.extent, + cfg.batch_size ? *cfg.batch_size : defaults::batch_size), m_offset(0), m_buffer_rows(0), m_consumers(consumers.getConsumers()), m_file_space(H5S_SIMPLE) { using internal::data_buffer_t; - if (batch_size < 1) { + if (m_par.batch_size < 1) { throw std::logic_error("batch size must be > 0"); } // create space - H5::DataSpace space = internal::getUnlimitedSpace(internal::vec(extent)); + H5::DataSpace space = internal::getUnlimitedSpace( + internal::vec(cfg.extent)); // create params - H5::DSetCreatPropList params = internal::getChunckedDatasetParams( - internal::vec(extent), batch_size); + H5::DSetCreatPropList params = internal::getChunckedDatasetParams(cfg); std::vector<data_buffer_t> default_value = internal::buildDefault( consumers.getConsumers()); params.setFillValue(m_par.type, default_value.data()); // create ds - internal::throwIfExists(name, group); + internal::throwIfExists(cfg.name, group); H5::CompType packed_type = buildWriteType(consumers.getConsumers()); - m_ds = group.createDataSet(name, packed_type, space, params); + m_ds = group.createDataSet(cfg.name, packed_type, space, params); m_file_space = m_ds.getSpace(); m_file_space.selectNone(); } @@ -501,7 +522,7 @@ namespace H5Utils { H5::Group& group, const std::string& name, const Consumers<I>& consumers, const std::array<hsize_t, N>& extent = internal::uniform<N>(5), - hsize_t batch_size = 2048) { + hsize_t batch_size = defaults::batch_size) { return Writer<N,I>(group, name, consumers, extent, batch_size); } diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/WriterConfiguration.h b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/WriterConfiguration.h new file mode 100644 index 0000000000000000000000000000000000000000..6d196e1220012ea5101708409382a34aacf54ad8 --- /dev/null +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/WriterConfiguration.h @@ -0,0 +1,24 @@ +/* + Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration +*/ +#ifndef HDF5UTILS_WRITER_CONFIGURATION_H +#define HDF5UTILS_WRITER_CONFIGURATION_H + +#include <optional> +#include <array> +#include <string> + +#include "H5public.h" + +namespace H5Utils { + template <size_t N> + struct WriterConfiguration + { + std::string name; + std::array<hsize_t,N> extent; + std::optional<hsize_t> batch_size{std::nullopt}; + std::optional<std::array<hsize_t,N>> chunks{std::nullopt}; + std::optional<int> deflate{std::nullopt}; + }; +} +#endif diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.h b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.h index a70eb528cfcac235fcda0ca8e12d185498c6bc3f..e84a38d5739ffccd2bfcf758a4d9c1f441077964 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.h +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration */ #ifndef H5UTILS_COMMON_H #define H5UTILS_COMMON_H @@ -10,6 +10,8 @@ namespace H5 { class DSetCreatPropList; } +#include "WriterConfiguration.h" + #include "H5Traits.h" #include "H5Cpp.h" @@ -25,9 +27,9 @@ namespace H5Utils { // functions which are used by writers to set up the data spaces // and datasets H5::DataSpace getUnlimitedSpace(const std::vector<hsize_t>& max_length); + template <size_t N> H5::DSetCreatPropList getChunckedDatasetParams( - const std::vector<hsize_t>& max_length, - hsize_t batch_size); + const WriterConfiguration<N>&); std::vector<hsize_t> getStriding(std::vector<hsize_t> max_length); // writer error handling @@ -37,4 +39,6 @@ namespace H5Utils { } } +#include "common.icc" + #endif diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.icc b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.icc new file mode 100644 index 0000000000000000000000000000000000000000..3271a1fc7cfbde4b71788ef806ea47202d7c7e26 --- /dev/null +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/common.icc @@ -0,0 +1,28 @@ +/* + Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration +*/ + +#include "defaults.h" + +namespace H5Utils { + namespace internal { + template <size_t N> + H5::DSetCreatPropList getChunckedDatasetParams( + const WriterConfiguration<N>& cfg) { + H5::DSetCreatPropList params; + hsize_t batch_size = ( + cfg.batch_size ? *cfg.batch_size : defaults::batch_size); + std::vector<hsize_t> chunk_size{batch_size}; + if (cfg.chunks) { + chunk_size.insert( + chunk_size.end(), cfg.chunks->begin(), cfg.chunks->end()); + } else { + chunk_size.insert( + chunk_size.end(), cfg.extent.begin(), cfg.extent.end()); + } + params.setChunk(chunk_size.size(), chunk_size.data()); + if (cfg.deflate) params.setDeflate(*cfg.deflate); + return params; + } + } +} diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/defaults.h b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/defaults.h new file mode 100644 index 0000000000000000000000000000000000000000..22b0fee6b252bd456585f1e4fafd2b25f89e19af --- /dev/null +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/defaults.h @@ -0,0 +1,14 @@ +/* + Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration +*/ +#ifndef HDF5UTILS_DEFAULTS_H +#define HDF5UTILS_DEFAULTS_H + +namespace H5Utils { + namespace defaults { + const hsize_t batch_size{2048}; + const int deflate{7}; + } +} + +#endif diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/HdfTuple.cxx b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/HdfTuple.cxx index 01696954d4780a1ca526e3c709ebbffe99ee410a..14be4850cc0061b0d08cd4e7535133a89f721ee9 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/HdfTuple.cxx +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/HdfTuple.cxx @@ -1,5 +1,5 @@ /* -Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration +Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration */ #include "HDF5Utils/HdfTuple.h" #include "HDF5Utils/common.h" @@ -31,6 +31,17 @@ namespace H5Utils { return type; } + H5::DSetCreatPropList getChunckedDatasetParams( + const std::vector<hsize_t>& extent, + hsize_t batch_size) { + H5::DSetCreatPropList params; + std::vector<hsize_t> chunk_size{batch_size}; + chunk_size.insert(chunk_size.end(), extent.begin(), extent.end()); + params.setChunk(chunk_size.size(), chunk_size.data()); + params.setDeflate(7); + return params; + } + } // _______________________________________________________________________ @@ -55,7 +66,7 @@ namespace H5Utils { H5::DataSpace space = internal::getUnlimitedSpace(max_length); // create params - H5::DSetCreatPropList params = internal::getChunckedDatasetParams( + H5::DSetCreatPropList params = getChunckedDatasetParams( max_length, batch_size); // calculate striding diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/common.cxx b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/common.cxx index 5e41130d7adfffcd945ad94603dea8b08869f1c6..8ec43d4bb28b744e998b823d3eac86e3cb87f2da 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/common.cxx +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/common.cxx @@ -1,6 +1,6 @@ // this is -*- C++ -*- /* - Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration */ #include "HDF5Utils/Writer.h" @@ -35,16 +35,6 @@ namespace H5Utils { eventual.insert(eventual.end(), extent.begin(), extent.end()); return H5::DataSpace(eventual.size(), initial.data(), eventual.data()); } - H5::DSetCreatPropList getChunckedDatasetParams( - const std::vector<hsize_t>& extent, - hsize_t batch_size) { - H5::DSetCreatPropList params; - std::vector<hsize_t> chunk_size{batch_size}; - chunk_size.insert(chunk_size.end(), extent.begin(), extent.end()); - params.setChunk(chunk_size.size(), chunk_size.data()); - params.setDeflate(7); - return params; - } std::vector<hsize_t> getStriding(std::vector<hsize_t> extent) { // calculate striding extent.push_back(1);