heter_service.h 2.3 KB
Newer Older
T
Thunderbrook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2018 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 <fstream>
#include <memory>
#include <mutex>  // NOLINT
#include <string>
#include <thread>         // NOLINT
#include <unordered_map>  // NOLINT
#include <unordered_set>  // NOLINT
#include <vector>
#include "paddle/fluid/framework/heter_service.pb.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
T
Thunderbrook 已提交
28
#if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS)
T
Thunderbrook 已提交
29 30 31
#include "brpc/channel.h"
#include "brpc/controller.h"
#include "brpc/server.h"
T
Thunderbrook 已提交
32
#include "paddle/fluid/platform/timer.h"
T
Thunderbrook 已提交
33
#endif
T
Thunderbrook 已提交
34 35 36 37

namespace paddle {
namespace framework {

T
Thunderbrook 已提交
38
#if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS)
T
Thunderbrook 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
typedef std::function<int(const HeterRequest*, HeterResponse*)>
    HeterServiceHandler;
class DataFeed;

class HeterXpuService : public HeterService {
 public:
  HeterXpuService() {}
  virtual ~HeterXpuService() {}
  void service(::google::protobuf::RpcController* controller,
               const HeterRequest* request, HeterResponse* response,
               ::google::protobuf::Closure* done) {
    brpc::ClosureGuard done_guard(done);
    int ret = 0;
    int cmd = request->cmd();
    auto itr = handler_map_.find(cmd);
    if (itr == handler_map_.end()) {
    } else {
      ret = itr->second(request, response);
    }
    // response->set_err_code(0);
    // response->set_err_msg("");
    if (ret != 0) {
      // response->set_err_code(-1);
      // response->set_err_msg("xpu service error");
    }
  }

  void RegisterServiceHandler(int cmd, HeterServiceHandler func) {
    VLOG(0) << "register heter service";
    handler_map_[cmd] = func;
  }

 private:
  std::unordered_map<int, HeterServiceHandler> handler_map_;
};

T
Thunderbrook 已提交
75
#endif
T
Thunderbrook 已提交
76 77 78

}  // namespace framework
}  // namespace paddle