channel.h 1.7 KB
Newer Older
C
chengduo 已提交
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 17

#include <stddef.h>  // for size_t
C
chengduo 已提交
18 19

namespace paddle {
C
chengduo 已提交
20
namespace framework {
C
chengduo 已提交
21

22
// Channel is the abstract class of buffered and un-buffered channels.
C
chengduo 已提交
23 24 25
template <typename T>
class Channel {
 public:
26 27 28
  virtual void Send(T*) = 0;
  virtual void Receive(T*) = 0;
  virtual size_t Cap() = 0;
C
chengduo 已提交
29

30 31 32 33
  // Don't delete channels; instead, call Channel::Close.
 protected:
  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 53 54 55 56
template <typename T>
void CloseChannel(Channel<T>* ch) {
  if (ch->Cap() > 0) {
    delete dynamic_cast<details::Buffered<T>*>(ch);
  } else {
    delete dynamic_cast<details::UnBuffered<T>*>(ch);
C
chengduo 已提交
57
  }
58
}
C
chengduo 已提交
59

60
}  // namespace framework
C
chengduo 已提交
61
}  // namespace paddle
62 63 64

#include "paddle/framework/details/buffered_channel.h"
#include "paddle/framework/details/unbuffered_channel.h"