reader.h 2.5 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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

F
fengjiayi 已提交
17
#include <atomic>
F
fengjiayi 已提交
18 19 20
#include <memory>
#include <vector>

Y
Yi Wang 已提交
21 22
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
23 24
#include "paddle/fluid/platform/place.h"

F
fengjiayi 已提交
25 26 27
namespace paddle {
namespace framework {

28 29
enum ReaderStatus { kRunning, kStopped };

F
fengjiayi 已提交
30
class ReaderBase {
F
fengjiayi 已提交
31
 public:
32 33 34
  void ReadNext(std::vector<LoDTensor>* out);

  void Shutdown();
F
fengjiayi 已提交
35

36
  void Start();
F
fengjiayi 已提交
37

Y
Yu Yang 已提交
38
  virtual ~ReaderBase();
39 40 41 42 43 44 45 46

 protected:
  virtual void ReadNextImpl(std::vector<LoDTensor>* out) = 0;

  virtual void ShutdownImpl() = 0;

  virtual void StartImpl() = 0;

F
fengjiayi 已提交
47
  std::atomic<ReaderStatus> status_{kRunning};
F
fengjiayi 已提交
48 49
};

F
fengjiayi 已提交
50
class DecoratedReader : public ReaderBase {
F
fengjiayi 已提交
51
 public:
F
fengjiayi 已提交
52 53
  explicit DecoratedReader(const std::shared_ptr<ReaderBase>& reader)
      : ReaderBase(), reader_(reader) {
F
fengjiayi 已提交
54 55 56
    PADDLE_ENFORCE_NOT_NULL(reader_);
  }

F
fengjiayi 已提交
57
 protected:
58 59 60 61
  void ShutdownImpl() override { reader_->Shutdown(); }

  void StartImpl() override { reader_->Start(); }

F
fengjiayi 已提交
62
  std::shared_ptr<ReaderBase> reader_;
F
fengjiayi 已提交
63 64
};

Y
Yu Yang 已提交
65
class FileReader : public ReaderBase {
66
 public:
67
  FileReader() : ReaderBase() {}
68 69

 protected:
70 71 72
  void ShutdownImpl() override {}

  void StartImpl() override {}
73 74
};

75 76
// The ReaderHolder is used as reader' unified wrapper,
// making it easier to access different type reader in Variables.
F
fengjiayi 已提交
77 78
class ReaderHolder {
 public:
F
fengjiayi 已提交
79
  void Reset(ReaderBase* reader) { reader_.reset(reader); }
F
fengjiayi 已提交
80

F
fengjiayi 已提交
81
  std::shared_ptr<ReaderBase> Get() const { return reader_; }
F
fengjiayi 已提交
82

83 84 85 86
  void ReadNext(std::vector<LoDTensor>* out) {
    PADDLE_ENFORCE_NOT_NULL(reader_);
    reader_->ReadNext(out);
  }
87 88 89 90 91 92 93 94 95 96 97

  void ResetAll() {
    // TODO(fengjiayi): The interface of reseting all.
  }

  void Shutdown() {
    PADDLE_ENFORCE_NOT_NULL(reader_);
    reader_->Shutdown();
  }

  void Start() {
98
    PADDLE_ENFORCE_NOT_NULL(reader_);
99
    reader_->Start();
100
  }
F
fengjiayi 已提交
101 102

 private:
F
fengjiayi 已提交
103
  std::shared_ptr<ReaderBase> reader_;
F
fengjiayi 已提交
104 105
};

F
fengjiayi 已提交
106 107
}  // namespace framework
}  // namespace paddle