device_buffer.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/aligned.hpp>
8 #include <rmm/cuda_device.hpp>
10 #include <rmm/detail/error.hpp>
11 #include <rmm/detail/export.hpp>
13 #include <rmm/resource_ref.hpp>
14 
15 #include <cuda/memory_resource>
16 #include <cuda_runtime_api.h>
17 
18 #include <cassert>
19 #include <cstddef>
20 
21 namespace RMM_NAMESPACE {
73  public:
74  // The copy constructor and copy assignment operator without a stream are deleted because they
75  // provide no way to specify an explicit stream
76  device_buffer(device_buffer const& other) = delete;
77  device_buffer& operator=(device_buffer const& other) = delete;
78 
82  // Note: we cannot use `device_buffer() = default;` because nvcc implicitly adds
83  // `__host__ __device__` specifiers to the defaulted constructor when it is called within the
84  // context of both host and device functions.
86 
101  explicit device_buffer(std::size_t size,
102  cuda_stream_view stream,
104 
115  explicit device_buffer(std::size_t size,
116  std::size_t alignment,
117  cuda_stream_view stream,
119 
143  device_buffer(void const* source_data,
144  std::size_t size,
145  cuda_stream_view stream,
147 
158  explicit device_buffer(void const* source_data,
159  std::size_t size,
160  std::size_t alignment,
161  cuda_stream_view stream,
189  cuda_stream_view stream,
191 
203  device_buffer(device_buffer&& other) noexcept;
204 
220 
228  ~device_buffer() noexcept;
229 
252  void reserve(std::size_t new_capacity, cuda_stream_view stream);
253 
283  void resize(std::size_t new_size, cuda_stream_view stream);
284 
302  void shrink_to_fit(cuda_stream_view stream);
303 
307  [[nodiscard]] void const* data() const noexcept { return _data; }
308 
312  void* data() noexcept { return _data; }
313 
317  [[nodiscard]] std::size_t size() const noexcept { return _size; }
318 
322  [[nodiscard]] std::int64_t ssize() const noexcept
323  {
324  assert(size() < static_cast<std::size_t>(std::numeric_limits<int64_t>::max()) &&
325  "Size overflows signed integer");
326  return static_cast<int64_t>(size());
327  }
328 
335  [[nodiscard]] bool is_empty() const noexcept { return 0 == size(); }
336 
344  [[nodiscard]] std::size_t capacity() const noexcept { return _capacity; }
345 
347  [[nodiscard]] std::size_t alignment() const noexcept { return _alignment; }
348 
352  [[nodiscard]] cuda_stream_view stream() const noexcept { return _stream; }
353 
365  void set_stream(cuda_stream_view stream) noexcept { _stream = stream; }
366 
370  [[nodiscard]] rmm::device_async_resource_ref memory_resource() noexcept { return _mr; }
371 
372  private:
373  void* _data{nullptr};
374  std::size_t _size{};
375  std::size_t _alignment{rmm::CUDA_ALLOCATION_ALIGNMENT};
376  std::size_t _capacity{};
377  cuda_stream_view _stream{};
378 
379  cuda::mr::any_resource<cuda::mr::device_accessible> _mr;
381  cuda_device_id _device{get_current_cuda_device()};
382 
392  void allocate_async(std::size_t bytes);
393 
403  void deallocate_async() noexcept;
404 
417  void copy_async(void const* source, std::size_t bytes);
418 };
419  // end of group
421 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
RAII construct for device memory allocation.
Definition: device_buffer.hpp:72
cuda_stream_view stream() const noexcept
The stream most recently specified for allocation/deallocation.
Definition: device_buffer.hpp:352
rmm::device_async_resource_ref memory_resource() noexcept
The resource used to allocate and deallocate.
Definition: device_buffer.hpp:370
void * data() noexcept
Pointer to the device memory allocation.
Definition: device_buffer.hpp:312
device_buffer(std::size_t size, std::size_t alignment, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
Constructs a new device buffer of size uninitialized bytes.
~device_buffer() noexcept
Destroy the device buffer object.
device_buffer(void const *source_data, std::size_t size, std::size_t alignment, 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...
device_buffer & operator=(device_buffer &&other) noexcept
Move assignment operator moves the contents from other.
device_buffer()
Default constructor creates an empty device_buffer
std::size_t capacity() const noexcept
Returns actual size in bytes of device memory allocation.
Definition: device_buffer.hpp:344
std::size_t alignment() const noexcept
Definition: device_buffer.hpp:347
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.
void set_stream(cuda_stream_view stream) noexcept
Sets the stream to be used for deallocation.
Definition: device_buffer.hpp:365
std::size_t size() const noexcept
The number of bytes.
Definition: device_buffer.hpp:317
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...
device_buffer(device_buffer &&other) noexcept
Constructs a new device_buffer by moving the contents of another device_buffer into the newly constru...
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,...
std::int64_t ssize() const noexcept
The signed number of bytes.
Definition: device_buffer.hpp:322
bool is_empty() const noexcept
Whether or not the buffer currently holds any data.
Definition: device_buffer.hpp:335
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:405
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
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25
Management of per-device device_memory_resources.