recv_impl.cc 1.9 KB
Newer Older
武毅 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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. */

#include "send_recv_impl.h"

namespace paddle {
namespace operators {
namespace detail {

Status SendRecvServerImpl::SendVariable(ServerContext *context,
                                        const VariableMessage *in_var,
T
typhoonzero 已提交
23
                                        VoidMessage *out_var) {
T
typhoonzero 已提交
24
  // TODO(typhoonzero): support different variable types.
武毅 已提交
25
  std::istringstream iss(in_var->serialized());
T
typhoonzero 已提交
26
  framework::LoDTensor t;
武毅 已提交
27
  framework::DeserializeFromStream(iss, &t);
T
typhoonzero 已提交
28 29 30 31
  TensorWithName tensor_with_name =
      std::make_pair(in_var->varname(), std::move(t));

  var_recv_queue_.Push(std::move(tensor_with_name));
T
typhoonzero 已提交
32 33 34 35 36 37
  return Status::OK;
}

Status SendRecvServerImpl::GetVariable(ServerContext *context,
                                       const VoidMessage *in_var,
                                       VariableMessage *out_var) {
武毅 已提交
38
  // Block util the sub graph is done.
T
typhoonzero 已提交
39
  auto out_tensor_with_name = var_return_queue_.Pop();
武毅 已提交
40
  std::ostringstream oss;
T
typhoonzero 已提交
41 42 43
  framework::SerializeToStream(oss, out_tensor_with_name.second,
                               platform::CPUDeviceContext());

武毅 已提交
44
  std::string *varname = out_var->mutable_varname();
T
typhoonzero 已提交
45
  *varname = out_tensor_with_name.first;
武毅 已提交
46 47 48 49 50 51 52 53
  std::string *serialized = out_var->mutable_serialized();
  *serialized = oss.str();
  return Status::OK;
}

}  // namespace detail
}  // namespace operators
}  // namespace paddle