device_uvector.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, 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 <thrust/iterator/reverse_iterator.h>
17 
18 #include <cstddef>
19 #include <type_traits>
20 #include <utility>
21 
22 namespace RMM_NAMESPACE {
66 template <typename T>
68  static_assert(std::is_trivially_copyable_v<T>,
69  "device_uvector only supports types that are trivially copyable.");
70 
71  public:
72  using value_type = T;
73  using size_type = std::size_t;
74  using reference = value_type&;
75  using const_reference = value_type const&;
77  using pointer = value_type*;
78  using const_pointer = value_type const*;
79  using iterator = pointer;
82  thrust::reverse_iterator<iterator>;
84  thrust::reverse_iterator<const_iterator>;
85 
86  RMM_EXEC_CHECK_DISABLE
87  ~device_uvector() = default;
88 
89  RMM_EXEC_CHECK_DISABLE
90  device_uvector(device_uvector&&) noexcept = default;
91 
92  RMM_EXEC_CHECK_DISABLE
93  device_uvector& operator=(device_uvector&&) noexcept =
94  default;
95 
99  device_uvector(device_uvector const&) = delete;
100 
104  device_uvector& operator=(device_uvector const&) = delete;
105 
109  device_uvector() = delete;
110 
122  explicit device_uvector(size_type size,
123  cuda_stream_view stream,
125  : _storage{elements_to_bytes(size), stream, mr}
126  {
127  }
128 
138  explicit device_uvector(device_uvector const& other,
139  cuda_stream_view stream,
141  : _storage{other._storage, stream, mr}
142  {
143  }
144 
153  [[nodiscard]] pointer element_ptr(size_type element_index) noexcept
154  {
155  assert(element_index < size());
156  return data() + element_index;
157  }
158 
167  [[nodiscard]] const_pointer element_ptr(size_type element_index) const noexcept
168  {
169  assert(element_index < size());
170  return data() + element_index;
171  }
172 
209  void set_element_async(size_type element_index, value_type const& value, cuda_stream_view stream)
210  {
211  RMM_EXPECTS(
212  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
213 
214  if constexpr (std::is_same_v<value_type, bool>) {
215  RMM_CUDA_TRY(
216  cudaMemsetAsync(element_ptr(element_index), value, sizeof(value), stream.value()));
217  return;
218  }
219 
220  if constexpr (std::is_fundamental_v<value_type>) {
221  if (value == value_type{0}) {
222  set_element_to_zero_async(element_index, stream);
223  return;
224  }
225  }
226 
227  RMM_CUDA_TRY(cudaMemcpyAsync(
228  element_ptr(element_index), &value, sizeof(value), cudaMemcpyDefault, stream.value()));
229  }
230 
231  // We delete the r-value reference overload to prevent asynchronously copying from a literal or
232  // implicit temporary value after it is deleted or goes out of scope.
233  void set_element_async(size_type, value_type const&&, cuda_stream_view) = delete;
234 
258  {
259  RMM_EXPECTS(
260  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
261  RMM_CUDA_TRY(
262  cudaMemsetAsync(element_ptr(element_index), 0, sizeof(value_type), stream.value()));
263  }
264 
294  void set_element(size_type element_index, T const& value, cuda_stream_view stream)
295  {
296  set_element_async(element_index, value, stream);
297  stream.synchronize_no_throw();
298  }
299 
312  [[nodiscard]] value_type element(size_type element_index, cuda_stream_view stream) const
313  {
314  RMM_EXPECTS(
315  element_index < size(), "Attempt to access out of bounds element.", rmm::out_of_range);
316  value_type value;
317  RMM_CUDA_TRY(cudaMemcpyAsync(
318  &value, element_ptr(element_index), sizeof(value), cudaMemcpyDefault, stream.value()));
319  stream.synchronize();
320  return value;
321  }
322 
334  [[nodiscard]] value_type front_element(cuda_stream_view stream) const
335  {
336  return element(0, stream);
337  }
338 
350  [[nodiscard]] value_type back_element(cuda_stream_view stream) const
351  {
352  return element(size() - 1, stream);
353  }
354 
367  void reserve(size_type new_capacity, cuda_stream_view stream)
368  {
369  _storage.reserve(elements_to_bytes(new_capacity), stream);
370  }
371 
388  void resize(size_type new_size, cuda_stream_view stream)
389  {
390  _storage.resize(elements_to_bytes(new_size), stream);
391  }
392 
400  void shrink_to_fit(cuda_stream_view stream) { _storage.shrink_to_fit(stream); }
401 
407  device_buffer release() noexcept { return std::move(_storage); }
408 
415  [[nodiscard]] size_type capacity() const noexcept
416  {
417  return bytes_to_elements(_storage.capacity());
418  }
419 
428  [[nodiscard]] pointer data() noexcept { return static_cast<pointer>(_storage.data()); }
429 
438  [[nodiscard]] const_pointer data() const noexcept
439  {
440  return static_cast<const_pointer>(_storage.data());
441  }
442 
450  [[nodiscard]] iterator begin() noexcept { return data(); }
451 
459  [[nodiscard]] const_iterator cbegin() const noexcept { return data(); }
460 
468  [[nodiscard]] const_iterator begin() const noexcept { return cbegin(); }
469 
478  [[nodiscard]] iterator end() noexcept { return data() + size(); }
479 
488  [[nodiscard]] const_iterator cend() const noexcept { return data() + size(); }
489 
498  [[nodiscard]] const_iterator end() const noexcept { return cend(); }
499 
507  [[nodiscard]] reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
508 
516  [[nodiscard]] const_reverse_iterator crbegin() const noexcept
517  {
518  return const_reverse_iterator(cend());
519  }
520 
528  [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return crbegin(); }
529 
538  [[nodiscard]] reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
539 
549  [[nodiscard]] const_reverse_iterator crend() const noexcept
550  {
551  return const_reverse_iterator(begin());
552  }
553 
562  [[nodiscard]] const_reverse_iterator rend() const noexcept { return crend(); }
563 
567  [[nodiscard]] size_type size() const noexcept { return bytes_to_elements(_storage.size()); }
568 
572  [[nodiscard]] std::int64_t ssize() const noexcept
573  {
574  assert(size() < static_cast<size_type>(std::numeric_limits<int64_t>::max()) &&
575  "Size overflows signed integer");
576  return static_cast<int64_t>(size());
577  }
578 
582  [[nodiscard]] bool is_empty() const noexcept { return size() == 0; }
583 
588  [[nodiscard]] rmm::device_async_resource_ref memory_resource() const noexcept
589  {
590  return _storage.memory_resource();
591  }
592 
596  [[nodiscard]] cuda_stream_view stream() const noexcept { return _storage.stream(); }
597 
609  void set_stream(cuda_stream_view stream) noexcept { _storage.set_stream(stream); }
610 
611  private:
612  device_buffer _storage{};
613 
614  [[nodiscard]] size_type constexpr elements_to_bytes(size_type num_elements) const noexcept
615  {
616  return num_elements * sizeof(value_type);
617  }
618 
619  [[nodiscard]] size_type constexpr bytes_to_elements(size_type num_bytes) const noexcept
620  {
621  return num_bytes / sizeof(value_type);
622  }
623 };
624  // end of group
626 } // 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:70
An uninitialized vector of elements in device memory.
Definition: device_uvector.hpp:67
reverse_iterator rend() noexcept
Returns reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:538
const_iterator cend() const noexcept
Returns a const_iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:488
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:549
value_type * pointer
The type of the pointer returned by data()
Definition: device_uvector.hpp:77
thrust::reverse_iterator< const_iterator > const_reverse_iterator
The type of the iterator returned by crbegin()
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:209
bool is_empty() const noexcept
true if the vector contains no elements, i.e. size() == 0
Definition: device_uvector.hpp:582
size_type size() const noexcept
The number of elements in the vector.
Definition: device_uvector.hpp:567
const_pointer data() const noexcept
Returns const pointer to underlying device storage.
Definition: device_uvector.hpp:438
void resize(size_type new_size, cuda_stream_view stream)
Resizes the vector to contain new_size elements.
Definition: device_uvector.hpp:388
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:294
thrust::reverse_iterator< iterator > reverse_iterator
The type of the iterator returned by rbegin()
Definition: device_uvector.hpp:82
reverse_iterator rbegin() noexcept
Returns a reverse_iterator to the last element.
Definition: device_uvector.hpp:507
pointer data() noexcept
Returns pointer to underlying device storage.
Definition: device_uvector.hpp:428
void shrink_to_fit(cuda_stream_view stream)
Forces deallocation of unused device memory.
Definition: device_uvector.hpp:400
iterator end() noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:478
std::size_t size_type
The type used for the size of the vector.
Definition: device_uvector.hpp:73
const_reverse_iterator crbegin() const noexcept
Returns a const_reverse_iterator to the last element.
Definition: device_uvector.hpp:516
size_type capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition: device_uvector.hpp:415
std::int64_t ssize() const noexcept
The signed number of elements in the vector.
Definition: device_uvector.hpp:572
T value_type
T; stored value type.
Definition: device_uvector.hpp:72
const_iterator cbegin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:459
value_type back_element(cuda_stream_view stream) const
Returns the last element.
Definition: device_uvector.hpp:350
const_pointer const_iterator
The type of the const iterator returned by cbegin()
Definition: device_uvector.hpp:80
device_buffer release() noexcept
Release ownership of device memory storage.
Definition: device_uvector.hpp:407
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:257
device_uvector(device_uvector &&) noexcept=default
Default move constructor.
pointer iterator
The type of the iterator returned by begin()
Definition: device_uvector.hpp:79
void reserve(size_type new_capacity, cuda_stream_view stream)
Increases the capacity of the vector to new_capacity elements.
Definition: device_uvector.hpp:367
value_type & reference
value_type&; reference type returned by operator[](size_type)
Definition: device_uvector.hpp:74
const_reverse_iterator rend() const noexcept
Returns const_reverse_iterator to the element preceding the first element of the vector.
Definition: device_uvector.hpp:562
const_iterator end() const noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:498
pointer element_ptr(size_type element_index) noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:153
value_type front_element(cuda_stream_view stream) const
Returns the first element.
Definition: device_uvector.hpp:334
value_type const * const_pointer
The type of the pointer returned by data() const.
Definition: device_uvector.hpp:78
device_uvector(device_uvector const &other, cuda_stream_view stream, device_async_resource_ref 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:138
value_type const & const_reference
Definition: device_uvector.hpp:76
const_reverse_iterator rbegin() const noexcept
Returns a const_reverse_iterator to the last element.
Definition: device_uvector.hpp:528
value_type element(size_type element_index, cuda_stream_view stream) const
Returns the specified element from device memory.
Definition: device_uvector.hpp:312
const_pointer element_ptr(size_type element_index) const noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:167
const_iterator begin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:468
iterator begin() noexcept
Returns an iterator to the first element.
Definition: device_uvector.hpp:450
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:400
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:32
Management of per-device device_memory_resources.