Newer
Older

Christian Godenschwager
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file BufferSystemHelper.h
//! \ingroup core
//! \author Martin Bauer <martin.bauer@fau.de>
//
//======================================================================================================================
#pragma once
#include "MPIWrapper.h"
#include "RecvBuffer.h"
#include "SendBuffer.h"
#include "core/DataTypes.h"
#include <list>
#include <map>
#include <vector>
namespace walberla {
namespace mpi {
namespace internal {
class AbstractCommunication
{
public:
AbstractCommunication( const MPI_Comm & communicator, int tag = 0 )
: communicator_( communicator), tag_(tag)
{}
virtual ~AbstractCommunication() {}
struct ReceiveInfo {
RecvBuffer buffer;
MPISize size;
};
/*************************************************************************************************************//**
* Send buffer content to receiver using MPI_ISend, request is stored internally -> see waitForSends()
*****************************************************************************************************************/
virtual void send( MPIRank rank, const SendBuffer & sendBuffer ) = 0;
/*************************************************************************************************************//**
* Wait for all sends to complete.
*****************************************************************************************************************/
virtual void waitForSends() = 0;
/*************************************************************************************************************//**
* Executes MPI_Irecv calls, with the recvInfos as target memory.
*
* \param recvInfos Map that has entries for all ranks where messages are expected.
* The KnownSizeCommunication also expects the size field to be valid ( defining the expected
* message size)
* Do not change/read the recvInfos after scheduleReceive() call
* and before waitForNextReceive() is completed
*
*****************************************************************************************************************/
virtual void scheduleReceives ( std::map<MPIRank, ReceiveInfo> & recvInfos ) = 0;
/*************************************************************************************************************//**
* Waits for the next receive to complete and returns.
*
* \param recvInfo the same receive info that was passed to scheduleReceives()
*
* \return The rank where the data was received -> recvInfos[rank] is now valid
* INVALID_RANK if all messages were received.
*****************************************************************************************************************/
virtual MPIRank waitForNextReceive( std::map<MPIRank, ReceiveInfo> & recvInfos ) = 0;
virtual int getTag() const { return tag_; }
virtual MPI_Comm getCommunicator() const { return communicator_; }

Christian Godenschwager
committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
protected:
MPI_Comm communicator_;
int tag_;
};
class KnownSizeCommunication : public AbstractCommunication
{
public:
KnownSizeCommunication( const MPI_Comm & communicator, int tag = 0 )
: AbstractCommunication( communicator, tag ), sending_(false), receiving_(false) {}
virtual ~KnownSizeCommunication() {}
virtual void send( MPIRank receiver, const SendBuffer & sendBuffer );
virtual void waitForSends();
virtual void scheduleReceives ( std::map<MPIRank, ReceiveInfo> & recvInfos );
/// size field of recvInfos is expected to be valid
virtual MPIRank waitForNextReceive( std::map<MPIRank, ReceiveInfo> & recvInfos );
private:
bool sending_;
bool receiving_;
std::vector<MPI_Request> sendRequests_;
std::vector<MPI_Request> recvRequests_;
};
class UnknownSizeCommunication : public AbstractCommunication
{
public:
UnknownSizeCommunication( const MPI_Comm & communicator, int tag = 0 )
: AbstractCommunication( communicator, tag ), sending_(false), receiving_(false) {}
virtual ~UnknownSizeCommunication() {}
virtual void send( MPIRank receiver, const SendBuffer & sendBuffer );
virtual void waitForSends();
virtual void scheduleReceives( std::map<MPIRank, ReceiveInfo> & recvInfos );

Christian Godenschwager
committed
/// size field of recvInfos can be invalid, is filled in with the actual message size
virtual MPIRank waitForNextReceive( std::map<MPIRank, ReceiveInfo> & recvInfos );
private:
bool sending_;
bool receiving_;
std::vector<MPI_Request> sendRequests_;
std::list<MPISize> outgoingBufferForSizes_;
std::vector<MPI_Request> recvRequests_;
std::vector<bool> sizeAlreadyReceived_;
};

Christian Godenschwager
committed
class NoMPICommunication : public AbstractCommunication
{
public:
NoMPICommunication( const MPI_Comm & communicator, int tag = 0 )
: AbstractCommunication( communicator, tag ), received_( false ) {}
virtual ~NoMPICommunication() {}
virtual void send( MPIRank receiver, const SendBuffer & sendBuffer );
virtual void waitForSends();
virtual void scheduleReceives( std::map<MPIRank, ReceiveInfo> & recvInfos );

Christian Godenschwager
committed
/// size field of recvInfos can be invalid, is filled in with the actual message size
virtual MPIRank waitForNextReceive( std::map<MPIRank, ReceiveInfo> & recvInfos );
private:
bool received_;
RecvBuffer tmpBuffer_;
};
} // namespace internal
} // namespace mpi
} // namespace walberla