cuda_stream_pool.hpp
转到此文件的文档。
1 /*
2  * Copyright (c) 2020-2024, 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 
17 #pragma once
18 
19 #include <rmm/cuda_stream.hpp>
20 #include <rmm/cuda_stream_view.hpp>
21 #include <rmm/detail/error.hpp>
22 #include <rmm/detail/export.hpp>
23 
24 #include <atomic>
25 #include <cstddef>
26 #include <vector>
27 
28 namespace RMM_NAMESPACE {
44  public
45  static constexpr std::size_t default_size{16};
46 
53  explicit cuda_stream_pool(std::size_t pool_size = default_size) : streams_(pool_size)
54  {
55  RMM_EXPECTS(pool_size > 0, "流池大小必须大于零");
56  }
57  ~cuda_stream_pool() = default;
58 
60  cuda_stream_pool(cuda_stream_pool const&) = delete;
61  cuda_stream_pool& operator=(cuda_stream_pool&&) = delete;
62  cuda_stream_pool& operator=(cuda_stream_pool const&) = delete;
63 
72  {
73  return streams_[(next_stream++) % streams_.size()].view();
74  }
75 
86  rmm::cuda_stream_view get_stream(std::size_t stream_id) const
87  {
88  return streams_[stream_id % streams_.size()].view();
89  }
90 
98  std::size_t get_pool_size() const noexcept { return streams_.size(); }
99 
100  private
101  std::vector<rmm::cuda_stream> streams_;
102  mutable std::atomic_size_t next_stream{};
103 };
104  // 组结束
106 } // 命名空间 RMM_NAMESPACE
一个 CUDA 流池。
定义: cuda_stream_pool.hpp:43
cuda_stream_pool(std::size_t pool_size=default_size)
构造一个指定非零大小的新 cuda 流池对象。
定义: cuda_stream_pool.hpp:53
std::size_t get_pool_size() const noexcept
获取池中流的数量。
定义: cuda_stream_pool.hpp:98
rmm::cuda_stream_view get_stream() const noexcept
获取池中某个流的 cuda_stream_view。
定义: cuda_stream_pool.hpp:71
rmm::cuda_stream_view get_stream(std::size_t stream_id) const
获取与 stream_id 关联的流的 cuda_stream_view。相同的 stream_id 值返回...
定义: cuda_stream_pool.hpp:86
CUDA 流的强类型非拥有包装器,带有默认构造函数。
定义: cuda_stream_view.hpp:39