socket_communicator.h 8.11 KB
Newer Older
1
2
3
4
5
/*!
 *  Copyright (c) 2019 by Contributors
 * \file communicator.h
 * \brief SocketCommunicator for DGL distributed training.
 */
6
7
#ifndef DGL_RPC_NETWORK_SOCKET_COMMUNICATOR_H_
#define DGL_RPC_NETWORK_SOCKET_COMMUNICATOR_H_
8
9
10
11

#include <thread>
#include <vector>
#include <string>
12
#include <unordered_map>
13
#include <memory>
14

15
#include "../../runtime/semaphore_wrapper.h"
16
17
18
#include "communicator.h"
#include "msg_queue.h"
#include "tcp_socket.h"
19
#include "common.h"
20
21
22
23

namespace dgl {
namespace network {

24
static constexpr int kMaxTryCount = 1024;    // maximal connection: 1024
25
static constexpr int kTimeOut = 10 * 60;     // 10 minutes (in seconds) for socket timeout
26
static constexpr int kMaxConnection = 1024;  // maximal connection: 1024
27
28

/*!
29
 * \breif Networking address
30
 */
31
32
33
struct IPAddr {
  std::string ip;
  int port;
34
35
36
};

/*!
37
 * \brief SocketSender for DGL distributed training.
38
 *
39
 * SocketSender is the communicator implemented by tcp socket.
40
41
 */
class SocketSender : public Sender {
42
43
 public:
  /*!
44
45
   * \brief Sender constructor
   * \param queue_size size of message queue 
46
   * \param max_thread_count size of thread pool. 0 for no limit
47
   */
48
49
  SocketSender(int64_t queue_size, int max_thread_count)
    : Sender(queue_size, max_thread_count) {}
50
51

  /*!
52
53
54
55
56
57
58
59
60
   * \brief Connect to a receiver.
   * 
   * When there are multiple receivers to be connected, application will call `ConnectReceiver`
   * for each and then call `ConnectReceiverFinalize` to make sure that either all the connections are
   * successfully established or some of them fail.
   * 
   * \param addr Networking address, e.g., 'tcp://127.0.0.1:50091'
   * \param recv_id receiver's ID
   * \return True for success and False for fail
61
   *
62
   * The function is *not* thread-safe; only one thread can invoke this API.
63
   */
64
  bool ConnectReceiver(const std::string& addr, int recv_id) override;
65

66
  /*!
67
68
   * \brief Finalize the action to connect to receivers. Make sure that either
   *        all connections are successfully established or connection fails.
69
70
   * \return True for success and False for fail
   *
71
72
73
74
75
76
77
78
79
80
81
82
83
   * The function is *not* thread-safe; only one thread can invoke this API.
   */
  bool ConnectReceiverFinalize() override;

  /*!
   * \brief Send RPCMessage to specified Receiver.
   * \param msg data message 
   * \param recv_id receiver's ID
   */
  void Send(const rpc::RPCMessage& msg, int recv_id) override;

  /*!
   * \brief Finalize TPSender
84
   */
85
86
87
88
89
90
91
92
93
  void Finalize() override;

  /*!
   * \brief Communicator type: 'socket'
   */
  const std::string &NetType() const override {
    static const std::string net_type = "socket";
    return net_type;
  }
94
95

  /*!
96
97
   * \brief Send data to specified Receiver. Actually pushing message to message queue.
   * \param msg data message
98
   * \param recv_id receiver's ID
99
100
101
102
103
104
105
106
   * \return Status code
   *
   * (1) The send is non-blocking. There is no guarantee that the message has been 
   *     physically sent out when the function returns.
   * (2) The communicator will assume the responsibility of the given message.
   * (3) The API is multi-thread safe.
   * (4) Messages sent to the same receiver are guaranteed to be received in the same order. 
   *     There is no guarantee for messages sent to different receivers.
107
   */
108
  STATUS Send(Message msg, int recv_id) override;
109

110
 private:
111
  /*!
112
113
   * \brief socket for each connection of receiver
   */ 
114
115
  std::vector<std::unordered_map<int /* receiver ID */,
    std::shared_ptr<TCPSocket>>> sockets_;
116

117
  /*!
118
   * \brief receivers' address
119
   */ 
120
  std::unordered_map<int /* receiver ID */, IPAddr> receiver_addrs_;
121

122
  /*!
123
   * \brief message queue for each thread
124
   */ 
125
  std::vector<std::shared_ptr<MessageQueue>> msg_queue_;
126
127

  /*!
128
   * \brief Independent thread
129
   */ 
130
  std::vector<std::shared_ptr<std::thread>> threads_;
131
132

  /*!
133
134
135
   * \brief Send-loop for each thread
   * \param sockets TCPSockets for current thread
   * \param queue message_queue for current thread
136
137
138
139
   * 
   * Note that, the SendLoop will finish its loop-job and exit thread
   * when the main thread invokes Signal() API on the message queue.
   */
140
141
142
143
  static void SendLoop(
    std::unordered_map<int /* Receiver (virtual) ID */,
      std::shared_ptr<TCPSocket>> sockets,
    std::shared_ptr<MessageQueue> queue);
144
145
146
};

/*!
147
 * \brief SocketReceiver for DGL distributed training.
148
 *
149
 * SocketReceiver is the communicator implemented by tcp socket.
150
151
152
153
 */
class SocketReceiver : public Receiver {
 public:
  /*!
154
155
   * \brief Receiver constructor
   * \param queue_size size of message queue.
156
   * \param max_thread_count size of thread pool. 0 for no limit
157
   */
158
159
  SocketReceiver(int64_t queue_size, int max_thread_count)
    : Receiver(queue_size, max_thread_count) {}
160
161

  /*!
162
   * \brief Wait for all the Senders to connect
163
   * \param addr Networking address, e.g., 'tcp://127.0.0.1:50051', 'mpi://0'
164
   * \param num_sender total number of Senders
165
   * \param blocking whether wait blockingly
166
167
168
   * \return True for success and False for fail
   *
   * Wait() is not thread-safe and only one thread can invoke this API.
169
   */
170
171
172
173
174
175
176
177
  bool Wait(const std::string &addr, int num_sender,
            bool blocking = true) override;

  /*!
   * \brief Recv RPCMessage from Sender. Actually removing data from queue.
   * \param msg pointer of RPCmessage
   */
  void Recv(rpc::RPCMessage* msg) override;
178
179
180
181
182
183
184
185
186
187
188

  /*!
   * \brief Recv data from Sender. Actually removing data from msg_queue.
   * \param msg pointer of data message
   * \param send_id which sender current msg comes from
   * \return Status code
   *
   * (1) The Recv() API is blocking, which will not 
   *     return until getting data from message queue.
   * (2) The Recv() API is thread-safe.
   * (3) Memory allocated by communicator but will not own it after the function returns.
189
   */
190
  STATUS Recv(Message* msg, int* send_id) override;
191
192
193
194
195
196
197
198
199
200
201
202

  /*!
   * \brief Recv data from a specified Sender. Actually removing data from msg_queue.
   * \param msg pointer of data message
   * \param send_id sender's ID
   * \return Status code
   *
   * (1) The RecvFrom() API is blocking, which will not 
   *     return until getting data from message queue.
   * (2) The RecvFrom() API is thread-safe.
   * (3) Memory allocated by communicator but will not own it after the function returns.
   */
203
  STATUS RecvFrom(Message* msg, int send_id) override;
204

205
  /*!
206
207
208
   * \brief Finalize SocketReceiver
   *
   * Finalize() is not thread-safe and only one thread can invoke this API.
209
   */
210
  void Finalize() override;
211
212

  /*!
213
   * \brief Communicator type: 'socket'
214
   */
215
216
217
218
  const std::string &NetType() const override {
    static const std::string net_type = "socket";
    return net_type;
  }
219

220
 private:
221
222
223
224
225
  struct RecvContext {
    int64_t data_size = -1;
    int64_t received_bytes = 0;
    char *buffer = nullptr;
  };
226
227
228
229
230
231
  /*!
   * \brief number of sender
   */
  int num_sender_;

  /*!
232
   * \brief server socket for listening connections
233
   */ 
234
  TCPSocket* server_socket_;
235
236

  /*!
237
   * \brief socket for each client connections
238
   */ 
239
240
  std::vector<std::unordered_map<int /* Sender (virutal) ID */,
    std::shared_ptr<TCPSocket>>> sockets_;
241
242

  /*!
243
   * \brief Message queue for each socket connection
244
   */ 
245
246
  std::unordered_map<int /* Sender (virtual) ID */,
    std::shared_ptr<MessageQueue>> msg_queue_;
247
  std::unordered_map<int, std::shared_ptr<MessageQueue>>::iterator mq_iter_;
248

249
  /*!
250
   * \brief Independent thead
251
   */ 
252
  std::vector<std::shared_ptr<std::thread>> threads_;
253

254
  /*!
255
256
257
258
259
260
261
262
263
   * \brief queue_sem_ semphore to indicate number of messages in multiple
   * message queues to prevent busy wait of Recv
   */
  runtime::Semaphore queue_sem_;

  /*!
   * \brief Recv-loop for each thread
   * \param sockets client sockets of current thread
   * \param queue message queues of current thread
264
265
266
   *
   * Note that, the RecvLoop will finish its loop-job and exit thread
   * when the main thread invokes Signal() API on the message queue.
267
   */ 
268
269
270
271
272
273
  static void RecvLoop(
    std::unordered_map<int /* Sender (virtual) ID */,
      std::shared_ptr<TCPSocket>> sockets,
    std::unordered_map<int /* Sender (virtual) ID */,
      std::shared_ptr<MessageQueue>> queues,
    runtime::Semaphore *queue_sem);
274
275
276
277
278
};

}  // namespace network
}  // namespace dgl

279
#endif  // DGL_RPC_NETWORK_SOCKET_COMMUNICATOR_H_