19 #ifndef DUNE_COMMUNICATOR_HEADER_INCLUDED
20 #define DUNE_COMMUNICATOR_HEADER_INCLUDED
27 #include <dune/common/version.hh>
29 #if DUNE_VERSION_NEWER(DUNE_COMMON,2,3)
30 #include <dune/common/parallel/mpihelper.hh>
31 #include <dune/common/parallel/collectivecommunication.hh>
35 #include <dune/common/parallel/mpicollectivecommunication.hh>
37 #else // DUNE_COMMON 2.2
38 #include <dune/common/mpihelper.hh>
39 #include <dune/common/collectivecommunication.hh>
43 #include <dune/common/mpicollectivecommunication.hh>
45 #endif // #if DUNE_VERSION_NEWER()
52 typedef std::vector< char > BufferType;
54 mutable BufferType buffer_;
61 : buffer_(), factor_( factor )
71 size_t size()
const {
return buffer_.size(); }
76 buffer_.reserve( size );
82 buffer_.resize( size );
90 const size_t tsize =
sizeof( T );
91 size_t pos = buffer_.size();
92 const size_t sizeNeeded = pos + tsize ;
94 if( buffer_.capacity() < sizeNeeded )
96 reserve(
size_t(factor_ * sizeNeeded) ) ;
99 buffer_.resize( sizeNeeded );
101 std::copy_n( reinterpret_cast<const char *> (&value), tsize, buffer_.data()+pos );
109 const size_t tsize =
sizeof( T );
110 assert( pos_ + tsize <= buffer_.size() );
111 std::copy_n( buffer_.data()+pos_, tsize,
reinterpret_cast<char *
> (&value) );
118 return std::make_pair( buffer_.data(), int(buffer_.size()) );
123 template <
class MsgBuffer >
134 typedef CollectiveCommunication< MPICommunicator > BaseType;
138 static const int messagetag = 234;
140 typedef std::map< int, int > linkage_t;
141 typedef std::vector< int > vector_t;
143 linkage_t sendLinkage_ ;
144 linkage_t recvLinkage_ ;
147 vector_t recvSource_ ;
149 mutable vector_t _recvBufferSizes;
150 mutable bool _recvBufferSizesComputed;
153 using BaseType :: rank;
154 using BaseType :: size;
167 virtual void localComputation () {}
183 inline int sendLinks ()
const {
return sendLinkage_.size(); }
186 inline int recvLinks ()
const {
return recvLinkage_.size(); }
194 assert (sendLinkage_.end () != sendLinkage_.find (rank)) ;
195 return (* sendLinkage_.find (rank)).second ;
201 assert (recvLinkage_.end () != recvLinkage_.find (rank)) ;
202 return (* recvLinkage_.find (rank)).second ;
206 const std::vector< int > &
sendDest ()
const {
return sendDest_; }
208 const std::vector< int > &
recvSource ()
const {
return recvSource_; }
214 virtual std::vector< MessageBufferType >
exchange (
const std::vector< MessageBufferType > &)
const;
217 virtual void exchange ( DataHandleInterface& )
const;
225 inline void computeDestinations(
const linkage_t& linkage, vector_t& dest );
228 static int getMessageTag(
const unsigned int increment )
230 static int tag = messagetag + 2 ;
232 const int retTag = tag;
238 tag = messagetag + 2 ;
244 static int getMessageTag()
246 return getMessageTag( 1 );
253 #include "p2pcommunicator_impl.hh"
255 #endif // #ifndef DUNE_COMMUNICATOR_HEADER_INCLUDED
SimpleMessageBuffer(const double factor=1.1)
constructor taking memory reserve estimation factor (default is 1.1, i.e.
Definition: p2pcommunicator.hh:60
void removeLinkage()
remove stored linkage
Definition: p2pcommunicator_impl.hh:30
virtual void exchangeCached(DataHandleInterface &) const
exchange data with peers, handle defines pack and unpack of data, if receive buffers are known from p...
Definition: p2pcommunicator_impl.hh:612
const vector_t & recvBufferSizes() const
return vector containing possible recv buffer sizes
Definition: p2pcommunicator.hh:189
int recvLinks() const
return number of processes we will receive data from
Definition: p2pcommunicator.hh:186
void resize(const size_t size)
resize buffer to 'size' entries
Definition: p2pcommunicator.hh:80
void write(const T &value)
write value to buffer, value must implement the operator= correctly (i.e.
Definition: p2pcommunicator.hh:87
void read(T &value) const
read value from buffer, value must implement the operator= correctly (i.e.
Definition: p2pcommunicator.hh:106
std::pair< char *, int > buffer() const
return pointer to buffer and size for use with MPI functions
Definition: p2pcommunicator.hh:116
int sendLink(const int rank) const
return send link number for a given send rank number
Definition: p2pcommunicator.hh:192
MsgBuffer MessageBufferType
type of message buffer used
Definition: p2pcommunicator.hh:131
const std::vector< int > & sendDest() const
return vector containing all process numbers we will send to
Definition: p2pcommunicator.hh:206
void clear()
clear the buffer
Definition: p2pcommunicator.hh:67
Definition: p2pcommunicator.hh:158
virtual std::vector< MessageBufferType > exchange(const std::vector< MessageBufferType > &) const
exchange message buffers with peers defined by inserted linkage
Definition: p2pcommunicator_impl.hh:595
size_t size() const
return size of buffer
Definition: p2pcommunicator.hh:71
const std::vector< int > & recvSource() const
return vector containing all process numbers we will receive from
Definition: p2pcommunicator.hh:208
Point-2-Point communicator for exchange messages between processes.
Definition: p2pcommunicator.hh:124
MPIHelper::MPICommunicator MPICommunicator
type of MPI communicator, either MPI_Comm or NoComm as defined in MPIHelper
Definition: p2pcommunicator.hh:128
int sendLinks() const
return number of processes we will send data to
Definition: p2pcommunicator.hh:183
int recvLink(const int rank) const
return recv link number for a given recv rank number
Definition: p2pcommunicator.hh:199
void insertRequest(const std::set< int > &sendLinks, const std::set< int > &recvLinks)
insert communication request with a set os ranks to send to and a set of ranks to receive from ...
Definition: p2pcommunicator_impl.hh:59
Definition: p2pcommunicator.hh:50
void resetReadPosition()
reset read position of buffer to beginning
Definition: p2pcommunicator.hh:69
void reserve(const size_t size)
reserve memory for 'size' entries
Definition: p2pcommunicator.hh:74
Point2PointCommunicator(const BaseType &comm)
constructor taking collective communication
Definition: p2pcommunicator.hh:176