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 m_comm.broadcast(m_buffer.data(), m_packSize, 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
67 m_buffer.resize(m_packSize);
68 m_comm.broadcast(m_buffer.data(), m_packSize, root);
69 this->unpack(data);
70 }
71 }
72
73 template<typename... Args>
74 void broadcast(int root, Args&&... args)
75 {
76 if (m_comm.size() == 1)
77 return;
78
79 if (m_comm.rank() == root) {
80 try {
81 this->pack(std::forward<Args>(args)...);
82 m_comm.broadcast(&m_packSize, 1, root);
83 m_comm.broadcast(m_buffer.data(), m_packSize, root);
84 } catch (...) {
85 m_packSize = std::numeric_limits<size_t>::max();
86 m_comm.broadcast(&m_packSize, 1, root);
87 throw;
88 }
89 } else {
90 m_comm.broadcast(&m_packSize, 1, root);
91 if (m_packSize == std::numeric_limits<size_t>::max()) {
92 throw std::runtime_error("Error detected in parallel serialization");
93 }
94 m_buffer.resize(m_packSize);
95 m_comm.broadcast(m_buffer.data(), m_packSize, root);
96 this->unpack(std::forward<Args>(args)...);
97 }
98 }
99
106 template<class T>
107 void append(T& data, int root = 0)
108 {
109 if (m_comm.size() == 1)
110 return;
111
112 T tmp;
113 T& bcast = m_comm.rank() == root ? data : tmp;
114 broadcast(bcast, root);
115
116 if (m_comm.rank() != root)
117 data.append(tmp);
118 }
119
120private:
121 const Mpi::Packer m_packer;
123};
124
125}
126
127#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:107
void broadcast(int root, Args &&... args)
Definition: MPISerializer.hpp:74
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
Struct handling packing of serialization for MPI communication.
Definition: MPIPacker.hpp:175