|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include <stddef.h> // for size_t |
| 17 | +#include <atomic> |
| 18 | +#include <condition_variable> |
| 19 | +#include <deque> |
| 20 | +#include "paddle/fluid/framework/channel.h" |
| 21 | +#include "paddle/fluid/platform/enforce.h" |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace framework { |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +class ChannelImpl : public paddle::framework::Channel<T> { |
| 28 | + friend Channel<T> *paddle::framework::MakeChannel<T>(size_t); |
| 29 | + friend void paddle::framework::CloseChannel<T>(Channel<T> *); |
| 30 | + |
| 31 | + public: |
| 32 | + virtual bool Send(T *); |
| 33 | + virtual bool Receive(T *); |
| 34 | + virtual size_t Cap() { return cap_; } |
| 35 | + virtual void Lock(); |
| 36 | + virtual void Unlock(); |
| 37 | + virtual void Close(); |
| 38 | + |
| 39 | + ChannelImpl(size_t); |
| 40 | + virtual ~ChannelImpl(); |
| 41 | + |
| 42 | + private: |
| 43 | + struct QueueMessage { |
| 44 | + T *data; |
| 45 | + std::condition_variable_any cond; |
| 46 | + bool chan_closed = false; |
| 47 | + bool completed = false; |
| 48 | + |
| 49 | + QueueMessage(T *item) : data(item) {} |
| 50 | + |
| 51 | + void Wait(std::unique_lock<std::recursive_mutex> &lock) { |
| 52 | + cond.wait(lock, [this]() { return completed; }); |
| 53 | + } |
| 54 | + |
| 55 | + void Notify() { |
| 56 | + completed = true; |
| 57 | + cond.notify_all(); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + bool send_return(bool value) { |
| 62 | + send_ctr--; |
| 63 | + destructor_cond_.notify_all(); |
| 64 | + return value; |
| 65 | + } |
| 66 | + |
| 67 | + bool recv_return(bool value) { |
| 68 | + recv_ctr--; |
| 69 | + destructor_cond_.notify_all(); |
| 70 | + return value; |
| 71 | + } |
| 72 | + |
| 73 | + size_t cap_; |
| 74 | + std::recursive_mutex mu_; |
| 75 | + bool closed_; |
| 76 | + std::deque<T> buf_; |
| 77 | + std::deque<std::shared_ptr<QueueMessage>> recvq; |
| 78 | + std::deque<std::shared_ptr<QueueMessage>> sendq; |
| 79 | + std::atomic<unsigned> send_ctr{0}; |
| 80 | + std::atomic<unsigned> recv_ctr{0}; |
| 81 | + std::condition_variable_any destructor_cond_; |
| 82 | +}; |
| 83 | + |
| 84 | +template <typename T> |
| 85 | +ChannelImpl<T>::ChannelImpl(size_t capacity) |
| 86 | + : cap_(capacity), closed_(false), send_ctr(0), recv_ctr(0) { |
| 87 | + PADDLE_ENFORCE_GE(capacity, 0); |
| 88 | +} |
| 89 | + |
| 90 | +template <typename T> |
| 91 | +bool ChannelImpl<T>::Send(T *item) { |
| 92 | + send_ctr++; |
| 93 | + std::unique_lock<std::recursive_mutex> lock{mu_}; |
| 94 | + |
| 95 | + // If channel is closed, do nothing |
| 96 | + if (closed_) { |
| 97 | + lock.unlock(); |
| 98 | + // TODO(abhinavarora) Should panic on closed channel |
| 99 | + return send_return(false); |
| 100 | + } |
| 101 | + |
| 102 | + // If there is a receiver, directly pass the value we want |
| 103 | + // to send to the receiver, bypassing the channel buffer if any |
| 104 | + if (!recvq.empty()) { |
| 105 | + std::shared_ptr<QueueMessage> m = recvq.front(); |
| 106 | + recvq.pop_front(); |
| 107 | + // Do the data transfer |
| 108 | + *(m->data) = std::move(*item); |
| 109 | + // Wake up the blocked process and unlock |
| 110 | + m->Notify(); |
| 111 | + lock.unlock(); |
| 112 | + return send_return(true); |
| 113 | + } |
| 114 | + |
| 115 | + // Unbuffered channel will always bypass this |
| 116 | + // If buffered channel has space in buffer, |
| 117 | + // write the element to the buffer. |
| 118 | + if (buf_.size() < cap_) { |
| 119 | + // Copy to buffer |
| 120 | + buf_.push_back(std::move(*item)); |
| 121 | + // Release lock and return true |
| 122 | + lock.unlock(); |
| 123 | + return send_return(true); |
| 124 | + } |
| 125 | + |
| 126 | + // Block on channel, because some receiver will complete |
| 127 | + // the operation for us |
| 128 | + auto m = std::make_shared<QueueMessage>(item); |
| 129 | + sendq.push_back(m); |
| 130 | + m->Wait(lock); |
| 131 | + // TODO(abhinavarora) Should panic on closed channel |
| 132 | + return send_return(!m->chan_closed); |
| 133 | +} |
| 134 | + |
| 135 | +template <typename T> |
| 136 | +bool ChannelImpl<T>::Receive(T *item) { |
| 137 | + recv_ctr++; |
| 138 | + std::unique_lock<std::recursive_mutex> lock{mu_}; |
| 139 | + |
| 140 | + // If channel is closed and buffer is empty or |
| 141 | + // channel is unbuffered |
| 142 | + if (closed_ && buf_.empty()) { |
| 143 | + lock.unlock(); |
| 144 | + return recv_return(false); |
| 145 | + } |
| 146 | + |
| 147 | + // If there is a sender, directly receive the value we want |
| 148 | + // from the sender, bypassing the channel buffer if any |
| 149 | + if (!sendq.empty()) { |
| 150 | + std::shared_ptr<QueueMessage> m = sendq.front(); |
| 151 | + sendq.pop_front(); |
| 152 | + // Do the data transfer |
| 153 | + *item = std::move(*(m->data)); |
| 154 | + // Wake up the blocked process and unlock |
| 155 | + m->Notify(); |
| 156 | + lock.unlock(); |
| 157 | + return recv_return(true); |
| 158 | + } |
| 159 | + |
| 160 | + // If this is a buffered channel and there are items in buffer |
| 161 | + if (buf_.size() > 0) { |
| 162 | + // Directly read from buffer |
| 163 | + *item = std::move(buf_.front()); |
| 164 | + buf_.pop_front(); |
| 165 | + // Release lock and return true |
| 166 | + lock.unlock(); |
| 167 | + return recv_return(true); |
| 168 | + } |
| 169 | + |
| 170 | + // No sender available, block on this channel |
| 171 | + // Some receiver will complete the option for us |
| 172 | + auto m = std::make_shared<QueueMessage>(item); |
| 173 | + recvq.push_back(m); |
| 174 | + m->Wait(lock); |
| 175 | + |
| 176 | + return recv_return(!m->chan_closed); |
| 177 | +} |
| 178 | + |
| 179 | +template <typename T> |
| 180 | +void ChannelImpl<T>::Lock() { |
| 181 | + mu_.lock(); |
| 182 | +} |
| 183 | + |
| 184 | +template <typename T> |
| 185 | +void ChannelImpl<T>::Unlock() { |
| 186 | + mu_.unlock(); |
| 187 | +} |
| 188 | + |
| 189 | +template <typename T> |
| 190 | +void ChannelImpl<T>::Close() { |
| 191 | + std::unique_lock<std::recursive_mutex> lock{mu_}; |
| 192 | + |
| 193 | + if (closed_) { |
| 194 | + // TODO(abhinavarora): closing an already closed channel should panic |
| 195 | + lock.unlock(); |
| 196 | + return; |
| 197 | + } |
| 198 | + |
| 199 | + closed_ = true; |
| 200 | + |
| 201 | + // Empty the readers |
| 202 | + while (!recvq.empty()) { |
| 203 | + std::shared_ptr<QueueMessage> m = recvq.front(); |
| 204 | + recvq.pop_front(); |
| 205 | + m->chan_closed = true; |
| 206 | + m->Notify(); |
| 207 | + } |
| 208 | + |
| 209 | + // Empty the senders |
| 210 | + while (!sendq.empty()) { |
| 211 | + std::shared_ptr<QueueMessage> m = sendq.front(); |
| 212 | + sendq.pop_front(); |
| 213 | + m->chan_closed = true; |
| 214 | + m->Notify(); |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +template <typename T> |
| 219 | +ChannelImpl<T>::~ChannelImpl() { |
| 220 | + Close(); |
| 221 | + // The destructor must wait for all readers and writers to complete their task |
| 222 | + // The channel has been closed, so we will not accept new readers and writers |
| 223 | + std::unique_lock<std::recursive_mutex> lock{mu_}; |
| 224 | + destructor_cond_.wait(lock, |
| 225 | + [this]() { return send_ctr == 0 && recv_ctr == 0; }); |
| 226 | +} |
| 227 | + |
| 228 | +} // namespace framework |
| 229 | +} // namespace paddle |
0 commit comments