diff --git a/ebos/eclmpiserializer.hh b/ebos/eclmpiserializer.hh index b627970a3..7e4c358da 100644 --- a/ebos/eclmpiserializer.hh +++ b/ebos/eclmpiserializer.hh @@ -93,6 +93,8 @@ public: pair(data); } else if constexpr (is_variant::value) { variant(data); + } else if constexpr (is_tuple::value) { + tuple(data); } else if constexpr (is_optional::value) { optional(data); } else if constexpr (is_vector::value) { @@ -177,6 +179,8 @@ public: } } + //! \brief Handler for arrays. + //! \param data The array to (de-)serialize template void array(Array& data) { @@ -260,7 +264,26 @@ public: const_cast&>(data) = res; } } + } + //! \brief Handler for std::tuple. + //! \param data The tuple to (de-)serialize + template + void tuple(const std::tuple& data) + { + if (m_op == Operation::PACKSIZE) { + m_op = Operation::PACKSIZE; + m_packSize = 0; + tuple_call(data); + } else if (m_op == Operation::PACK) { + m_position = 0; + m_buffer.resize(m_packSize); + tuple_call(data); + } else if (m_op == Operation::UNPACK) { + m_position = 0; + m_op = Operation::UNPACK; + tuple_call(data); + } } //! \brief Handler for maps. @@ -321,6 +344,9 @@ public: } } + //! \brief Handler for sets. + //! \tparam Set set type + //! \param data The set to (de-)serialize template void set(Set& data) { @@ -498,6 +524,20 @@ protected: variadic_call(std::forward(args)...); } + template + typename std::enable_if::value, void>::type + tuple_call(const Tuple&) + { + } + + template + typename std::enable_if::value, void>::type + tuple_call(const Tuple& tuple) + { + (*this)(std::get(tuple)); + tuple_call(tuple); + } + //! \brief Enumeration of operations. enum class Operation { PACKSIZE, //!< Calculating serialization buffer size @@ -538,6 +578,17 @@ protected: constexpr static bool value = true; }; + //! \brief Predicate for detecting tuples. + template + struct is_tuple { + constexpr static bool value = false; + }; + + template + struct is_tuple> { + constexpr static bool value = true; + }; + //! \brief Predicate for smart pointers. template struct is_ptr { diff --git a/opm/simulators/utils/MPIPacker.cpp b/opm/simulators/utils/MPIPacker.cpp index bb18ae01a..bd035da96 100644 --- a/opm/simulators/utils/MPIPacker.cpp +++ b/opm/simulators/utils/MPIPacker.cpp @@ -61,26 +61,6 @@ std::size_t packSize(const T* data, std::size_t l, Opm::Parallel::MPIComm comm) return packSize(data, l, comm, typename std::is_pod::type()); } -template -typename std::enable_if::value, std::size_t>::type -pack_size_tuple_entry(const Tuple&, Opm::Parallel::MPIComm) -{ - return 0; -} - -template -typename std::enable_if::value, std::size_t>::type -pack_size_tuple_entry(const Tuple& tuple, Opm::Parallel::MPIComm comm) -{ - return packSize(std::get(tuple), comm) + pack_size_tuple_entry(tuple, comm); -} - -template -std::size_t packSize(const std::tuple& data, Opm::Parallel::MPIComm comm) -{ - return pack_size_tuple_entry(data, comm); -} - std::size_t packSize(const char* str, Opm::Parallel::MPIComm comm) { #if HAVE_MPI @@ -175,29 +155,6 @@ void pack(const T* data, std::size_t l, std::vector& buffer, int& position pack(data, l, buffer, position, comm, typename std::is_pod::type()); } -template -typename std::enable_if::value, void>::type -pack_tuple_entry(const Tuple&, std::vector&, int&, - Opm::Parallel::MPIComm) -{ -} - -template -typename std::enable_if::value, void>::type -pack_tuple_entry(const Tuple& tuple, std::vector& buffer, - int& position, Opm::Parallel::MPIComm comm) -{ - pack(std::get(tuple), buffer, position, comm); - pack_tuple_entry(tuple, buffer, position, comm); -} - -template -void pack(const std::tuple& data, std::vector& buffer, - int& position, Opm::Parallel::MPIComm comm) -{ - pack_tuple_entry(data, buffer, position, comm); -} - void pack(const char* str, std::vector& buffer, int& position, Opm::Parallel::MPIComm comm) { @@ -268,29 +225,6 @@ void unpack(T* data, const std::size_t& l, std::vector& buffer, int& posit unpack(data, l, buffer, position, comm, typename std::is_pod::type()); } -template -typename std::enable_if::value, void>::type -unpack_tuple_entry(Tuple&, std::vector&, int&, - Opm::Parallel::MPIComm) -{ -} - -template -typename std::enable_if::value, void>::type -unpack_tuple_entry(Tuple& tuple, std::vector& buffer, - int& position, Opm::Parallel::MPIComm comm) -{ - unpack(std::get(tuple), buffer, position, comm); - unpack_tuple_entry(tuple, buffer, position, comm); -} - -template -void unpack(std::tuple& data, std::vector& buffer, - int& position, Opm::Parallel::MPIComm comm) -{ - unpack_tuple_entry(data, buffer, position, comm); -} - void unpack(char* str, std::size_t length, std::vector& buffer, int& position, Opm::Parallel::MPIComm comm) { diff --git a/opm/simulators/utils/MPIPacker.hpp b/opm/simulators/utils/MPIPacker.hpp index 2eadee486..e497e1918 100644 --- a/opm/simulators/utils/MPIPacker.hpp +++ b/opm/simulators/utils/MPIPacker.hpp @@ -27,7 +27,6 @@ #include #include #include -#include #include namespace Opm @@ -74,9 +73,6 @@ std::size_t packSize(const T& data, Opm::Parallel::MPIComm comm) return packSize(data, comm, typename std::is_pod::type()); } -template -std::size_t packSize(const std::tuple& data, Opm::Parallel::MPIComm comm); - std::size_t packSize(const char* str, Opm::Parallel::MPIComm comm); template @@ -125,10 +121,6 @@ void pack(const T& data, std::vector& buffer, int& position, pack(data, buffer, position, comm, typename std::is_pod::type()); } -template -void pack(const std::tuple& data, std::vector& buffer, - int& position, Opm::Parallel::MPIComm comm); - void pack(const char* str, std::vector& buffer, int& position, Opm::Parallel::MPIComm comm); @@ -180,10 +172,6 @@ void unpack(T& data, std::vector& buffer, int& position, unpack(data, buffer, position, comm, typename std::is_pod::type()); } -template -void unpack(std::tuple& data, std::vector& buffer, - int& position, Opm::Parallel::MPIComm comm); - void unpack(char* str, std::size_t length, std::vector& buffer, int& position, Opm::Parallel::MPIComm comm);