recv_op.cc 2.8 KB
Newer Older
L
Luo Tao 已提交
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
武毅 已提交
2

L
Luo Tao 已提交
3 4 5
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
武毅 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
武毅 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
武毅 已提交
14 15 16

#include <ostream>

T
typhoonzero 已提交
17
#include "paddle/framework/data_type.h"
武毅 已提交
18 19 20 21
#include "paddle/framework/framework.pb.h"
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/op_registry.h"

T
typhoonzero 已提交
22 23
#include <future>
#include "paddle/operators/detail/grpc_client.h"
T
typhoonzero 已提交
24

武毅 已提交
25 26 27
namespace paddle {
namespace operators {

T
typhoonzero 已提交
28
class SendOp : public framework::OperatorBase {
武毅 已提交
29
 public:
T
typhoonzero 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  SendOp(const std::string& type, const framework::VariableNameMap& inputs,
         const framework::VariableNameMap& outputs,
         const framework::AttributeMap& attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

  void Run(const framework::Scope& scope,
           const platform::Place& place) const override {
    auto outs = Outputs("Out");
    std::vector<std::string> epmap = Attr<std::vector<std::string>>("epmap");

    platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
    auto& ctx = *pool.Get(place);

    for (size_t i = 0; i < outs.size(); i++) {
      VLOG(3) << "getting " << outs[i];
      client_.AsyncGetVariable(epmap[i], ctx, scope, outs[i]);
武毅 已提交
46
    }
T
done  
typhoonzero 已提交
47

T
typhoonzero 已提交
48
    PADDLE_ENFORCE(client_.Wait());
武毅 已提交
49 50
  }

T
typhoonzero 已提交
51 52
 private:
  mutable detail::RPCClient client_;
武毅 已提交
53 54
};

T
typhoonzero 已提交
55
class SendOpMaker : public framework::OpProtoAndCheckerMaker {
武毅 已提交
56
 public:
T
typhoonzero 已提交
57
  SendOpMaker(OpProto* proto, OpAttrChecker* op_checker)
武毅 已提交
58
      : OpProtoAndCheckerMaker(proto, op_checker) {
T
typhoonzero 已提交
59 60 61
    AddInput("X", "(Tensor) Input tensor to be sent").AsDuplicable();
    AddOutput("Out", "(Tensor) Output tensor to be received from server")
        .AsDuplicable();
武毅 已提交
62
    AddComment(R"DOC(
T
typhoonzero 已提交
63
Send operator
武毅 已提交
64

T
typhoonzero 已提交
65
This operator will send tensor to recv_op at the parameter server.
武毅 已提交
66
)DOC");
T
typhoonzero 已提交
67 68 69
    AddAttr<std::vector<std::string>>("endpoints",
                                      "(string vector, default 127.0.0.1:6164)"
                                      "Server endpoints to send variables to.")
Y
Yancey1989 已提交
70
        .SetDefault({});
T
typhoonzero 已提交
71 72 73 74
    AddAttr<std::vector<std::string>>("epmap",
                                      "(string vector, default 127.0.0.1:6164)"
                                      "Server endpoints in the order of input "
                                      "variables for mapping")
Y
Yancey1989 已提交
75
        .SetDefault({});
武毅 已提交
76 77 78 79 80 81 82 83
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

T
typhoonzero 已提交
84
REGISTER_OPERATOR(send, ops::SendOp, ops::SendOpMaker);