cufile.hpp
1 /*
2  * 版权所有 (c) 2022-2025, NVIDIA CORPORATION.
3  *
4  * 根据 Apache 许可证 2.0 版 (以下简称"许可证") 获得许可;
5  * 您除非遵守本许可证的规定,否则不得使用此文件。
6  * 您可以访问以下网址获取本许可证的副本:
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * 除非适用法律要求或书面同意,根据本许可证分发的软件
11  * 是按"原样"分发的,没有任何形式的明示或
12  * 暗示的担保或条件。
13  * 请参见许可证中规定的管理权限和
14  * 限制条件。
15  */
16 #pragma once
17 
18 #include <kvikio/shim/cufile_h_wrapper.hpp>
19 #include <kvikio/shim/utils.hpp>
20 
21 namespace kvikio {
22 
30 /// @brief cuFile C-API 的 Shim 层。
31 class cuFileAPI {
32  public
33  decltype(cuFileHandleRegister)* HandleRegister{nullptr};
34  decltype(cuFileHandleDeregister)* HandleDeregister{nullptr};
35  decltype(cuFileRead)* Read{nullptr};
36  decltype(cuFileWrite)* Write{nullptr};
37  decltype(cuFileBufRegister)* BufRegister{nullptr};
38  decltype(cuFileBufDeregister)* BufDeregister{nullptr};
39  decltype(cuFileDriverGetProperties)* DriverGetProperties{nullptr};
40  decltype(cuFileDriverSetPollMode)* DriverSetPollMode{nullptr};
41  decltype(cuFileDriverSetMaxCacheSize)* DriverSetMaxCacheSize{nullptr};
42  decltype(cuFileDriverSetMaxPinnedMemSize)* DriverSetMaxPinnedMemSize{nullptr};
43  decltype(cuFileBatchIOSetUp)* BatchIOSetUp{nullptr};
44  decltype(cuFileBatchIOSubmit)* BatchIOSubmit{nullptr};
45  decltype(cuFileBatchIOGetStatus)* BatchIOGetStatus{nullptr};
46  decltype(cuFileBatchIOCancel)* BatchIOCancel{nullptr};
47  decltype(cuFileBatchIODestroy)* BatchIODestroy{nullptr};
48  decltype(cuFileReadAsync)* ReadAsync{nullptr};
49  decltype(cuFileWriteAsync)* WriteAsync{nullptr};
50  decltype(cuFileStreamRegister)* StreamRegister{nullptr};
51  decltype(cuFileStreamDeregister)* StreamDeregister{nullptr};
52 
53  private
54  // 不要直接调用 driver open 和 close,使用 `.driver_open()` 和 `.driver_close()`。
55  decltype(cuFileDriverOpen)* DriverOpen{nullptr};
56  decltype(cuFileDriverClose)* DriverClose{nullptr};
57 
58  // 不要直接调用 `GetVersion`,使用 `cuFileAPI::instance().version`。
59  decltype(cuFileGetVersion)* GetVersion{nullptr};
60 
61  public
62  int version{0};
63 
64  private
65  cuFileAPI();
66 
67 #ifdef KVIKIO_CUFILE_FOUND
68  // 注意,即使在 main 之后不允许调用 CUDA [1],我们仍必须在程序退出时关闭驱动程序(如果打开了)。
69  // 这是因为,如果驱动程序在程序退出时没有关闭,cuFile 会发生段错误,即我们进退两难,但
70  // 这似乎是两害相权取其轻。
71  // [1] <https://docs.nvda.net.cn/cuda/cuda-c-programming-guide/index.html#initialization>
72  ~cuFileAPI();
73 #endif
74 
75  public
76  cuFileAPI(cuFileAPI const&) = delete;
77  void operator=(cuFileAPI const&) = delete;
78  cuFileAPI(cuFileAPI const&&) = delete;
79  void operator=(cuFileAPI const&&) = delete;
80 
81  KVIKIO_EXPORT static cuFileAPI& instance();
82 
89  /// @brief 打开 cuFile 驱动程序。
90  void driver_open();
94  /// @brief 关闭 cuFile 驱动程序。
95  void driver_close();
96 };
104 /// @brief 检查 cuFile 库是否可用。
105 #ifdef KVIKIO_CUFILE_FOUND
106 bool is_cufile_library_available() noexcept;
107 #else
108 constexpr bool is_cufile_library_available() noexcept { return false; }
109 #endif
118 /// @brief 检查 cuFile 是否可用且预期能够工作。
119 bool is_cufile_available() noexcept;
131 /// @brief 获取 cuFile 版本(如果早于 v1.8 则为零)。
132 #ifdef KVIKIO_CUFILE_FOUND
133 int cufile_version() noexcept;
134 #else
135 constexpr int cufile_version() noexcept { return 0; }
136 #endif
146 /// @brief 检查 cuFile 的批处理 API 是否可用。
147 bool is_batch_api_available() noexcept;
157 /// @brief 检查 cuFile 的流 (异步) API 是否可用。
158 bool is_stream_api_available() noexcept;
159 
cuFile C-API 的 Shim 层。
定义: cufile.hpp:30
void driver_open()
打开 cuFile 驱动程序。
void driver_close()
关闭 cuFile 驱动程序。
KvikIO 命名空间。
定义: batch.hpp:27
constexpr bool is_cufile_library_available() noexcept
检查 cuFile 库是否可用。
定义: cufile.hpp:107
bool is_cufile_available() noexcept
检查 cuFile 是否可用且预期能够工作。
bool is_stream_api_available() noexcept
检查 cuFile 的流 (异步) API 是否可用。
constexpr int cufile_version() noexcept
获取 cuFile 版本(如果早于 v1.8 则为零)。
定义: cufile.hpp:134
bool is_batch_api_available() noexcept
检查 cuFile 的批处理 API 是否可用。