MPISerializer.hpp
Go to the documentation of this file.
1/*
2 This file is part of the Open Porous Media project (OPM).
3
4 OPM is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 OPM is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with OPM. If not, see <http://www.gnu.org/licenses/>.
16
17 Consult the COPYING file in the top-level source directory of this
18 module for the precise wording of the license and the list of
19 copyright holders.
20*/
21#ifndef MPI_SERIALIZER_HPP
22#define MPI_SERIALIZER_HPP
23
24#include <opm/common/utility/Serializer.hpp>
27
28namespace Opm::Parallel {
29
31class MpiSerializer : public Serializer<Mpi::Packer> {
32public:
34 : Serializer<Mpi::Packer>(m_packer)
35 , m_packer(comm)
36 , m_comm(comm)
37 {}
38
45 template<class T>
46 void broadcast(T& data, int root = 0)
47 {
48 if (m_comm.size() == 1)
49 return;
50
51 if (m_comm.rank() == root) {
52 try {
53 this->pack(data);
54 m_comm.broadcast(&m_packSize, 1, root);
55 broadcast_chunked(root);
56 } catch (...) {
57 m_packSize = std::numeric_limits<size_t>::max();
58 m_comm.broadcast(&m_packSize, 1, root);
59 throw;
60 }
61 } else {
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");
65 }
66 m_buffer.resize(m_packSize);
67 broadcast_chunked(root);
68 this->unpack(data);
69 }
70 }
71
72 template<typename... Args>
73 void broadcast(int root, Args&&... args)
74 {
75 if (m_comm.size() == 1)
76 return;
77
78 if (m_comm.rank() == root) {
79 try {
80 this->pack(std::forward<Args>(args)...);
81 m_comm.broadcast(&m_packSize, 1, root);
82 broadcast_chunked(root);
83 } catch (...) {
84 m_packSize = std::numeric_limits<size_t>::max();
85 m_comm.broadcast(&m_packSize, 1, root);
86 throw;
87 }
88 } else {
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");
92 }
93 m_buffer.resize(m_packSize);
94 broadcast_chunked(root);
95 this->unpack(std::forward<Args>(args)...);
96 }
97 }
98
105 template<class T>
106 void append(T& data, int root = 0)
107 {
108 if (m_comm.size() == 1)
109 return;
110
111 T tmp;
112 T& bcast = m_comm.rank() == root ? data : tmp;
113 broadcast(bcast, root);
114
115 if (m_comm.rank() != root)
116 data.append(tmp);
117 }
118
119private:
120 void broadcast_chunked(int root) {
121 const int maxChunkSize = std::numeric_limits<int>::max();
122 std::size_t remainingSize = m_packSize;
123 std::size_t pos = 0;
124 while (remainingSize > maxChunkSize) {
125 m_comm.broadcast(m_buffer.data()+pos, maxChunkSize, root);
126 pos += maxChunkSize;
127 remainingSize -= maxChunkSize;
128 }
129 m_comm.broadcast(m_buffer.data()+pos, static_cast<int>(remainingSize), root);
130 }
131
132 const Mpi::Packer m_packer;
134};
135
136}
137
138#endif
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