20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/exec_check_disable.hpp>
22 #include <rmm/detail/export.hpp>
27 #include <cuda/memory_resource>
30 #include <type_traits>
33 namespace RMM_NAMESPACE {
79 static_assert(std::is_trivially_copyable_v<T>,
80 "device_uvector only supports types that are trivially copyable.");
93 RMM_EXEC_CHECK_DISABLE
96 RMM_EXEC_CHECK_DISABLE
99 RMM_EXEC_CHECK_DISABLE
132 : _storage{elements_to_bytes(size), stream, mr}
148 : _storage{other._storage, stream, mr}
162 assert(element_index < size());
163 return data() + element_index;
176 assert(element_index < size());
177 return data() + element_index;
221 element_index < size(),
"Attempt to access out of bounds element.",
rmm::out_of_range);
223 if constexpr (std::is_same_v<value_type, bool>) {
225 cudaMemsetAsync(element_ptr(element_index), value,
sizeof(value), stream.
value()));
229 if constexpr (std::is_fundamental_v<value_type>) {
231 set_element_to_zero_async(element_index, stream);
236 RMM_CUDA_TRY(cudaMemcpyAsync(
237 element_ptr(element_index), &value,
sizeof(value), cudaMemcpyDefault, stream.
value()));
242 void set_element_async(std::size_t, value_type
const&&,
cuda_stream_view) =
delete;
269 element_index < size(),
"Attempt to access out of bounds element.",
rmm::out_of_range);
271 cudaMemsetAsync(element_ptr(element_index), 0,
sizeof(
value_type), stream.
value()));
305 set_element_async(element_index, value, stream);
324 element_index < size(),
"Attempt to access out of bounds element.",
rmm::out_of_range);
326 RMM_CUDA_TRY(cudaMemcpyAsync(
327 &value, element_ptr(element_index),
sizeof(value), cudaMemcpyDefault, stream.
value()));
345 return element(0, stream);
361 return element(size() - 1, stream);
378 _storage.reserve(elements_to_bytes(new_capacity), stream);
399 _storage.resize(elements_to_bytes(new_size), stream);
424 [[nodiscard]] std::size_t
capacity() const noexcept
426 return bytes_to_elements(_storage.capacity());
512 [[nodiscard]] std::size_t
size() const noexcept {
return bytes_to_elements(_storage.size()); }
517 [[nodiscard]] std::int64_t
ssize() const noexcept
519 assert(size() <
static_cast<std::size_t
>(std::numeric_limits<int64_t>::max()) &&
520 "Size overflows signed integer");
521 return static_cast<int64_t
>(size());
527 [[nodiscard]]
bool is_empty() const noexcept {
return size() == 0; }
535 return _storage.memory_resource();
541 [[nodiscard]]
cuda_stream_view stream() const noexcept {
return _storage.stream(); }
554 void set_stream(
cuda_stream_view stream) noexcept { _storage.set_stream(stream); }
559 [[nodiscard]] std::size_t constexpr elements_to_bytes(std::size_t num_elements)
const noexcept
561 return num_elements *
sizeof(value_type);
564 [[nodiscard]] std::size_t constexpr bytes_to_elements(std::size_t num_bytes)
const noexcept
566 return num_bytes /
sizeof(value_type);
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
constexpr cudaStream_t value() const noexcept
Get the wrapped stream.
Definition: cuda_stream_view.hpp:73
void synchronize() const
Synchronize the viewed CUDA stream.
Definition: cuda_stream_view.hpp:106
void synchronize_no_throw() const noexcept
Synchronize the viewed CUDA stream. Does not throw if there is an error.
Definition: cuda_stream_view.hpp:113
RAII construct for device memory allocation.
Definition: device_buffer.hpp:83
An uninitialized vector of elements in device memory.
Definition: device_uvector.hpp:78
const_iterator cend() const noexcept
Returns a const_iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:497
std::size_t capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition: device_uvector.hpp:424
void resize(std::size_t new_size, cuda_stream_view stream)
Resizes the vector to contain new_size elements.
Definition: device_uvector.hpp:397
value_type * pointer
The type of the pointer returned by data()
Definition: device_uvector.hpp:88
const_pointer element_ptr(std::size_t element_index) const noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:174
bool is_empty() const noexcept
true if the vector contains no elements, i.e. size() == 0
Definition: device_uvector.hpp:527
const_pointer data() const noexcept
Returns const pointer to underlying device storage.
Definition: device_uvector.hpp:447
std::size_t size() const noexcept
The number of elements in the vector.
Definition: device_uvector.hpp:512
pointer data() noexcept
Returns pointer to underlying device storage.
Definition: device_uvector.hpp:437
void shrink_to_fit(cuda_stream_view stream)
Forces deallocation of unused device memory.
Definition: device_uvector.hpp:409
iterator end() noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:487
std::size_t size_type
The type used for the size of the vector.
Definition: device_uvector.hpp:84
pointer element_ptr(std::size_t element_index) noexcept
Returns pointer to the specified element.
Definition: device_uvector.hpp:160
std::int64_t ssize() const noexcept
The signed number of elements in the vector.
Definition: device_uvector.hpp:517
T value_type
T; stored value type.
Definition: device_uvector.hpp:83
const_iterator cbegin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:468
value_type back_element(cuda_stream_view stream) const
Returns the last element.
Definition: device_uvector.hpp:359
void set_element_to_zero_async(std::size_t element_index, cuda_stream_view stream)
Asynchronously sets the specified element to zero in device memory.
Definition: device_uvector.hpp:266
const_pointer const_iterator
The type of the const iterator returned by cbegin()
Definition: device_uvector.hpp:91
device_buffer release() noexcept
Release ownership of device memory storage.
Definition: device_uvector.hpp:416
void set_element_async(std::size_t 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:216
device_uvector(device_uvector &&) noexcept=default
Default move constructor.
pointer iterator
The type of the iterator returned by begin()
Definition: device_uvector.hpp:90
value_type & reference
value_type&; reference type returned by operator[](size_type)
Definition: device_uvector.hpp:85
const_iterator end() const noexcept
Returns an iterator to the element following the last element of the vector.
Definition: device_uvector.hpp:507
void reserve(std::size_t new_capacity, cuda_stream_view stream)
Increases the capacity of the vector to new_capacity elements.
Definition: device_uvector.hpp:376
value_type element(std::size_t element_index, cuda_stream_view stream) const
Returns the specified element from device memory.
Definition: device_uvector.hpp:321
value_type front_element(cuda_stream_view stream) const
Returns the first element.
Definition: device_uvector.hpp:343
value_type const * const_pointer
The type of the pointer returned by data() const.
Definition: device_uvector.hpp:89
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:145
value_type const & const_reference
Definition: device_uvector.hpp:87
void set_element(std::size_t 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:303
const_iterator begin() const noexcept
Returns a const_iterator to the first element.
Definition: device_uvector.hpp:477
iterator begin() noexcept
Returns an iterator to the first element.
Definition: device_uvector.hpp:459
Exception thrown when attempting to access outside of a defined range.
Definition: error.hpp:110
cuda::mr::async_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:41
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:411
Management of per-device device_memory_resources.