preCICE v3.1.1
Loading...
Searching...
No Matches
MPICommunication.cpp
Go to the documentation of this file.
1#ifndef PRECICE_NO_MPI
2
3#include <cstddef>
4#include <ostream>
5
7#include "com/MPIRequest.hpp"
10#include "utils/span_tools.hpp"
11
12template <size_t>
14
15template <>
20
21template <>
26
27template <>
32
33template <>
38
39#define MPI_BOOL MPI_Select_unsigned_integer_datatype<sizeof(bool)>::datatype
40
41namespace precice::com {
43
44void MPICommunication::send(std::string const &itemToSend, Rank rankReceiver)
45{
46 PRECICE_TRACE(itemToSend, rankReceiver);
47 rankReceiver = adjustRank(rankReceiver);
48 PRECICE_DEBUG("Message: " + itemToSend);
49 MPI_Send(const_cast<char *>(itemToSend.c_str()),
50 itemToSend.size(),
51 MPI_CHAR,
52 rank(rankReceiver),
53 0,
54 communicator(rankReceiver));
55}
56
58{
59 PRECICE_TRACE(itemsToSend.size());
60 rankReceiver = adjustRank(rankReceiver);
61 MPI_Send(const_cast<int *>(itemsToSend.data()),
62 itemsToSend.size(),
63 MPI_INT,
64 rank(rankReceiver),
65 0,
66 communicator(rankReceiver));
67}
68
70{
71 PRECICE_TRACE(itemsToSend.size());
72 rankReceiver = adjustRank(rankReceiver);
73
74 MPI_Request request;
75 MPI_Isend(const_cast<int *>(itemsToSend.data()),
76 itemsToSend.size(),
77 MPI_INT,
78 rank(rankReceiver),
79 0,
80 communicator(rankReceiver),
81 &request);
82
83 return PtrRequest(new MPIRequest(request));
84}
85
87{
88 PRECICE_TRACE(itemsToSend.size());
89 rankReceiver = adjustRank(rankReceiver);
90 MPI_Send(const_cast<double *>(itemsToSend.data()),
91 itemsToSend.size(),
92 MPI_DOUBLE,
93 rank(rankReceiver),
94 0,
95 communicator(rankReceiver));
96}
97
99{
100 PRECICE_TRACE(itemsToSend.size(), rankReceiver);
101 rankReceiver = adjustRank(rankReceiver);
102
103 MPI_Request request;
104 MPI_Isend(const_cast<double *>(itemsToSend.data()),
105 itemsToSend.size(),
106 MPI_DOUBLE,
107 rank(rankReceiver),
108 0,
109 communicator(rankReceiver),
110 &request);
111
112 return PtrRequest(new MPIRequest(request));
113}
114
115void MPICommunication::send(double itemToSend, Rank rankReceiver)
116{
117 PRECICE_TRACE(itemToSend, rankReceiver);
118 rankReceiver = adjustRank(rankReceiver);
119 MPI_Send(&itemToSend,
120 1,
121 MPI_DOUBLE,
122 rank(rankReceiver),
123 0,
124 communicator(rankReceiver));
125}
126
127PtrRequest MPICommunication::aSend(const double &itemToSend, Rank rankReceiver)
128{
129 return aSend(precice::refToSpan<const double>(itemToSend), rankReceiver);
130}
131
132void MPICommunication::send(int itemToSend, Rank rankReceiver)
133{
134 PRECICE_TRACE(itemToSend, rankReceiver);
135 rankReceiver = adjustRank(rankReceiver);
136 MPI_Send(&itemToSend,
137 1,
138 MPI_INT,
139 rank(rankReceiver),
140 0,
141 communicator(rankReceiver));
142}
143
144PtrRequest MPICommunication::aSend(const int &itemToSend, Rank rankReceiver)
145{
146 return aSend(precice::refToSpan<const int>(itemToSend), rankReceiver);
147}
148
149void MPICommunication::send(bool itemToSend, Rank rankReceiver)
150{
151 PRECICE_TRACE(itemToSend, rankReceiver);
152 rankReceiver = adjustRank(rankReceiver);
153 MPI_Send(&itemToSend,
154 1,
155 MPI_BOOL,
156 rank(rankReceiver),
157 0,
158 communicator(rankReceiver));
159}
160
161PtrRequest MPICommunication::aSend(const bool &itemToSend, Rank rankReceiver)
162{
164 rankReceiver = adjustRank(rankReceiver);
165
166 MPI_Request request;
167 MPI_Isend(const_cast<bool *>(&itemToSend),
168 1,
169 MPI_BOOL,
170 rank(rankReceiver),
171 0,
172 communicator(rankReceiver),
173 &request);
174
175 return PtrRequest(new MPIRequest(request));
176}
177
178void MPICommunication::receive(std::string &itemToReceive, Rank rankSender)
179{
180 PRECICE_TRACE(itemToReceive, rankSender);
181 rankSender = adjustRank(rankSender);
182 int length;
183 MPI_Status status;
184 MPI_Probe(rank(rankSender), 0, communicator(rankSender), &status);
185 MPI_Get_count(&status, MPI_CHAR, &length);
186 PRECICE_DEBUG("Stringlength = {}", length);
187 itemToReceive = std::string(length, '\0');
188 MPI_Recv(const_cast<char *>(itemToReceive.data()),
189 length,
190 MPI_CHAR,
191 rank(rankSender),
192 0,
193 communicator(rankSender),
194 MPI_STATUS_IGNORE);
195 PRECICE_DEBUG("Received \"{}\" from rank {}", itemToReceive, rankSender);
196}
197
198void MPICommunication::receive(precice::span<int> itemsToReceive, Rank rankSender)
199{
200 PRECICE_TRACE(itemsToReceive.size());
201 rankSender = adjustRank(rankSender);
202
203 MPI_Status status;
204 MPI_Recv(itemsToReceive.data(),
205 itemsToReceive.size(),
206 MPI_INT,
207 rank(rankSender),
208 0,
209 communicator(rankSender),
210 &status);
211}
212
214{
215 PRECICE_TRACE(itemsToReceive.size());
216 rankSender = adjustRank(rankSender);
217
218 MPI_Status status;
219 MPI_Recv(itemsToReceive.data(),
220 itemsToReceive.size(),
221 MPI_DOUBLE,
222 rank(rankSender),
223 0,
224 communicator(rankSender),
225 &status);
226}
227
229{
230 PRECICE_TRACE(itemsToReceive.size());
231 rankSender = adjustRank(rankSender);
232
233 MPI_Request request;
234 MPI_Irecv(itemsToReceive.data(),
235 itemsToReceive.size(),
236 MPI_DOUBLE,
237 rank(rankSender),
238 0,
239 communicator(rankSender),
240 &request);
241
242 return PtrRequest(new MPIRequest(request));
243}
244
245void MPICommunication::receive(double &itemToReceive, Rank rankSender)
246{
247 PRECICE_TRACE(rankSender);
248 rankSender = adjustRank(rankSender);
249
250 MPI_Status status;
251 MPI_Recv(&itemToReceive,
252 1,
253 MPI_DOUBLE,
254 rank(rankSender),
255 0,
256 communicator(rankSender),
257 &status);
258 PRECICE_DEBUG("Received {} from rank {}", itemToReceive, rankSender);
259}
260
261PtrRequest MPICommunication::aReceive(double &itemToReceive, Rank rankSender)
262{
263 return aReceive(precice::refToSpan<double>(itemToReceive), rankSender);
264}
265
266void MPICommunication::receive(int &itemToReceive, Rank rankSender)
267{
268 PRECICE_TRACE(rankSender);
269 rankSender = adjustRank(rankSender);
270
271 MPI_Status status;
272 MPI_Recv(&itemToReceive,
273 1,
274 MPI_INT,
275 rank(rankSender),
276 0,
277 communicator(rankSender),
278 &status);
279 PRECICE_DEBUG("Received {} from rank {}", itemToReceive, rankSender);
280}
281
282PtrRequest MPICommunication::aReceive(int &itemToReceive, Rank rankSender)
283{
284 PRECICE_TRACE(rankSender);
285 rankSender = adjustRank(rankSender);
286
287 MPI_Request request;
288 MPI_Irecv(&itemToReceive,
289 1,
290 MPI_INT,
291 rank(rankSender),
292 0,
293 communicator(rankSender),
294 &request);
295
296 return PtrRequest(new MPIRequest(request));
297}
298
299void MPICommunication::receive(bool &itemToReceive, Rank rankSender)
300{
301 PRECICE_TRACE(rankSender);
302 rankSender = adjustRank(rankSender);
303
304 MPI_Status status;
305 MPI_Recv(&itemToReceive,
306 1,
307 MPI_BOOL,
308 rank(rankSender),
309 0,
310 communicator(rankSender),
311 &status);
312 PRECICE_DEBUG("Received {} from rank {}", itemToReceive, rankSender);
313}
314
315PtrRequest MPICommunication::aReceive(bool &itemToReceive, Rank rankSender)
316{
317 PRECICE_TRACE(rankSender);
318 rankSender = adjustRank(rankSender);
319
320 MPI_Request request;
321 MPI_Irecv(&itemToReceive,
322 1,
323 MPI_BOOL,
324 rank(rankSender),
325 0,
326 communicator(rankSender),
327 &request);
328
329 return PtrRequest(new MPIRequest(request));
330}
331
332} // namespace precice::com
333
334#endif // not PRECICE_NO_MPI
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:64
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
#define MPI_BOOL
T c_str(T... args)
virtual int adjustRank(Rank rank) const
Adjusts the given rank bases on the _rankOffset.
virtual void receive(std::string &itemToReceive, Rank rankSender) override
Receives a std::string from process with given rank.
virtual PtrRequest aReceive(precice::span< double > itemsToReceive, int rankSender) override
Asynchronously receives an array of double values.
virtual MPI_Comm & communicator(Rank rank)=0
Returns the communicator.
virtual PtrRequest aSend(precice::span< const int > itemsToSend, Rank rankReceiver) override
Asynchronously sends an array of integer values.
virtual void send(std::string const &itemToSend, Rank rankReceiver) override
Sends a std::string to process with given rank.
virtual Rank rank(int rank)=0
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition span.hpp:284
constexpr pointer data() const noexcept
Definition span.hpp:500
constexpr size_type size() const noexcept
Definition span.hpp:469
T data(T... args)
contains the data communication abstraction layer.
std::shared_ptr< Request > PtrRequest
int Rank
Definition Types.hpp:37
T size(T... args)