communication.cc 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2019 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
#include "paddle/fluid/pybind/communication.h"

17
#include <Python.h>
18 19 20 21
// Avoid a problem with copysign defined in pyconfig.h on Windows.
#ifdef copysign
#undef copysign
#endif
22 23 24 25
#include <pybind11/chrono.h>
#include <pybind11/complex.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>
26

27
#include <chrono>
28
#include <memory>
29 30
#include <string>

31
#include "paddle/phi/core/distributed/comm_context_manager.h"
L
LiYuRio 已提交
32
#include "paddle/phi/core/distributed/store/store_utils.h"
33
#include "paddle/phi/core/distributed/store/tcp_store.h"
34 35 36 37 38 39

namespace py = pybind11;

namespace paddle {
namespace pybind {

40 41 42 43 44
void BindCommContextManager(py::module *m) {
  auto CommContextManager =
      py::class_<phi::distributed::CommContextManager,
                 std::shared_ptr<phi::distributed::CommContextManager>>(
          *m, "CommContextManager")
45 46 47
          .def_static("set_device_id",
                      &phi::distributed::CommContextManager::SetDeviceId,
                      py::call_guard<py::gil_scoped_release>())
48 49 50 51 52
#if defined(PADDLE_WITH_RCCL) || defined(PADDLE_WITH_NCCL)
          .def_static(
              "create_nccl_comm_context",
              &phi::distributed::CommContextManager::CreateNCCLCommContext,
              py::call_guard<py::gil_scoped_release>())
53 54 55 56 57 58
#endif
#if defined(PADDLE_WITH_GLOO)
          .def_static(
              "create_gloo_comm_context",
              &phi::distributed::CommContextManager::CreateGlooCommContext,
              py::call_guard<py::gil_scoped_release>())
59 60 61 62 63 64
#endif
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
          .def_static(
              "create_xccl_comm_context",
              &phi::distributed::CommContextManager::CreateXCCLCommContext,
              py::call_guard<py::gil_scoped_release>())
65 66 67 68 69
#endif
          .def("set_store", &phi::distributed::CommContextManager::SetStore);
}

using TCPStore = phi::distributed::TCPStore;
70

71
void BindTCPStore(py::module *m) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  auto Store = py::class_<phi::distributed::Store,
                          std::shared_ptr<phi::distributed::Store>>(*m, "Store")
                   .def(py::init<>())
                   .def(
                       "set",
                       [](phi::distributed::Store &self,
                          const std::string &key,
                          const std::string &value) {
                         std::vector<uint8_t> data(value.begin(), value.end());
                         self.set(key, data);
                       },
                       py::arg("key"),
                       py::arg("value"),
                       py::call_guard<py::gil_scoped_release>())
                   .def(
                       "get",
                       [](phi::distributed::Store &self,
                          const std::string &key) -> py::bytes {
                         auto data = self.get(key);
91
                         std::string s(data.begin(), data.end());
F
Frank Lin 已提交
92
                         py::gil_scoped_acquire acquire;
93
                         return py::bytes(s);
94 95 96 97 98 99 100 101 102
                       },
                       py::arg("key"),
                       py::call_guard<py::gil_scoped_release>())
                   .def("add",
                        &phi::distributed::Store::add,
                        py::call_guard<py::gil_scoped_release>())
                   .def("wait",
                        &phi::distributed::Store::wait,
                        py::call_guard<py::gil_scoped_release>());
103 104

  py::class_<TCPStore, std::shared_ptr<TCPStore>>(*m, "TCPStore", Store)
G
gongweibao 已提交
105 106 107 108 109 110 111
      .def(py::init([](std::string hostname,
                       uint16_t port,
                       bool is_master,
                       size_t world_size,
                       int timeout) {
             return std::make_shared<TCPStore>(
                 hostname, port, is_master, world_size, timeout);
112
           }),
G
gongweibao 已提交
113 114 115
           py::arg("hostname"),
           py::arg("port"),
           py::arg("is_master"),
116
           py::arg("world_size"),
G
gongweibao 已提交
117
           py::arg("timeout") = 900,
118
           py::call_guard<py::gil_scoped_release>());
119 120 121

  m->def("create_or_get_global_tcp_store",
         &phi::distributed::CreateOrGetGlobalTCPStore);
122 123 124 125
}

}  // namespace pybind
}  // namespace paddle