device_buffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/cuda_device.hpp>
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/export.hpp>
23 #include <rmm/resource_ref.hpp>
24 
25 #include <cuda_runtime_api.h>
26 
27 #include <cassert>
28 #include <cstddef>
29 
30 namespace RMM_NAMESPACE {
82  public:
83  // The copy constructor and copy assignment operator without a stream are deleted because they
84  // provide no way to specify an explicit stream
85  device_buffer(device_buffer const& other) = delete;
86  device_buffer& operator=(device_buffer const& other) = delete;
87 
91  // Note: we cannot use `device_buffer() = default;` because nvcc implicitly adds
92  // `__host__ __device__` specifiers to the defaulted constructor when it is called within the
93  // context of both host and device functions.
95 
106  explicit device_buffer(std::size_t size,
107  cuda_stream_view stream,
109 
129  device_buffer(void const* source_data,
130  std::size_t size,
131  cuda_stream_view stream,
133 
156  cuda_stream_view stream,
158 
170  device_buffer(device_buffer&& other) noexcept;
171 
187 
195  ~device_buffer() noexcept;
196 
215  void reserve(std::size_t new_capacity, cuda_stream_view stream);
216 
242  void resize(std::size_t new_size, cuda_stream_view stream);
243 
257  void shrink_to_fit(cuda_stream_view stream);
258 
262  [[nodiscard]] void const* data() const noexcept { return _data; }
263 
267  void* data() noexcept { return _data; }
268 
272  [[nodiscard]] std::size_t size() const noexcept { return _size; }
273 
277  [[nodiscard]] std::int64_t ssize() const noexcept
278  {
279  assert(size() < static_cast<std::size_t>(std::numeric_limits<int64_t>::max()) &&
280  "Size overflows signed integer");
281  return static_cast<int64_t>(size());
282  }
283 
290  [[nodiscard]] bool is_empty() const noexcept { return 0 == size(); }
291 
299  [[nodiscard]] std::size_t capacity() const noexcept { return _capacity; }
300 
304  [[nodiscard]] cuda_stream_view stream() const noexcept { return _stream; }
305 
317  void set_stream(cuda_stream_view stream) noexcept { _stream = stream; }
318 
322  [[nodiscard]] rmm::device_async_resource_ref memory_resource() const noexcept { return _mr; }
323 
324  private:
325  void* _data{nullptr};
326  std::size_t _size{};
327  std::size_t _capacity{};
328  cuda_stream_view _stream{};
329 
333  cuda_device_id _device{get_current_cuda_device()};
334 
344  void allocate_async(std::size_t bytes);
345 
355  void deallocate_async() noexcept;
356 
369  void copy_async(void const* source, std::size_t bytes);
370 };
371  // end of group
373 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
RAII construct for device memory allocation.
Definition: device_buffer.hpp:81
cuda_stream_view stream() const noexcept
The stream most recently specified for allocation/deallocation.
Definition: device_buffer.hpp:304
void * data() noexcept
Pointer to the device memory allocation.
Definition: device_buffer.hpp:267
~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:299
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:317
std::size_t size() const noexcept
The number of bytes.
Definition: device_buffer.hpp:272
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:277
bool is_empty() const noexcept
Whether or not the buffer currently holds any data.
Definition: device_buffer.hpp:290
rmm::device_async_resource_ref memory_resource() const noexcept
The resource used to allocate and deallocate.
Definition: device_buffer.hpp:322
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
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:40
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.