device_uvector.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
9 #include <rmm/detail/error.hpp>
10 #include <rmm/detail/exec_check_disable.hpp>
11 #include <rmm/detail/export.hpp>
12 #include <rmm/device_buffer.hpp>
14 #include <rmm/resource_ref.hpp>
15 
16 #include <cuda/std/iterator>
17 #include <cuda/std/span>
18 
19 #include <cstddef>
20 #include <limits>
21 #include <type_traits>
22 #include <utility>
23 
24 namespace RMM_NAMESPACE {
68 template <typename T>
70  static_assert(std::is_trivially_copyable_v<T>,
71  "device_uvector only supports types that are trivially copyable.");
72 
73  public:
74  using value_type = T;
75  using size_type = std::size_t;
76  using reference = value_type&;
78  value_type const&;
79  using pointer = value_type*;
80  using const_pointer = value_type const*;
81  using iterator = pointer;
84  cuda::std::reverse_iterator<iterator>;
86  cuda::std::reverse_iterator<const_iterator>;
88 
89  RMM_EXEC_CHECK_DISABLE
90  ~device_uvector() = default;
91 
92  RMM_EXEC_CHECK_DISABLE
93  device_uvector(device_uvector&&) noexcept = default;
94 
95  RMM_EXEC_CHECK_DISABLE
96  device_uvector& operator=(device_uvector&&) noexcept =
97  default;
98 
102  device_uvector(device_uvector const&) = delete;
103 
107  device_uvector& operator=(device_uvector const&) = delete;
108 
112  device_uvector() = delete;
113 
129  explicit device_uvector(
130  size_type size,
131  cuda_stream_view stream,
132  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref())
133  : _storage{elements_to_bytes(size), std::alignment_of_v<T>, stream, std::move(mr)}
134  {
135  }
136 
146  explicit device_uvector(
147  device_uvector const& other,
148  cuda_stream_view stream,
149  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref())
150  : _storage{other._storage, stream, std::move(mr)}
151  {
152  }
153 
162  [[nodiscard]] pointer element_ptr(size_type element_index) noexcept
163  {
164  assert(element_index < size());
165  return data() + element_index;
166  }
167 
176  [[nodiscard]] const_pointer element_ptr(size_type element_index) const noexcept
177  {
178  assert(element_index < size());
179  return data() + element_index;
180  }
181 
215  void set_element_async(size_type element_index, value_type const& value, cuda_stream_view stream)
216  {
217  RMM_EXPECTS(
218  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
219  RMM_CUDA_TRY(cudaMemcpyAsync(
220  element_ptr(element_index), &value, sizeof(value), cudaMemcpyDefault, stream.value()));
221  }
222 
223  // We delete the r-value reference overload to prevent asynchronously copying from a literal or
224  // implicit temporary value after it is deleted or goes out of scope.
225  void set_element_async(size_type, value_type const&&, cuda_stream_view) = delete;
226 
250  {
251  RMM_EXPECTS(
252  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
253  RMM_CUDA_TRY(
254  cudaMemsetAsync(element_ptr(element_index), 0, sizeof(value_type), stream.value()));
255  }
256 
286  void set_element(size_type element_index, T const& value, cuda_stream_view stream)
287  {
288  set_element_async(element_index, value, stream);
289  stream.synchronize_no_throw();
290  }
291 
304  [[nodiscard]] value_type element(size_type element_index, cuda_stream_view stream) const
305  {
306  RMM_EXPECTS(
307  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
308  value_type value;
309  RMM_CUDA_TRY(cudaMemcpyAsync(
310  &value, element_ptr(element_index), sizeof(value), cudaMemcpyDefault, stream.value()));
311  stream.synchronize();
312  return value;
313  }
314 
326  [[nodiscard]] value_type front_element(cuda_stream_view stream) const
327  {
328  return element(0, stream);
329  }
330 
342  [[nodiscard]] value_type back_element(cuda_stream_view stream) const
343  {
344  return element(size() - 1, stream);
345  }
346 
361  void reserve(size_type new_capacity, cuda_stream_view stream)
362  {
363  _storage.reserve(elements_to_bytes(new_capacity), stream);
364  }
365 
384  void resize(size_type new_size, cuda_stream_view stream)
385  {
386  _storage.resize(elements_to_bytes(new_size), stream);
387  }
388 
396  void shrink_to_fit(cuda_stream_view stream) { _storage.shrink_to_fit(stream); }
397 
403  device_buffer release() noexcept { return std::move(_storage); }
404 
411  [[nodiscard]] size_type capacity() const noexcept
412  {
413  return bytes_to_elements(_storage.capacity());
414  }
415 
424  [[nodiscard]] pointer data() noexcept { return static_cast<pointer>(_storage.data()); }
425 
434  [[nodiscard]] const_pointer data() const noexcept
435  {
436  return static_cast<const_pointer>(_storage.data());
437  }
438 
446  [[nodiscard]] iterator begin() noexcept { return data(); }
447 
455  [[nodiscard]] const_iterator cbegin() const noexcept { return data(); }
456 
464  [[nodiscard]] const_iterator begin() const noexcept { return cbegin(); }
465 
474  [[nodiscard]] iterator end() noexcept { return data() + size(); }
475 
484  [[nodiscard]] const_iterator cend() const noexcept { return data() + size(); }
485 
494  [[nodiscard]] const_iterator end() const noexcept { return cend(); }
495 
503  [[nodiscard]] reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
504 
512  [[nodiscard]] const_reverse_iterator crbegin() const noexcept
513  {
514  return const_reverse_iterator(cend());
515  }
516 
524  [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return crbegin(); }
525 
534  [[nodiscard]] reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
535 
545  [[nodiscard]] const_reverse_iterator crend() const noexcept
546  {
547  return const_reverse_iterator(begin());
548  }
549 
558  [[nodiscard]] const_reverse_iterator rend() const noexcept { return crend(); }
559 
563  [[nodiscard]] size_type size() const noexcept { return bytes_to_elements(_storage.size()); }
564 
568  [[nodiscard]] std::int64_t ssize() const noexcept
569  {
570  assert(size() < static_cast<size_type>(std::numeric_limits<int64_t>::max()) &&
571  "Size overflows signed integer");
572  return static_cast<int64_t>(size());
573  }
574 
578  [[nodiscard]] bool is_empty() const noexcept { return size() == 0; }
579 
583  [[nodiscard]] operator cuda::std::span<T const>() const noexcept
584  {
585  return cuda::std::span<T const>(data(), size());
586  }
587 
591  [[nodiscard]] operator cuda::std::span<T>() noexcept
592  {
593  return cuda::std::span<T>(data(), size());
594  }
595 
600  [[nodiscard]] rmm::device_async_resource_ref memory_resource() noexcept
601  {
602  return _storage.memory_resource();
603  }
604 
608  [[nodiscard]] cuda_stream_view stream() const noexcept { return _storage.stream(); }
609 
621  void set_stream(cuda_stream_view stream) noexcept { _storage.set_stream(stream); }
622 
623  private:
624  device_buffer _storage{};
625 
626  [[nodiscard]] size_type elements_to_bytes(size_type num_elements) const
627  {
628  RMM_EXPECTS(num_elements <= std::numeric_limits<size_type>::max() / sizeof(value_type),
629  "Requested size overflows device_uvector storage.",
631  return num_elements * sizeof(value_type);
632  }
633 
634  [[nodiscard]] size_type constexpr bytes_to_elements(size_type num_bytes) const noexcept
635  {
636  return num_bytes / sizeof(value_type);
637  }
638 };
639  // end of group
641 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
void synchronize() const
Synchronize the viewed CUDA stream.
void synchronize_no_throw() const noexcept
Synchronize the viewed CUDA stream. Does not throw if there is an error.
cudaStream_t value() const noexcept
Get the wrapped stream.
RAII construct for device memory allocation.
Definition: device_buffer.hpp:72
An uninitialized vector of elements in device memory.
Definition: device_uvector.hpp:69
reverse_iterator rend() noexcept
Returns reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:534
const_iterator cend() const noexcept
Returns a const_iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:484
const_reverse_iterator crend() const noexcept
Returns a const_reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:545
value_type * pointer
The type of the pointer returned by data()
Definition: device_uvector.hpp:79
cuda::std::reverse_iterator< iterator > reverse_iterator
The type of the iterator returned by rbegin()
Definition: device_uvector.hpp:84
void set_element_async(size_type element_index, value_type const &value, cuda_stream_view stream)
Performs an asynchronous copy of v to the specified element in device memory.
Definition: device_uvector.hpp:215
bool is_empty() const noexcept
true if the vector contains no elements, i.e. size() == 0
Definition: device_uvector.hpp:578
size_type size() const noexcept
The number of elements in the vector.
Definition: device_uvector.hpp:563
const_pointer data() const noexcept
Returns const pointer to underlying device storage.
Definition: device_uvector.hpp:434
void resize(size_type new_size, cuda_stream_view stream)
Resizes the vector to contain new_size elements.
Definition: device_uvector.hpp:384
void set_element(size_type element_index, T const &value, cuda_stream_view stream)
Performs a synchronous copy of v to the specified element in device memory.
Definition: device_uvector.hpp:286
reverse_iterator rbegin() noexcept
Returns a reverse_iterator to the last element.
Definition: device_uvector.hpp:503
pointer data() noexcept
Returns pointer to underlying device storage.
Definition: device_uvector.hpp:424
void shrink_to_fit(cuda_stream_view stream)
Forces deallocation of unused device memory.
Definition: device_uvector.hpp:396
iterator end() noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:474
std::size_t size_type
The type used for the size of the vector.
Definition: device_uvector.hpp:75
const_reverse_iterator crbegin() const noexcept
Returns a const_reverse_iterator to the last element.
Definition: device_uvector.hpp:512
size_type capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition: device_uvector.hpp:411
std::int64_t ssize() const noexcept
The signed number of elements in the vector.
Definition: device_uvector.hpp:568
T value_type
Stored value type.
Definition: device_uvector.hpp:74
const_iterator cbegin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:455
value_type back_element(cuda_stream_view stream) const
Returns the last element.
Definition: device_uvector.hpp:342
const_pointer const_iterator
The type of the const iterator returned by cbegin()
Definition: device_uvector.hpp:82
device_buffer release() noexcept
Release ownership of device memory storage.
Definition: device_uvector.hpp:403
void set_element_to_zero_async(size_type element_index, cuda_stream_view stream)
Asynchronously sets the specified element to zero in device memory.
Definition: device_uvector.hpp:249
device_uvector(device_uvector &&) noexcept=default
Default move constructor.
pointer iterator
The type of the iterator returned by begin()
Definition: device_uvector.hpp:81
void reserve(size_type new_capacity, cuda_stream_view stream)
Increases the capacity of the vector to new_capacity elements.
Definition: device_uvector.hpp:361
value_type & reference
Reference type returned by operator[](size_type)
Definition: device_uvector.hpp:76
const_reverse_iterator rend() const noexcept
Returns const_reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:558
const_iterator end() const noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:494
pointer element_ptr(size_type element_index) noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:162
value_type front_element(cuda_stream_view stream) const
Returns the first element.
Definition: device_uvector.hpp:326
value_type const * const_pointer
The type of the pointer returned by data() const.
Definition: device_uvector.hpp:80
device_uvector(device_uvector const &other, cuda_stream_view stream, cuda::mr::any_resource< cuda::mr::device_accessible > mr=mr::get_current_device_resource_ref())
Construct a new device_uvector by deep copying the contents of another device_uvector.
Definition: device_uvector.hpp:146
value_type const & const_reference
Constant reference type returned by operator[](size_type) const.
Definition: device_uvector.hpp:78
const_reverse_iterator rbegin() const noexcept
Returns a const_reverse_iterator to the last element.
Definition: device_uvector.hpp:524
value_type element(size_type element_index, cuda_stream_view stream) const
Returns the specified element from device memory.
Definition: device_uvector.hpp:304
const_pointer element_ptr(size_type element_index) const noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:176
cuda::std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: device_uvector.hpp:87
const_iterator begin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:464
iterator begin() noexcept
Returns an iterator to the first element.
Definition: device_uvector.hpp:446
Exception thrown when an argument to a function is invalid.
Definition: error.hpp:108
Exception thrown when attempting to access outside of a defined range.
Definition: error.hpp:99
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:187
cuda::mr::resource_ref< cuda::mr::device_accessible > device_async_resource_ref
Alias for a cuda::mr::resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:30
Management of per-device memory resources.