batch.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <cstddef>
8 #include <ctime>
9 #include <utility>
10 #include <vector>
11 
12 #include <kvikio/error.hpp>
13 #include <kvikio/file_handle.hpp>
14 #include <kvikio/shim/cufile.hpp>
15 
16 namespace kvikio {
17 
21 struct BatchOp {
22  // The file handle of the file to read or write
23  FileHandle& file_handle;
24  // Base address of buffer in device memory (host memory not supported).
25  void* devPtr_base;
26  // Offset in the file to read from or write to.
27  off_t file_offset;
28  // Offset relative to the `devPtr_base` pointer to write into or read from.
29  off_t devPtr_offset;
30  // Size in bytes to read or write.
31  size_t size;
32  // The operation type: CUFILE_READ or CUFILE_WRITE.
33  CUfileOpcode_t opcode;
34 };
35 
36 #ifdef KVIKIO_CUFILE_BATCH_API_FOUND
37 
51 class BatchHandle {
52  private:
53  bool _initialized{false};
54  int _max_num_events{};
55  CUfileBatchHandle_t _handle{};
56 
57  public:
58  BatchHandle() noexcept = default;
59 
65  BatchHandle(int max_num_events);
66 
70  BatchHandle(BatchHandle const&) = delete;
71  BatchHandle& operator=(BatchHandle const&) = delete;
72  BatchHandle(BatchHandle&& o) noexcept;
73  ~BatchHandle() noexcept;
74 
75  [[nodiscard]] bool closed() const noexcept;
76 
80  void close() noexcept;
81 
88  void submit(std::vector<BatchOp> const& operations);
89 
100  std::vector<CUfileIOEvents_t> status(unsigned min_nr,
101  unsigned max_nr,
102  struct timespec* timeout = nullptr);
103 
104  void cancel();
105 };
106 
107 #else
108 
109 class BatchHandle {
110  public:
111  BatchHandle() noexcept = default;
112 
113  BatchHandle(int max_num_events);
114 
115  [[nodiscard]] bool closed() const noexcept;
116 
117  void close() noexcept;
118 
119  void submit(std::vector<BatchOp> const& operations);
120 
121  std::vector<CUfileIOEvents_t> status(unsigned min_nr,
122  unsigned max_nr,
123  struct timespec* timeout = nullptr);
124 
125  void cancel();
126 };
127 
128 #endif
129 
130 } // namespace kvikio
Handle of an open file registered with cufile.
Definition: file_handle.hpp:32
KvikIO namespace.
Definition: batch.hpp:16
IO operation used when submitting batches.
Definition: batch.hpp:21