gen_comm_id_helper.cc 14.5 KB
Newer Older
W
WangXi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

15 16 17
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) ||          \
    defined(PADDLE_WITH_XPU_BKCL) || defined(PADDLE_WITH_ASCEND_CL) || \
    defined(PADDLE_WITH_CNCL)
18
#include "paddle/fluid/platform/gen_comm_id_helper.h"
W
WangXi 已提交
19 20 21 22 23 24

#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
25
#include <algorithm>
W
WangXi 已提交
26
#include <string>
27
#include <thread>  // NOLINT
W
WangXi 已提交
28 29 30 31 32

#include "glog/logging.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/string/split.h"

33 34 35 36
#if defined(PADDLE_WITH_XPU_BKCL)
#include "xpu/bkcl.h"
#endif

37 38 39 40
#if defined(PADDLE_WITH_ASCEND_CL)
#include "paddle/fluid/platform/collective_helper.h"
#endif

41 42 43 44
#if defined(PADDLE_WITH_CNCL)
#include <cncl.h>
#endif

B
Baibaifan 已提交
45 46
DECLARE_int32(get_host_by_name_time);

W
WangXi 已提交
47
namespace paddle {
48
namespace platform {
W
WangXi 已提交
49

50 51
std::once_flag SocketServer::init_flag_;

52 53 54 55
struct CommHead {
  int version = 1;  // unused for now
  int ring_id = 0;
};
W
WangXi 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

// Check system calls, such as socket, bind.
#define CHECK_SYS_CALL(call, name)          \
  do {                                      \
    int retval;                             \
    CHECK_SYS_CALL_VAL(call, name, retval); \
  } while (false)

#define CHECK_SYS_CALL_VAL(call, name, retval)                            \
  do {                                                                    \
    RETRY_SYS_CALL_VAL(call, name, retval);                               \
    if (retval == -1) {                                                   \
      PADDLE_THROW(platform::errors::Unavailable("Call to %s failed: %s", \
                                                 name, strerror(errno))); \
    }                                                                     \
  } while (false)

#define RETRY_SYS_CALL_VAL(call, name, retval)                           \
  do {                                                                   \
    retval = (call);                                                     \
    if (retval == -1 &&                                                  \
        (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)) {   \
      LOG(WARNING) << "Call " << name << " returned " << strerror(errno) \
                   << " retry";                                          \
    } else {                                                             \
      break;                                                             \
    }                                                                    \
  } while (true)

static int SocketSend(int fd, const char* buffer, int size) {
  int offset = 0;
  int bytes = 0;
  while (offset < size) {
    bytes = send(fd, buffer + offset, size - offset, 0);
    if (bytes == -1) {
      if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) {
        // send failed
        return -1;
      } else {
        bytes = 0;
      }
    }
    offset += bytes;
  }
  return offset;
}

static int SocketRecv(int fd, char* buffer, int size) {
  int offset = 0;
  int bytes = 0;
  while (offset < size) {
    bytes = recv(fd, buffer + offset, size - offset, 0);
    if (bytes == 0) {
      // closed by client, maybe probing alive client
      return 0;
    }
    if (bytes == -1) {
      if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) {
        return -1;
      } else {
        bytes = 0;
      }
    }
    offset += bytes;
  }
  return offset;
}

static void BindOrConnectFailed(int timeout, int* try_times, int* total_time,
                                const char* op, const std::string& ep) {
  PADDLE_ENFORCE_LT(
      *total_time, timeout,
      platform::errors::Unavailable("%s addr=%s timeout, failed reason: %s", op,
                                    ep.c_str(), strerror(errno)));
  ++(*try_times);
  int retry_time = std::min(*try_times * 500, 3000);  // max 3 seconds
  *total_time += retry_time;

  LOG(WARNING) << op << " addr=" << ep << " failed " << *try_times
               << " times with reason: " << strerror(errno) << " retry after "
               << retry_time / 1000.0 << " seconds";
  std::this_thread::sleep_for(std::chrono::milliseconds(retry_time));
}

int CreateListenSocket(const std::string& ep) {
  auto addr = paddle::string::Split(ep, ':');
  PADDLE_ENFORCE_EQ(
      addr.size(), 2UL,
      platform::errors::InvalidArgument(
          "The endpoint should contain host and port, but got %s.", ep));
  std::string host = addr[0];
  int port = std::stoi(addr[1]);

  // creating socket fd
  int server_fd = -1;
  CHECK_SYS_CALL_VAL(socket(AF_INET, SOCK_STREAM, 0), "socket", server_fd);

  // NOTE. Solutions to `Address already in use`.
  // 1. Reuse addr&port. Otherwise, once the server closes the socket
  // before client, the server will enter TIME-WAIT status. If we bind port
  // again, the error `Address already in use` will appear.
  // 2. Or we can close the client first to ensure that the server does
  // not enter the TIME-WAIT state. But this is obviously not as convenient
  // as the reuse method.
  int opt = 1;
161 162 163 164 165 166 167 168 169 170

  // NOTE. The linger is used for skipping TIME-WAIT status forcefully.
  linger ling;
  ling.l_onoff = 1;
  ling.l_linger = 0;

  CHECK_SYS_CALL(
      setsockopt(server_fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)),
      "setsockopt set linger");

W
WangXi 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
#if defined(SO_REUSEPORT)
  // since Linux kernel 3.9
  CHECK_SYS_CALL(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
                            &opt, sizeof(opt)),
                 "setsockopt");
#else
  CHECK_SYS_CALL(
      setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)),
      "setsockopt");
#endif

  struct sockaddr_in address;
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(port);

  // TODO(wangxi) Set from env, default 900s=15min
  int timeout = 900 * 1000;
  int try_times = 0;
  int total_time = 0;
  while (true) {
    int ret_val = -1;
    RETRY_SYS_CALL_VAL(
        bind(server_fd, (struct sockaddr*)&address, sizeof(address)), "bind",
        ret_val);

    if (ret_val == -1) {
      BindOrConnectFailed(timeout, &try_times, &total_time, "bind", ep);
      continue;
    }
    break;
  }

  CHECK_SYS_CALL(listen(server_fd, 3), "listen");
  LOG(INFO) << "Server listening on: " << ep << " successful.";
  return server_fd;
}

void CloseSocket(int fd) { CHECK_SYS_CALL(close(fd), "close"); }

211 212 213 214
static int SocketAccept(int server_fd, const CommHead head) {
  static_assert(sizeof(CommHead) <= 1024,
                "sizeof(CommHead) must <= buffer size");

W
WangXi 已提交
215 216 217 218
  struct sockaddr_in client_addr;
  socklen_t addr_length = sizeof(client_addr);
  char buffer[1024] = {0};
  int conn = -1;
219
  const char* phead = reinterpret_cast<const char*>(&head);
W
WangXi 已提交
220 221 222 223 224 225 226

  while (true) {
    CHECK_SYS_CALL_VAL(
        accept(server_fd, reinterpret_cast<struct sockaddr*>(&client_addr),
               &addr_length),
        "accept", conn);

227 228 229 230
    int ret_val = SocketRecv(conn, buffer, sizeof(head));
    if (ret_val > 0 && memcmp(buffer, phead, sizeof(head)) == 0) {
      // send a message to the sender, indicating that the link is correct
      CHECK_SYS_CALL(SocketSend(conn, phead, sizeof(head)), "send");
W
WangXi 已提交
231 232 233 234 235 236 237 238 239
      break;  // accept client
    } else {
      VLOG(3) << "socket read failed with ret_val=" << ret_val;
      CloseSocket(conn);
    }
  }
  return conn;
}

240
static int ConnectAddr(const std::string& ep, const CommHead head) {
W
WangXi 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  auto addr = paddle::string::Split(ep, ':');
  PADDLE_ENFORCE_EQ(
      addr.size(), 2UL,
      platform::errors::InvalidArgument(
          "The endpoint should contain host and port, but got %s.", ep));
  std::string host = addr[0];
  int port = std::stoi(addr[1]);

  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);

  char* ip = NULL;
  struct hostent* hp = NULL;
B
Baibaifan 已提交
256 257 258 259 260 261 262 263 264 265

  // sleep for get_host_by_name_time seconds.
  for (int i = 0; 2 * i < FLAGS_get_host_by_name_time; i++) {
    hp = gethostbyname(host.c_str());
    if (hp != NULL) {
      break;
    }
    std::this_thread::sleep_for(std::chrono::seconds(2));
    LOG(WARNING) << "gethostbyname " << host.c_str() << " error!";
  }
W
WangXi 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279
  PADDLE_ENFORCE_NOT_NULL(hp, platform::errors::InvalidArgument(
                                  "Fail to get host by name %s.", host));

  int i = 0;
  while (hp->h_addr_list[i] != NULL) {
    ip = inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]);
    VLOG(3) << "gethostbyname  host:" << host << "  ->ip: " << ip;
    break;
  }

  PADDLE_ENFORCE_GT(inet_pton(AF_INET, ip, &server_addr.sin_addr), 0,
                    platform::errors::Unavailable("Open address %s failed: %s",
                                                  ep, strerror(errno)));

280 281 282 283 284
  static_assert(sizeof(CommHead) <= 1024,
                "sizeof(CommHead) must <= buffer size");
  char buffer[1024] = {0};
  const char* phead = reinterpret_cast<const char*>(&head);

W
WangXi 已提交
285 286 287 288
  // TODO(wangxi) Set from env, default 900s=15min
  int timeout = 900 * 1000;
  int try_times = 0;
  int total_time = 0;
289 290 291

  int sock = -1;
  CHECK_SYS_CALL_VAL(socket(AF_INET, SOCK_STREAM, 0), "socket", sock);
W
WangXi 已提交
292 293 294 295 296 297 298 299 300 301 302
  while (true) {
    int ret_val = -1;
    RETRY_SYS_CALL_VAL(
        connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)),
        "connect", ret_val);

    if (ret_val == -1) {
      BindOrConnectFailed(timeout, &try_times, &total_time, "connect", ep);
      continue;
    }

303 304 305 306 307 308 309 310 311 312 313 314 315
    CHECK_SYS_CALL(SocketSend(sock, phead, sizeof(head)), "send");
    ret_val = SocketRecv(sock, buffer, sizeof(head));
    if (ret_val > 0 && memcmp(buffer, phead, sizeof(head)) == 0) {
      // recv same message from recver, indicating that the link is correct
      break;  // accept client
    } else {
      VLOG(3) << "socket read failed with ret_val=" << ret_val;
      CloseSocket(sock);
    }
    sock = -1;
    CHECK_SYS_CALL_VAL(socket(AF_INET, SOCK_STREAM, 0), "socket", sock);
    // unmatched link, retry after 80ms
    std::this_thread::sleep_for(std::chrono::milliseconds(80));
W
WangXi 已提交
316 317 318 319
  }
  return sock;
}

320 321 322 323 324 325 326
// TODO(WANGXI): maybe need to unify this hard code
#ifdef PADDLE_WITH_ASCEND_CL
#define MAX_COMMUNIQUEID_LEN 4108
#else
#define MAX_COMMUNIQUEID_LEN 1024
#endif

327 328
template <typename CommUniqueId>
static void RecvCommID(int conn, CommUniqueId* nccl_id) {
329 330
  char buffer[MAX_COMMUNIQUEID_LEN] = {0};
  static_assert(sizeof(CommUniqueId) <= MAX_COMMUNIQUEID_LEN,
W
WangXi 已提交
331 332
                "nccl id bytes must <= buffer size");

333 334 335
  CHECK_SYS_CALL(SocketRecv(conn, buffer, sizeof(CommUniqueId)),
                 "recv comm unique id");
  memcpy(nccl_id, buffer, sizeof(CommUniqueId));
W
WangXi 已提交
336 337
}

338 339
template <typename CommUniqueId>
static void SendCommID(int conn, CommUniqueId* nccl_id) {
340
  char buffer[MAX_COMMUNIQUEID_LEN] = {0};
341
  memcpy(buffer, nccl_id, sizeof(CommUniqueId));
W
WangXi 已提交
342

343 344
  CHECK_SYS_CALL(SocketSend(conn, buffer, sizeof(CommUniqueId)),
                 "send comm unique id");
W
WangXi 已提交
345 346
}

347 348
template <typename CommUniqueId>
void SendBroadCastCommID(std::vector<std::string> servers,
349 350 351 352
                         std::vector<CommUniqueId>* nccl_ids, int ring_id) {
  CommHead head;
  head.ring_id = ring_id;

W
WangXi 已提交
353 354 355 356
  // connect with server
  std::vector<int> connects;
  for (auto server : servers) {
    VLOG(3) << "connecting endpoint: " << server;
357
    int conn = ConnectAddr(server, head);
W
WangXi 已提交
358 359 360 361
    connects.push_back(conn);
  }
  VLOG(3) << "connecting completed...";

362
  for (size_t i = 0; i < nccl_ids->size(); ++i) {
W
WangXi 已提交
363 364
    int j = 0;
    for (auto conn : connects) {
365 366
      VLOG(3) << "sending comm_id to " << servers[j] << " nccl_comm_no: " << i;
      SendCommID(conn, &(*nccl_ids)[i]);
W
WangXi 已提交
367 368 369 370 371 372 373 374 375 376
      ++j;
    }
  }

  // close client
  for (auto conn : connects) {
    CloseSocket(conn);
  }
}

377 378
template <typename CommUniqueId>
void RecvBroadCastCommID(std::string endpoint,
379
                         std::vector<CommUniqueId>* nccl_ids, int ring_id) {
W
WangXi 已提交
380
  int server = CreateListenSocket(endpoint);
381
  RecvBroadCastCommID(server, endpoint, nccl_ids, ring_id);
W
WangXi 已提交
382 383 384
  CloseSocket(server);
}

385 386
template <typename CommUniqueId>
void RecvBroadCastCommID(int server_fd, std::string endpoint,
387 388 389 390
                         std::vector<CommUniqueId>* nccl_ids, int ring_id) {
  CommHead head;
  head.ring_id = ring_id;
  int client = SocketAccept(server_fd, head);
W
WangXi 已提交
391

392 393 394 395
  for (size_t i = 0; i < nccl_ids->size(); ++i) {
    VLOG(3) << "trainer: " << endpoint
            << " receiving comm_id from trainer 0, nccl_comm_no: " << i;
    RecvCommID(client, &(*nccl_ids)[i]);
W
WangXi 已提交
396
  }
397

W
WangXi 已提交
398 399 400 401
  VLOG(3) << "receiving completed...";
  CloseSocket(client);
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
SocketServer& SocketServer::GetInstance(const std::string& end_point) {
  static SocketServer instance;
  std::call_once(init_flag_, [&]() {
    instance.server_fd_ = CreateListenSocket(end_point);
    instance.end_point_ = end_point;
  });
  PADDLE_ENFORCE_NE(instance.server_fd_, -1,
                    platform::errors::Unavailable(
                        "listen socket failed with end_point=%s", end_point));
  PADDLE_ENFORCE_EQ(instance.end_point_, end_point,
                    platform::errors::InvalidArgument(
                        "old end_point=%s must equal with new end_point=%s",
                        instance.end_point_, end_point));
  return instance;
}

418
/// template instantiation
419 420 421 422 423 424 425 426 427
#define INSTANT_TEMPLATE(Type)                                                 \
  template void SendBroadCastCommID<Type>(std::vector<std::string> servers,    \
                                          std::vector<Type> * nccl_ids,        \
                                          int ring_id = 0);                    \
  template void RecvBroadCastCommID<Type>(                                     \
      std::string endpoint, std::vector<Type> * nccl_ids, int ring_id = 0);    \
  template void RecvBroadCastCommID<Type>(int server_fd, std::string endpoint, \
                                          std::vector<Type>* nccl_ids,         \
                                          int ring_id = 0);
428

429
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
430 431 432
INSTANT_TEMPLATE(ncclUniqueId)
#endif
#ifdef PADDLE_WITH_XPU_BKCL
433
INSTANT_TEMPLATE(BKCLUniqueId)
434
#endif
435 436 437
#ifdef PADDLE_WITH_ASCEND_CL
INSTANT_TEMPLATE(HcclRootInfo)
#endif
438 439 440
#ifdef PADDLE_WITH_CNCL
INSTANT_TEMPLATE(cnclCliqueId)
#endif
441
}  // namespace platform
W
WangXi 已提交
442
}  // namespace paddle
443 444

#endif