reader.h 2.8 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

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

#include <memory>
#include <thread>
#include <vector>
F
fengjiayi 已提交
24 25 26 27

namespace paddle {
namespace framework {

F
fengjiayi 已提交
28
class ReaderBase {
F
fengjiayi 已提交
29
 public:
F
fengjiayi 已提交
30
  virtual void ReadNext(std::vector<LoDTensor>* out) = 0;
F
fengjiayi 已提交
31

F
fengjiayi 已提交
32 33
  virtual void ReInit() = 0;

Y
Yu Yang 已提交
34 35
  virtual bool HasNext() const = 0;

36 37
  virtual std::vector<std::unique_ptr<ReaderBase>> SplitReader(
      const platform::PlaceList& places);
F
fengjiayi 已提交
38

Y
Yu Yang 已提交
39
  virtual ~ReaderBase();
F
fengjiayi 已提交
40 41
};

F
fengjiayi 已提交
42
class DecoratedReader : public ReaderBase {
F
fengjiayi 已提交
43
 public:
Y
Yu Yang 已提交
44
  explicit DecoratedReader(ReaderBase* reader) : ReaderBase(), reader_(reader) {
F
fengjiayi 已提交
45 46 47
    PADDLE_ENFORCE_NOT_NULL(reader_);
  }

F
fengjiayi 已提交
48 49
  void ReInit() override { reader_->ReInit(); }

Y
Yu Yang 已提交
50 51
  bool HasNext() const override { return reader_->HasNext(); }

F
fengjiayi 已提交
52 53 54 55
 protected:
  ReaderBase* reader_;
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
class ThreadSafeReader : public DecoratedReader {
 public:
  ThreadSafeReader(ReaderBase* reader, const std::shared_ptr<std::mutex>& mutex)
      : DecoratedReader(reader), mutex_(mutex) {}

  void ReadNext(std::vector<LoDTensor>* out) override;

  void ReInit() override;

  bool HasNext() const override;

  std::vector<std::unique_ptr<ReaderBase>> SplitReader(
      const platform::PlaceList& places) override;

 private:
  std::shared_ptr<std::mutex> mutex_;
};

class FileReaderBase : public ReaderBase {
 public:
  explicit FileReaderBase(const std::vector<DDim>& dims);

  void ReadNext(std::vector<LoDTensor>* out) override;

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

 private:
  std::vector<DDim> dims_;
};

87 88
// The ReaderHolder is used as reader' unified wrapper,
// making it easier to access different type reader in Variables.
F
fengjiayi 已提交
89 90 91 92 93 94
class ReaderHolder {
 public:
  void Reset(ReaderBase* reader) { reader_.reset(reader); }

  ReaderBase* Get() const { return reader_.get(); }

95 96 97 98 99 100 101 102
  void ReadNext(std::vector<LoDTensor>* out) {
    PADDLE_ENFORCE_NOT_NULL(reader_);
    reader_->ReadNext(out);
  }
  void ReInit() {
    PADDLE_ENFORCE_NOT_NULL(reader_);
    reader_->ReInit();
  }
F
fengjiayi 已提交
103

Y
Yu Yang 已提交
104 105
  bool HasNext() const { return reader_->HasNext(); }

F
fengjiayi 已提交
106 107 108 109
 private:
  std::unique_ptr<ReaderBase> reader_;
};

F
fengjiayi 已提交
110 111
}  // namespace framework
}  // namespace paddle