recv_impl.cc 2.0 KB
Newer Older
武毅 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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 17 18 19 20 21 22

#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) {
Y
Yancey 已提交
24 25 26
  MessageWithName msg_with_name =
      std::make_pair(in_var->varname(), std::move(*in_var));
  var_recv_queue_.Push(std::move(msg_with_name));
T
typhoonzero 已提交
27 28 29 30
  return Status::OK;
}

Status SendRecvServerImpl::GetVariable(ServerContext *context,
T
typhoonzero 已提交
31
                                       const VariableMessage *in_var,
T
typhoonzero 已提交
32
                                       VariableMessage *out_var) {
T
typhoonzero 已提交
33 34
  std::string get_var_name = in_var->varname();
  auto *var = scope_->FindVar(get_var_name);
T
typhoonzero 已提交
35

Y
Yancey 已提交
36
  SerializeToMessage(get_var_name, var, platform::CPUDeviceContext(), out_var);
武毅 已提交
37 38 39
  return Status::OK;
}

T
typhoonzero 已提交
40 41 42
Status SendRecvServerImpl::Wait(ServerContext *context,
                                const VoidMessage *in_var,
                                VoidMessage *out_var) {
T
typhoonzero 已提交
43 44 45 46
  {
    std::unique_lock<std::mutex> lock(this->mutex_);
    condition_.wait(lock, [=] { return this->done_ == true; });
  }
T
typhoonzero 已提交
47 48 49
  return Status::OK;
}

T
typhoonzero 已提交
50
void SendRecvServerImpl::Reset() {
T
typhoonzero 已提交
51
  std::lock_guard<std::mutex> lock(this->mutex_);
T
typhoonzero 已提交
52 53 54 55
  done_ = false;
}

void SendRecvServerImpl::Done() {
T
typhoonzero 已提交
56 57 58 59
  {
    std::lock_guard<std::mutex> lock(this->mutex_);
    done_ = true;
  }
T
typhoonzero 已提交
60 61 62
  condition_.notify_all();
}

武毅 已提交
63 64 65
}  // namespace detail
}  // namespace operators
}  // namespace paddle