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
#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 40 41

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

class RequestBase;

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

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

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

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

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

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

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

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

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

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

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

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