18 #ifndef DUNE_COMMON_PARALLEL_MPIPACK_HH
19 #define DUNE_COMMON_PARALLEL_MPIPACK_HH
31 std::vector<char> _buffer;
59 constexpr
bool has_static_size = decltype(
getMPIData(std::declval<T&>()))::static_size;
62 if (_position +
size > 0 &&
size_t(_position +
size) > _buffer.size())
63 _buffer.resize(_position +
size);
65 int size = mpidata.size();
66 MPI_Pack(&
size, 1, MPI_INT, _buffer.data(), _buffer.size(),
69 MPI_Pack(mpidata.ptr(), mpidata.size(),
70 mpidata.type(), _buffer.data(), _buffer.size(),
80 -> std::enable_if_t<decltype(
getMPIData(data))::static_size, void>
83 MPI_Unpack(_buffer.data(), _buffer.size(), &_position,
84 mpidata.ptr(), mpidata.size(),
85 mpidata.type(), _comm);
94 -> std::enable_if_t<!decltype(
getMPIData(data))::static_size, void>
98 MPI_Unpack(_buffer.data(), _buffer.size(), &_position,
101 mpidata.resize(
size);
102 MPI_Unpack(_buffer.data(), _buffer.size(), &_position,
103 mpidata.ptr(), mpidata.size(),
104 mpidata.type(), _comm);
140 _buffer.resize(
size);
146 _buffer.resize(_buffer.size() + s);
152 return _buffer.size();
172 return std::size_t(_position)==_buffer.size();
178 static int getPackSize(
int len,
const MPI_Comm& comm,
const MPI_Datatype& dt){
180 MPI_Pack_size(len, dt, comm, &
size);
185 return a._buffer == b._buffer && a._comm == b._comm;
194 struct MPIData<P, std::enable_if_t<std::is_same<std::remove_const_t<P>, MPIPack>::value>> {
196 friend auto getMPIData<P>(P& t);
204 return (
void*)
data_._buffer.data();
Implements an utility class that provides MPI's collective communication methods.
Interface class to translate objects to a MPI_Datatype, void* and size used for MPI calls.
Dune namespace.
Definition: alignedallocator.hh:14
auto getMPIData(T &t)
Definition: mpidata.hh:41
Specialization of Communication for MPI.
Definition: mpicommunication.hh:106
Definition: mpidata.hh:48
T & data_
Definition: mpidata.hh:51
int size() const
Definition: mpidata.hh:65
static constexpr bool static_size
Definition: mpidata.hh:63
Definition: mpipack.hh:30
MPIPack & operator=(const MPIPack &other)=delete
void enlarge(int s)
Enlarges the internal buffer.
Definition: mpipack.hh:145
MPIPack & write(const T &t)
Packs the data into the object. Enlarges the internal buffer if necessary.
Definition: mpipack.hh:131
friend MPIPack & operator>>(MPIPack &p, T &t)
Unpacks data from the object.
Definition: mpipack.hh:117
friend bool operator!=(const MPIPack &a, const MPIPack &b)
Definition: mpipack.hh:187
size_t size() const
Returns the size of the internal buffer.
Definition: mpipack.hh:151
MPIPack(Communication< MPI_Comm > comm, std::size_t size=0)
Definition: mpipack.hh:38
void pack(const T &data)
Packs the data into the object. Enlarges the internal buffer if necessary.
Definition: mpipack.hh:56
MPIPack & read(T &t)
Unpacks data from the object.
Definition: mpipack.hh:124
auto unpack(T &data) -> std::enable_if_t<!decltype(getMPIData(data))::static_size, void >
Unpacks data from the object.
Definition: mpipack.hh:93
auto unpack(T &data) -> std::enable_if_t< decltype(getMPIData(data))::static_size, void >
Unpacks data from the object.
Definition: mpipack.hh:79
friend bool operator==(const MPIPack &a, const MPIPack &b)
Definition: mpipack.hh:184
MPIPack(const MPIPack &)=delete
void seek(int p)
Sets the position in the buffer where the next pack/unpack operation should take place.
Definition: mpipack.hh:158
bool eof() const
Checks whether the end of the buffer is reached.
Definition: mpipack.hh:171
void resize(size_t size)
Resizes the internal buffer.
Definition: mpipack.hh:139
int tell() const
Gets the position in the buffer where the next pack/unpack operation should take place.
Definition: mpipack.hh:165
friend MPIPack & operator<<(MPIPack &p, const T &t)
Packs the data into the object. Enlarges the internal buffer if necessary.
Definition: mpipack.hh:110
MPIPack(MPIPack &&)=default
static int getPackSize(int len, const MPI_Comm &comm, const MPI_Datatype &dt)
Returns the size of the data needed to store the data in an MPIPack. See MPI_Pack_size.
Definition: mpipack.hh:178
MPI_Datatype type() const
Definition: mpipack.hh:211
MPIData(P &t)
Definition: mpipack.hh:197
int size()
Definition: mpipack.hh:207
void * ptr()
Definition: mpipack.hh:203
P & data_
Definition: mpipack.hh:219
void resize(int size)
Definition: mpipack.hh:215