device_scalar.hpp
转到此文件的文档。
1 /*
2  * 版权所有 (c) 2019-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 
17 #pragma once
18 
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/export.hpp>
21 #include <rmm/device_uvector.hpp>
23 #include <rmm/resource_ref.hpp>
24 
25 #include <type_traits>
26 
27 namespace RMM_NAMESPACE {
41 template <typename T>
43  public
44  static_assert(std::is_trivially_copyable_v<T>, "Scalar type must be trivially copyable");
45 
50  using pointer =
54 
55  RMM_EXEC_CHECK_DISABLE
56  ~device_scalar() = default;
57 
58  RMM_EXEC_CHECK_DISABLE
59  device_scalar(device_scalar&&) noexcept = default;
60 
66  device_scalar& operator=(device_scalar&&) noexcept = default;
67 
71  device_scalar(device_scalar const&) = delete;
72 
76  device_scalar& operator=(device_scalar const&) = delete;
77 
81  device_scalar() = delete;
82 
99  : _storage{1, stream, mr}
100  {
101  }
102 
119  explicit device_scalar(value_type const& initial_value,
120  cuda_stream_view stream,
122  : _storage{1, stream, mr}
123  {
124  set_value_async(initial_value, stream);
125  }
126 
140  cuda_stream_view stream,
142  : _storage{other._storage, stream, mr}
143  {
144  }
145 
162  [[nodiscard]] value_type value(cuda_stream_view stream) const
163  {
164  return _storage.front_element(stream);
165  }
166 
204  void set_value_async(value_type const& value, cuda_stream_view stream)
205  {
206  _storage.set_element_async(0, value, stream);
207  }
208 
209  // Disallow passing literals to set_value to avoid race conditions where the memory holding the
210  // literal can be freed before the async memcpy / memset executes.
211  void set_value_async(value_type&&, cuda_stream_view) = delete;
212 
228  {
229  _storage.set_element_to_zero_async(value_type{0}, stream);
230  }
231 
242  [[nodiscard]] pointer data() noexcept { return static_cast<pointer>(_storage.data()); }
243 
254  [[nodiscard]] const_pointer data() const noexcept
255  {
256  return static_cast<const_pointer>(_storage.data());
257  }
258 
262  [[nodiscard]] constexpr size_type size() const noexcept { return 1; }
263 
267  [[nodiscard]] cuda_stream_view stream() const noexcept { return _storage.stream(); }
268 
274  void set_stream(cuda_stream_view stream) noexcept { _storage.set_stream(stream); }
275 
276  private
277  rmm::device_uvector<T> _storage;
278 };
279  // 组结束
281 } // 命名空间 RMM_NAMESPACE
用于 CUDA 流的强类型非拥有包装器,带有默认构造函数。
定义: cuda_stream_view.hpp:39
设备内存中类型 T 的单个对象的容器。
定义: device_scalar.hpp:42
typename device_uvector< T >::value_type value_type
T,标量元素的类型。
定义: device_scalar.hpp:46
constexpr size_type size() const noexcept
标量的大小:始终为 1。
定义: device_scalar.hpp:262
const_pointer data() const noexcept
返回设备内存中对象的 const 指针。
定义: device_scalar.hpp:254
typename device_uvector< T >::const_reference const_reference
const value_type&
定义: device_scalar.hpp:49
typename device_uvector< T >::size_type size_type
用于大小的类型。
定义: device_scalar.hpp:47
device_scalar(device_scalar &&) noexcept=default
默认移动构造函数。
void set_stream(cuda_stream_view stream) noexcept
设置用于解除分配的流。
定义: device_scalar.hpp:274
device_scalar(value_type const &initial_value, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
构造一个新的带有初始值的 device_scalar。
定义: device_scalar.hpp:119
device_scalar(device_scalar const &other, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
通过深度复制另一个 device_scalar 的内容构造新的 device_scalar,...
定义: device_scalar.hpp:139
typename device_uvector< T >::const_pointer const_pointer
定义: device_scalar.hpp:53
cuda_stream_view stream() const noexcept
与设备内存分配关联的流。
定义: device_scalar.hpp:267
pointer data() noexcept
返回设备内存中对象的指针。
定义: device_scalar.hpp:242
value_type value(cuda_stream_view stream) const
将值从设备复制到主机,同步并返回值。
定义: device_scalar.hpp:162
void set_value_to_zero_async(cuda_stream_view stream)
在指定的流上将 device_scalar 的值设置为零。
定义: device_scalar.hpp:227
void set_value_async(value_type const &value, cuda_stream_view stream)
将 device_scalar 的值设置为 v 的值。
定义: device_scalar.hpp:204
typename device_uvector< T >::pointer pointer
由 data() 返回的指针类型
定义: device_scalar.hpp:51
typename device_uvector< T >::reference reference
value_type&
定义: device_scalar.hpp:48
设备内存中未初始化的元素向量。
定义: device_uvector.hpp:76
value_type * pointer
由 data() 返回的指针类型
定义: device_uvector.hpp:86
std::size_t size_type
用于向量大小的类型。
定义: device_uvector.hpp:82
T value_type
T;存储值的类型。
定义: device_uvector.hpp:81
value_type & reference
value_type&;operator[](size_type) 返回的引用类型
定义: device_uvector.hpp:83
value_type const * const_pointer
由 data() const 返回的指针类型。
定义: device_uvector.hpp:87
value_type const & const_reference
定义: device_uvector.hpp:85
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
具有 cuda::mr::device_accessible 属性的 cuda::mr::async_resource_ref 的别名。
定义: resource_ref.hpp:40
device_async_resource_ref get_current_device_resource_ref()
获取当前设备的 device_async_resource_ref。
定义: per_device_resource.hpp:411
每个设备的 device_memory_resource 管理。