listen_and_serv_op.h 2.3 KB
Newer Older
T
typhoonzero 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2016 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

#include <stdint.h>
#include <ostream>
19
#include <string>
T
typhoonzero 已提交
20 21 22 23 24 25 26 27 28 29 30

#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/threadpool.h"
#include "paddle/fluid/operators/detail/grpc_server.h"

namespace paddle {
namespace operators {

constexpr char kOptimizeBlock[] = "OptimizeBlock";
31
constexpr char kPrefetchBlock[] = "PrefetchBlock";
T
typhoonzero 已提交
32 33 34 35 36

void RunServer(std::shared_ptr<detail::AsyncGRPCServer> service);

class ListenAndServOp : public framework::OperatorBase {
 public:
Q
qiaolongfei 已提交
37 38 39 40
  ListenAndServOp(const std::string& type,
                  const framework::VariableNameMap& inputs,
                  const framework::VariableNameMap& outputs,
                  const framework::AttributeMap& attrs);
T
typhoonzero 已提交
41

T
typhoonzero 已提交
42
  int GetSelectedPort() const;
T
typhoonzero 已提交
43

44 45 46 47
  void RunSyncLoop(framework::Executor* executor,
                   framework::ProgramDesc* program,
                   framework::Scope* recv_scope,
                   framework::BlockDesc* prefetch_block) const;
Q
qiaolongfei 已提交
48

49 50 51 52
  void RunAsyncLoop(framework::Executor* executor,
                    framework::ProgramDesc* program,
                    framework::Scope* recv_scope,
                    framework::BlockDesc* prefetch_block) const;
Q
qiaolongfei 已提交
53

T
wip  
typhoonzero 已提交
54 55 56 57
  void StartServerThread();

  void ServerThreadEntry();

T
typhoonzero 已提交
58 59
  void Stop() override;

Q
qiaolongfei 已提交
60 61
  void RunImpl(const framework::Scope& scope,
               const platform::Place& dev_place) const override;
T
typhoonzero 已提交
62 63 64 65

 protected:
  mutable std::shared_ptr<detail::AsyncGRPCServer> rpc_service_;
  mutable std::shared_ptr<std::thread> server_thread_;
T
wip  
typhoonzero 已提交
66 67
  std::mutext server_ready_mutex_;
  std::condition_variable server_ready_;
T
typhoonzero 已提交
68 69 70 71
};

}  // namespace operators
}  // namespace paddle