preCICE v3.1.2
Loading...
Searching...
No Matches
Communication.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <set>
4#include <stddef.h>
5#include <string>
6#include <vector>
7
8#include "boost/range/irange.hpp"
9#include "com/Request.hpp"
10#include "com/SharedPointer.hpp"
11#include "logging/Logger.hpp"
13#include "precice/span.hpp"
14
15namespace precice {
16namespace com {
17
21template <typename T>
23};
24
25/* TODO When moving to C++17 use inline variable:
26 *
27 * template<typename T>
28 * inline constexpr auto asVector = Communication::AsVectorTag<T>{};
29 */
30
66
67public:
69
72 {
73 }
74
77
79 virtual bool isConnected()
80 {
81 return _isConnected;
82 }
83
89 virtual size_t getRemoteCommunicatorSize() = 0;
90
99 {
100 return boost::irange<Rank>(0, static_cast<Rank>(getRemoteCommunicatorSize()));
101 }
102
117 virtual void acceptConnection(std::string const &acceptorName,
118 std::string const &requesterName,
119 std::string const &tag,
120 int acceptorRank,
121 int rankOffset = 0) = 0;
122
138 virtual void acceptConnectionAsServer(std::string const &acceptorName,
139 std::string const &requesterName,
140 std::string const &tag,
141 int acceptorRank,
142 int requesterCommunicatorSize) = 0;
143
159 virtual void requestConnection(std::string const &acceptorName,
160 std::string const &requesterName,
161 std::string const &tag,
162 int requesterRank,
163 int requesterCommunicatorSize) = 0;
164
179 virtual void requestConnectionAsClient(std::string const & acceptorName,
180 std::string const & requesterName,
181 std::string const & tag,
182 std::set<int> const &acceptorRanks,
183 int requesterRank) = 0;
184
193 void connectIntraComm(std::string const &participantName,
194 std::string const &tag,
195 int rank,
196 int size);
197
203 virtual void closeConnection() = 0;
204
211 virtual void prepareEstablishment(std::string const &acceptorName,
212 std::string const &requesterName) {}
213
220 virtual void cleanupEstablishment(std::string const &acceptorName,
221 std::string const &requesterName) {}
222
224
227
229 virtual void reduceSum(precice::span<double const> itemsToSend, precice::span<double> itemsToReceive, Rank primaryRank);
231 virtual void reduceSum(precice::span<double const> itemsToSend, precice::span<double> itemsToReceive);
232
233 virtual void reduceSum(int itemToSend, int &itemToReceive, Rank primaryRank);
234 virtual void reduceSum(int itemsToSend, int &itemsToReceive);
235
236 virtual void allreduceSum(precice::span<double const> itemsToSend, precice::span<double> itemsToReceive, Rank primaryRank);
237 virtual void allreduceSum(precice::span<double const> itemsToSend, precice::span<double> itemsToReceive);
238
239 virtual void allreduceSum(double itemToSend, double &itemToReceive, Rank primaryRank);
240 virtual void allreduceSum(double itemToSend, double &itemToReceive);
241
242 virtual void allreduceSum(int itemToSend, int &itemToReceive, Rank primaryRank);
243 virtual void allreduceSum(int itemToSend, int &itemToReceive);
244
246
249
250 virtual void broadcast(precice::span<const int> itemsToSend);
251 virtual void broadcast(precice::span<int> itemsToReceive, Rank rankBroadcaster);
252
253 virtual void broadcast(int itemToSend);
254 virtual void broadcast(int &itemToReceive, Rank rankBroadcaster);
255
256 virtual void broadcast(precice::span<const double> itemsToSend);
257 virtual void broadcast(precice::span<double> itemsToReceive, Rank rankBroadcaster);
258
259 virtual void broadcast(double itemToSend);
260 virtual void broadcast(double &itemToReceive, Rank rankBroadcaster);
261
262 virtual void broadcast(bool itemToSend);
263 virtual void broadcast(bool &itemToReceive, Rank rankBroadcaster);
264
265 virtual void broadcast(std::vector<int> const &v);
266 virtual void broadcast(std::vector<int> &v, Rank rankBroadcaster);
267
268 virtual void broadcast(std::vector<double> const &v);
269 virtual void broadcast(std::vector<double> &v, Rank rankBroadcaster);
270
272
275
277 virtual void send(std::string const &itemToSend, Rank rankReceiver) = 0;
278
280 virtual void send(precice::span<const int> itemsToSend, Rank rankReceiver) = 0;
281
284 virtual PtrRequest aSend(precice::span<const int> itemsToSend, Rank rankReceiver) = 0;
285
287 virtual void send(precice::span<const double> itemsToSend, Rank rankReceiver) = 0;
288
291 virtual PtrRequest aSend(precice::span<const double> itemsToSend, Rank rankReceiver) = 0;
292
294 virtual void send(double itemToSend, Rank rankReceiver) = 0;
295
298 virtual PtrRequest aSend(const double &itemToSend, Rank rankReceiver) = 0;
299
301 virtual void send(int itemToSend, Rank rankReceiver) = 0;
302
305 virtual PtrRequest aSend(const int &itemToSend, Rank rankReceiver) = 0;
306
308 virtual void send(bool itemToSend, Rank rankReceiver) = 0;
309
312 virtual PtrRequest aSend(const bool &itemToSend, Rank rankReceiver) = 0;
313
315
318
320 virtual void receive(std::string &itemToReceive, Rank rankSender) = 0;
321
323 virtual void receive(precice::span<int> itemsToReceive, Rank rankSender) = 0;
324
326 virtual void receive(precice::span<double> itemsToReceive, Rank rankSender) = 0;
327
329 virtual PtrRequest aReceive(precice::span<double> itemsToReceive, int rankSender) = 0;
330
332 virtual void receive(double &itemToReceive, Rank rankSender) = 0;
333
335 virtual PtrRequest aReceive(double &itemToReceive, Rank rankSender) = 0;
336
338 virtual void receive(int &itemToReceive, Rank rankSender) = 0;
339
341 virtual PtrRequest aReceive(int &itemToReceive, Rank rankSender) = 0;
342
344 virtual void receive(bool &itemToReceive, Rank rankSender) = 0;
345
347 virtual PtrRequest aReceive(bool &itemToReceive, Rank rankSender) = 0;
348
350
353
355 void sendRange(precice::span<const double> itemsToSend, Rank rankReceiver);
356
358 void sendRange(precice::span<const int> itemsToSend, Rank rankReceiver);
359
362
365
367
369 void setRankOffset(Rank rankOffset)
370 {
371 _rankOffset = rankOffset;
372 }
373
374protected:
376 int _rankOffset = 0;
377
378 bool _isConnected = false;
379
381 virtual int adjustRank(Rank rank) const;
382
383private:
384 logging::Logger _log{"com::Communication"};
385};
386
393 std::string const & participantName,
394 std::string const & tag,
395 int rank,
396 int size,
397 com::Communication &left,
398 com::Communication &right);
399
400} // namespace com
401} // namespace precice
Interface for all interprocess communication classes.
virtual void receive(bool &itemToReceive, Rank rankSender)=0
Receives a bool from process with given rank.
virtual void cleanupEstablishment(std::string const &acceptorName, std::string const &requesterName)
Clean-up environment used to establish the communication.
virtual void receive(double &itemToReceive, Rank rankSender)=0
Receives a double from process with given rank.
virtual void receive(int &itemToReceive, Rank rankSender)=0
Receives an int from process with given rank.
virtual PtrRequest aSend(const double &itemToSend, Rank rankReceiver)=0
virtual PtrRequest aSend(precice::span< const int > itemsToSend, Rank rankReceiver)=0
virtual void receive(std::string &itemToReceive, Rank rankSender)=0
Receives a std::string from process with given rank.
virtual void requestConnectionAsClient(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, std::set< int > const &acceptorRanks, int requesterRank)=0
Connects to another communicator, which has to call acceptConnectionAsServer().
void setRankOffset(Rank rankOffset)
Set rank offset.
virtual void send(precice::span< const int > itemsToSend, Rank rankReceiver)=0
Sends an array of integer values.
std::vector< int > receiveRange(Rank rankSender, AsVectorTag< int >)
Receives a range of ints as a vector<int>
void connectIntraComm(std::string const &participantName, std::string const &tag, int rank, int size)
auto remoteCommunicatorRanks()
Returns a range over all valid remote ranks.
virtual void prepareEstablishment(std::string const &acceptorName, std::string const &requesterName)
Prepare environment used to establish the communication.
virtual void send(int itemToSend, Rank rankReceiver)=0
Sends an int to process with given rank.
virtual bool isConnected()
Returns true, if a connection to a remote participant has been setup.
virtual size_t getRemoteCommunicatorSize()=0
Returns the number of processes in the remote communicator.
virtual void acceptConnectionAsServer(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int acceptorRank, int requesterCommunicatorSize)=0
Accepts connection from another communicator, which has to call requestConnectionAsClient().
virtual void acceptConnection(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int acceptorRank, int rankOffset=0)=0
Accepts connection from another communicator, which has to call requestConnection().
Communication & operator=(Communication &&)=delete
virtual PtrRequest aReceive(double &itemToReceive, Rank rankSender)=0
Asynchronously receives a double from process with given rank.
int _rankOffset
Rank offset for primaries-secondary communication, since ranks are from 0 to size-2.
virtual ~Communication()
Destructor, empty.
virtual PtrRequest aSend(precice::span< const double > itemsToSend, Rank rankReceiver)=0
virtual void send(precice::span< const double > itemsToSend, Rank rankReceiver)=0
Sends an array of double values.
virtual void send(bool itemToSend, Rank rankReceiver)=0
Sends a bool to process with given rank.
void sendRange(precice::span< const double > itemsToSend, Rank rankReceiver)
Sends a range of doubles (size + content)
virtual int adjustRank(Rank rank) const
Adjusts the given rank bases on the _rankOffset.
virtual void receive(precice::span< int > itemsToReceive, Rank rankSender)=0
Receives an array of integer values.
virtual PtrRequest aReceive(bool &itemToReceive, Rank rankSender)=0
Asynchronously receives a bool from process with given rank.
virtual void requestConnection(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int requesterRank, int requesterCommunicatorSize)=0
Connects to another communicator, which has to call acceptConnection().
virtual void receive(precice::span< double > itemsToReceive, Rank rankSender)=0
Receives an array of double values.
virtual PtrRequest aReceive(precice::span< double > itemsToReceive, int rankSender)=0
Asynchronously receives an array of double values.
virtual void send(double itemToSend, Rank rankReceiver)=0
Sends a double to process with given rank.
virtual PtrRequest aSend(const int &itemToSend, Rank rankReceiver)=0
virtual void allreduceSum(precice::span< double const > itemsToSend, precice::span< double > itemsToReceive, Rank primaryRank)
virtual PtrRequest aSend(const bool &itemToSend, Rank rankReceiver)=0
virtual void send(std::string const &itemToSend, Rank rankReceiver)=0
Sends a std::string to process with given rank.
virtual void closeConnection()=0
Disconnects from communication space, i.e. participant.
virtual void reduceSum(precice::span< double const > itemsToSend, precice::span< double > itemsToReceive, Rank primaryRank)
Performs a reduce summation on the rank given by primaryRank.
virtual PtrRequest aReceive(int &itemToReceive, Rank rankSender)=0
Asynchronously receives an int from process with given rank.
virtual void broadcast(precice::span< const int > itemsToSend)
This class provides a lightweight logger.
Definition Logger.hpp:16
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition span.hpp:284
void connectCircularComm(std::string const &participantName, std::string const &tag, int rank, int size, com::Communication &left, com::Communication &right)
Main namespace of the precice library.
int Rank
Definition Types.hpp:37