file_handle.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 
10 #include <cstddef>
11 #include <cstdlib>
12 
13 #include <kvikio/buffer.hpp>
14 #include <kvikio/compat_mode.hpp>
15 #include <kvikio/compat_mode_manager.hpp>
16 #include <kvikio/cufile/config.hpp>
17 #include <kvikio/defaults.hpp>
18 #include <kvikio/error.hpp>
19 #include <kvikio/file_utils.hpp>
20 #include <kvikio/shim/cufile.hpp>
21 #include <kvikio/shim/cufile_h_wrapper.hpp>
22 #include <kvikio/stream.hpp>
23 #include <kvikio/utils.hpp>
24 
25 namespace kvikio {
26 
32 class FileHandle {
33  private:
34  // We use two file descriptors, one opened with the O_DIRECT flag and one without.
35  FileWrapper _file_direct_on{};
36  FileWrapper _file_direct_off{};
37  bool _initialized{false};
38  mutable std::size_t _nbytes{0}; // The size of the underlying file, zero means unknown.
39  CUFileHandleWrapper _cufile_handle{};
40  CompatModeManager _compat_mode_manager;
41  friend class CompatModeManager;
42 
43  public:
44  // 644 is a common setting of Unix file permissions: read and write for owner, read-only for group
45  // and others.
46  static constexpr mode_t m644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
47  FileHandle() noexcept = default;
48 
65  FileHandle(std::string const& file_path,
66  std::string const& flags = "r",
67  mode_t mode = m644,
68  CompatMode compat_mode = defaults::compat_mode());
69 
73  FileHandle(FileHandle const&) = delete;
74  FileHandle& operator=(FileHandle const&) = delete;
75  FileHandle(FileHandle&& o) noexcept;
76  FileHandle& operator=(FileHandle&& o) noexcept;
77  ~FileHandle() noexcept;
78 
84  [[nodiscard]] bool closed() const noexcept;
85 
89  void close() noexcept;
90 
99  [[nodiscard]] CUfileHandle_t handle();
100 
110  [[nodiscard]] int fd(bool o_direct = false) const noexcept;
111 
122  [[nodiscard]] int fd_open_flags(bool o_direct = false) const;
123 
131  [[nodiscard]] std::size_t nbytes() const;
132 
163  std::size_t read(void* devPtr_base,
164  std::size_t size,
165  std::size_t file_offset,
166  std::size_t devPtr_offset,
167  bool sync_default_stream = true);
168 
200  std::size_t write(void const* devPtr_base,
201  std::size_t size,
202  std::size_t file_offset,
203  std::size_t devPtr_offset,
204  bool sync_default_stream = true);
205 
236  std::future<std::size_t> pread(void* buf,
237  std::size_t size,
238  std::size_t file_offset = 0,
239  std::size_t task_size = defaults::task_size(),
240  std::size_t gds_threshold = defaults::gds_threshold(),
241  bool sync_default_stream = true);
242 
273  std::future<std::size_t> pwrite(void const* buf,
274  std::size_t size,
275  std::size_t file_offset = 0,
276  std::size_t task_size = defaults::task_size(),
277  std::size_t gds_threshold = defaults::gds_threshold(),
278  bool sync_default_stream = true);
279 
314  void read_async(void* devPtr_base,
315  std::size_t* size_p,
316  off_t* file_offset_p,
317  off_t* devPtr_offset_p,
318  ssize_t* bytes_read_p,
319  CUstream stream);
320 
346  [[nodiscard]] StreamFuture read_async(void* devPtr_base,
347  std::size_t size,
348  off_t file_offset = 0,
349  off_t devPtr_offset = 0,
350  CUstream stream = nullptr);
351 
387  void write_async(void* devPtr_base,
388  std::size_t* size_p,
389  off_t* file_offset_p,
390  off_t* devPtr_offset_p,
391  ssize_t* bytes_written_p,
392  CUstream stream);
393 
419  [[nodiscard]] StreamFuture write_async(void* devPtr_base,
420  std::size_t size,
421  off_t file_offset = 0,
422  off_t devPtr_offset = 0,
423  CUstream stream = nullptr);
424 
433 };
434 
435 } // namespace kvikio
Class that provides RAII for the cuFile handle.
Definition: file_utils.hpp:75
Store and manage the compatibility mode data associated with a FileHandle.
Handle of an open file registered with cufile.
Definition: file_handle.hpp:32
void close() noexcept
Deregister the file and close the two files.
int fd(bool o_direct=false) const noexcept
Get one of the file descriptors.
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)
Reads specified bytes from the file into the device or host memory in parallel.
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)
Reads specified bytes from the file into the device memory asynchronously.
int fd_open_flags(bool o_direct=false) const
Get the flags of one of the file descriptors (see 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)
Writes specified bytes from device or host memory into the file in parallel.
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)
Writes specified bytes from the device memory into the file.
CUfileHandle_t handle()
Get the underlying cuFile file handle.
const CompatModeManager & get_compat_mode_manager() const noexcept
Get the associated compatibility mode manager, which can be used to query the original requested comp...
FileHandle(std::string const &file_path, std::string const &flags="r", mode_t mode=m644, CompatMode compat_mode=defaults::compat_mode())
Construct a file handle from a file path.
bool closed() const noexcept
Whether the file is closed according to its initialization status.
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)
Writes specified bytes from the device memory into the file asynchronously.
std::size_t nbytes() const
Get the file size.
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)
Reads specified bytes from the file into the device memory.
FileHandle(FileHandle const &)=delete
FileHandle support move semantic but isn't copyable.
Class that provides RAII for file handling.
Definition: file_utils.hpp:16
Future of an asynchronous IO operation.
Definition: stream.hpp:35
Singleton class of default values used throughout KvikIO.
Definition: defaults.hpp:112
static CompatMode compat_mode()
Return whether the KvikIO library is running in compatibility mode or not.
KvikIO namespace.
Definition: batch.hpp:16
CompatMode
I/O compatibility mode.
Definition: compat_mode.hpp:15