channel.h 7.1 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;
C
chengduo 已提交
37 38
  virtual bool Send(T*) = 0;
  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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/*
 * 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>
  bool Send(T* data) {
    if (!IsInitialized()) return false;
    PADDLE_ENFORCE_EQ(holder_->Type(), std::type_index(typeid(T)));
    // Static cast should be safe because we have ensured that types are same
    Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
    return channel != nullptr ? channel->Send(data) : false;
  }

  template <typename T>
  bool Receive(T* data) {
    if (!IsInitialized()) return false;
    PADDLE_ENFORCE_EQ(holder_->Type(), std::type_index(typeid(T)));
    Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
    return channel != nullptr ? channel->Receive(data) : false;
  }

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  bool IsClosed() {
    if (IsInitialized()) {
      return holder_->IsClosed();
    }
    return false;
  }

  bool CanSend() {
    if (IsInitialized()) {
      return holder_->CanSend();
    }
    return false;
  }

  bool CanReceive() {
    if (IsInitialized()) {
      return holder_->CanReceive();
    }
    return false;
  }

124 125 126 127
  void close() {
    if (IsInitialized()) holder_->Close();
  }

128 129 130 131 132 133 134 135 136 137 138 139 140
  size_t Cap() {
    if (IsInitialized()) return holder_->Cap();
    return -1;
  }

  void Lock() {
    if (IsInitialized()) holder_->Lock();
  }

  void Unlock() {
    if (IsInitialized()) holder_->Unlock();
  }

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  template <typename T>
  void AddToSendQ(const void* referrer, T* data,
                  std::shared_ptr<std::condition_variable_any> cond,
                  std::function<bool(ChannelAction)> cb) {
    if (IsInitialized()) {
      Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
      if (channel != nullptr) {
        channel->AddToSendQ(referrer, data, cond, cb);
      }
    }
  }

  template <typename T>
  void AddToReceiveQ(const void* referrer, T* data,
                     std::shared_ptr<std::condition_variable_any> cond,
                     std::function<bool(ChannelAction)> cb) {
    if (IsInitialized()) {
      Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
      if (channel != nullptr) {
        channel->AddToReceiveQ(referrer, data, cond, cb);
      }
    }
  }

  template <typename T>
  void RemoveFromSendQ(const void* referrer) {
    if (IsInitialized()) {
      Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
      if (channel != nullptr) {
        channel->RemoveFromSendQ(referrer);
      }
    }
  }

  template <typename T>
  void RemoveFromReceiveQ(const void* referrer) {
    if (IsInitialized()) {
      Channel<T>* channel = static_cast<Channel<T>*>(holder_->Ptr());
      if (channel != nullptr) {
        channel->RemoveFromReceiveQ(referrer);
      }
    }
  }

185 186
  inline bool IsInitialized() const { return holder_ != nullptr; }

187 188 189 190 191
  inline const std::type_index Type() {
    PADDLE_ENFORCE_EQ(IsInitialized(), true);
    return holder_->Type();
  }

192 193 194 195 196 197 198 199 200
 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;
201 202 203
    virtual bool IsClosed() = 0;
    virtual bool CanSend() = 0;
    virtual bool CanReceive() = 0;
204
    virtual void Close() = 0;
205 206 207
    virtual void Lock() = 0;
    virtual void Unlock() = 0;
    virtual size_t Cap() = 0;
208 209 210 211 212 213 214 215 216
  };

  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_; }
217

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

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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;
    }

241 242 243 244
    virtual void Close() {
      if (channel_) channel_->Close();
    }

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    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();
    }

260
    std::unique_ptr<Channel<T>> channel_;
261 262 263 264 265 266 267
    const std::type_index type_;
  };

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

268
}  // namespace framework
C
chengduo 已提交
269
}  // namespace paddle
270

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