20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/export.hpp>
25 #include <cuda_runtime_api.h>
31 namespace RMM_NAMESPACE {
110 : _stream{stream}, _mr{mr}
113 allocate_async(size);
139 : _stream{stream}, _mr{mr}
142 allocate_async(size);
143 copy_async(source_data, size);
186 : _data{other._data},
188 _capacity{other._capacity},
191 _device{other._device}
193 other._data =
nullptr;
197 other._device = cuda_device_id{-1};
216 if (&other !=
this) {
222 _capacity = other._capacity;
223 set_stream(other.
stream());
225 _device = other._device;
227 other._data =
nullptr;
271 if (new_capacity > capacity()) {
274 auto const old_size = size();
275 RMM_CUDA_TRY(cudaMemcpyAsync(tmp.data(), data(), size(), cudaMemcpyDefault, stream.
value()));
276 *
this = std::move(tmp);
311 if (new_size <= capacity()) {
316 RMM_CUDA_TRY(cudaMemcpyAsync(tmp.data(), data(), size(), cudaMemcpyDefault, stream.
value()));
317 *
this = std::move(tmp);
337 if (size() != capacity()) {
343 std::swap(tmp, *
this);
350 [[nodiscard]]
void const*
data() const noexcept {
return _data; }
355 void*
data() noexcept {
return _data; }
360 [[nodiscard]] std::size_t
size() const noexcept {
return _size; }
365 [[nodiscard]] std::int64_t
ssize() const noexcept
367 assert(size() <
static_cast<std::size_t
>(std::numeric_limits<int64_t>::max()) &&
368 "Size overflows signed integer");
369 return static_cast<int64_t
>(size());
378 [[nodiscard]]
bool is_empty() const noexcept {
return 0 == size(); }
387 [[nodiscard]] std::size_t
capacity() const noexcept {
return _capacity; }
413 void* _data{
nullptr};
415 std::size_t _capacity{};
416 cuda_stream_view _stream{};
432 void allocate_async(std::size_t bytes)
436 _data = (bytes > 0) ? _mr.allocate_async(bytes, stream()) :
nullptr;
448 void deallocate_async() noexcept
450 if (capacity() > 0) { _mr.deallocate_async(data(), capacity(), stream()); }
468 void copy_async(
void const* source, std::size_t bytes)
471 RMM_EXPECTS(
nullptr != source,
"Invalid copy from nullptr.");
472 RMM_EXPECTS(
nullptr != _data,
"Invalid copy to nullptr.");
474 RMM_CUDA_TRY(cudaMemcpyAsync(_data, source, bytes, cudaMemcpyDefault, stream().value()));
CUDA 流的强类型非拥有包装器,带有默认构造函数。
定义于: cuda_stream_view.hpp:39
constexpr cudaStream_t value() const noexcept
获取包装的流。
定义于: cuda_stream_view.hpp:73
用于设备内存分配的 RAII 构造。
定义于: device_buffer.hpp:82
cuda_stream_view stream() const noexcept
最近为分配/释放指定的流。
定义于: device_buffer.hpp:392
void resize(std::size_t new_size, cuda_stream_view stream)
调整设备内存分配的大小。
定义于: device_buffer.hpp:306
void * data() noexcept
指向设备内存分配的指针。
定义于: device_buffer.hpp:355
~device_buffer() noexcept
销毁设备缓冲区对象。
定义于: device_buffer.hpp:243
device_buffer & operator=(device_buffer &&other) noexcept
移动赋值运算符从 other 移动内容。
定义于: device_buffer.hpp:214
device_buffer()
默认构造函数创建一个空的 device_buffer
定义于: device_buffer.hpp:95
std::size_t capacity() const noexcept
返回设备内存分配的实际大小(以字节为单位)。
定义于: device_buffer.hpp:387
void const * data() const noexcept
指向设备内存分配的常量指针。
定义于: device_buffer.hpp:350
void reserve(std::size_t new_capacity, cuda_stream_view stream)
增加设备内存分配的容量。
定义于: device_buffer.hpp:268
device_buffer(std::size_t size, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
构造一个指定大小(未初始化字节)的新设备缓冲区。
定义于: device_buffer.hpp:107
void set_stream(cuda_stream_view stream) noexcept
设置用于释放的流。
定义于: device_buffer.hpp:405
std::size_t size() const noexcept
字节数。
定义于: device_buffer.hpp:360
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())
通过从指向现有主机或设备内存分配的原始指针复制来构造新的设备缓冲区...
定义于: device_buffer.hpp:135
device_buffer(device_buffer &&other) noexcept
通过将另一个 device_buffer 的内容移动到新构造的缓冲区中来构造新的 device_buffer...
定义于: device_buffer.hpp:185
device_buffer(device_buffer const &other, cuda_stream_view stream, device_async_resource_ref mr=mr::get_current_device_resource_ref())
通过深度复制另一个 device_buffer 的内容来构造新的 device_buffer,...
定义于: device_buffer.hpp:167
void shrink_to_fit(cuda_stream_view stream)
强制释放未使用的内存。
定义于: device_buffer.hpp:334
std::int64_t ssize() const noexcept
带符号的字节数。
定义于: device_buffer.hpp:365
bool is_empty() const noexcept
缓冲区当前是否包含任何数据。
定义于: device_buffer.hpp:378
rmm::device_async_resource_ref memory_resource() const noexcept
用于分配和释放的资源。
定义于: device_buffer.hpp:410
cuda_device_id get_current_cuda_device()
返回当前设备的 cuda_device_id。
定义于: cuda_device.hpp:99
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
带属性 cuda::mr::device_accessible 的 cuda::mr::async_resource_ref 的别名。
定义于: resource_ref.hpp:40
device_async_resource_ref get_current_device_resource_ref()
获取当前设备的 device_async_resource_ref。
定义于: per_device_resource.hpp:411
每设备 device_memory_resources 的管理。
CUDA 设备标识符的强类型。
定义于: cuda_device.hpp:41
RAII 类,在构造时将当前 CUDA 设备设置为指定设备,并在...时恢复
定义于: cuda_device.hpp:151