21#ifndef MPI_SERIALIZER_HPP
22#define MPI_SERIALIZER_HPP
24#include <opm/common/utility/Serializer.hpp>
34 : Serializer<Mpi::Packer>(m_packer)
48 if (m_comm.size() == 1)
51 if (m_comm.rank() == root) {
54 m_comm.broadcast(&m_packSize, 1, root);
55 broadcast_chunked(root);
57 m_packSize = std::numeric_limits<size_t>::max();
58 m_comm.broadcast(&m_packSize, 1, root);
62 m_comm.broadcast(&m_packSize, 1, root);
63 if (m_packSize == std::numeric_limits<size_t>::max()) {
64 throw std::runtime_error(
"Error detected in parallel serialization");
66 m_buffer.resize(m_packSize);
67 broadcast_chunked(root);
72 template<
typename... Args>
75 if (m_comm.size() == 1)
78 if (m_comm.rank() == root) {
80 this->
pack(std::forward<Args>(args)...);
81 m_comm.broadcast(&m_packSize, 1, root);
82 broadcast_chunked(root);
84 m_packSize = std::numeric_limits<size_t>::max();
85 m_comm.broadcast(&m_packSize, 1, root);
89 m_comm.broadcast(&m_packSize, 1, root);
90 if (m_packSize == std::numeric_limits<size_t>::max()) {
91 throw std::runtime_error(
"Error detected in parallel serialization");
93 m_buffer.resize(m_packSize);
94 broadcast_chunked(root);
95 this->unpack(std::forward<Args>(args)...);
108 if (m_comm.size() == 1)
112 T& bcast = m_comm.rank() == root ? data : tmp;
115 if (m_comm.rank() != root)
120 void broadcast_chunked(
int root) {
121 const int maxChunkSize = std::numeric_limits<int>::max();
122 std::size_t remainingSize = m_packSize;
124 while (remainingSize > maxChunkSize) {
125 m_comm.broadcast(m_buffer.data()+pos, maxChunkSize, root);
127 remainingSize -= maxChunkSize;
129 m_comm.broadcast(m_buffer.data()+pos,
static_cast<int>(remainingSize), root);
132 const Mpi::Packer m_packer;
Class for serializing and broadcasting data using MPI.
Definition: MPISerializer.hpp:31
void append(T &data, int root=0)
Serialize and broadcast on root process, de-serialize and append on others.
Definition: MPISerializer.hpp:106
void broadcast(int root, Args &&... args)
Definition: MPISerializer.hpp:73
MpiSerializer(Parallel::Communication comm)
Definition: MPISerializer.hpp:33
void broadcast(T &data, int root=0)
Serialize and broadcast on root process, de-serialize on others.
Definition: MPISerializer.hpp:46
std::size_t pack(const PV &privar)
Definition: priVarsPacking.hpp:31
Definition: MPISerializer.hpp:28
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30