channel.h 8.4 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
C
chengduo 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
16 17

#include <stddef.h>  // for size_t
18
#include <condition_variable>
19 20
#include <typeindex>
#include "paddle/fluid/platform/enforce.h"
C
chengduo 已提交
21 22

namespace paddle {
C
chengduo 已提交
23
namespace framework {
C
chengduo 已提交
24

25 26 27 28 29 30
enum class ChannelAction {
  SEND = 0,
  RECEIVE = 1,
  CLOSE = 2,
};

31
// Channel is the abstract class of buffered and un-buffered channels.
C
chengduo 已提交
32 33 34
template <typename T>
class Channel {
 public:
35 36
  virtual bool CanSend() = 0;
  virtual bool CanReceive() = 0;
37
  virtual void Send(T*) = 0;
C
chengduo 已提交
38
  virtual bool Receive(T*) = 0;
39
  virtual size_t Cap() = 0;
40
  virtual void Lock() = 0;
41

42
  virtual void Unlock() = 0;
43
  virtual bool IsClosed() = 0;
C
chengduo 已提交
44
  virtual void Close() = 0;
45
  virtual ~Channel() {}
46 47 48 49 50 51 52 53 54

  virtual void AddToSendQ(const void* referrer, T* data,
                          std::shared_ptr<std::condition_variable_any> cond,
                          std::function<bool(ChannelAction)> cb) = 0;
  virtual void AddToReceiveQ(const void* referrer, T* data,
                             std::shared_ptr<std::condition_variable_any> cond,
                             std::function<bool(ChannelAction)> cb) = 0;
  virtual void RemoveFromSendQ(const void* referrer) = 0;
  virtual void RemoveFromReceiveQ(const void* referrer) = 0;
55
};
C
chengduo 已提交
56

57 58
// Forward declaration of channel implementations.
template <typename T>
59
class ChannelImpl;
C
chengduo 已提交
60

61 62
template <typename T>
Channel<T>* MakeChannel(size_t buffer_size) {
63
  return new ChannelImpl<T>(buffer_size);
64
}
C
chengduo 已提交
65

66 67
template <typename T>
void CloseChannel(Channel<T>* ch) {
C
chengduo 已提交
68
  ch->Close();
69
}
C
chengduo 已提交
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * The ChannelHolder class serves two main purposes:
 * 1. It acts as a unified wrapper for the different kinds of
 *    channels, i.e. Buffered and Unbuffered channels. This is
 *    similar to the ReaderHolder class.
 * 2. It also helps us in TypeHiding. This is similar to the
 *    PlaceHolder implementations in variable.h and tensor.h.
 */
class ChannelHolder {
 public:
  template <typename T>
  void Reset(size_t buffer_size) {
    holder_.reset(new PlaceholderImpl<T>(buffer_size));
  }

  template <typename T>
87 88 89 90 91 92
  void Send(T* data) {
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    PADDLE_ENFORCE_EQ(
        holder_->Type(), std::type_index(typeid(T)),
        "Channel type is not same as the type of the data being sent");
93 94
    // Static cast should be safe because we have ensured that types are same
    Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
95 96
    PADDLE_ENFORCE_EQ(channel != nullptr, true, "Channel should not be null.");
    channel->Send(data);
97 98 99 100
  }

  template <typename T>
  bool Receive(T* data) {
101 102 103 104 105
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    PADDLE_ENFORCE_EQ(
        holder_->Type(), std::type_index(typeid(T)),
        "Channel type is not same as the type of the data being sent");
106
    Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
107 108
    PADDLE_ENFORCE_EQ(channel != nullptr, true, "Channel should not be null.");
    return channel->Receive(data);
109 110
  }

111
  bool IsClosed() {
112 113 114
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    return holder_->IsClosed();
115 116 117
  }

  bool CanSend() {
118 119 120
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    return holder_->CanSend();
121 122 123
  }

  bool CanReceive() {
124 125 126
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    return holder_->CanReceive();
127 128
  }

129
  void close() {
130 131 132
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    holder_->Close();
133 134
  }

135
  size_t Cap() {
136 137 138
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    return holder_->Cap();
139 140 141
  }

  void Lock() {
142 143 144
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    holder_->Lock();
145 146 147
  }

  void Unlock() {
148 149 150
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    holder_->Unlock();
151 152
  }

153 154 155 156
  template <typename T>
  void AddToSendQ(const void* referrer, T* data,
                  std::shared_ptr<std::condition_variable_any> cond,
                  std::function<bool(ChannelAction)> cb) {
157 158 159 160 161
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
    if (channel != nullptr) {
      channel->AddToSendQ(referrer, data, cond, cb);
162 163 164 165 166 167 168
    }
  }

  template <typename T>
  void AddToReceiveQ(const void* referrer, T* data,
                     std::shared_ptr<std::condition_variable_any> cond,
                     std::function<bool(ChannelAction)> cb) {
169 170 171 172 173
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
    if (channel != nullptr) {
      channel->AddToReceiveQ(referrer, data, cond, cb);
174 175 176 177
    }
  }

  void RemoveFromSendQ(const void* referrer) {
178 179 180
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    holder_->RemoveFromSendQ(referrer);
181 182 183
  }

  void RemoveFromReceiveQ(const void* referrer) {
184 185 186
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
    holder_->RemoveFromReceiveQ(referrer);
187 188
  }

189 190
  inline bool IsInitialized() const { return holder_ != nullptr; }

191
  inline const std::type_index Type() {
192 193
    PADDLE_ENFORCE_EQ(IsInitialized(), true,
                      "The Channel hasn't been initialized");
194 195 196
    return holder_->Type();
  }

197 198 199 200 201 202 203 204 205
 private:
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a template
   *          parameter of ChannelHolder.
   */
  struct Placeholder {
    virtual ~Placeholder() {}
    virtual const std::type_index Type() const = 0;
    virtual void* Ptr() const = 0;
206 207 208
    virtual bool IsClosed() = 0;
    virtual bool CanSend() = 0;
    virtual bool CanReceive() = 0;
T
Thuan Nguyen 已提交
209 210
    virtual void RemoveFromSendQ(const void* referrer) = 0;
    virtual void RemoveFromReceiveQ(const void* referrer) = 0;
211
    virtual void Close() = 0;
212 213 214
    virtual void Lock() = 0;
    virtual void Unlock() = 0;
    virtual size_t Cap() = 0;
215 216 217 218 219 220 221 222 223
  };

  template <typename T>
  struct PlaceholderImpl : public Placeholder {
    PlaceholderImpl(size_t buffer_size) : type_(std::type_index(typeid(T))) {
      channel_.reset(MakeChannel<T>(buffer_size));
    }

    virtual const std::type_index Type() const { return type_; }
224

225
    virtual void* Ptr() const { return static_cast<void*>(channel_.get()); }
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    virtual bool IsClosed() {
      if (channel_) {
        return channel_->IsClosed();
      }
      return false;
    }

    virtual bool CanSend() {
      if (channel_) {
        return channel_->CanSend();
      }
      return false;
    }

    virtual bool CanReceive() {
      if (channel_) {
        return channel_->CanReceive();
      }
      return false;
    }

T
Thuan Nguyen 已提交
248 249 250 251 252 253 254 255 256 257 258 259
    virtual void RemoveFromSendQ(const void* referrer) {
      if (channel_) {
        channel_->RemoveFromSendQ(referrer);
      }
    }

    virtual void RemoveFromReceiveQ(const void* referrer) {
      if (channel_) {
        channel_->RemoveFromReceiveQ(referrer);
      }
    }

260 261 262 263
    virtual void Close() {
      if (channel_) channel_->Close();
    }

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    virtual size_t Cap() {
      if (channel_)
        return channel_->Cap();
      else
        return -1;
    }

    virtual void Lock() {
      if (channel_) channel_->Lock();
    }

    virtual void Unlock() {
      if (channel_) channel_->Unlock();
    }

279
    std::unique_ptr<Channel<T>> channel_;
280 281 282 283 284 285 286
    const std::type_index type_;
  };

  // Pointer to a PlaceholderImpl object
  std::unique_ptr<Placeholder> holder_;
};

287
}  // namespace framework
C
chengduo 已提交
288
}  // namespace paddle
289

290
#include "paddle/fluid/framework/channel_impl.h"