communicator.h 2.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*!
 *  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 {

/*!
 * \brief Communicator for DGL distributed training.
 *
 * Communicator is a set of interface for network communication, which
 * can be implemented by real network libraries, such as grpc, mpi, as well
 * as raw socket. There has two types of Communicator, one is Sender 
 * (is_sender = true), and another is Receiver. For Sender, it can send binary 
 * data to remote Receiver node. For Receiver, it can listen on a specified 
 * endpoint and receive the binary data sent from Sender node. Note that, a 
 * receiver node can recv messages from multiple senders concurrently.
 */
class Communicator {
 public:
  virtual ~Communicator() {}

  /*!
   * \brief Initialize Communicator
   * \param is_sender true for sender and false for receiver
   * \param ip ip address
   * \param port end port
   * (e.g. "168.123.2.43:50051"). For Receiver, this address identifies
   * the local listening endpoint (e.g. "0.0.0.0:50051").
   * \param num_sender number of senders, only used for receiver.
   * \param queue_size the size of message queue, only for receiver.
   * \return true for success and false for error
   */
  virtual bool Initialize(bool is_sender,
                          const char* ip,
                          int port,
                          int num_sender = 1,
                          int64_t queue_size = 5 * 1024 * 1024) = 0;
  /*!
   * \brief Send message to receiver node
   * \param src data pointer
   * \param size data size
   * \return bytes send
   *   > 0 : bytes send
   *   - 1 : error
   */
  virtual int64_t Send(char* src, int64_t size) = 0;

  /*!
   * \brief Receive mesesage from sender node, we
   * actually reading data from local message queue.
   * \param dest destination data pointer
   * \param max_size maximal data size
   * \return bytes received
   *   > 0 : bytes received
   *   - 1 : error
   */
  virtual int64_t Receive(char* dest, int64_t max_size) = 0;

  /*!
   * \brief Finalize the Communicator class
   */
  virtual void Finalize() = 0;
};

}  // namespace network
}  // namespace dgl

#endif  // DGL_GRAPH_NETWORK_COMMUNICATOR_H_