27#ifndef OPM_MATERIAL_MPIUTIL_HH
28#define OPM_MATERIAL_MPIUTIL_HH
30#include <dune/common/parallel/mpitraits.hh>
51 MPI_Pack_size(1, Dune::MPITraits<T>::getType(), MPI_COMM_WORLD, &pack_size);
64 static void pack(
const T& content, std::vector<char>& buf,
int& offset)
66 MPI_Pack(&content, 1, Dune::MPITraits<T>::getType(), buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
69 static T
unpack(
const std::vector<char>& recv_buffer,
int& offset)
72 auto* data =
const_cast<char*
>(recv_buffer.data());
73 MPI_Unpack(data, recv_buffer.size(), &offset, &content, 1, Dune::MPITraits<T>::getType(), MPI_COMM_WORLD);
82 static int size(
const std::string& content)
84 return packSize<unsigned int>() + content.size()*packSize<char>();
87 static void pack(
const std::string& content, std::vector<char>& buf,
int& offset)
89 unsigned int size = content.size();
92 MPI_Pack(
const_cast<char*
>(content.c_str()),
size, MPI_CHAR, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
96 static std::string
unpack(
const std::vector<char>& recv_buffer,
int& offset)
101 auto* data =
const_cast<char*
>(recv_buffer.data());
102 std::vector<char> chars(
size);
103 MPI_Unpack(data, recv_buffer.size(), &offset, chars.data(),
size, MPI_CHAR, MPI_COMM_WORLD);
104 text = std::string(chars.data(),
size);
111 template <
typename T>
114 static int size(
const std::string& content)
117 sz += packSize<unsigned int>();
118 for (
const T& elem : content) {
124 static void pack(
const std::vector<T>& content, std::vector<char>& buf,
int& offset)
126 unsigned int size = content.size();
128 for (
const T& elem : content) {
133 static std::vector<T>
unpack(
const std::vector<char>& recv_buffer,
int& offset)
136 std::vector<T> content;
137 content.reserve(
size);
138 for (
unsigned int i = 0; i <
size; ++i) {
153 inline std::vector<std::string>
gatherStrings(
const std::string& local_string)
158 const int message_size = StringPacker::size(local_string);
159 std::vector<char> buffer(message_size);
161 StringPacker::pack(local_string, buffer, offset);
162 assert(offset == message_size);
165 int num_processes = -1;
166 MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
167 std::vector<int> message_sizes(num_processes);
168 MPI_Allgather(&message_size, 1, MPI_INT, message_sizes.data(), 1, MPI_INT, MPI_COMM_WORLD);
169 std::vector<int> displ(num_processes + 1, 0);
170 std::partial_sum(message_sizes.begin(), message_sizes.end(), displ.begin() + 1);
173 std::vector<char> recv_buffer(displ.back());
174 MPI_Allgatherv(buffer.data(), buffer.size(), MPI_PACKED,
175 const_cast<char*
>(recv_buffer.data()), message_sizes.data(),
176 displ.data(), MPI_PACKED,
180 std::vector<std::string> ret;
181 for (
int process = 0; process < num_processes; ++process) {
182 offset = displ[process];
183 std::string s = StringPacker::unpack(recv_buffer, offset);
187 assert(offset == displ[process + 1]);
198 inline std::vector<std::string>
gatherStrings(
const std::string& local_string)
200 if (local_string.empty()) {
203 return { local_string };
Definition: blackoilboundaryratevector.hh:37
std::vector< std::string > gatherStrings(const std::string &local_string)
From each rank, gather its string (if not empty) into a vector.
Definition: mpiutil.hh:153
Definition: mpiutil.hh:45
int packSize()
Definition: mpiutil.hh:48
Definition: mpiutil.hh:81
static void pack(const std::string &content, std::vector< char > &buf, int &offset)
Definition: mpiutil.hh:87
static int size(const std::string &content)
Definition: mpiutil.hh:82
static std::string unpack(const std::vector< char > &recv_buffer, int &offset)
Definition: mpiutil.hh:96
static void pack(const std::vector< T > &content, std::vector< char > &buf, int &offset)
Definition: mpiutil.hh:124
static int size(const std::string &content)
Definition: mpiutil.hh:114
static std::vector< T > unpack(const std::vector< char > &recv_buffer, int &offset)
Definition: mpiutil.hh:133
Definition: mpiutil.hh:58
static int size(const T &)
Definition: mpiutil.hh:59
static void pack(const T &content, std::vector< char > &buf, int &offset)
Definition: mpiutil.hh:64
static T unpack(const std::vector< char > &recv_buffer, int &offset)
Definition: mpiutil.hh:69