communicator.h 2.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*!
 *  Copyright (c) 2019 by Contributors
 * \file communicator.h
 * \brief Communicator for DGL distributed training.
 */
#ifndef DGL_GRAPH_NETWORK_COMMUNICATOR_H_
#define DGL_GRAPH_NETWORK_COMMUNICATOR_H_

#include <string>

namespace dgl {
namespace network {

/*!
15
 * \brief Network Sender for DGL distributed training.
16
 *
17
18
19
20
 * Sender is an abstract class that defines a set of APIs for sending 
 * binary data over network. It can be implemented by different underlying 
 * networking libraries such TCP socket and ZMQ. One Sender can connect to 
 * multiple receivers, and it can send data to specified receiver via receiver's ID.
21
 */
22
class Sender {
23
 public:
24
  virtual ~Sender() {}
25
26

  /*!
27
28
29
30
   * \brief Add receiver address and it's ID to the namebook
   * \param ip receviver's IP address
   * \param port receiver's port
   * \param id receiver's ID
31
   */
32
33
  virtual void AddReceiver(const char* ip, int port, int id) = 0;

34
  /*!
35
36
   * \brief Connect with all the Receivers
   * \return True for sucess and False for fail
37
   */
38
  virtual bool Connect() = 0;
39
40

  /*!
41
42
43
44
45
46
   * \brief Send data to specified Receiver
   * \param data data buffer for sending
   * \param size data size for sending
   * \param recv_id receiver's ID
   * \return bytes we sent
   *   > 0 : bytes we sent
47
48
   *   - 1 : error
   */
49
  virtual int64_t Send(const char* data, int64_t size, int recv_id) = 0;
50
51

  /*!
52
   * \brief Finalize Sender
53
54
   */
  virtual void Finalize() = 0;
55
56
57
58
59
60
61
62
63
64
65
66
67
};

/*!
 * \brief Network Receiver for DGL distributed training.
 *
 * Receiver is an abstract class that defines a set of APIs for receiving binary 
 * data over network. It can be implemented by different underlying networking libraries 
 * such TCP socket and ZMQ. One Receiver can connect with multiple Senders, and it can receive 
 * data from these Senders concurrently via multi-threading and message queue.
 */
class Receiver {
 public:
  virtual ~Receiver() {}
68
69

  /*!
70
71
72
73
74
75
   * \brief Wait all of the Senders to connect
   * \param ip Receiver's IP address
   * \param port Receiver's port
   * \param num_sender total number of Senders
   * \param queue_size size of message queue
   * \return True for sucess and False for fail
76
   */
77
  virtual bool Wait(const char* ip, int port, int num_sender, int queue_size) = 0;
78
79

  /*!
80
81
82
83
84
85
   * \brief Recv data from Sender (copy data from message queue)
   * \param dest data buffer of destination
   * \param max_size maximul size of data buffer
   * \return bytes we received
   *   > 0 : bytes we received
   *   - 1 : error
86
   */
87
88
89
90
91
92
  virtual int64_t Recv(char* dest, int64_t max_size) = 0;

  /*!
   * \brief Finalize Receiver
   */
  virtual void Finalize() = 0;
93
94
95
96
97
98
};

}  // namespace network
}  // namespace dgl

#endif  // DGL_GRAPH_NETWORK_COMMUNICATOR_H_