preCICE v3.1.2
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
precice::com::SocketCommunication Class Reference

Implements Communication by using sockets. More...

#include <SocketCommunication.hpp>

Inheritance diagram for precice::com::SocketCommunication:
[legend]
Collaboration diagram for precice::com::SocketCommunication:
[legend]

Public Member Functions

 SocketCommunication (unsigned short portNumber=0, bool reuseAddress=false, std::string networkName=utils::networking::loopbackInterfaceName(), std::string addressDirectory=".")
 
 SocketCommunication (std::string const &addressDirectory)
 
virtual ~SocketCommunication ()
 
virtual size_t getRemoteCommunicatorSize () override
 Returns the number of processes in the remote communicator.
 
virtual void acceptConnection (std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int acceptorRank, int rankOffset=0) override
 Accepts connection from another communicator, which has to call requestConnection().
 
virtual void acceptConnectionAsServer (std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int acceptorRank, int requesterCommunicatorSize) override
 Accepts connection from another communicator, which has to call requestConnectionAsClient().
 
virtual void requestConnection (std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int requesterRank, int requesterCommunicatorSize) override
 Connects to another communicator, which has to call acceptConnection().
 
virtual void requestConnectionAsClient (std::string const &acceptorName, std::string const &requesterName, std::string const &tag, std::set< int > const &acceptorRanks, int requesterRank) override
 Connects to another communicator, which has to call acceptConnectionAsServer().
 
virtual void closeConnection () override
 Disconnects from communication space, i.e. participant.
 
virtual void send (std::string const &itemToSend, Rank rankReceiver) override
 Sends a std::string to process with given rank.
 
virtual void send (precice::span< const int > itemsToSend, Rank rankReceiver) override
 Sends an array of integer values.
 
virtual PtrRequest aSend (precice::span< const int > itemsToSend, Rank rankReceiver) override
 Asynchronously sends an array of integer values.
 
virtual void send (precice::span< const double > itemsToSend, Rank rankReceiver) override
 Sends an array of double values.
 
virtual PtrRequest aSend (precice::span< const double > itemsToSend, Rank rankReceiver) override
 Asynchronously sends an array of double values.
 
virtual void send (double itemToSend, Rank rankReceiver) override
 Sends a double to process with given rank.
 
virtual PtrRequest aSend (const double &itemToSend, Rank rankReceiver) override
 Asynchronously sends a double to process with given rank.
 
virtual void send (int itemToSend, Rank rankReceiver) override
 Sends an int to process with given rank.
 
virtual PtrRequest aSend (const int &itemToSend, Rank rankReceiver) override
 Asynchronously sends an int to process with given rank.
 
virtual void send (bool itemToSend, Rank rankReceiver) override
 Sends a bool to process with given rank.
 
virtual PtrRequest aSend (const bool &itemToSend, Rank rankReceiver) override
 Asynchronously sends a bool to process with given rank.
 
virtual void receive (std::string &itemToReceive, Rank rankSender) override
 Receives a std::string from process with given rank.
 
virtual void receive (precice::span< int > itemsToReceive, Rank rankSender) override
 Receives an array of integer values.
 
virtual void receive (precice::span< double > itemsToReceive, Rank rankSender) override
 Receives an array of double values.
 
virtual PtrRequest aReceive (precice::span< double > itemsToReceive, int rankSender) override
 Asynchronously receives an array of double values.
 
virtual void receive (double &itemToReceive, Rank rankSender) override
 Receives a double from process with given rank.
 
virtual PtrRequest aReceive (double &itemToReceive, Rank rankSender) override
 Asynchronously receives a double from process with given rank.
 
virtual void receive (int &itemToReceive, Rank rankSender) override
 Receives an int from process with given rank.
 
virtual PtrRequest aReceive (int &itemToReceive, Rank rankSender) override
 Asynchronously receives an int from process with given rank.
 
virtual void receive (bool &itemToReceive, Rank rankSender) override
 Receives a bool from process with given rank.
 
virtual PtrRequest aReceive (bool &itemToReceive, Rank rankSender) override
 Asynchronously receives a bool from process with given rank.
 
virtual void prepareEstablishment (std::string const &acceptorName, std::string const &requesterName) override
 Prepare environment used to establish the communication.
 
virtual void cleanupEstablishment (std::string const &acceptorName, std::string const &requesterName) override
 Clean-up environment used to establish the communication.
 
- Public Member Functions inherited from precice::com::Communication
Communicationoperator= (Communication &&)=delete
 
virtual ~Communication ()
 Destructor, empty.
 
void setRankOffset (Rank rankOffset)
 Set rank offset.
 
virtual bool isConnected ()
 Returns true, if a connection to a remote participant has been setup.
 
auto remoteCommunicatorRanks ()
 Returns a range over all valid remote ranks.
 
void connectIntraComm (std::string const &participantName, std::string const &tag, int rank, int size)
 
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 void reduceSum (precice::span< double const > itemsToSend, precice::span< double > itemsToReceive)
 Performs a reduce summation on the primary rank, every other rank has to call reduceSum.
 
virtual void reduceSum (int itemToSend, int &itemToReceive, Rank primaryRank)
 
virtual void reduceSum (int itemsToSend, int &itemsToReceive)
 
virtual void allreduceSum (precice::span< double const > itemsToSend, precice::span< double > itemsToReceive, Rank primaryRank)
 
virtual void allreduceSum (precice::span< double const > itemsToSend, precice::span< double > itemsToReceive)
 
virtual void allreduceSum (double itemToSend, double &itemToReceive, Rank primaryRank)
 
virtual void allreduceSum (double itemToSend, double &itemToReceive)
 
virtual void allreduceSum (int itemToSend, int &itemToReceive, Rank primaryRank)
 
virtual void allreduceSum (int itemToSend, int &itemToReceive)
 
virtual void broadcast (precice::span< const int > itemsToSend)
 
virtual void broadcast (precice::span< int > itemsToReceive, Rank rankBroadcaster)
 
virtual void broadcast (int itemToSend)
 
virtual void broadcast (int &itemToReceive, Rank rankBroadcaster)
 
virtual void broadcast (precice::span< const double > itemsToSend)
 
virtual void broadcast (precice::span< double > itemsToReceive, Rank rankBroadcaster)
 
virtual void broadcast (double itemToSend)
 
virtual void broadcast (double &itemToReceive, Rank rankBroadcaster)
 
virtual void broadcast (bool itemToSend)
 
virtual void broadcast (bool &itemToReceive, Rank rankBroadcaster)
 
virtual void broadcast (std::vector< int > const &v)
 
virtual void broadcast (std::vector< int > &v, Rank rankBroadcaster)
 
virtual void broadcast (std::vector< double > const &v)
 
virtual void broadcast (std::vector< double > &v, Rank rankBroadcaster)
 
void sendRange (precice::span< const double > itemsToSend, Rank rankReceiver)
 Sends a range of doubles (size + content)
 
void sendRange (precice::span< const int > itemsToSend, Rank rankReceiver)
 Sends a range of ints (size + content)
 
std::vector< int > receiveRange (Rank rankSender, AsVectorTag< int >)
 Receives a range of ints as a vector<int>
 
std::vector< double > receiveRange (Rank rankSender, AsVectorTag< double >)
 Receives a range of doubles as a vector<double>
 

Private Types

using IOService = boost::asio::io_service
 
using Socket = boost::asio::ip::tcp::socket
 
using Work = boost::asio::io_service::work
 

Private Member Functions

bool isClient ()
 
bool isServer ()
 
std::string getIpAddress ()
 

Private Attributes

logging::Logger _log {"com::SocketCommunication"}
 
unsigned short _portNumber
 Port used for socket connection.
 
bool _reuseAddress
 
std::string _networkName
 Name of network to communicate over.
 
std::string _addressDirectory
 Directory where IP address is exchanged by file.
 
std::shared_ptr< IOService_ioService
 
std::shared_ptr< Work_work
 
std::thread _thread
 
std::map< int, std::shared_ptr< Socket > > _sockets
 Remote rank -> socket map.
 
SocketSendQueue _queue
 

Additional Inherited Members

- Protected Member Functions inherited from precice::com::Communication
virtual int adjustRank (Rank rank) const
 Adjusts the given rank bases on the _rankOffset.
 
- Protected Attributes inherited from precice::com::Communication
int _rankOffset = 0
 Rank offset for primaries-secondary communication, since ranks are from 0 to size-2.
 
bool _isConnected = false
 

Detailed Description

Implements Communication by using sockets.

Definition at line 21 of file SocketCommunication.hpp.

Member Typedef Documentation

◆ IOService

using precice::com::SocketCommunication::IOService = boost::asio::io_service
private

Definition at line 144 of file SocketCommunication.hpp.

◆ Socket

using precice::com::SocketCommunication::Socket = boost::asio::ip::tcp::socket
private

Definition at line 145 of file SocketCommunication.hpp.

◆ Work

using precice::com::SocketCommunication::Work = boost::asio::io_service::work
private

Definition at line 146 of file SocketCommunication.hpp.

Constructor & Destructor Documentation

◆ SocketCommunication() [1/2]

precice::com::SocketCommunication::SocketCommunication ( unsigned short portNumber = 0,
bool reuseAddress = false,
std::string networkName = utils::networking::loopbackInterfaceName(),
std::string addressDirectory = "." )

Definition at line 22 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ SocketCommunication() [2/2]

precice::com::SocketCommunication::SocketCommunication ( std::string const & addressDirectory)
explicit

Definition at line 37 of file SocketCommunication.cpp.

◆ ~SocketCommunication()

precice::com::SocketCommunication::~SocketCommunication ( )
virtual

Definition at line 42 of file SocketCommunication.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ acceptConnection()

void precice::com::SocketCommunication::acceptConnection ( std::string const & acceptorName,
std::string const & requesterName,
std::string const & tag,
int acceptorRank,
int rankOffset = 0 )
overridevirtual

Accepts connection from another communicator, which has to call requestConnection().

Establishes a 1-to-N communication, whereas the acceptor's side is the "1". Contrary to acceptConnectionAsServer(), the other side needs to be a proper communicator with ranks from 0 to N-1. It is not necessary to know this "N" a-priori on the acceptor's side. This communication is used for the 1:1 communication between two primary ranks and for the intra-participant communication. For the last case, setRankOffset() has to be set.

Parameters
[in]acceptorNameName of calling participant.
[in]requesterNameName of remote participant to connect to.
[in]tagTag for establishing this connection
[in]acceptorRankRank of the accpeting process, usually the calling one.

Implements precice::com::Communication.

Definition at line 55 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ acceptConnectionAsServer()

void precice::com::SocketCommunication::acceptConnectionAsServer ( std::string const & acceptorName,
std::string const & requesterName,
std::string const & tag,
int acceptorRank,
int requesterCommunicatorSize )
overridevirtual

Accepts connection from another communicator, which has to call requestConnectionAsClient().

Establishes a 1-to-N communication, whereas the acceptor's side is the "1". Contrary to acceptConnection(), the other side can have arbitrary ranks. However, we need to know its size "N" a-priori. This communication is only used in PointToPointCommunication, i.e. for the M-to-N communication between two participants.

Parameters
[in]acceptorNameName of calling participant.
[in]requesterNameName of remote participant to connect to.
[in]tagTag for establishing this connection
[in]acceptorRankRank of accepting server, usually the rank of the current process.
[in]requesterCommunicatorSizeSize of the requester (N)

Implements precice::com::Communication.

Definition at line 137 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aReceive() [1/4]

PtrRequest precice::com::SocketCommunication::aReceive ( bool & itemToReceive,
Rank rankSender )
overridevirtual

Asynchronously receives a bool from process with given rank.

Implements precice::com::Communication.

Definition at line 690 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aReceive() [2/4]

PtrRequest precice::com::SocketCommunication::aReceive ( double & itemToReceive,
Rank rankSender )
overridevirtual

Asynchronously receives a double from process with given rank.

Implements precice::com::Communication.

Definition at line 628 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aReceive() [3/4]

PtrRequest precice::com::SocketCommunication::aReceive ( int & itemToReceive,
Rank rankSender )
overridevirtual

Asynchronously receives an int from process with given rank.

Implements precice::com::Communication.

Definition at line 649 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aReceive() [4/4]

PtrRequest precice::com::SocketCommunication::aReceive ( precice::span< double > itemsToReceive,
int rankSender )
overridevirtual

Asynchronously receives an array of double values.

Implements precice::com::Communication.

Definition at line 587 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aSend() [1/5]

PtrRequest precice::com::SocketCommunication::aSend ( const bool & itemToSend,
Rank rankReceiver )
overridevirtual

Asynchronously sends a bool to process with given rank.

Implements precice::com::Communication.

Definition at line 515 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aSend() [2/5]

PtrRequest precice::com::SocketCommunication::aSend ( const double & itemToSend,
Rank rankReceiver )
overridevirtual

Asynchronously sends a double to process with given rank.

Implements precice::com::Communication.

Definition at line 473 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aSend() [3/5]

PtrRequest precice::com::SocketCommunication::aSend ( const int & itemToSend,
Rank rankReceiver )
overridevirtual

Asynchronously sends an int to process with given rank.

Implements precice::com::Communication.

Definition at line 494 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aSend() [4/5]

PtrRequest precice::com::SocketCommunication::aSend ( precice::span< const double > itemsToSend,
Rank rankReceiver )
overridevirtual

Asynchronously sends an array of double values.

Implements precice::com::Communication.

Definition at line 438 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ aSend() [5/5]

PtrRequest precice::com::SocketCommunication::aSend ( precice::span< const int > itemsToSend,
Rank rankReceiver )
overridevirtual

Asynchronously sends an array of integer values.

Implements precice::com::Communication.

Definition at line 403 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ cleanupEstablishment()

void precice::com::SocketCommunication::cleanupEstablishment ( std::string const & acceptorName,
std::string const & requesterName )
overridevirtual

Clean-up environment used to establish the communication.

Parameters
[in]acceptorNameName of calling participant.
[in]requesterNameName of remote participant to connect to.

Reimplemented from precice::com::Communication.

Definition at line 390 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ closeConnection()

void precice::com::SocketCommunication::closeConnection ( )
overridevirtual

Disconnects from communication space, i.e. participant.

This method is called on destruction.

Implements precice::com::Communication.

Definition at line 316 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ getIpAddress()

std::string precice::com::SocketCommunication::getIpAddress ( )
private

Definition at line 765 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ getRemoteCommunicatorSize()

size_t precice::com::SocketCommunication::getRemoteCommunicatorSize ( )
overridevirtual

Returns the number of processes in the remote communicator.

Precondition
A connection to the remote participant has been set up.

Implements precice::com::Communication.

Definition at line 48 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ isClient()

bool precice::com::SocketCommunication::isClient ( )
private

◆ isServer()

bool precice::com::SocketCommunication::isServer ( )
private

◆ prepareEstablishment()

void precice::com::SocketCommunication::prepareEstablishment ( std::string const & acceptorName,
std::string const & requesterName )
overridevirtual

Prepare environment used to establish the communication.

Parameters
[in]acceptorNameName of calling participant.
[in]requesterNameName of remote participant to connect to.

Reimplemented from precice::com::Communication.

Definition at line 377 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ receive() [1/6]

void precice::com::SocketCommunication::receive ( bool & itemToReceive,
Rank rankSender )
overridevirtual

Receives a bool from process with given rank.

Implements precice::com::Communication.

Definition at line 674 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ receive() [2/6]

void precice::com::SocketCommunication::receive ( double & itemToReceive,
Rank rankSender )
overridevirtual

Receives a double from process with given rank.

Implements precice::com::Communication.

Definition at line 612 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ receive() [3/6]

void precice::com::SocketCommunication::receive ( int & itemToReceive,
Rank rankSender )
overridevirtual

Receives an int from process with given rank.

Implements precice::com::Communication.

Definition at line 633 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ receive() [4/6]

void precice::com::SocketCommunication::receive ( precice::span< double > itemsToReceive,
Rank rankSender )
overridevirtual

Receives an array of double values.

Implements precice::com::Communication.

Definition at line 571 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ receive() [5/6]

void precice::com::SocketCommunication::receive ( precice::span< int > itemsToReceive,
Rank rankSender )
overridevirtual

Receives an array of integer values.

Implements precice::com::Communication.

Definition at line 555 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ receive() [6/6]

void precice::com::SocketCommunication::receive ( std::string & itemToReceive,
Rank rankSender )
overridevirtual

Receives a std::string from process with given rank.

Implements precice::com::Communication.

Definition at line 534 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ requestConnection()

void precice::com::SocketCommunication::requestConnection ( std::string const & acceptorName,
std::string const & requesterName,
std::string const & tag,
int requesterRank,
int requesterCommunicatorSize )
overridevirtual

Connects to another communicator, which has to call acceptConnection().

Establishes a 1-to-N communication, whereas the requestor's side is the "N". Contrary to requestConnectionAsClient(), this side needs to be a proper communicator with ranks from 0 to N-1. All ranks need to call this function. This communication is used for the 1:1 communication between two primary ranks, and for the intra-participant communication.

Parameters
[in]acceptorNameName of remote participant to connect to.
[in]requesterNameName of calling participant.
[in]tagTag for establishing this connection
[in]requesterRankRank of the requester (has to go from 0 to N-1)
[in]requesterCommunicatorSizeSize of the requester (N)

Implements precice::com::Communication.

Definition at line 200 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ requestConnectionAsClient()

void precice::com::SocketCommunication::requestConnectionAsClient ( std::string const & acceptorName,
std::string const & requesterName,
std::string const & tag,
std::set< int > const & acceptorRanks,
int requesterRank )
overridevirtual

Connects to another communicator, which has to call acceptConnectionAsServer().

Establishes a 1-to-N communication, whereas the requestor's side is the "N". Contrary to requestConnection(), this side can have arbitrary ranks (e.g. 2,3,7). All ranks need to call this function. This communication is only used in PointToPointCommunication, i.e. for the M-to-N communication between two participants.

Parameters
[in]acceptorNameName of calling participant.
[in]requesterNameName of remote participant to connect to
[in]tagTag for establishing this connection
[in]acceptorRanksSet of ranks that accept a connection
[in]requesterRankRank that requests the connection, usually the caller's rank

Implements precice::com::Communication.

Definition at line 259 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ send() [1/6]

void precice::com::SocketCommunication::send ( bool itemToSend,
Rank rankReceiver )
overridevirtual

Sends a bool to process with given rank.

Implements precice::com::Communication.

Definition at line 499 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ send() [2/6]

void precice::com::SocketCommunication::send ( double itemToSend,
Rank rankReceiver )
overridevirtual

Sends a double to process with given rank.

Implements precice::com::Communication.

Definition at line 457 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ send() [3/6]

void precice::com::SocketCommunication::send ( int itemToSend,
Rank rankReceiver )
overridevirtual

Sends an int to process with given rank.

Implements precice::com::Communication.

Definition at line 478 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ send() [4/6]

void precice::com::SocketCommunication::send ( precice::span< const double > itemsToSend,
Rank rankReceiver )
overridevirtual

Sends an array of double values.

Implements precice::com::Communication.

Definition at line 422 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ send() [5/6]

void precice::com::SocketCommunication::send ( precice::span< const int > itemsToSend,
Rank rankReceiver )
overridevirtual

Sends an array of integer values.

Implements precice::com::Communication.

Definition at line 361 of file SocketCommunication.cpp.

Here is the call graph for this function:

◆ send() [6/6]

void precice::com::SocketCommunication::send ( std::string const & itemToSend,
Rank rankReceiver )
overridevirtual

Sends a std::string to process with given rank.

Implements precice::com::Communication.

Definition at line 343 of file SocketCommunication.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ _addressDirectory

std::string precice::com::SocketCommunication::_addressDirectory
private

Directory where IP address is exchanged by file.

Definition at line 142 of file SocketCommunication.hpp.

◆ _ioService

std::shared_ptr<IOService> precice::com::SocketCommunication::_ioService
private

Definition at line 148 of file SocketCommunication.hpp.

◆ _log

logging::Logger precice::com::SocketCommunication::_log {"com::SocketCommunication"}
private

Definition at line 131 of file SocketCommunication.hpp.

◆ _networkName

std::string precice::com::SocketCommunication::_networkName
private

Name of network to communicate over.

Definition at line 139 of file SocketCommunication.hpp.

◆ _portNumber

unsigned short precice::com::SocketCommunication::_portNumber
private

Port used for socket connection.

Definition at line 134 of file SocketCommunication.hpp.

◆ _queue

SocketSendQueue precice::com::SocketCommunication::_queue
private

Definition at line 155 of file SocketCommunication.hpp.

◆ _reuseAddress

bool precice::com::SocketCommunication::_reuseAddress
private

Definition at line 136 of file SocketCommunication.hpp.

◆ _sockets

std::map<int, std::shared_ptr<Socket> > precice::com::SocketCommunication::_sockets
private

Remote rank -> socket map.

Definition at line 153 of file SocketCommunication.hpp.

◆ _thread

std::thread precice::com::SocketCommunication::_thread
private

Definition at line 150 of file SocketCommunication.hpp.

◆ _work

std::shared_ptr<Work> precice::com::SocketCommunication::_work
private

Definition at line 149 of file SocketCommunication.hpp.


The documentation for this class was generated from the following files: