device_buffer.hpp File Reference

RAII construct for device memory allocation. More...

#include <rmm/cuda_stream_view.hpp>
#include <rmm/detail/error.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>
#include <rmm/mr/device/per_device_resource.hpp>
#include <cuda_runtime_api.h>
#include <cassert>
#include <stdexcept>
#include <utility>
Include dependency graph for device_buffer.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


class  rmm::device_buffer

Detailed Description

RAII construct for device memory allocation.

This class allocates untyped and uninitialized device memory using a device_memory_resource. If not explicitly specified, the memory resource returned from get_current_device_resource() is used.

Unlike std::vector or thrust::device_vector, the device memory allocated by a device_buffer is uninitialized. Therefore, it is undefined behavior to read the contents of data() before first initializing it.


//Allocates at least 100 bytes of device memory using the default memory
//resource and default stream.
device_buffer buff(100);
// allocates at least 100 bytes using the custom memory resource and
// specified stream
custom_memory_resource mr;
cuda_stream_view stream = cuda_stream_view{};
device_buffer custom_buff(100, stream, &mr);
// deep copies `buff` into a new device buffer using the default stream
device_buffer buff_copy(buff);
// deep copies `buff` into a new device buffer using the specified stream
device_buffer buff_copy(buff, stream);
// shallow copies `buff` into a new device_buffer, `buff` is now empty
device_buffer buff_move(std::move(buff));
// Default construction. Buffer is empty
device_buffer buff_default{};
// If the requested size is larger than the current size, resizes allocation
// to the new size and deep copies any previous contents. Otherwise, simply
// updates the value of `size()` to the newly requested size without any
// allocations or copies. Uses the optionally specified stream or the default
// stream if none specified.
buff_default.resize(100, stream);