From 975e096d4c0d2ff3021a1c2a3cde815f62ee1f99 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 8 Sep 2022 11:32:23 +0200 Subject: [PATCH] changed: make container handlers in eclmpiserializer protected users only use operator() --- ebos/eclmpiserializer.hh | 260 +++++++++--------- opm/simulators/utils/MPIPacker.cpp | 5 +- opm/simulators/utils/ParallelEclipseState.hpp | 2 +- 3 files changed, 133 insertions(+), 134 deletions(-) diff --git a/ebos/eclmpiserializer.hh b/ebos/eclmpiserializer.hh index 66890e7f1..970394c2a 100644 --- a/ebos/eclmpiserializer.hh +++ b/ebos/eclmpiserializer.hh @@ -113,6 +113,136 @@ public: } } + //! \brief Call this to serialize data. + //! \tparam T Type of class to serialize + //! \param data Class to serialize + template + void pack(const T& data) + { + m_op = Operation::PACKSIZE; + m_packSize = 0; + (*this)(data); + m_position = 0; + m_buffer.resize(m_packSize); + m_op = Operation::PACK; + (*this)(data); + } + + //! \brief Call this to de-serialize data. + //! \tparam T Type of class to de-serialize + //! \param data Class to de-serialize + template + void unpack(T& data) + { + m_position = 0; + m_op = Operation::UNPACK; + (*this)(data); + } + + //! \brief Serialize and broadcast on root process, de-serialize on + //! others. + //! + //! \tparam T Type of class to broadcast + //! \param data Class to broadcast + //! \param root Process to broadcast from + template + void broadcast(T& data, int root = 0) + { + if (m_comm.size() == 1) + return; + + if (m_comm.rank() == root) { + try { + pack(data); + m_packSize = m_position; + m_comm.broadcast(&m_packSize, 1, root); + m_comm.broadcast(m_buffer.data(), m_position, root); + } catch (...) { + m_packSize = std::numeric_limits::max(); + m_comm.broadcast(&m_packSize, 1, root); + throw; + } + } else { + m_comm.broadcast(&m_packSize, 1, root); + if (m_packSize == std::numeric_limits::max()) { + throw std::runtime_error("Error detected in parallel serialization"); + } + + m_buffer.resize(m_packSize); + m_comm.broadcast(m_buffer.data(), m_packSize, root); + unpack(data); + } + } + + template + void broadcast(int root, Args&&... args) + { + if (m_comm.size() == 1) + return; + + if (m_comm.rank() == root) { + try { + m_op = Operation::PACKSIZE; + m_packSize = 0; + variadic_call(args...); + m_position = 0; + m_buffer.resize(m_packSize); + m_op = Operation::PACK; + variadic_call(args...); + m_packSize = m_position; + m_comm.broadcast(&m_packSize, 1, root); + m_comm.broadcast(m_buffer.data(), m_position, root); + } catch (...) { + m_packSize = std::numeric_limits::max(); + m_comm.broadcast(&m_packSize, 1, root); + throw; + } + } else { + m_comm.broadcast(&m_packSize, 1, root); + if (m_packSize == std::numeric_limits::max()) { + throw std::runtime_error("Error detected in parallel serialization"); + } + m_buffer.resize(m_packSize); + m_comm.broadcast(m_buffer.data(), m_packSize, root); + m_position = 0; + m_op = Operation::UNPACK; + variadic_call(std::forward(args)...); + } + } + + //! \brief Serialize and broadcast on root process, de-serialize and append on + //! others. + //! + //! \tparam T Type of class to broadcast + //! \param data Class to broadcast + //! \param root Process to broadcast from + template + void append(T& data, int root = 0) + { + if (m_comm.size() == 1) + return; + + T tmp; + T& bcast = m_comm.rank() == root ? data : tmp; + broadcast(bcast); + + if (m_comm.rank() != root) + data.append(tmp); + } + + //! \brief Returns current position in buffer. + size_t position() const + { + return m_position; + } + + //! \brief Returns true if we are currently doing a serialization operation. + bool isSerializing() const + { + return m_op != Operation::UNPACK; + } + +protected: //! \brief Handler for vectors. //! \tparam T Type for vector elements //! \param data The vector to (de-)serialize @@ -281,136 +411,6 @@ public: } } - //! \brief Call this to serialize data. - //! \tparam T Type of class to serialize - //! \param data Class to serialize - template - void pack(const T& data) - { - m_op = Operation::PACKSIZE; - m_packSize = 0; - (*this)(data); - m_position = 0; - m_buffer.resize(m_packSize); - m_op = Operation::PACK; - (*this)(data); - } - - //! \brief Call this to de-serialize data. - //! \tparam T Type of class to de-serialize - //! \param data Class to de-serialize - template - void unpack(T& data) - { - m_position = 0; - m_op = Operation::UNPACK; - (*this)(data); - } - - //! \brief Serialize and broadcast on root process, de-serialize on - //! others. - //! - //! \tparam T Type of class to broadcast - //! \param data Class to broadcast - //! \param root Process to broadcast from - template - void broadcast(T& data, int root = 0) - { - if (m_comm.size() == 1) - return; - - if (m_comm.rank() == root) { - try { - pack(data); - m_packSize = m_position; - m_comm.broadcast(&m_packSize, 1, root); - m_comm.broadcast(m_buffer.data(), m_position, root); - } catch (...) { - m_packSize = std::numeric_limits::max(); - m_comm.broadcast(&m_packSize, 1, root); - throw; - } - } else { - m_comm.broadcast(&m_packSize, 1, root); - if (m_packSize == std::numeric_limits::max()) { - throw std::runtime_error("Error detected in parallel serialization"); - } - - m_buffer.resize(m_packSize); - m_comm.broadcast(m_buffer.data(), m_packSize, root); - unpack(data); - } - } - - template - void broadcast(int root, Args&&... args) - { - if (m_comm.size() == 1) - return; - - if (m_comm.rank() == root) { - try { - m_op = Operation::PACKSIZE; - m_packSize = 0; - variadic_call(args...); - m_position = 0; - m_buffer.resize(m_packSize); - m_op = Operation::PACK; - variadic_call(args...); - m_packSize = m_position; - m_comm.broadcast(&m_packSize, 1, root); - m_comm.broadcast(m_buffer.data(), m_position, root); - } catch (...) { - m_packSize = std::numeric_limits::max(); - m_comm.broadcast(&m_packSize, 1, root); - throw; - } - } else { - m_comm.broadcast(&m_packSize, 1, root); - if (m_packSize == std::numeric_limits::max()) { - throw std::runtime_error("Error detected in parallel serialization"); - } - m_buffer.resize(m_packSize); - m_comm.broadcast(m_buffer.data(), m_packSize, root); - m_position = 0; - m_op = Operation::UNPACK; - variadic_call(std::forward(args)...); - } - } - - //! \brief Serialize and broadcast on root process, de-serialize and append on - //! others. - //! - //! \tparam T Type of class to broadcast - //! \param data Class to broadcast - //! \param root Process to broadcast from - template - void append(T& data, int root = 0) - { - if (m_comm.size() == 1) - return; - - T tmp; - T& bcast = m_comm.rank() == root ? data : tmp; - broadcast(bcast); - - if (m_comm.rank() != root) - data.append(tmp); - } - - //! \brief Returns current position in buffer. - size_t position() const - { - return m_position; - } - - //! \brief Returns true if we are currently doing a serialization operation. - bool isSerializing() const - { - return m_op != Operation::UNPACK; - } - -protected: template void variadic_call(T& first, Args&&... args) diff --git a/opm/simulators/utils/MPIPacker.cpp b/opm/simulators/utils/MPIPacker.cpp index 40077fd75..45240c024 100644 --- a/opm/simulators/utils/MPIPacker.cpp +++ b/opm/simulators/utils/MPIPacker.cpp @@ -128,9 +128,8 @@ unpack(time_point& data, } template struct Packing>; +template struct Packing>; -} - +} // end namespace detail } // end namespace Mpi - } // end namespace Opm diff --git a/opm/simulators/utils/ParallelEclipseState.hpp b/opm/simulators/utils/ParallelEclipseState.hpp index 9fa548929..4be23d401 100644 --- a/opm/simulators/utils/ParallelEclipseState.hpp +++ b/opm/simulators/utils/ParallelEclipseState.hpp @@ -116,7 +116,7 @@ public: template void serializeOp(Serializer& serializer) { - serializer.map(m_tran); + serializer(m_tran); } protected: