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
/* 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>
T
done  
typhoonzero 已提交
18
#include <atomic>
T
typhoonzero 已提交
19
#include <ostream>
20
#include <string>
T
typhoonzero 已提交
21 22 23 24 25 26 27 28 29 30 31

#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";
32
constexpr char kPrefetchBlock[] = "PrefetchBlock";
T
typhoonzero 已提交
33 34 35 36 37

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

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

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

48
  void RunAsyncLoop(framework::Executor* executor,
Q
qiaolongfei 已提交
49
                    framework::ProgramDesc* program) const;
Q
qiaolongfei 已提交
50

Y
yi.wu 已提交
51
  void SavePort() const;
T
done  
typhoonzero 已提交
52 53

  void WaitServerReady();
T
wip  
typhoonzero 已提交
54

T
done  
typhoonzero 已提交
55
  int GetSelectedPort() { return selected_port_; }
T
wip  
typhoonzero 已提交
56

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

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

T
done  
typhoonzero 已提交
62 63
  static void ResetPort() { selected_port_ = 0; }

T
typhoonzero 已提交
64 65 66
 protected:
  mutable std::shared_ptr<detail::AsyncGRPCServer> rpc_service_;
  mutable std::shared_ptr<std::thread> server_thread_;
T
done  
typhoonzero 已提交
67 68
  // FIXME(wuyi): it's static so that the operator can be cloned.
  static std::atomic_int selected_port_;
T
typhoonzero 已提交
69 70 71 72
};

}  // namespace operators
}  // namespace paddle