Loading...
Searching...
No Matches
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
21RMM_NAMESPACE_BEGIN
72class device_buffer {
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
84 // Note: we cannot use `device_buffer() = default;` because nvcc implicitly adds
85 // `__host__ __device__` specifiers to the defaulted constructor when it is called within the
86 // context of both host and device functions.
88
104 std::size_t size,
105 cuda::stream_ref stream,
106 cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
107
108 // clang-format off
110 // clang-format on
119 std::size_t size,
120 std::size_t alignment,
121 cuda::stream_ref stream,
122 cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
123
148 void const* source_data,
149 std::size_t size,
150 cuda::stream_ref stream,
151 cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
152
153 // clang-format off
155 // clang-format on
164 void const* source_data,
165 std::size_t size,
166 std::size_t alignment,
167 cuda::stream_ref stream,
168 cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
196 device_buffer const& other,
197 cuda::stream_ref stream,
198 cuda::mr::any_resource<cuda::mr::device_accessible> mr = mr::get_current_device_resource_ref());
199
211 device_buffer(device_buffer&& other) noexcept;
212
228 device_buffer& operator=(device_buffer&& other) noexcept;
229
237 ~device_buffer() noexcept;
238
261 void reserve(std::size_t new_capacity, cuda::stream_ref stream);
262
292 void resize(std::size_t new_size, cuda::stream_ref stream);
293
311 void shrink_to_fit(cuda::stream_ref stream);
312
316 [[nodiscard]] void const* data() const noexcept { return _data; }
317
321 void* data() noexcept { return _data; }
322
326 [[nodiscard]] std::size_t size() const noexcept { return _size; }
327
331 [[nodiscard]] std::int64_t ssize() const noexcept
332 {
333 assert(size() < static_cast<std::size_t>(std::numeric_limits<int64_t>::max()) &&
334 "Size overflows signed integer");
335 return static_cast<int64_t>(size());
336 }
337
344 [[nodiscard]] bool is_empty() const noexcept { return 0 == size(); }
345
353 [[nodiscard]] std::size_t capacity() const noexcept { return _capacity; }
354
356 [[nodiscard]] std::size_t alignment() const noexcept { return _alignment; }
357
361 [[nodiscard]] cuda::stream_ref stream() const noexcept { return _stream; }
362
374 void set_stream(cuda::stream_ref stream) noexcept { _stream = stream; }
375
379 [[nodiscard]] rmm::device_async_resource_ref memory_resource() noexcept { return _mr; }
380
381 private:
382 void* _data{nullptr};
383 std::size_t _size{};
384 std::size_t _alignment{rmm::CUDA_ALLOCATION_ALIGNMENT};
385 std::size_t _capacity{};
386 cuda::stream_ref _stream{
387 cuda::invalid_stream_t{}};
388
389 cuda::mr::any_resource<cuda::mr::device_accessible> _mr;
391 cuda_device_id _device{get_current_cuda_device()};
392
402 void allocate_async(std::size_t bytes);
403
413 void deallocate_async() noexcept;
414
427 void copy_async(void const* source, std::size_t bytes);
428};
429 // end of group
431RMM_NAMESPACE_END
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:379
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 shrink_to_fit(cuda::stream_ref stream)
Forces the deallocation of unused memory.
~device_buffer() noexcept
Destroy the device buffer object.
void * data() noexcept
Pointer to the device memory allocation.
Definition device_buffer.hpp:321
void set_stream(cuda::stream_ref stream) noexcept
Sets the stream to be used for deallocation.
Definition device_buffer.hpp:374
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()
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:353
std::size_t alignment() const noexcept
Definition device_buffer.hpp:356
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 (invalid if none).
Definition device_buffer.hpp:361
void reserve(std::size_t new_capacity, cuda::stream_ref stream)
Increase the capacity of the device memory allocation.
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.
void resize(std::size_t new_size, cuda::stream_ref stream)
Resize the device memory allocation.
void const * data() const noexcept
Const pointer to the device memory allocation.
Definition device_buffer.hpp:316
std::size_t size() const noexcept
The number of bytes.
Definition device_buffer.hpp:326
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 & operator=(device_buffer &&other) noexcept
Move assignment operator moves the contents from other.
std::int64_t ssize() const noexcept
The signed number of bytes.
Definition device_buffer.hpp:331
bool is_empty() const noexcept
Whether or not the buffer currently holds any data.
Definition device_buffer.hpp:344
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
Memory Resource classes and adaptors.
Management of per-device memory resources.