gen_comm_id_helper.h 2.0 KB
Newer Older
W
WangXi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) || \
18
    defined(PADDLE_WITH_XPU_BKCL) || defined(PADDLE_WITH_ASCEND_CL)
W
WangXi 已提交
19
#include <functional>
20 21
#include <memory>
#include <mutex>
W
WangXi 已提交
22 23 24
#include <string>
#include <vector>

25 26
#include "glog/logging.h"

W
WangXi 已提交
27
namespace paddle {
28
namespace platform {
W
WangXi 已提交
29 30 31 32 33

int CreateListenSocket(const std::string& ep);

void CloseSocket(int fd);

34 35
template <typename CommUniqueId>
void SendBroadCastCommID(std::vector<std::string> servers,
36
                         std::vector<CommUniqueId>* nccl_ids, int ring_id = 0);
W
WangXi 已提交
37

38 39
template <typename CommUniqueId>
void RecvBroadCastCommID(std::string endpoint,
40
                         std::vector<CommUniqueId>* nccl_ids, int ring_id = 0);
W
WangXi 已提交
41 42

// recv nccl id from socket
43 44
template <typename CommUniqueId>
void RecvBroadCastCommID(int server_fd, std::string endpoint,
45
                         std::vector<CommUniqueId>* nccl_ids, int ring_id = 0);
46 47 48 49 50

class SocketServer {
 public:
  SocketServer() = default;

51 52 53 54 55
  ~SocketServer() {
    if (server_fd_ != -1) {
      CloseSocket(server_fd_);
    }
  }
56 57 58

  int socket() const { return server_fd_; }

59 60 61 62 63 64
  void Release() {
    VLOG(3) << "Server will be closed by external call.";
    CloseSocket(server_fd_);
    server_fd_ = -1;
  }

65 66 67 68 69 70 71 72 73
  static SocketServer& GetInstance(const std::string& end_point);

 private:
  int server_fd_{-1};
  std::string end_point_;

  static std::once_flag init_flag_;
};

74
}  // namespace platform
W
WangXi 已提交
75
}  // namespace paddle
76 77

#endif