stream.hpp
1 /*
2  * 版权所有 (c) 2023-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  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <sys/types.h>
19 #include <cstdlib>
20 #include <tuple>
21 #include <utility>
22 
23 #include <kvikio/shim/cuda.hpp>
24 
25 namespace kvikio {
26 
46 /**
46  * @brief 异步IO操作的Future。
46  */
47 class StreamFuture {
48  private
49  struct ArgByVal {
50  std::size_t size;
51  off_t file_offset;
52  off_t devPtr_offset;
53  ssize_t bytes_done;
54  };
55 
56  void* _devPtr_base{nullptr};
57  CUstream _stream{nullptr};
58  ArgByVal* _val{nullptr};
59  bool _stream_synchronized{false};
60 
61  public
62  StreamFuture() noexcept = default;
63 
65  void* devPtr_base, std::size_t size, off_t file_offset, off_t devPtr_offset, CUstream stream);
65 
69 /**
69  * @brief StreamFuture支持移动语义但不允许复制。
69  */
69  StreamFuture(StreamFuture const&) = delete;
70  StreamFuture& operator=(StreamFuture& o) = delete;
71  StreamFuture(StreamFuture&& o) noexcept;
72  StreamFuture& operator=(StreamFuture&& o) noexcept;
73 
80 /**
80  * @brief 返回Future调用的参数。
80  */
80  std::tuple<void*, std::size_t*, off_t*, off_t*, ssize_t*, CUstream> get_args() const;
81 
89 /**
89  * @brief 返回Future操作读取或写入的字节数。
89  */
89  std::size_t check_bytes_done();
90 
95 /**
95  * @brief 释放按值传递的参数,并确保关联的CUDA流已同步。
95  */
95  ~StreamFuture() noexcept;
96 };
97 
98 } // namespace kvikio
kvikio::StreamFuture
异步IO操作的Future。