"tests/vscode:/vscode.git/clone" did not exist on "40caf1ab50f641bc8c3d078c4e2daa4207bc727f"
communicator.h 4.07 KB
Newer Older
1
2
3
4
5
/*!
 *  Copyright (c) 2019 by Contributors
 * \file communicator.h
 * \brief Communicator for DGL distributed training.
 */
6
7
#ifndef DGL_RPC_NETWORK_COMMUNICATOR_H_
#define DGL_RPC_NETWORK_COMMUNICATOR_H_
8

9
10
#include <dmlc/logging.h>

11
12
#include <string>

13
#include "../net_type.h"
14
15
#include "msg_queue.h"

16
17
18
19
namespace dgl {
namespace network {

/*!
20
 * \brief Network Sender for DGL distributed training.
21
 *
22
23
24
25
 * Sender is an abstract class that defines a set of APIs for sending binary 
 * data message over network. It can be implemented by different underlying 
 * networking libraries such TCP socket and MPI. One Sender can connect to 
 * multiple receivers and it can send data to specified receiver via receiver's ID.
26
 */
27
class Sender : public rpc::RPCSender {
28
 public:
29
30
31
  /*!
   * \brief Sender constructor
   * \param queue_size size (bytes) of message queue. 
32
   * \param max_thread_count size of thread pool. 0 for no limit
33
34
   * Note that, the queue_size parameter is optional.
   */
35
  explicit Sender(int64_t queue_size = 0, int max_thread_count = 0) {
36
    CHECK_GE(queue_size, 0);
37
    CHECK_GE(max_thread_count, 0);
38
    queue_size_ = queue_size;
39
    max_thread_count_ = max_thread_count;
40
41
  }

42
  virtual ~Sender() {}
43
44

  /*!
45
46
   * \brief Send data to specified Receiver.
   * \param msg data message
47
   * \param recv_id receiver's ID
48
49
50
51
52
53
54
55
   * \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.
56
   */
57
  virtual STATUS Send(Message msg, int recv_id) = 0;
58

59
 protected:
60
  /*!
61
   * \brief Size of message queue
62
   */
63
  int64_t queue_size_;
64
65
66
67
  /*!
   * \brief Size of thread pool. 0 for no limit
   */
  int max_thread_count_;
68
69
70
71
72
};

/*!
 * \brief Network Receiver for DGL distributed training.
 *
73
74
75
76
 * Receiver is an abstract class that defines a set of APIs for receiving binary data 
 * message over network. It can be implemented by different underlying networking 
 * libraries such as TCP socket and MPI. One Receiver can connect with multiple Senders 
 * and it can receive data from multiple Senders concurrently.
77
 */
78
class Receiver : public rpc::RPCReceiver {
79
 public:
80
81
82
  /*!
   * \brief Receiver constructor
   * \param queue_size size of message queue.
83
   * \param max_thread_count size of thread pool. 0 for no limit
84
85
   * Note that, the queue_size parameter is optional.
   */
86
  explicit Receiver(int64_t queue_size = 0, int max_thread_count = 0) {
87
88
89
    if (queue_size < 0) {
      LOG(FATAL) << "queue_size cannot be a negative number.";
    }
90
    CHECK_GE(max_thread_count, 0);
91
    queue_size_ = queue_size;
92
    max_thread_count_ = max_thread_count;
93
94
  }

95
  virtual ~Receiver() {}
96

97
98
99
100
101
102
103
104
105
106
  /*!
   * \brief Recv data from Sender
   * \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.
107
   */
108
  virtual STATUS Recv(Message* msg, int* send_id) = 0;
109
110

  /*!
111
112
113
114
115
116
117
118
119
   * \brief Recv data from a specified Sender
   * \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.
120
   */
121
  virtual STATUS RecvFrom(Message* msg, int send_id) = 0;
122

123
 protected:
124
  /*!
125
   * \brief Size of message queue
126
   */
127
  int64_t queue_size_;
128
129
130
131
  /*!
   * \brief Size of thread pool. 0 for no limit
   */
  int max_thread_count_;
132
133
134
135
136
};

}  // namespace network
}  // namespace dgl

137
#endif  // DGL_RPC_NETWORK_COMMUNICATOR_H_