cuLite v0.3.1
A lite CUDA C++ Interface
Loading...
Searching...
No Matches
culite::DeviceBuffer< T > Class Template Reference

A RAII wrapper for device memory allocation. More...

Public Member Functions

 DeviceBuffer ()
 Default constructor.
 DeviceBuffer (std::size_t n)
 Constructs a device buffer with specified capacity.
 ~DeviceBuffer ()
 Destructor.
void reserve (std::size_t n)
 Reserves device memory for at least the specified number of elements.
void clear () noexcept
 Frees the allocated device memory.
T * data () noexcept
 Returns a pointer to the device memory buffer.
const T * data () const noexcept
 Returns a const pointer to the device memory buffer.
std::size_t size () const noexcept
 Returns the number of elements in the buffer.

Detailed Description

template<typename T>
class culite::DeviceBuffer< T >

A RAII wrapper for device memory allocation.

This class manages a dynamically allocated buffer on the device. It automatically handles memory allocation and deallocation following RAII principles, ensuring that device memory is properly freed when the buffer goes out of scope.

Template Parameters
TThe type of elements stored in the device buffer.

Constructor & Destructor Documentation

◆ DeviceBuffer() [1/2]

template<typename T>
culite::DeviceBuffer< T >::DeviceBuffer ( )
inline

Default constructor.

Constructs an empty device buffer with no allocated memory.

◆ DeviceBuffer() [2/2]

template<typename T>
culite::DeviceBuffer< T >::DeviceBuffer ( std::size_t n)
inlineexplicit

Constructs a device buffer with specified capacity.

Constructs a device buffer and reserves memory for n elements on the device.

Parameters
[in]nThe number of elements to allocate.

◆ ~DeviceBuffer()

template<typename T>
culite::DeviceBuffer< T >::~DeviceBuffer ( )
inline

Destructor.

Automatically frees the allocated device memory.

Member Function Documentation

◆ reserve()

template<typename T>
void culite::DeviceBuffer< T >::reserve ( std::size_t n)
inline

Reserves device memory for at least the specified number of elements.

If n is greater than the current size, the existing memory is freed and new memory is allocated. If the allocation fails, the buffer is left empty.

Parameters
[in]nThe number of elements to reserve.

◆ clear()

template<typename T>
void culite::DeviceBuffer< T >::clear ( )
inlinenoexcept

Frees the allocated device memory.

Releases all device memory and resets the buffer to an empty state.

Note
This function is marked noexcept and is guaranteed not to throw exceptions.

◆ data() [1/2]

template<typename T>
T * culite::DeviceBuffer< T >::data ( )
inlinenoexcept

Returns a pointer to the device memory buffer.

Provides direct access to the underlying device memory.

Returns
A pointer to the device buffer.

◆ data() [2/2]

template<typename T>
const T * culite::DeviceBuffer< T >::data ( ) const
inlinenoexcept

Returns a const pointer to the device memory buffer.

Provides direct read-only access to the underlying device memory.

Returns
A const pointer to the device buffer.

◆ size()

template<typename T>
std::size_t culite::DeviceBuffer< T >::size ( ) const
inlinenoexcept

Returns the number of elements in the buffer.

Returns the capacity of the buffer (number of elements allocated).

Returns
The number of elements the buffer can hold.