device_buffer.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/aligned.hpp>
8 #include <rmm/cuda_device.hpp>
9 #include <rmm/detail/error.hpp>
10 #include <rmm/detail/export.hpp>
12 #include <rmm/resource_ref.hpp>
13 
14 #include <cuda/memory_resource>
15 #include <cuda/stream>
16 #include <cuda_runtime_api.h>
17 
18 #include <cassert>
19 #include <cstddef>
20 
21 RMM_NAMESPACE_BEGIN
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(
102  std::size_t size,
103  cuda::stream_ref stream,
104  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
105 
106  // clang-format off
108  // clang-format on
116  explicit device_buffer(
117  std::size_t size,
118  std::size_t alignment,
119  cuda::stream_ref stream,
120  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
121 
146  void const* source_data,
147  std::size_t size,
148  cuda::stream_ref stream,
149  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
150 
151  // clang-format off
153  // clang-format on
161  explicit device_buffer(
162  void const* source_data,
163  std::size_t size,
164  std::size_t alignment,
165  cuda::stream_ref stream,
166  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
194  device_buffer const& other,
195  cuda::stream_ref stream,
196  cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
197 
209  device_buffer(device_buffer&& other) noexcept;
210 
226 
234  ~device_buffer() noexcept;
235 
258  void reserve(std::size_t new_capacity, cuda::stream_ref stream);
259 
289  void resize(std::size_t new_size, cuda::stream_ref stream);
290 
308  void shrink_to_fit(cuda::stream_ref stream);
309 
313  [[nodiscard]] void const* data() const noexcept { return _data; }
314 
318  void* data() noexcept { return _data; }
319 
323  [[nodiscard]] std::size_t size() const noexcept { return _size; }
324 
328  [[nodiscard]] std::int64_t ssize() const noexcept
329  {
330  assert(size() < static_cast<std::size_t>(std::numeric_limits<int64_t>::max()) &&
331  "Size overflows signed integer");
332  return static_cast<int64_t>(size());
333  }
334 
341  [[nodiscard]] bool is_empty() const noexcept { return 0 == size(); }
342 
350  [[nodiscard]] std::size_t capacity() const noexcept { return _capacity; }
351 
353  [[nodiscard]] std::size_t alignment() const noexcept { return _alignment; }
354 
358  [[nodiscard]] cuda::stream_ref stream() const noexcept { return _stream; }
359 
371  void set_stream(cuda::stream_ref stream) noexcept { _stream = stream; }
372 
376  [[nodiscard]] rmm::device_async_resource_ref memory_resource() noexcept { return _mr; }
377 
378  private:
379  void* _data{nullptr};
380  std::size_t _size{};
381  std::size_t _alignment{rmm::CUDA_ALLOCATION_ALIGNMENT};
382  std::size_t _capacity{};
383  cuda::stream_ref _stream{cuda::stream_ref{
384  cudaStream_t{cudaStreamDefault}}};
385 
386  cuda::mr::any_resource<cuda::mr::device_accessible> _mr;
388  cuda_device_id _device{get_current_cuda_device()};
389 
399  void allocate_async(std::size_t bytes);
400 
410  void deallocate_async() noexcept;
411 
424  void copy_async(void const* source, std::size_t bytes);
425 };
426  // end of group
428 RMM_NAMESPACE_END
RAII construct for device memory allocation.
Definition: device_buffer.hpp:72
device_buffer(device_buffer const &other, cuda::stream_ref stream, cuda::mr::any_resource< cuda::mr::device_accessible > mr=mr::get_current_device_resource_ref())
Construct a new device_buffer by deep copying the contents of another device_buffer,...
rmm::device_async_resource_ref memory_resource() noexcept
The resource used to allocate and deallocate.
Definition: device_buffer.hpp:376
device_buffer(void const *source_data, std::size_t size, std::size_t alignment, cuda::stream_ref stream, cuda::mr::any_resource< cuda::mr::device_accessible > 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...
void * data() noexcept
Pointer to the device memory allocation.
Definition: device_buffer.hpp:318
~device_buffer() noexcept
Destroy the device buffer object.
void set_stream(cuda::stream_ref stream) noexcept
Sets the stream to be used for deallocation.
Definition: device_buffer.hpp:371
device_buffer(void const *source_data, std::size_t size, cuda::stream_ref stream, cuda::mr::any_resource< cuda::mr::device_accessible > 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:350
std::size_t alignment() const noexcept
Definition: device_buffer.hpp:353
device_buffer(std::size_t size, cuda::stream_ref stream, cuda::mr::any_resource< cuda::mr::device_accessible > mr=mr::get_current_device_resource_ref())
Constructs a new device buffer of size uninitialized bytes.
cuda::stream_ref stream() const noexcept
The stream most recently specified for allocation/deallocation.
Definition: device_buffer.hpp:358
device_buffer(std::size_t size, std::size_t alignment, cuda::stream_ref stream, cuda::mr::any_resource< cuda::mr::device_accessible > mr=mr::get_current_device_resource_ref())
Constructs a new device buffer of size uninitialized bytes.
std::size_t size() const noexcept
The number of bytes.
Definition: device_buffer.hpp:323
device_buffer(device_buffer &&other) noexcept
Constructs a new device_buffer by moving the contents of another device_buffer into the newly constru...
std::int64_t ssize() const noexcept
The signed number of bytes.
Definition: device_buffer.hpp:328
bool is_empty() const noexcept
Whether or not the buffer currently holds any data.
Definition: device_buffer.hpp:341
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:187
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
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25
Management of per-device memory resources.