file_handle.hpp
1 /*
2  * Copyright (c) 2021-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  * https://apache.ac.cn/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 <sys/stat.h>
19 #include <sys/types.h>
20 
21 #include <cstddef>
22 #include <cstdlib>
23 #include <stdexcept>
24 #include <system_error>
25 #include <utility>
26 
27 #include <kvikio/buffer.hpp>
28 #include <kvikio/compat_mode.hpp>
29 #include <kvikio/cufile/config.hpp>
30 #include <kvikio/defaults.hpp>
31 #include <kvikio/error.hpp>
32 #include <kvikio/file_utils.hpp>
33 #include <kvikio/parallel_operation.hpp>
34 #include <kvikio/posix_io.hpp>
35 #include <kvikio/shim/cufile.hpp>
36 #include <kvikio/shim/cufile_h_wrapper.hpp>
37 #include <kvikio/stream.hpp>
38 #include <kvikio/utils.hpp>
39 
40 namespace kvikio {
41 
47 class FileHandle {
48  private
49  // We use two file descriptors, one opened with the O_DIRECT flag and one without.
50  FileWrapper _file_direct_on{};
51  FileWrapper _file_direct_off{};
52  bool _initialized{false};
53  mutable std::size_t _nbytes{0}; // The size of the underlying file, zero means unknown.
54  CUFileHandleWrapper _cufile_handle{};
55  CompatModeManager _compat_mode_manager;
56  friend class CompatModeManager;
57 
58  public
59  // 644 is a common setting of Unix file permissions: read and write for owner, read-only for group
60  // and others.
61  static constexpr mode_t m644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
62  FileHandle() noexcept = default;
63 
80  FileHandle(std::string const& file_path,
81  std::string const& flags = "r",
82  mode_t mode = m644,
83  CompatMode compat_mode = defaults::compat_mode());
84 
88  FileHandle(FileHandle const&) = delete;
89  FileHandle& operator=(FileHandle const&) = delete;
90  FileHandle(FileHandle&& o) noexcept;
91  FileHandle& operator=(FileHandle&& o) noexcept;
92  ~FileHandle() noexcept;
93 
99  [[nodiscard]] bool closed() const noexcept;
100 
104  void close() noexcept;
105 
114  [[nodiscard]] CUfileHandle_t handle();
115 
125  [[nodiscard]] int fd(bool o_direct = false) const noexcept;
126 
137  [[nodiscard]] int fd_open_flags(bool o_direct = false) const;
138 
146  [[nodiscard]] std::size_t nbytes() const;
147 
178  std::size_t read(void* devPtr_base,
179  std::size_t size,
180  std::size_t file_offset,
181  std::size_t devPtr_offset,
182  bool sync_default_stream = true);
183 
215  std::size_t write(void const* devPtr_base,
216  std::size_t size,
217  std::size_t file_offset,
218  std::size_t devPtr_offset,
219  bool sync_default_stream = true);
220 
251  std::future<std::size_t> pread(void* buf,
252  std::size_t size,
253  std::size_t file_offset = 0,
254  std::size_t task_size = defaults::task_size(),
255  std::size_t gds_threshold = defaults::gds_threshold(),
256  bool sync_default_stream = true);
257 
288  std::future<std::size_t> pwrite(void const* buf,
289  std::size_t size,
290  std::size_t file_offset = 0,
291  std::size_t task_size = defaults::task_size(),
292  std::size_t gds_threshold = defaults::gds_threshold(),
293  bool sync_default_stream = true);
294 
329  void read_async(void* devPtr_base,
330  std::size_t* size_p,
331  off_t* file_offset_p,
332  off_t* devPtr_offset_p,
333  ssize_t* bytes_read_p,
334  CUstream stream);
335 
361  [[nodiscard]] StreamFuture read_async(void* devPtr_base,
362  std::size_t size,
363  off_t file_offset = 0,
364  off_t devPtr_offset = 0,
365  CUstream stream = nullptr);
366 
402  void write_async(void* devPtr_base,
403  std::size_t* size_p,
404  off_t* file_offset_p,
405  off_t* devPtr_offset_p,
406  ssize_t* bytes_written_p,
407  CUstream stream);
408 
434  [[nodiscard]] StreamFuture write_async(void* devPtr_base,
435  std::size_t size,
436  off_t file_offset = 0,
437  off_t devPtr_offset = 0,
438  CUstream stream = nullptr);
439 
448 };
449 
450 } // namespace kvikio
为 cuFile 句柄提供 RAII 的类。
存储和管理与 FileHandle 关联的兼容模式数据。
向 cufile 注册的已打开文件的句柄。
void close() noexcept
注销文件并关闭两个文件。
int fd(bool o_direct=false) const noexcept
获取其中一个文件描述符。
std::future< std::size_t > pread(void *buf, std::size_t size, std::size_t file_offset=0, std::size_t task_size=defaults::task_size(), std::size_t gds_threshold=defaults::gds_threshold(), bool sync_default_stream=true)
从文件并行读取指定字节到设备或主机内存中。
void read_async(void *devPtr_base, std::size_t *size_p, off_t *file_offset_p, off_t *devPtr_offset_p, ssize_t *bytes_read_p, CUstream stream)
从文件异步读取指定字节到设备内存中。
int fd_open_flags(bool o_direct=false) const
获取其中一个文件描述符的标志 (参见 open(2))
std::future< std::size_t > pwrite(void const *buf, std::size_t size, std::size_t file_offset=0, std::size_t task_size=defaults::task_size(), std::size_t gds_threshold=defaults::gds_threshold(), bool sync_default_stream=true)
从设备或主机内存并行写入指定字节到文件中。
std::size_t write(void const *devPtr_base, std::size_t size, std::size_t file_offset, std::size_t devPtr_offset, bool sync_default_stream=true)
将指定字节从设备内存写入到文件中。
CUfileHandle_t handle()
获取底层的 cuFile 文件句柄。
const CompatModeManager & get_compat_mode_manager() const noexcept
获取关联的兼容模式管理器,可用于查询最初请求的兼容模式...
FileHandle(std::string const &file_path, std::string const &flags="r", mode_t mode=m644, CompatMode compat_mode=defaults::compat_mode())
从文件路径构造文件句柄。
bool closed() const noexcept
根据其初始化状态判断文件是否已关闭。
void write_async(void *devPtr_base, std::size_t *size_p, off_t *file_offset_p, off_t *devPtr_offset_p, ssize_t *bytes_written_p, CUstream stream)
从设备内存异步写入指定字节到文件中。
std::size_t nbytes() const
获取文件大小。
std::size_t read(void *devPtr_base, std::size_t size, std::size_t file_offset, std::size_t devPtr_offset, bool sync_default_stream=true)
从文件读取指定字节到设备内存中。
FileHandle(FileHandle const &)=delete
FileHandle 支持移动语义但不可复制。
为文件处理提供 RAII 的类。
异步 IO 操作的 Future。
定义: stream.hpp:46
整个 KvikIO 中使用的默认值的单例类。
定义: defaults.hpp:63
static CompatMode compat_mode()
返回 KvikIO 库是否正在兼容模式下运行。
KvikIO 命名空间。
定义: batch.hpp:27
CompatMode
I/O 兼容模式。