channel.h 1.5 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:
C
chengduo 已提交
26 27
  virtual bool Send(T*) = 0;
  virtual bool Receive(T*) = 0;
28
  virtual size_t Cap() = 0;
C
chengduo 已提交
29
  virtual void Close() = 0;
30 31
  virtual ~Channel() {}
};
C
chengduo 已提交
32

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

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

49 50
template <typename T>
void CloseChannel(Channel<T>* ch) {
C
chengduo 已提交
51
  ch->Close();
52
}
C
chengduo 已提交
53

54
}  // namespace framework
C
chengduo 已提交
55
}  // namespace paddle
56 57 58

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