channel.h 5.6 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2019 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 <string>
#include <utility>
G
guru4elephant 已提交
18
#include "core/predictor/common/inner_common.h"
W
wangguibao 已提交
19 20 21 22 23 24 25 26

namespace baidu {
namespace paddle_serving {
namespace predictor {

class Channel;

class Bus {
W
wangguibao 已提交
27 28
 public:
  Bus() { clear(); }
W
wangguibao 已提交
29

W
wangguibao 已提交
30 31 32 33 34 35
  int regist(const std::string& op, Channel* channel) {
    std::pair<boost::unordered_map<std::string, Channel*>::iterator, bool> r =
        _op_channels.insert(std::make_pair(op, channel));
    if (!r.second) {
      LOG(ERROR) << "Failed insert op&channel into bus:" << op;
      return -1;
W
wangguibao 已提交
36
    }
W
wangguibao 已提交
37 38
    return 0;
  }
W
wangguibao 已提交
39

W
wangguibao 已提交
40 41 42 43 44 45
  Channel* channel_by_name(const std::string& op_name) {
    typename boost::unordered_map<std::string, Channel*>::iterator it =
        _op_channels.find(op_name);
    if (it == _op_channels.end()) {
      LOG(WARNING) << "Not found channel in bus, op_name:" << op_name << ".";
      return NULL;
W
wangguibao 已提交
46 47
    }

W
wangguibao 已提交
48 49
    return it->second;
  }
W
wangguibao 已提交
50

W
wangguibao 已提交
51 52 53
  void clear() { _op_channels.clear(); }

  size_t size() const { return _op_channels.size(); }
W
wangguibao 已提交
54

W
wangguibao 已提交
55 56
 private:
  boost::unordered_map<std::string, Channel*> _op_channels;
W
wangguibao 已提交
57 58 59
};

class Channel {
W
wangguibao 已提交
60 61
 public:
  Channel() {}
W
wangguibao 已提交
62

W
wangguibao 已提交
63 64 65 66 67
  void init(uint32_t id, const char* op) {
    _id = id;
    _op = std::string(op);
    clear_data();
  }
W
wangguibao 已提交
68

W
wangguibao 已提交
69
  void deinit() { clear_data(); }
W
wangguibao 已提交
70

W
wangguibao 已提交
71
  uint32_t id() const { return _id; }
W
wangguibao 已提交
72

W
wangguibao 已提交
73
  const std::string& op() { return _op; }
W
wangguibao 已提交
74

B
barriery 已提交
75
  int share_to_bus(Bus* bus, const uint64_t log_id) {
W
wangguibao 已提交
76
    if (bus->regist(_op, this) != 0) {
B
barriery 已提交
77 78
      LOG(ERROR) << "(logid=" << log_id << ") Failed regist channel[" << _op
                 << "] to bus!";
W
wangguibao 已提交
79
      return -1;
W
wangguibao 已提交
80 81
    }

W
wangguibao 已提交
82 83 84 85
    return 0;
  }

  virtual void clear_data() = 0;
W
wangguibao 已提交
86

W
wangguibao 已提交
87 88
  virtual void* param() = 0;
  virtual const void* param() const = 0;
W
wangguibao 已提交
89

W
wangguibao 已提交
90 91
  virtual google::protobuf::Message* message() = 0;
  virtual const google::protobuf::Message* message() const = 0;
W
wangguibao 已提交
92

W
wangguibao 已提交
93
  virtual Channel& operator=(const Channel& channel) = 0;
W
wangguibao 已提交
94

W
wangguibao 已提交
95
  virtual std::string debug_string() const = 0;
W
wangguibao 已提交
96

W
wangguibao 已提交
97 98 99
 private:
  uint32_t _id;
  std::string _op;
W
wangguibao 已提交
100 101
};

W
wangguibao 已提交
102
template <typename T>
W
wangguibao 已提交
103
class OpChannel : public Channel {
W
wangguibao 已提交
104 105
 public:
  OpChannel() {}
W
wangguibao 已提交
106

W
wangguibao 已提交
107
  void clear_data() { _data.Clear(); }
W
wangguibao 已提交
108

W
wangguibao 已提交
109
  void* param() { return &_data; }
W
wangguibao 已提交
110

W
wangguibao 已提交
111
  const void* param() const { return &_data; }
W
wangguibao 已提交
112

W
wangguibao 已提交
113 114 115 116 117
  google::protobuf::Message* message() {
    return message_impl(
        derived_from_message<
            TIsDerivedFromB<T, google::protobuf::Message>::RESULT>());
  }
W
wangguibao 已提交
118

W
wangguibao 已提交
119
  google::protobuf::Message* message_impl(derived_from_message<true>) {
W
wangguibao 已提交
120
    return dynamic_cast<google::protobuf::Message*>(&_data);
W
wangguibao 已提交
121
  }
W
wangguibao 已提交
122

W
wangguibao 已提交
123 124 125 126 127
  google::protobuf::Message* message_impl(derived_from_message<false>) {
    LOG(ERROR) << "Current type: " << typeid(T).name()
               << " is not derived from protobuf.";
    return NULL;
  }
W
wangguibao 已提交
128

W
wangguibao 已提交
129 130 131 132 133
  const google::protobuf::Message* message() const {
    return message_impl(
        derived_from_message<
            TIsDerivedFromB<T, google::protobuf::Message>::RESULT>());
  }
W
wangguibao 已提交
134

W
wangguibao 已提交
135 136 137 138
  const google::protobuf::Message* message_impl(
      derived_from_message<true>) const {
    return dynamic_cast<const google::protobuf::Message*>(&_data);
  }
W
wangguibao 已提交
139

W
wangguibao 已提交
140 141 142 143 144 145
  const google::protobuf::Message* message_impl(
      derived_from_message<false>) const {
    LOG(ERROR) << "Current type: " << typeid(T).name()
               << " is not derived from protobuf.";
    return NULL;
  }
W
wangguibao 已提交
146

W
wangguibao 已提交
147 148 149 150
  Channel& operator=(const Channel& channel) {
    _data = *(dynamic_cast<const OpChannel<T>&>(channel)).data();
    return *this;
  }
W
wangguibao 已提交
151

W
wangguibao 已提交
152
  std::string debug_string() const { return _data.ShortDebugString(); }
W
wangguibao 已提交
153

W
wangguibao 已提交
154
  // functions of derived class
W
wangguibao 已提交
155

W
wangguibao 已提交
156
  T* data() { return &_data; }
W
wangguibao 已提交
157

W
wangguibao 已提交
158 159 160 161 162 163 164 165 166
  const T* data() const { return &_data; }

  Channel& operator=(const T& obj) {
    _data = obj;
    return *this;
  }

 private:
  T _data;
W
wangguibao 已提交
167 168
};

W
wangguibao 已提交
169
template <>
W
wangguibao 已提交
170
class OpChannel<google::protobuf::Message> : public Channel {
W
wangguibao 已提交
171 172
 public:
  OpChannel<google::protobuf::Message>() : _data(NULL) {}
W
wangguibao 已提交
173

W
wangguibao 已提交
174
  virtual ~OpChannel<google::protobuf::Message>() { _data = NULL; }
W
wangguibao 已提交
175

W
wangguibao 已提交
176
  void clear_data() { _data = NULL; }
W
wangguibao 已提交
177

W
wangguibao 已提交
178
  void* param() { return const_cast<void*>((const void*)_data); }
W
wangguibao 已提交
179

W
wangguibao 已提交
180
  const void* param() const { return _data; }
W
wangguibao 已提交
181

W
wangguibao 已提交
182 183 184
  google::protobuf::Message* message() {
    return const_cast<google::protobuf::Message*>(_data);
  }
W
wangguibao 已提交
185

W
wangguibao 已提交
186
  const google::protobuf::Message* message() const { return _data; }
W
wangguibao 已提交
187

W
wangguibao 已提交
188 189 190 191
  Channel& operator=(const Channel& channel) {
    _data = channel.message();
    return *this;
  }
W
wangguibao 已提交
192

W
wangguibao 已提交
193 194 195 196 197
  std::string debug_string() const {
    if (_data) {
      return _data->ShortDebugString();
    } else {
      return "{\"Error\": \"Null Message Ptr\"}";
W
wangguibao 已提交
198
    }
W
wangguibao 已提交
199
  }
W
wangguibao 已提交
200

W
wangguibao 已提交
201 202 203 204
  // derived function imiplements
  google::protobuf::Message* data() {
    return const_cast<google::protobuf::Message*>(_data);
  }
W
wangguibao 已提交
205

W
wangguibao 已提交
206
  const google::protobuf::Message* data() const { return _data; }
W
wangguibao 已提交
207

W
wangguibao 已提交
208 209 210 211 212
  OpChannel<google::protobuf::Message>& operator=(
      google::protobuf::Message* message) {
    _data = message;
    return *this;
  }
W
wangguibao 已提交
213

W
wangguibao 已提交
214 215 216 217 218
  OpChannel<google::protobuf::Message>& operator=(
      const google::protobuf::Message* message) {
    _data = message;
    return *this;
  }
W
wangguibao 已提交
219

W
wangguibao 已提交
220 221
 private:
  const google::protobuf::Message* _data;
W
wangguibao 已提交
222 223
};

W
wangguibao 已提交
224 225 226
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu