communication.cc 2.8 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 18 19 20 21
#include <Python.h>
#include <pybind11/chrono.h>
#include <pybind11/complex.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>
22

23 24 25 26 27 28 29 30 31 32 33 34
#include <chrono>
#include <string>

#include "paddle/fluid/distributed/store/tcp_store.h"

namespace py = pybind11;

namespace paddle {
namespace pybind {

using TCPStore = paddle::distributed::TCPStore;

35 36 37 38 39
void BindTCPStore(py::module *m) {
  auto Store =
      py::class_<distributed::Store, std::shared_ptr<distributed::Store>>(
          *m, "Store")
          .def(py::init<>())
40 41
          .def(
              "set",
G
gongweibao 已提交
42 43
              [](distributed::Store &self,
                 const std::string &key,
44 45 46 47
                 const std::string &value) {
                std::vector<uint8_t> data(value.begin(), value.end());
                self.set(key, data);
              },
G
gongweibao 已提交
48 49
              py::arg("key"),
              py::arg("value"),
50 51 52 53 54 55 56 57 58
              py::call_guard<py::gil_scoped_release>())
          .def(
              "get",
              [](distributed::Store &self,
                 const std::string &key) -> py::bytes {
                auto data = self.get(key);
                return py::bytes(reinterpret_cast<char *>(data.data()),
                                 data.size());
              },
G
gongweibao 已提交
59 60 61 62
              py::arg("key"),
              py::call_guard<py::gil_scoped_release>())
          .def("add",
               &distributed::Store::add,
63
               py::call_guard<py::gil_scoped_release>())
G
gongweibao 已提交
64 65
          .def("wait",
               &distributed::Store::wait,
66 67 68
               py::call_guard<py::gil_scoped_release>());

  py::class_<TCPStore, std::shared_ptr<TCPStore>>(*m, "TCPStore", Store)
G
gongweibao 已提交
69 70 71 72 73 74 75
      .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);
76
           }),
G
gongweibao 已提交
77 78 79
           py::arg("hostname"),
           py::arg("port"),
           py::arg("is_master"),
80
           py::arg("world_size"),
G
gongweibao 已提交
81
           py::arg("timeout") = 900,
82
           py::call_guard<py::gil_scoped_release>());
83 84 85 86
}

}  // namespace pybind
}  // namespace paddle