20#ifndef DUNE_COMMON_PARALLEL_MPIPACK_HH
21#define DUNE_COMMON_PARALLEL_MPIPACK_HH
33 std::vector<char> _buffer;
61 constexpr bool has_static_size =
decltype(
getMPIData(std::declval<T&>()))::static_size;
64 if (_position +
size > 0 &&
size_t(_position +
size) > _buffer.size())
65 _buffer.resize(_position +
size);
67 int size = mpidata.size();
68 MPI_Pack(&
size, 1, MPI_INT, _buffer.data(), _buffer.size(),
71 MPI_Pack(mpidata.ptr(), mpidata.size(),
72 mpidata.type(), _buffer.data(), _buffer.size(),
82 -> std::enable_if_t<
decltype(
getMPIData(data))::static_size,
void>
85 MPI_Unpack(_buffer.data(), _buffer.size(), &_position,
86 mpidata.ptr(), mpidata.size(),
87 mpidata.type(), _comm);
96 -> std::enable_if_t<!
decltype(
getMPIData(data))::static_size,
void>
100 MPI_Unpack(_buffer.data(), _buffer.size(), &_position,
103 mpidata.resize(
size);
104 MPI_Unpack(_buffer.data(), _buffer.size(), &_position,
105 mpidata.ptr(), mpidata.size(),
106 mpidata.type(), _comm);
142 _buffer.resize(
size);
148 _buffer.resize(_buffer.size() + s);
154 return _buffer.size();
174 return std::size_t(_position)==_buffer.size();
180 static int getPackSize(
int len,
const MPI_Comm& comm,
const MPI_Datatype& dt){
182 MPI_Pack_size(len, dt, comm, &
size);
187 return a._buffer == b._buffer && a._comm == b._comm;
196 struct MPIData<P,
std::enable_if_t<std::is_same<std::remove_const_t<P>, MPIPack>::value>> {
198 friend auto getMPIData<P>(P& t);
206 return (
void*)
data_._buffer.data();
Interface class to translate objects to a MPI_Datatype, void* and size used for MPI calls.
Implements an utility class that provides MPI's collective communication methods.
Dune namespace.
Definition alignedallocator.hh:13
auto getMPIData(T &t)
Definition mpidata.hh:43
Collective communication interface and sequential default implementation.
Definition communication.hh:100
T & data_
Definition mpidata.hh:53
int size() const
Definition mpidata.hh:67
static constexpr bool static_size
Definition mpidata.hh:65
void enlarge(int s)
Enlarges the internal buffer.
Definition mpipack.hh:147
friend MPIPack & operator>>(MPIPack &p, T &t)
Unpacks data from the object.
Definition mpipack.hh:119
friend MPIPack & operator<<(MPIPack &p, const T &t)
Packs the data into the object. Enlarges the internal buffer if necessary.
Definition mpipack.hh:112
friend bool operator!=(const MPIPack &a, const MPIPack &b)
Definition mpipack.hh:189
size_t size() const
Returns the size of the internal buffer.
Definition mpipack.hh:153
MPIPack & operator=(const MPIPack &other)=delete
MPIPack(Communication< MPI_Comm > comm, std::size_t size=0)
Definition mpipack.hh:40
void pack(const T &data)
Packs the data into the object. Enlarges the internal buffer if necessary.
Definition mpipack.hh:58
auto unpack(T &data) -> std::enable_if_t<!decltype(getMPIData(data))::static_size, void >
Unpacks data from the object.
Definition mpipack.hh:95
auto unpack(T &data) -> std::enable_if_t< decltype(getMPIData(data))::static_size, void >
Unpacks data from the object.
Definition mpipack.hh:81
friend bool operator==(const MPIPack &a, const MPIPack &b)
Definition mpipack.hh:186
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:160
bool eof() const
Checks whether the end of the buffer is reached.
Definition mpipack.hh:173
void resize(size_t size)
Resizes the internal buffer.
Definition mpipack.hh:141
int tell() const
Gets the position in the buffer where the next pack/unpack operation should take place.
Definition mpipack.hh:167
MPIPack(MPIPack &&)=default
MPIPack & read(T &t)
Unpacks data from the object.
Definition mpipack.hh:126
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:180
MPIPack & write(const T &t)
Packs the data into the object. Enlarges the internal buffer if necessary.
Definition mpipack.hh:133
MPI_Datatype type() const
Definition mpipack.hh:213
MPIData(P &t)
Definition mpipack.hh:199
int size()
Definition mpipack.hh:209
void * ptr()
Definition mpipack.hh:205
P & data_
Definition mpipack.hh:221
void resize(int size)
Definition mpipack.hh:217