20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/export.hpp>
25 #include <cuda/memory_resource>
26 #include <cuda_runtime_api.h>
33 namespace RMM_NAMESPACE {
113 : _stream{stream}, _mr{mr}
116 allocate_async(size);
142 : _stream{stream}, _mr{mr}
145 allocate_async(size);
146 copy_async(source_data, size);
189 : _data{other._data},
191 _capacity{other._capacity},
194 _device{other._device}
196 other._data =
nullptr;
200 other._device = cuda_device_id{-1};
219 if (&other !=
this) {
225 _capacity = other._capacity;
226 set_stream(other.
stream());
228 _device = other._device;
230 other._data =
nullptr;
274 if (new_capacity > capacity()) {
277 auto const old_size = size();
278 RMM_CUDA_TRY(cudaMemcpyAsync(tmp.data(), data(), size(), cudaMemcpyDefault, stream.
value()));
279 *
this = std::move(tmp);
314 if (new_size <= capacity()) {
319 RMM_CUDA_TRY(cudaMemcpyAsync(tmp.data(), data(), size(), cudaMemcpyDefault, stream.
value()));
320 *
this = std::move(tmp);
340 if (size() != capacity()) {
346 std::swap(tmp, *
this);
353 [[nodiscard]]
void const*
data() const noexcept {
return _data; }
358 void*
data() noexcept {
return _data; }
363 [[nodiscard]] std::size_t
size() const noexcept {
return _size; }
368 [[nodiscard]] std::int64_t
ssize() const noexcept
370 assert(size() <
static_cast<std::size_t
>(std::numeric_limits<int64_t>::max()) &&
371 "Size overflows signed integer");
372 return static_cast<int64_t
>(size());
381 [[nodiscard]]
bool is_empty() const noexcept {
return 0 == size(); }
390 [[nodiscard]] std::size_t
capacity() const noexcept {
return _capacity; }
416 void* _data{
nullptr};
418 std::size_t _capacity{};
419 cuda_stream_view _stream{};
435 void allocate_async(std::size_t bytes)
439 _data = (bytes > 0) ? _mr.allocate_async(bytes, stream()) :
nullptr;
451 void deallocate_async() noexcept
453 if (capacity() > 0) { _mr.deallocate_async(data(), capacity(), stream()); }
471 void copy_async(
void const* source, std::size_t bytes)
474 RMM_EXPECTS(
nullptr != source,
"Invalid copy from nullptr.");
475 RMM_EXPECTS(
nullptr != _data,
"Invalid copy to nullptr.");
477 RMM_CUDA_TRY(cudaMemcpyAsync(_data, source, bytes, cudaMemcpyDefault, stream().value()));
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
constexpr cudaStream_t value() const noexcept
Get the wrapped stream.
Definition: cuda_stream_view.hpp:75
RAII construct for device memory allocation.
Definition: device_buffer.hpp:84
cuda_stream_view stream() const noexcept
The stream most recently specified for allocation/deallocation.
Definition: device_buffer.hpp:395
void resize(std::size_t new_size, cuda_stream_view stream)
Resize the device memory allocation.
Definition: device_buffer.hpp:309
void * data() noexcept
Pointer to the device memory allocation.
Definition: device_buffer.hpp:358
~device_buffer() noexcept
Destroy the device buffer object.
Definition: device_buffer.hpp:246
device_buffer & operator=(device_buffer &&other) noexcept
Move assignment operator moves the contents from other.
Definition: device_buffer.hpp:217
device_buffer()
Default constructor creates an empty device_buffer
Definition: device_buffer.hpp:98
std::size_t capacity() const noexcept
Returns actual size in bytes of device memory allocation.
Definition: device_buffer.hpp:390
void const * data() const noexcept
Const pointer to the device memory allocation.
Definition: device_buffer.hpp:353
void reserve(std::size_t new_capacity, cuda_stream_view stream)
Increase the capacity of the device memory allocation.
Definition: device_buffer.hpp:271
device_buffer(std::size_t size, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
Constructs a new device buffer of size uninitialized bytes.
Definition: device_buffer.hpp:110
void set_stream(cuda_stream_view stream) noexcept
Sets the stream to be used for deallocation.
Definition: device_buffer.hpp:408
std::size_t size() const noexcept
The number of bytes.
Definition: device_buffer.hpp:363
device_buffer(void const *source_data, std::size_t size, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
Construct a new device buffer by copying from a raw pointer to an existing host or device memory allo...
Definition: device_buffer.hpp:138
device_buffer(device_buffer &&other) noexcept
Constructs a new device_buffer by moving the contents of another device_buffer into the newly constru...
Definition: device_buffer.hpp:188
device_buffer(device_buffer const &other, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
Construct a new device_buffer by deep copying the contents of another device_buffer,...
Definition: device_buffer.hpp:170
void shrink_to_fit(cuda_stream_view stream)
Forces the deallocation of unused memory.
Definition: device_buffer.hpp:337
std::int64_t ssize() const noexcept
The signed number of bytes.
Definition: device_buffer.hpp:368
bool is_empty() const noexcept
Whether or not the buffer currently holds any data.
Definition: device_buffer.hpp:381
rmm::device_async_resource_ref memory_resource() const noexcept
The resource used to allocate and deallocate.
Definition: device_buffer.hpp:413
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
Definition: cuda_device.hpp:96
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.
Strong type for a CUDA device identifier.
Definition: cuda_device.hpp:38
RAII class that sets the current CUDA device to the specified device on construction and restores the...
Definition: cuda_device.hpp:148