channel.h 4.2 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 19
#include <typeindex>
#include "paddle/fluid/platform/enforce.h"
C
chengduo 已提交
20 21

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

24
// Channel is the abstract class of buffered and un-buffered channels.
C
chengduo 已提交
25 26 27
template <typename T>
class Channel {
 public:
C
chengduo 已提交
28 29
  virtual bool Send(T*) = 0;
  virtual bool Receive(T*) = 0;
30
  virtual size_t Cap() = 0;
31 32
  virtual void Lock() = 0;
  virtual void Unlock() = 0;
C
chengduo 已提交
33
  virtual void Close() = 0;
34 35
  virtual ~Channel() {}
};
C
chengduo 已提交
36

37 38
// Forward declaration of channel implementations.
template <typename T>
39
class ChannelImpl;
C
chengduo 已提交
40

41 42
template <typename T>
Channel<T>* MakeChannel(size_t buffer_size) {
43
  return new ChannelImpl<T>(buffer_size);
44
}
C
chengduo 已提交
45

46 47
template <typename T>
void CloseChannel(Channel<T>* ch) {
C
chengduo 已提交
48
  ch->Close();
49
}
C
chengduo 已提交
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 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>
  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;
  }

  void close() {
    if (IsInitialized()) holder_->Close();
  }

87 88 89 90 91 92 93 94 95 96 97 98 99
  size_t Cap() {
    if (IsInitialized()) return holder_->Cap();
    return -1;
  }

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

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

100 101
  inline bool IsInitialized() const { return holder_ != nullptr; }

102 103 104 105 106
  inline const std::type_index Type() {
    PADDLE_ENFORCE_EQ(IsInitialized(), true);
    return holder_->Type();
  }

107 108 109 110 111 112 113 114 115
 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;
116
    virtual void Close() = 0;
117 118 119
    virtual void Lock() = 0;
    virtual void Unlock() = 0;
    virtual size_t Cap() = 0;
120 121 122 123 124 125 126 127 128
  };

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

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

132 133 134 135
    virtual void Close() {
      if (channel_) channel_->Close();
    }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    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();
    }

151
    std::unique_ptr<Channel<T>> channel_;
152 153 154 155 156 157 158
    const std::type_index type_;
  };

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

159
}  // namespace framework
C
chengduo 已提交
160
}  // namespace paddle
161

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