grpc_server.h 2.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
gongweibao 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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 <map>
18
#include <memory>
19
#include <set>
Y
Yi Wang 已提交
20 21 22
#include <string>
#include <thread>  // NOLINT
#include <utility>
X
Xin Pan 已提交
23
#include <vector>
24

Y
Yi Wang 已提交
25
#include "grpc++/grpc++.h"
T
typhoonzero 已提交
26
#include "paddle/fluid/framework/blocking_queue.h"
27
#include "paddle/fluid/framework/executor.h"
Y
Yi Wang 已提交
28
#include "paddle/fluid/framework/lod_tensor.h"
29
#include "paddle/fluid/framework/program_desc.h"
Y
Yi Wang 已提交
30 31 32
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/selected_rows.h"
#include "paddle/fluid/framework/var_type.h"
W
Wu Yi 已提交
33 34
#include "paddle/fluid/operators/distributed/distributed_pb.h"
#include "paddle/fluid/operators/distributed/grpc/grpc_service.h"
35 36 37
#include "paddle/fluid/operators/distributed/request_handler.h"
#include "paddle/fluid/operators/distributed/rpc_server.h"
#include "paddle/fluid/operators/distributed/sendrecvop_utils.h"
X
Xin Pan 已提交
38
#include "paddle/fluid/platform/profiler.h"
G
gongweibao 已提交
39

W
wanghuancoder 已提交
40 41 42 43
namespace grpc {
class ServerCompletionQueue;
}  // namespace grpc

G
gongweibao 已提交
44 45
namespace paddle {
namespace operators {
46
namespace distributed {
G
gongweibao 已提交
47 48 49

class RequestBase;

50
class AsyncGRPCServer final : public RPCServer {
G
gongweibao 已提交
51
 public:
52 53
  explicit AsyncGRPCServer(const std::string& address, int client_num)
      : RPCServer(address, client_num), ready_(0) {}
G
gongweibao 已提交
54

55 56 57
  virtual ~AsyncGRPCServer() {}
  void WaitServerReady() override;
  void StartServer() override;
G
gongweibao 已提交
58

59
 private:
X
Xin Pan 已提交
60
  // HandleRequest needs to be thread-safe.
61 62 63
  void HandleRequest(
      ::grpc::ServerCompletionQueue* cq, const std::string& rpc_name,
      std::function<void(const std::string&, int)> TryToRegisterNewOne);
G
gongweibao 已提交
64

65
  void TryToRegisterNewOne(const std::string& rpc_name, int req_id);
G
gongweibao 已提交
66
  void ShutdownQueue();
67
  void ShutDownImpl() override;
G
gongweibao 已提交
68 69

 private:
70
  static const int kRequestBufSize = 100;
X
Xin Pan 已提交
71

G
gongweibao 已提交
72 73
  std::mutex cq_mutex_;
  volatile bool is_shut_down_ = false;
X
Xin Pan 已提交
74

75
  std::unique_ptr<GrpcService::AsyncService> service_;
76
  std::unique_ptr<::grpc::Server> server_;
G
gongweibao 已提交
77 78

  // condition of the sub program
T
typhoonzero 已提交
79
  std::condition_variable barrier_condition_;
G
gongweibao 已提交
80

T
done  
typhoonzero 已提交
81
  std::mutex mutex_ready_;
T
wip  
typhoonzero 已提交
82
  std::condition_variable condition_ready_;
83

T
wip  
typhoonzero 已提交
84
  int ready_;
85 86 87 88

  std::map<std::string, std::unique_ptr<::grpc::ServerCompletionQueue>> rpc_cq_;
  std::map<std::string, std::vector<std::unique_ptr<std::thread>>> rpc_threads_;
  std::map<std::string, std::vector<RequestBase*>> rpc_reqs_;
G
gongweibao 已提交
89 90
};

91
};  // namespace distributed
G
gongweibao 已提交
92 93
};  // namespace operators
};  // namespace paddle