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

Y
Yi Wang 已提交
24
#include "grpc++/grpc++.h"
T
typhoonzero 已提交
25
#include "paddle/fluid/framework/blocking_queue.h"
26
#include "paddle/fluid/framework/executor.h"
Y
Yi Wang 已提交
27
#include "paddle/fluid/framework/lod_tensor.h"
28
#include "paddle/fluid/framework/program_desc.h"
Y
Yi Wang 已提交
29 30 31
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/selected_rows.h"
#include "paddle/fluid/framework/var_type.h"
W
Wu Yi 已提交
32 33
#include "paddle/fluid/operators/distributed/distributed_pb.h"
#include "paddle/fluid/operators/distributed/grpc/grpc_service.h"
34 35 36
#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 已提交
37
#include "paddle/fluid/platform/profiler.h"
G
gongweibao 已提交
38 39 40

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

class RequestBase;

45
class AsyncGRPCServer final : public RPCServer {
G
gongweibao 已提交
46
 public:
47 48
  explicit AsyncGRPCServer(const std::string& address, int client_num)
      : RPCServer(address, client_num), ready_(0) {}
G
gongweibao 已提交
49

50 51 52
  virtual ~AsyncGRPCServer() {}
  void WaitServerReady() override;
  void StartServer() override;
G
gongweibao 已提交
53

54
 private:
X
Xin Pan 已提交
55
  // HandleRequest needs to be thread-safe.
56 57 58
  void HandleRequest(
      ::grpc::ServerCompletionQueue* cq, const std::string& rpc_name,
      std::function<void(const std::string&, int)> TryToRegisterNewOne);
G
gongweibao 已提交
59

60
  void TryToRegisterNewOne(const std::string& rpc_name, int req_id);
G
gongweibao 已提交
61
  void ShutdownQueue();
62
  void ShutDownImpl() override;
G
gongweibao 已提交
63 64

 private:
65
  static const int kRequestBufSize = 100;
X
Xin Pan 已提交
66

G
gongweibao 已提交
67 68
  std::mutex cq_mutex_;
  volatile bool is_shut_down_ = false;
X
Xin Pan 已提交
69

70 71
  GrpcService::AsyncService service_;
  std::unique_ptr<::grpc::Server> server_;
G
gongweibao 已提交
72 73

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

T
done  
typhoonzero 已提交
76
  std::mutex mutex_ready_;
T
wip  
typhoonzero 已提交
77
  std::condition_variable condition_ready_;
78

T
wip  
typhoonzero 已提交
79
  int ready_;
80 81 82 83

  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 已提交
84 85
};

86
};  // namespace distributed
G
gongweibao 已提交
87 88
};  // namespace operators
};  // namespace paddle