linkers.h 8.8 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
#ifndef LIGHTGBM_SRC_NETWORK_LINKERS_H_
#define LIGHTGBM_SRC_NETWORK_LINKERS_H_
Guolin Ke's avatar
Guolin Ke committed
7

8
9
10
11
12
#include <LightGBM/config.h>
#include <LightGBM/meta.h>
#include <LightGBM/network.h>
#include <LightGBM/utils/common.h>

13
#include <string>
Guolin Ke's avatar
Guolin Ke committed
14
#include <algorithm>
Guolin Ke's avatar
Guolin Ke committed
15
16
#include <chrono>
#include <ctime>
17
#include <memory>
Guolin Ke's avatar
Guolin Ke committed
18
19
#include <thread>
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
20
21
22

#ifdef USE_SOCKET
#include "socket_wrapper.hpp"
Guolin Ke's avatar
Guolin Ke committed
23
24
25
26
27
28
29
30
31
32
#endif

#ifdef USE_MPI
#include <mpi.h>
#define MPI_SAFE_CALL(mpi_return) CHECK((mpi_return) == MPI_SUCCESS)
#endif

namespace LightGBM {

/*!
33
34
* \brief A network basic communication wrapper.
* Will wrap low level communication methods, e.g. mpi, socket and so on.
Guolin Ke's avatar
Guolin Ke committed
35
36
37
* This class will wrap all linkers to other machines if needs
*/
class Linkers {
Nikita Titov's avatar
Nikita Titov committed
38
 public:
39
40
41
  Linkers() {
    is_init_ = false;
  }
Guolin Ke's avatar
Guolin Ke committed
42
43
44
45
  /*!
  * \brief Constructor
  * \param config Config of network settings
  */
Guolin Ke's avatar
Guolin Ke committed
46
  explicit Linkers(Config config);
Guolin Ke's avatar
Guolin Ke committed
47
48
49
50
51
52
53
54
  /*!
  * \brief Destructor
  */
  ~Linkers();
  /*!
  * \brief Recv data, blocking
  * \param rank Which rank will send data to local machine
  * \param data Pointer of receive data
55
  * \param len Recv size, will block until receive len size of data
Guolin Ke's avatar
Guolin Ke committed
56
57
  */
  inline void Recv(int rank, char* data, int len) const;
Guolin Ke's avatar
Guolin Ke committed
58
59
60

  inline void Recv(int rank, char* data, int64_t len) const;

Guolin Ke's avatar
Guolin Ke committed
61
62
63
64
  /*!
  * \brief Send data, blocking
  * \param rank Which rank local machine will send to
  * \param data Pointer of send data
65
  * \param len Send size
Guolin Ke's avatar
Guolin Ke committed
66
67
  */
  inline void Send(int rank, char* data, int len) const;
Guolin Ke's avatar
Guolin Ke committed
68
69

  inline void Send(int rank, char* data, int64_t len) const;
Guolin Ke's avatar
Guolin Ke committed
70
71
72
73
  /*!
  * \brief Send and Recv at same time, blocking
  * \param send_rank
  * \param send_data
74
  * \param send_len
Guolin Ke's avatar
Guolin Ke committed
75
76
  * \param recv_rank
  * \param recv_data
77
  * \param recv_len
Guolin Ke's avatar
Guolin Ke committed
78
79
  */
  inline void SendRecv(int send_rank, char* send_data, int send_len,
Guolin Ke's avatar
Guolin Ke committed
80
81
82
83
                       int recv_rank, char* recv_data, int recv_len);

  inline void SendRecv(int send_rank, char* send_data, int64_t send_len,
                       int recv_rank, char* recv_data, int64_t recv_len);
Guolin Ke's avatar
Guolin Ke committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  /*!
  * \brief Get rank of local machine
  */
  inline int rank();
  /*!
  * \brief Get total number of machines
  */
  inline int num_machines();
  /*!
  * \brief Get Bruck map of this network
  */
  inline const BruckMap& bruck_map();
  /*!
  * \brief Get Recursive Halving map of this network
  */
  inline const RecursiveHalvingMap& recursive_halving_map();

  #ifdef USE_SOCKET
  /*!
  * \brief Bind local listen to port
  * \param port Local listen port
  */
  void TryBind(int port);
  /*!
  * \brief Set socket to rank
  * \param rank
  * \param socket
  */
  void SetLinker(int rank, const TcpSocket& socket);
  /*!
  * \brief Thread for listening
  * \param incoming_cnt Number of incoming machines
  */
  void ListenThread(int incoming_cnt);
  /*!
  * \brief Construct network topo
  */
  void Construct();
  /*!
  * \brief Parser machines information from file
124
  * \param machines
Guolin Ke's avatar
Guolin Ke committed
125
126
  * \param filename
  */
127
  void ParseMachineList(const std::string& machines, const std::string& filename);
Guolin Ke's avatar
Guolin Ke committed
128
129
130
131
132
133
134
  /*!
  * \brief Check one linker is connected or not
  * \param rank
  * \return True if linker is connected
  */
  bool CheckLinker(int rank);
  /*!
Andrew Ziem's avatar
Andrew Ziem committed
135
  * \brief Print connected linkers
Guolin Ke's avatar
Guolin Ke committed
136
137
138
139
140
  */
  void PrintLinkers();

  #endif  // USE_SOCKET

141
  #ifdef USE_MPI
Guolin Ke's avatar
Guolin Ke committed
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  /*!
  * \brief Check if MPI has been initialized
  */
  static bool IsMpiInitialized();

  /*!
  * \brief Finalize the MPI session if it was initialized
  */
  static void MpiFinalizeIfIsParallel();

  /*!
  * \brief Abort the MPI session if it was initialized (called in case there was a error that needs abrupt ending)
  */
  static void MpiAbortIfIsParallel();

  #endif
159
160

 private:
Guolin Ke's avatar
Guolin Ke committed
161
162
163
164
165
166
167
168
169
170
171
  /*! \brief Rank of local machine */
  int rank_;
  /*! \brief Total number machines */
  int num_machines_;
  /*! \brief Bruck map */
  BruckMap bruck_map_;
  /*! \brief Recursive Halving map */
  RecursiveHalvingMap recursive_halving_map_;

  std::chrono::duration<double, std::milli> network_time_;

172
173
  bool is_init_;

Guolin Ke's avatar
Guolin Ke committed
174
175
176
177
178
179
180
181
182
183
  #ifdef USE_SOCKET
  /*! \brief use to store client ips */
  std::vector<std::string> client_ips_;
  /*! \brief use to store client ports */
  std::vector<int> client_ports_;
  /*! \brief time out for sockets, in minutes */
  int socket_timeout_;
  /*! \brief Local listen ports */
  int local_listen_port_;
  /*! \brief Linkers */
Guolin Ke's avatar
Guolin Ke committed
184
  std::vector<std::unique_ptr<TcpSocket>> linkers_;
Guolin Ke's avatar
Guolin Ke committed
185
  /*! \brief Local socket listener */
Guolin Ke's avatar
Guolin Ke committed
186
  std::unique_ptr<TcpSocket> listener_;
Guolin Ke's avatar
Guolin Ke committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  #endif  // USE_SOCKET
};


inline int Linkers::rank() {
  return rank_;
}

inline int Linkers::num_machines() {
  return num_machines_;
}

inline const BruckMap& Linkers::bruck_map() {
  return bruck_map_;
}

inline const RecursiveHalvingMap& Linkers::recursive_halving_map() {
  return recursive_halving_map_;
}

Guolin Ke's avatar
Guolin Ke committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
inline void Linkers::Recv(int rank, char* data, int64_t len) const {
  int64_t used = 0;
  do {
    int cur_size = static_cast<int>(std::min<int64_t>(len - used, INT32_MAX));
    Recv(rank, data + used, cur_size);
    used += cur_size;
  } while (used < len);
}

inline void Linkers::Send(int rank, char* data, int64_t len) const {
  int64_t used = 0;
  do {
    int cur_size = static_cast<int>(std::min<int64_t>(len - used, INT32_MAX));
    Send(rank, data + used, cur_size);
    used += cur_size;
  } while (used < len);
}

inline void Linkers::SendRecv(int send_rank, char* send_data, int64_t send_len,
                              int recv_rank, char* recv_data, int64_t recv_len) {
  auto start_time = std::chrono::high_resolution_clock::now();
  std::thread send_worker(
    [this, send_rank, send_data, send_len]() {
    Send(send_rank, send_data, send_len);
  });
  Recv(recv_rank, recv_data, recv_len);
  send_worker.join();
  // wait for send complete
  auto end_time = std::chrono::high_resolution_clock::now();
  // output used time on each iteration
  network_time_ += std::chrono::duration<double, std::milli>(end_time - start_time);
}

Guolin Ke's avatar
Guolin Ke committed
240
241
242
243
244
#ifdef USE_SOCKET

inline void Linkers::Recv(int rank, char* data, int len) const {
  int recv_cnt = 0;
  while (recv_cnt < len) {
Guolin Ke's avatar
Guolin Ke committed
245
    recv_cnt += linkers_[rank]->Recv(data + recv_cnt,
246
247
      // len - recv_cnt
      std::min(len - recv_cnt, SocketConfig::kMaxReceiveSize));
Guolin Ke's avatar
Guolin Ke committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  }
}

inline void Linkers::Send(int rank, char* data, int len) const {
  if (len <= 0) {
    return;
  }
  int send_cnt = 0;
  while (send_cnt < len) {
    send_cnt += linkers_[rank]->Send(data + send_cnt, len - send_cnt);
  }
}

inline void Linkers::SendRecv(int send_rank, char* send_data, int send_len,
Guolin Ke's avatar
Guolin Ke committed
262
                              int recv_rank, char* recv_data, int recv_len) {
Guolin Ke's avatar
Guolin Ke committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  auto start_time = std::chrono::high_resolution_clock::now();
  if (send_len < SocketConfig::kSocketBufferSize) {
    // if buffer is enough, send will non-blocking
    Send(send_rank, send_data, send_len);
    Recv(recv_rank, recv_data, recv_len);
  } else {
    // if buffer is not enough, use another thread to send, since send will be blocking
    std::thread send_worker(
      [this, send_rank, send_data, send_len]() {
      Send(send_rank, send_data, send_len);
    });
    Recv(recv_rank, recv_data, recv_len);
    send_worker.join();
  }
  // wait for send complete
  auto end_time = std::chrono::high_resolution_clock::now();
  // output used time on each iteration
  network_time_ += std::chrono::duration<double, std::milli>(end_time - start_time);
}

#endif  // USE_SOCKET

#ifdef USE_MPI

inline void Linkers::Recv(int rank, char* data, int len) const {
  MPI_Status status;
  int read_cnt = 0;
  while (read_cnt < len) {
    MPI_SAFE_CALL(MPI_Recv(data + read_cnt, len - read_cnt, MPI_BYTE, rank, MPI_ANY_TAG, MPI_COMM_WORLD, &status));
    int cur_cnt;
    MPI_SAFE_CALL(MPI_Get_count(&status, MPI_BYTE, &cur_cnt));
    read_cnt += cur_cnt;
  }
}

inline void Linkers::Send(int rank, char* data, int len) const {
  if (len <= 0) {
    return;
  }
  MPI_Status status;
  MPI_Request send_request;
  MPI_SAFE_CALL(MPI_Isend(data, len, MPI_BYTE, rank, 0, MPI_COMM_WORLD, &send_request));
  MPI_SAFE_CALL(MPI_Wait(&send_request, &status));
}

inline void Linkers::SendRecv(int send_rank, char* send_data, int send_len,
Guolin Ke's avatar
Guolin Ke committed
309
                              int recv_rank, char* recv_data, int recv_len) {
Guolin Ke's avatar
Guolin Ke committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  MPI_Request send_request;
  // send first, non-blocking
  MPI_SAFE_CALL(MPI_Isend(send_data, send_len, MPI_BYTE, send_rank, 0, MPI_COMM_WORLD, &send_request));
  // then receive, blocking
  MPI_Status status;
  int read_cnt = 0;
  while (read_cnt < recv_len) {
    MPI_SAFE_CALL(MPI_Recv(recv_data + read_cnt, recv_len - read_cnt, MPI_BYTE, recv_rank, 0, MPI_COMM_WORLD, &status));
    int cur_cnt;
    MPI_SAFE_CALL(MPI_Get_count(&status, MPI_BYTE, &cur_cnt));
    read_cnt += cur_cnt;
  }
  // wait for send complete
  MPI_SAFE_CALL(MPI_Wait(&send_request, &status));
}

#endif  // USE_MPI
}  // namespace LightGBM
328
#endif   // LIGHTGBM_SRC_NETWORK_LINKERS_H_