rpc_client.h 5.1 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2018 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.

#pragma once

17
#include <condition_variable>  // NOLINT
Q
Qiao Longfei 已提交
18
#include <memory>
G
gongweibao 已提交
19 20
#include <string>

W
wanghuancoder 已提交
21
#include "gflags/gflags.h"
G
gongweibao 已提交
22 23 24
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/scope.h"
25
#include "paddle/fluid/operators/distributed/request_handler.h"
G
gongweibao 已提交
26

W
wanghuancoder 已提交
27 28 29 30 31 32 33 34 35
namespace paddle {
namespace framework {
class Scope;
}  // namespace framework
namespace platform {
class DeviceContext;
}  // namespace platform
}  // namespace paddle

G
gongweibao 已提交
36
DECLARE_int32(rpc_deadline);
37
DECLARE_int32(rpc_retry_times);
W
Wu Yi 已提交
38

G
gongweibao 已提交
39 40
namespace paddle {
namespace operators {
41
namespace distributed {
G
gongweibao 已提交
42 43 44

class RPCClient {
 public:
G
gongweibao 已提交
45 46
  RPCClient() {}
  virtual ~RPCClient() {}
47 48 49 50 51 52 53 54 55 56
  virtual VarHandlePtr AsyncSendVar(const std::string& ep,
                                    const platform::DeviceContext& ctx,
                                    const framework::Scope& scope,
                                    const std::string& var_name,
                                    int64_t time_out = FLAGS_rpc_deadline) = 0;

  virtual VarHandlePtr AsyncGetVar(const std::string& ep,
                                   const platform::DeviceContext& ctx,
                                   const framework::Scope& scope,
                                   const std::string& var_name,
57
                                   const std::string& out_varname,
Q
Qiao Longfei 已提交
58
                                   const std::string& table_name = "",
59 60
                                   int64_t time_out = FLAGS_rpc_deadline) = 0;

61 62 63 64 65 66
  virtual VarHandlePtr AsyncGetVarNoBarrier(
      const std::string& ep, const platform::DeviceContext& ctx,
      const framework::Scope& scope, const std::string& var_name,
      const std::string& out_varname,
      int64_t time_out = FLAGS_rpc_deadline) = 0;

67 68 69 70 71
  virtual VarHandlePtr AsyncGetMonomerVariable(
      const std::string& ep, const platform::DeviceContext& ctx,
      const framework::Scope& scope, const std::string& var_name,
      int64_t time_out = FLAGS_rpc_deadline) = 0;

72 73 74
  virtual VarHandlePtr AsyncPrefetchVar(
      const std::string& ep, const platform::DeviceContext& ctx,
      const framework::Scope& scope, const std::string& in_var_name,
Q
Qiao Longfei 已提交
75
      const std::string& out_var_name, const std::string& table_name = "",
76 77 78 79 80 81 82 83
      int64_t time_out = FLAGS_rpc_deadline) = 0;

  virtual VarHandlePtr AsyncSendBatchBarrier(
      const std::string& ep, int64_t time_out = FLAGS_rpc_deadline) = 0;

  virtual VarHandlePtr AsyncSendFetchBarrier(
      const std::string& ep, int64_t time_out = FLAGS_rpc_deadline) = 0;

84 85 86 87
  virtual VarHandlePtr AsyncGetMonomerBarrier(
      const std::string& ep, const std::string& var_name,
      int64_t time_out = FLAGS_rpc_deadline) = 0;

88
  virtual VarHandlePtr AsyncCheckpointNotify(
89
      const std::string& ep, const std::string& dirname,
90 91
      const std::string& varname, const int mode,
      int64_t time_out = FLAGS_rpc_deadline) = 0;
92

93
  virtual VarHandlePtr AsyncDistributeNotify(
1
123malin 已提交
94 95
      const std::string& ep, const platform::DeviceContext& ctx,
      const framework::Scope& scope, const std::string& var_name,
96 97
      int64_t time_out = FLAGS_rpc_deadline) = 0;

98 99 100 101 102 103
  virtual VarHandlePtr AsyncSendAndRecv(
      const std::string& ep, const platform::DeviceContext& ctx,
      const framework::Scope& scope, const std::string& send_var_name,
      const std::string& recv_var_name, const std::string& table_name = "",
      int64_t time_out = FLAGS_rpc_deadline) = 0;

104 105
  virtual VarHandlePtr AsyncSendComplete(
      const std::string& ep, int64_t time_out = FLAGS_rpc_deadline) = 0;
Y
Yancey1989 已提交
106

Y
Yancey1989 已提交
107 108
  // Complete tells all the pserver instances that finishe the training,
  // the pserver can reduce it's barrier count, and continue to train
Y
Yancey1989 已提交
109
  // with other trainers.
Y
Yancey1989 已提交
110
  virtual void SendComplete() = 0;
W
Wu Yi 已提交
111

Y
Yancey1989 已提交
112
  virtual bool Wait() = 0;
G
gongweibao 已提交
113 114

  template <typename T>
W
Wu Yi 已提交
115 116
  static RPCClient* GetInstance(int trainer_id) {
    std::call_once(init_flag_, &RPCClient::Init<T>, trainer_id);
G
gongweibao 已提交
117 118 119 120 121
    return rpc_client_.get();
  }

  // Init is called by GetInstance.
  template <typename T>
W
Wu Yi 已提交
122
  static void Init(int trainer_id) {
123
    VLOG(1) << "init rpc client with trainer_id " << trainer_id;
W
Wu Yi 已提交
124
    trainer_id_ = trainer_id;
G
gongweibao 已提交
125 126 127 128 129 130 131
    if (rpc_client_.get() == nullptr) {
      rpc_client_.reset(new T());
      rpc_client_->InitImpl();
    }
  }

  virtual void InitImpl() {}
132 133

 protected:
W
Wu Yi 已提交
134 135
  // each trainer have exact one trainer id, it should be static
  static int trainer_id_;
G
gongweibao 已提交
136 137 138 139 140

 private:
  static std::once_flag init_flag_;
  static std::unique_ptr<RPCClient> rpc_client_;
};
141
}  // namespace distributed
G
gongweibao 已提交
142 143
}  // namespace operators
}  // namespace paddle