All Classes Files Functions Enumerations Enumerator Pages
batch.hpp
1 /*
2  * Copyright (c) 2023-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstddef>
19 #include <ctime>
20 #include <utility>
21 #include <vector>
22 
23 #include <kvikio/error.hpp>
24 #include <kvikio/file_handle.hpp>
25 #include <kvikio/shim/cufile.hpp>
26 
27 namespace kvikio {
28 
32 struct BatchOp {
33  // The file handle of the file to read or write
34  FileHandle& file_handle;
35  // Base address of buffer in device memory (host memory not supported).
36  void* devPtr_base;
37  // Offset in the file to read from or write to.
38  off_t file_offset;
39  // Offset relative to the `devPtr_base` pointer to write into or read from.
40  off_t devPtr_offset;
41  // Size in bytes to read or write.
42  size_t size;
43  // The operation type: CUFILE_READ or CUFILE_WRITE.
44  CUfileOpcode_t opcode;
45 };
46 
47 #ifdef KVIKIO_CUFILE_BATCH_API_FOUND
48 
62 class BatchHandle {
63  private:
64  bool _initialized{false};
65  int _max_num_events{};
66  CUfileBatchHandle_t _handle{};
67 
68  public:
69  BatchHandle() noexcept = default;
70 
76  BatchHandle(int max_num_events);
77 
81  BatchHandle(const BatchHandle&) = delete;
82  BatchHandle& operator=(BatchHandle const&) = delete;
83  BatchHandle(BatchHandle&& o) noexcept;
84  ~BatchHandle() noexcept;
85 
86  [[nodiscard]] bool closed() const noexcept;
87 
91  void close() noexcept;
92 
99  void submit(const std::vector<BatchOp>& operations);
100 
111  std::vector<CUfileIOEvents_t> status(unsigned min_nr,
112  unsigned max_nr,
113  struct timespec* timeout = nullptr);
114 
115  void cancel();
116 };
117 
118 #else
119 
120 class BatchHandle {
121  public:
122  BatchHandle() noexcept = default;
123 
124  BatchHandle(int max_num_events);
125 
126  [[nodiscard]] bool closed() const noexcept;
127 
128  void close() noexcept;
129 
130  void submit(const std::vector<BatchOp>& operations);
131 
132  std::vector<CUfileIOEvents_t> status(unsigned min_nr,
133  unsigned max_nr,
134  struct timespec* timeout = nullptr);
135 
136  void cancel();
137 };
138 
139 #endif
140 
141 } // namespace kvikio
Handle of an open file registered with cufile.
Definition: file_handle.hpp:44
IO operation used when submitting batches.
Definition: batch.hpp:32