buffered_reader.h 3.9 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
40 41 42 43
#ifdef PADDLE_WITH_CUSTOM_DEVICE
#include "paddle/phi/backends/event.h"
#include "paddle/phi/backends/stream.h"
#endif
Y
yuyang18 已提交
44 45 46 47 48
namespace paddle {
namespace operators {
namespace reader {

class BufferedReader : public framework::DecoratedReader {
49
  using TensorVec = paddle::framework::LoDTensorArray;
Y
yuyang18 已提交
50 51 52 53
  using VecFuture = std::future<TensorVec>;

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

  ~BufferedReader() override;

 private:
Y
yuyang18 已提交
61
  void ReadTillBufferFullAsync();
Y
yuyang18 已提交
62

Y
yuyang18 已提交
63
  void ReadAsync(size_t i);
Y
yuyang18 已提交
64 65 66 67

 protected:
  void ShutdownImpl() override;
  void StartImpl() override;
68
  void ReadNextImpl(paddle::framework::LoDTensorArray* out) override;
Y
yuyang18 已提交
69 70 71 72 73

 private:
  ThreadPool thread_pool_;
  platform::Place place_;
  const size_t buffer_size_;
74
  bool pin_memory_;
Y
yuyang18 已提交
75 76

  std::queue<std::future<size_t>> position_;
Y
yuyang18 已提交
77 78 79

  // The buffer for reading data.
  // NOTE: the simplest way to implement buffered reader is do not use any
Y
yuyang18 已提交
80 81
  // 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 已提交
82
  // buffers and prevent alloc every time.
Y
yuyang18 已提交
83
  std::vector<TensorVec> cpu_buffer_;
84
  std::vector<TensorVec> cuda_buffer_;
85
  std::vector<TensorVec> npu_buffer_;
F
fwenguang 已提交
86
  std::vector<TensorVec> mlu_buffer_;
87
  std::vector<TensorVec> xpu_buffer_;
88
  std::vector<TensorVec> custom_device_buffer_;
Y
yuyang18 已提交
89
  size_t prev_pos_{-1UL};
90 91
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
  gpuStream_t compute_stream_;
92 93 94
  std::shared_ptr<platform::CudaStreamObject> stream_;
  std::vector<std::shared_ptr<platform::CudaEventObject>> events_;
#endif
95 96 97 98 99 100

#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 已提交
101 102 103 104 105 106

#ifdef PADDLE_WITH_MLU
  mluStream compute_stream_;
  std::shared_ptr<platform::MluStreamObject> stream_;
  std::vector<std::shared_ptr<platform::MluEventObject>> events_;
#endif
107 108 109 110 111 112

#ifdef PADDLE_WITH_XPU
  xpuStream compute_stream_;
  std::shared_ptr<platform::XpuStreamObject> stream_;
  std::vector<std::shared_ptr<platform::XpuEventObject>> events_;
#endif
113 114 115 116 117 118

#ifdef PADDLE_WITH_CUSTOM_DEVICE
  std::shared_ptr<phi::stream::Stream> custom_device_compute_stream_;
  std::shared_ptr<phi::stream::Stream> custom_device_stream_;
  std::vector<std::shared_ptr<phi::event::Event>> custom_device_events_;
#endif
Y
yuyang18 已提交
119 120 121 122 123
};

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