buffered_reader.h 3.5 KB
Newer Older
1
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Y
yuyang18 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// 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

#include <list>
S
sneaxiy 已提交
18
#include <memory>
Y
yuyang18 已提交
19
#include <queue>
Y
yuyang18 已提交
20
#include <vector>
W
wanghuancoder 已提交
21

Y
yuyang18 已提交
22 23
#include "ThreadPool.h"
#include "paddle/fluid/framework/reader.h"
24
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
25 26
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
#include "paddle/fluid/platform/device/gpu/gpu_resource_pool.h"
D
Dun Liang 已提交
27
#endif
28
#ifdef PADDLE_WITH_ASCEND_CL
29 30
#include "paddle/fluid/platform/device/npu/npu_info.h"
#include "paddle/fluid/platform/device/npu/npu_resource_pool.h"
31
#endif
F
fwenguang 已提交
32 33 34 35
#ifdef PADDLE_WITH_MLU
#include "paddle/fluid/platform/device/mlu/mlu_info.h"
#include "paddle/fluid/platform/device/mlu/mlu_resource_pool.h"
#endif
36 37 38 39
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/platform/device/xpu/xpu_info.h"
#include "paddle/fluid/platform/device/xpu/xpu_resource_pool.h"
#endif
F
fwenguang 已提交
40

Y
yuyang18 已提交
41 42 43 44 45 46 47 48 49 50
namespace paddle {
namespace operators {
namespace reader {

class BufferedReader : public framework::DecoratedReader {
  using TensorVec = std::vector<framework::LoDTensor>;
  using VecFuture = std::future<TensorVec>;

 public:
  BufferedReader(const std::shared_ptr<framework::ReaderBase>& reader,
51 52
                 const platform::Place& place, size_t buffer_size,
                 bool pin_memory = false);
Y
yuyang18 已提交
53 54 55 56

  ~BufferedReader() override;

 private:
Y
yuyang18 已提交
57
  void ReadTillBufferFullAsync();
Y
yuyang18 已提交
58

Y
yuyang18 已提交
59
  void ReadAsync(size_t i);
Y
yuyang18 已提交
60 61 62 63 64 65 66 67 68 69

 protected:
  void ShutdownImpl() override;
  void StartImpl() override;
  void ReadNextImpl(std::vector<framework::LoDTensor>* out) override;

 private:
  ThreadPool thread_pool_;
  platform::Place place_;
  const size_t buffer_size_;
70
  bool pin_memory_;
Y
yuyang18 已提交
71 72

  std::queue<std::future<size_t>> position_;
Y
yuyang18 已提交
73 74 75

  // The buffer for reading data.
  // NOTE: the simplest way to implement buffered reader is do not use any
Y
yuyang18 已提交
76 77
  // buffer, just read async and create futures as buffer size. However, to
  // malloc tensors every time is extremely slow. Here we store all data in
Y
yuyang18 已提交
78
  // buffers and prevent alloc every time.
Y
yuyang18 已提交
79
  std::vector<TensorVec> cpu_buffer_;
80
  std::vector<TensorVec> cuda_buffer_;
81
  std::vector<TensorVec> npu_buffer_;
F
fwenguang 已提交
82
  std::vector<TensorVec> mlu_buffer_;
83
  std::vector<TensorVec> xpu_buffer_;
Y
yuyang18 已提交
84
  size_t prev_pos_{-1UL};
85 86
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
  gpuStream_t compute_stream_;
87 88 89
  std::shared_ptr<platform::CudaStreamObject> stream_;
  std::vector<std::shared_ptr<platform::CudaEventObject>> events_;
#endif
90 91 92 93 94 95

#ifdef PADDLE_WITH_ASCEND_CL
  aclrtStream compute_stream_;
  std::shared_ptr<platform::NpuStreamObject> stream_;
  std::vector<std::shared_ptr<platform::NpuEventObject>> events_;
#endif
F
fwenguang 已提交
96 97 98 99 100 101

#ifdef PADDLE_WITH_MLU
  mluStream compute_stream_;
  std::shared_ptr<platform::MluStreamObject> stream_;
  std::vector<std::shared_ptr<platform::MluEventObject>> events_;
#endif
102 103 104 105 106 107

#ifdef PADDLE_WITH_XPU
  xpuStream compute_stream_;
  std::shared_ptr<platform::XpuStreamObject> stream_;
  std::vector<std::shared_ptr<platform::XpuEventObject>> events_;
#endif
Y
yuyang18 已提交
108 109 110 111 112
};

}  // namespace reader
}  // namespace operators
}  // namespace paddle