gen_comm_id_helper.h 2.1 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 18 19
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) ||          \
    defined(PADDLE_WITH_XPU_BKCL) || defined(PADDLE_WITH_ASCEND_CL) || \
    defined(PADDLE_WITH_CNCL)
W
WangXi 已提交
20
#include <functional>
21 22
#include <memory>
#include <mutex>
W
WangXi 已提交
23 24 25
#include <string>
#include <vector>

26 27
#include "glog/logging.h"

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

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

void CloseSocket(int fd);

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

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

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

class SocketServer {
 public:
  SocketServer() = default;

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

  int socket() const { return server_fd_; }

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

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

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

  static std::once_flag init_flag_;
};

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

#endif