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 <type_traits>
21 #include <utility>
22 
23 namespace RMM_NAMESPACE {
67 template <typename T>
69  static_assert(std::is_trivially_copyable_v<T>,
70  "device_uvector only supports types that are trivially copyable.");
71 
72  public:
73  using value_type = T;
74  using size_type = std::size_t;
75  using reference = value_type&;
77  value_type const&;
78  using pointer = value_type*;
79  using const_pointer = value_type const*;
80  using iterator = pointer;
83  cuda::std::reverse_iterator<iterator>;
85  cuda::std::reverse_iterator<const_iterator>;
87 
88  RMM_EXEC_CHECK_DISABLE
89  ~device_uvector() = default;
90 
91  RMM_EXEC_CHECK_DISABLE
92  device_uvector(device_uvector&&) noexcept = default;
93 
94  RMM_EXEC_CHECK_DISABLE
95  device_uvector& operator=(device_uvector&&) noexcept =
96  default;
97 
101  device_uvector(device_uvector const&) = delete;
102 
106  device_uvector& operator=(device_uvector const&) = delete;
107 
111  device_uvector() = delete;
112 
127  explicit device_uvector(
128  size_type size,
129  cuda_stream_view stream,
130  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref())
131  : _storage{elements_to_bytes(size), std::alignment_of_v<T>, stream, std::move(mr)}
132  {
133  }
134 
144  explicit device_uvector(
145  device_uvector const& other,
146  cuda_stream_view stream,
147  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref())
148  : _storage{other._storage, stream, std::move(mr)}
149  {
150  }
151 
160  [[nodiscard]] pointer element_ptr(size_type element_index) noexcept
161  {
162  assert(element_index < size());
163  return data() + element_index;
164  }
165 
174  [[nodiscard]] const_pointer element_ptr(size_type element_index) const noexcept
175  {
176  assert(element_index < size());
177  return data() + element_index;
178  }
179 
213  void set_element_async(size_type element_index, value_type const& value, cuda_stream_view stream)
214  {
215  RMM_EXPECTS(
216  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
217  RMM_CUDA_TRY(cudaMemcpyAsync(
218  element_ptr(element_index), &value, sizeof(value), cudaMemcpyDefault, stream.value()));
219  }
220 
221  // We delete the r-value reference overload to prevent asynchronously copying from a literal or
222  // implicit temporary value after it is deleted or goes out of scope.
223  void set_element_async(size_type, value_type const&&, cuda_stream_view) = delete;
224 
248  {
249  RMM_EXPECTS(
250  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
251  RMM_CUDA_TRY(
252  cudaMemsetAsync(element_ptr(element_index), 0, sizeof(value_type), stream.value()));
253  }
254 
284  void set_element(size_type element_index, T const& value, cuda_stream_view stream)
285  {
286  set_element_async(element_index, value, stream);
287  stream.synchronize_no_throw();
288  }
289 
302  [[nodiscard]] value_type element(size_type element_index, cuda_stream_view stream) const
303  {
304  RMM_EXPECTS(
305  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
306  value_type value;
307  RMM_CUDA_TRY(cudaMemcpyAsync(
308  &value, element_ptr(element_index), sizeof(value), cudaMemcpyDefault, stream.value()));
309  stream.synchronize();
310  return value;
311  }
312 
324  [[nodiscard]] value_type front_element(cuda_stream_view stream) const
325  {
326  return element(0, stream);
327  }
328 
340  [[nodiscard]] value_type back_element(cuda_stream_view stream) const
341  {
342  return element(size() - 1, stream);
343  }
344 
357  void reserve(size_type new_capacity, cuda_stream_view stream)
358  {
359  _storage.reserve(elements_to_bytes(new_capacity), stream);
360  }
361 
378  void resize(size_type new_size, cuda_stream_view stream)
379  {
380  _storage.resize(elements_to_bytes(new_size), stream);
381  }
382 
390  void shrink_to_fit(cuda_stream_view stream) { _storage.shrink_to_fit(stream); }
391 
397  device_buffer release() noexcept { return std::move(_storage); }
398 
405  [[nodiscard]] size_type capacity() const noexcept
406  {
407  return bytes_to_elements(_storage.capacity());
408  }
409 
418  [[nodiscard]] pointer data() noexcept { return static_cast<pointer>(_storage.data()); }
419 
428  [[nodiscard]] const_pointer data() const noexcept
429  {
430  return static_cast<const_pointer>(_storage.data());
431  }
432 
440  [[nodiscard]] iterator begin() noexcept { return data(); }
441 
449  [[nodiscard]] const_iterator cbegin() const noexcept { return data(); }
450 
458  [[nodiscard]] const_iterator begin() const noexcept { return cbegin(); }
459 
468  [[nodiscard]] iterator end() noexcept { return data() + size(); }
469 
478  [[nodiscard]] const_iterator cend() const noexcept { return data() + size(); }
479 
488  [[nodiscard]] const_iterator end() const noexcept { return cend(); }
489 
497  [[nodiscard]] reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
498 
506  [[nodiscard]] const_reverse_iterator crbegin() const noexcept
507  {
508  return const_reverse_iterator(cend());
509  }
510 
518  [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return crbegin(); }
519 
528  [[nodiscard]] reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
529 
539  [[nodiscard]] const_reverse_iterator crend() const noexcept
540  {
541  return const_reverse_iterator(begin());
542  }
543 
552  [[nodiscard]] const_reverse_iterator rend() const noexcept { return crend(); }
553 
557  [[nodiscard]] size_type size() const noexcept { return bytes_to_elements(_storage.size()); }
558 
562  [[nodiscard]] std::int64_t ssize() const noexcept
563  {
564  assert(size() < static_cast<size_type>(std::numeric_limits<int64_t>::max()) &&
565  "Size overflows signed integer");
566  return static_cast<int64_t>(size());
567  }
568 
572  [[nodiscard]] bool is_empty() const noexcept { return size() == 0; }
573 
577  [[nodiscard]] operator cuda::std::span<T const>() const noexcept
578  {
579  return cuda::std::span<T const>(data(), size());
580  }
581 
585  [[nodiscard]] operator cuda::std::span<T>() noexcept
586  {
587  return cuda::std::span<T>(data(), size());
588  }
589 
594  [[nodiscard]] rmm::device_async_resource_ref memory_resource() noexcept
595  {
596  return _storage.memory_resource();
597  }
598 
602  [[nodiscard]] cuda_stream_view stream() const noexcept { return _storage.stream(); }
603 
615  void set_stream(cuda_stream_view stream) noexcept { _storage.set_stream(stream); }
616 
617  private:
618  device_buffer _storage{};
619 
620  [[nodiscard]] size_type constexpr elements_to_bytes(size_type num_elements) const noexcept
621  {
622  return num_elements * sizeof(value_type);
623  }
624 
625  [[nodiscard]] size_type constexpr bytes_to_elements(size_type num_bytes) const noexcept
626  {
627  return num_bytes / sizeof(value_type);
628  }
629 };
630  // end of group
632 } // 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:68
reverse_iterator rend() noexcept
Returns reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:528
const_iterator cend() const noexcept
Returns a const_iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:478
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:539
value_type * pointer
The type of the pointer returned by data()
Definition: device_uvector.hpp:78
cuda::std::reverse_iterator< iterator > reverse_iterator
The type of the iterator returned by rbegin()
Definition: device_uvector.hpp:83
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:213
bool is_empty() const noexcept
true if the vector contains no elements, i.e. size() == 0
Definition: device_uvector.hpp:572
size_type size() const noexcept
The number of elements in the vector.
Definition: device_uvector.hpp:557
const_pointer data() const noexcept
Returns const pointer to underlying device storage.
Definition: device_uvector.hpp:428
void resize(size_type new_size, cuda_stream_view stream)
Resizes the vector to contain new_size elements.
Definition: device_uvector.hpp:378
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:284
reverse_iterator rbegin() noexcept
Returns a reverse_iterator to the last element.
Definition: device_uvector.hpp:497
pointer data() noexcept
Returns pointer to underlying device storage.
Definition: device_uvector.hpp:418
void shrink_to_fit(cuda_stream_view stream)
Forces deallocation of unused device memory.
Definition: device_uvector.hpp:390
iterator end() noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:468
std::size_t size_type
The type used for the size of the vector.
Definition: device_uvector.hpp:74
const_reverse_iterator crbegin() const noexcept
Returns a const_reverse_iterator to the last element.
Definition: device_uvector.hpp:506
size_type capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition: device_uvector.hpp:405
std::int64_t ssize() const noexcept
The signed number of elements in the vector.
Definition: device_uvector.hpp:562
T value_type
Stored value type.
Definition: device_uvector.hpp:73
const_iterator cbegin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:449
value_type back_element(cuda_stream_view stream) const
Returns the last element.
Definition: device_uvector.hpp:340
const_pointer const_iterator
The type of the const iterator returned by cbegin()
Definition: device_uvector.hpp:81
device_buffer release() noexcept
Release ownership of device memory storage.
Definition: device_uvector.hpp:397
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:247
device_uvector(device_uvector &&) noexcept=default
Default move constructor.
pointer iterator
The type of the iterator returned by begin()
Definition: device_uvector.hpp:80
void reserve(size_type new_capacity, cuda_stream_view stream)
Increases the capacity of the vector to new_capacity elements.
Definition: device_uvector.hpp:357
value_type & reference
Reference type returned by operator[](size_type)
Definition: device_uvector.hpp:75
const_reverse_iterator rend() const noexcept
Returns const_reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:552
const_iterator end() const noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:488
pointer element_ptr(size_type element_index) noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:160
value_type front_element(cuda_stream_view stream) const
Returns the first element.
Definition: device_uvector.hpp:324
value_type const * const_pointer
The type of the pointer returned by data() const.
Definition: device_uvector.hpp:79
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:144
value_type const & const_reference
Constant reference type returned by operator[](size_type) const.
Definition: device_uvector.hpp:77
const_reverse_iterator rbegin() const noexcept
Returns a const_reverse_iterator to the last element.
Definition: device_uvector.hpp:518
value_type element(size_type element_index, cuda_stream_view stream) const
Returns the specified element from device memory.
Definition: device_uvector.hpp:302
const_pointer element_ptr(size_type element_index) const noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:174
cuda::std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: device_uvector.hpp:86
const_iterator begin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:458
iterator begin() noexcept
Returns an iterator to the first element.
Definition: device_uvector.hpp:440
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:223
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.