channel.h 3.7 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;
C
chengduo 已提交
31
  virtual void Close() = 0;
32 33
  virtual ~Channel() {}
};
C
chengduo 已提交
34

35 36 37 38 39 40 41
// Forward declaration of channel implementations.
namespace details {
template <typename T>
class Buffered;
template <typename T>
class UnBuffered;
}  // namespace details
C
chengduo 已提交
42

43 44 45 46
template <typename T>
Channel<T>* MakeChannel(size_t buffer_size) {
  if (buffer_size > 0) {
    return new details::Buffered<T>(buffer_size);
C
chengduo 已提交
47
  }
48 49
  return new details::UnBuffered<T>();
}
C
chengduo 已提交
50

51 52
template <typename T>
void CloseChannel(Channel<T>* ch) {
C
chengduo 已提交
53
  ch->Close();
54
}
C
chengduo 已提交
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 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;
  }

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

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

 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;
103
    virtual void Close() = 0;
104 105 106 107 108 109 110 111 112 113 114 115 116 117
  };

  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_; }
    virtual void* Ptr() const { return static_cast<void*>(channel_.get()); }
    virtual void Close() {
      if (channel_) channel_->Close();
    }

118
    std::unique_ptr<Channel<T>> channel_;
119 120 121 122 123 124 125
    const std::type_index type_;
  };

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

126
}  // namespace framework
C
chengduo 已提交
127
}  // namespace paddle
128

Y
Yi Wang 已提交
129 130
#include "paddle/fluid/framework/details/buffered_channel.h"
#include "paddle/fluid/framework/details/unbuffered_channel.h"