From dfd2109665902a8d43607cd7626d29f30243ce5a Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 23 Sep 2020 12:23:18 +0200 Subject: [PATCH 1/2] added: support for std::optional in eclmpiserializer --- ebos/eclmpiserializer.hh | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ebos/eclmpiserializer.hh b/ebos/eclmpiserializer.hh index 13f15932a..e2cfd946b 100644 --- a/ebos/eclmpiserializer.hh +++ b/ebos/eclmpiserializer.hh @@ -22,6 +22,7 @@ #define ECL_MPI_SERIALIZER_HH #include +#include #include namespace Opm { @@ -51,6 +52,8 @@ public: pair(data); } else if constexpr (is_variant::value) { variant(data); + } else if constexpr (is_optional::value) { + optional(data); } else { if (m_op == Operation::PACKSIZE) m_packSize += Mpi::packSize(data, m_comm); @@ -194,6 +197,45 @@ public: } + //! \brief Handler for std::optional. + //! \tparam T Type for data + //! \param data The optional to (de-)serialize + template + void optional(const std::optional& data) + { + if (m_op == Operation::PACKSIZE) { + m_packSize += Mpi::packSize(data.has_value(), m_comm); + if (data.has_value()) { + if constexpr (has_serializeOp::value) { + const_cast(*data).serializeOp(*this); + } else + m_packSize += Mpi::packSize(*data, m_comm); + } + } else if (m_op == Operation::PACK) { + Mpi::pack(data.has_value(), m_buffer, m_position, m_comm); + if (data.has_value()) { + if constexpr (has_serializeOp::value) { + const_cast(*data).serializeOp(*this); + } else { + Mpi::pack(*data, m_buffer, m_position, m_comm); + } + } + } else if (m_op == Operation::UNPACK) { + bool has; + Mpi::unpack(has, m_buffer, m_position, m_comm); + if (has) { + T res; + if constexpr (has_serializeOp::value) { + res.serializeOp(*this); + } else { + Mpi::unpack(res, m_buffer, m_position, m_comm); + } + const_cast&>(data) = res; + } + } + + } + //! \brief Handler for maps. //! \tparam Map map type //! \tparam complexType Whether or not Data in map is a complex type @@ -371,6 +413,17 @@ protected: constexpr static bool value = true; }; + //! \brief Predicate for std::optional. + template + struct is_optional { + constexpr static bool value = false; + }; + + template + struct is_optional> { + constexpr static bool value = true; + }; + //! \brief Handler for pairs. //! \details If data is POD or a string, we pass it to the underlying serializer, //! if not we assume a complex type. @@ -404,6 +457,21 @@ protected: data->serializeOp(*this); } + //! \brief Checks if a type has a serializeOp member. + //! \detail Ideally we would check for the serializeOp member, + //! but this is a member template. For simplicity, + //! we use serializeObject as our check for now. + template + class has_serializeOp + { + using yes_type = char; + using no_type = long; + template static yes_type test(decltype(&U::serializeObject)); + template static no_type test(...); + public: + static constexpr bool value = sizeof(test(0)) == sizeof(yes_type); + }; + Dune::CollectiveCommunication m_comm; //!< Communicator to broadcast using Operation m_op = Operation::PACKSIZE; //!< Current operation From f846bbe9594bf0e4d4863dce62c7a0b81277c4c8 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 23 Sep 2020 12:23:30 +0200 Subject: [PATCH 2/2] remove Mpi::pack handling for GPMaint this is not implemented, and should go through the serializer support --- opm/simulators/utils/ParallelRestart.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/opm/simulators/utils/ParallelRestart.cpp b/opm/simulators/utils/ParallelRestart.cpp index bf5aba25b..ca3787ba9 100644 --- a/opm/simulators/utils/ParallelRestart.cpp +++ b/opm/simulators/utils/ParallelRestart.cpp @@ -922,7 +922,6 @@ INSTANTIATE_PACK(unsigned char) INSTANTIATE_PACK(std::map,std::pair>) INSTANTIATE_PACK(std::optional) INSTANTIATE_PACK(std::optional) -INSTANTIATE_PACK(std::optional) INSTANTIATE_PACK(std::pair) INSTANTIATE_PACK(std::optional>) INSTANTIATE_PACK(std::map>)