buffered_channel.h 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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
#include <atomic>
17 18 19 20 21
#include <condition_variable>
#include <deque>
#include <mutex>

#include "paddle/framework/channel.h"
C
chengduo 已提交
22
#include "paddle/platform/enforce.h"
23 24 25 26 27 28 29 30 31 32 33

namespace paddle {
namespace framework {
namespace details {

template <typename T>
class Buffered : public paddle::framework::Channel<T> {
  friend Channel<T>* paddle::framework::MakeChannel<T>(size_t);
  friend void paddle::framework::CloseChannel<T>(Channel<T>*);

 public:
C
chengduo 已提交
34 35
  virtual bool Send(T*);
  virtual bool Receive(T*);
36
  virtual size_t Cap() { return cap_; }
C
chengduo 已提交
37 38
  virtual void Close();
  virtual ~Buffered();
39 40 41 42 43 44

 private:
  size_t cap_;
  std::mutex mu_;
  std::condition_variable empty_cond_var_;
  std::condition_variable full_cond_var_;
45
  std::condition_variable destructor_cond_var_;
46
  std::deque<T> channel_;
47
  std::atomic<bool> closed_{false};
48 49
  std::atomic<unsigned> send_ctr{0};
  std::atomic<unsigned> recv_ctr{0};
50

C
chengduo 已提交
51 52 53
  Buffered(size_t cap) : cap_(cap), closed_(false) {
    PADDLE_ENFORCE_GT(cap, 0);
  }
54

55
  void NotifyAllParticipants(std::unique_lock<std::mutex>*);
56 57 58
};

template <typename T>
C
chengduo 已提交
59
bool Buffered<T>::Send(T* item) {
60 61 62 63
  bool ret = false;
  if (closed_) {
    return ret;
  }
64
  send_ctr++;
65
  std::unique_lock<std::mutex> lock(mu_);
C
chengduo 已提交
66 67 68 69 70 71
  full_cond_var_.wait(lock,
                      [this]() { return channel_.size() < cap_ || closed_; });
  if (!closed_) {
    channel_.push_back(std::move(*item));
    lock.unlock();
    empty_cond_var_.notify_one();
C
chengduo 已提交
72
    ret = true;
C
chengduo 已提交
73
  }
74 75
  send_ctr--;
  destructor_cond_var_.notify_one();
C
chengduo 已提交
76
  return ret;
77 78 79
}

template <typename T>
C
chengduo 已提交
80
bool Buffered<T>::Receive(T* item) {
81 82 83 84 85 86 87
  bool ret = false;
  // Once the channel has been closed and all data has been consumed,
  // just return false. Don't even try acquiring the mutex.
  if (closed_ && channel_.empty()) {
    return false;
  }
  recv_ctr++;
88
  std::unique_lock<std::mutex> lock(mu_);
C
chengduo 已提交
89
  empty_cond_var_.wait(lock, [this]() { return !channel_.empty() || closed_; });
C
chengduoZH 已提交
90
  if (!channel_.empty()) {
C
chengduo 已提交
91 92
    *item = std::move(channel_.front());
    channel_.pop_front();
C
chengduo 已提交
93 94
    full_cond_var_.notify_one();
    ret = true;
C
chengduo 已提交
95
  }
96 97
  recv_ctr--;
  destructor_cond_var_.notify_one();
C
chengduo 已提交
98
  return ret;
C
chengduo 已提交
99 100 101 102
}

template <typename T>
void Buffered<T>::Close() {
103 104 105
  if (closed_) {
    return;
  }
C
chengduo 已提交
106 107
  std::unique_lock<std::mutex> lock(mu_);
  closed_ = true;
108
  NotifyAllParticipants(&lock);
109 110 111 112 113
}

template <typename T>
Buffered<T>::~Buffered() {
  std::unique_lock<std::mutex> lock(mu_);
C
chengduo 已提交
114
  closed_ = true;
115
  channel_.clear();
116
  NotifyAllParticipants(&lock);
117 118 119 120 121 122

  // The destructor must wait for all readers and writers to complete their task
  // The channel has been closed, so we will not accept new readers and writers
  lock.lock();
  destructor_cond_var_.wait(
      lock, [this]() { return send_ctr == 0 && recv_ctr == 0; });
123 124
}

125 126 127 128 129 130 131
template <typename T>
void Buffered<T>::NotifyAllParticipants(std::unique_lock<std::mutex>* lock) {
  lock->unlock();
  full_cond_var_.notify_all();
  empty_cond_var_.notify_all();
}

132 133 134
}  // namespace details
}  // namespace framework
}  // namespace paddle