device_buffer.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #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_runtime_api.h>
15 
16 #include <cassert>
17 #include <cstddef>
18 
19 namespace RMM_NAMESPACE {
71  public:
72  // The copy constructor and copy assignment operator without a stream are deleted because they
73  // provide no way to specify an explicit stream
74  device_buffer(device_buffer const& other) = delete;
75  device_buffer& operator=(device_buffer const& other) = delete;
76 
80  // Note: we cannot use `device_buffer() = default;` because nvcc implicitly adds
81  // `__host__ __device__` specifiers to the defaulted constructor when it is called within the
82  // context of both host and device functions.
84 
95  explicit device_buffer(std::size_t size,
96  cuda_stream_view stream,
98 
118  device_buffer(void const* source_data,
119  std::size_t size,
120  cuda_stream_view stream,
122 
145  cuda_stream_view stream,
147 
159  device_buffer(device_buffer&& other) noexcept;
160 
176 
184  ~device_buffer() noexcept;
185 
208  void reserve(std::size_t new_capacity, cuda_stream_view stream);
209 
239  void resize(std::size_t new_size, cuda_stream_view stream);
240 
258  void shrink_to_fit(cuda_stream_view stream);
259 
263  [[nodiscard]] void const* data() const noexcept { return _data; }
264 
268  void* data() noexcept { return _data; }
269 
273  [[nodiscard]] std::size_t size() const noexcept { return _size; }
274 
278  [[nodiscard]] std::int64_t ssize() const noexcept
279  {
280  assert(size() < static_cast<std::size_t>(std::numeric_limits<int64_t>::max()) &&
281  "Size overflows signed integer");
282  return static_cast<int64_t>(size());
283  }
284 
291  [[nodiscard]] bool is_empty() const noexcept { return 0 == size(); }
292 
300  [[nodiscard]] std::size_t capacity() const noexcept { return _capacity; }
301 
305  [[nodiscard]] cuda_stream_view stream() const noexcept { return _stream; }
306 
318  void set_stream(cuda_stream_view stream) noexcept { _stream = stream; }
319 
323  [[nodiscard]] rmm::device_async_resource_ref memory_resource() const noexcept { return _mr; }
324 
325  private:
326  void* _data{nullptr};
327  std::size_t _size{};
328  std::size_t _capacity{};
329  cuda_stream_view _stream{};
330 
334  cuda_device_id _device{get_current_cuda_device()};
335 
345  void allocate_async(std::size_t bytes);
346 
356  void deallocate_async() noexcept;
357 
370  void copy_async(void const* source, std::size_t bytes);
371 };
372  // end of group
374 } // 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:70
cuda_stream_view stream() const noexcept
The stream most recently specified for allocation/deallocation.
Definition: device_buffer.hpp:305
void * data() noexcept
Pointer to the device memory allocation.
Definition: device_buffer.hpp:268
~device_buffer() noexcept
Destroy the device buffer object.
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:300
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:318
std::size_t size() const noexcept
The number of bytes.
Definition: device_buffer.hpp:273
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:278
bool is_empty() const noexcept
Whether or not the buffer currently holds any data.
Definition: device_buffer.hpp:291
rmm::device_async_resource_ref memory_resource() const noexcept
The resource used to allocate and deallocate.
Definition: device_buffer.hpp:323
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: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.