Mila
Deep Neural Network Library
|
A buffer for storing tensor data with configurable memory management. More...
Public Member Functions | |
TensorBuffer (const TensorBuffer &)=delete | |
Copy operations are explicitly deleted. | |
TensorBuffer (size_t size, T *data_ptr) | |
Constructs a new TensorBuffer object with externally managed memory. | |
TensorBuffer (size_t size, T value=T{}) | |
Constructs a new TensorBuffer object with owned memory and optional value initialization. | |
~TensorBuffer () | |
Destroys the TensorBuffer object and deallocates memory if owned. | |
size_t | aligned_size () const |
Gets the aligned size of the buffer in bytes. | |
T * | data () |
Gets a pointer to the buffer's data. | |
bool | is_aligned () const noexcept |
Checks if the buffer's data pointer is properly aligned. | |
TensorBuffer & | operator= (const TensorBuffer &)=delete |
void | resize (size_t new_size) |
Resizes the buffer to a new size, preserving existing data when possible. | |
size_t | size () const |
Gets the size of the buffer. | |
Static Public Attributes | |
static constexpr size_t | alignment = detail::get_alignment<MR>() |
The alignment boundary for memory allocation based on the memory resource type. | |
Private Member Functions | |
size_t | calculateAlignedSize (size_t num_elements) const |
Calculates the aligned size in bytes for a given number of elements. | |
std::unique_ptr< Compute::MemoryResource > | createMemoryResource () |
Creates a memory resource based on the template parameter. | |
void | fillBuffer (T value=T{}) |
Initializes the buffer with a specified value. | |
Private Attributes | |
size_t | aligned_size_ { 0 } |
The actual size in bytes of the allocated memory, including alignment padding. | |
T * | data_ { nullptr } |
A pointer to the allocated memory containing the buffer's data. | |
std::unique_ptr< Compute::MemoryResource > | mr_ { nullptr } |
Memory resource used for allocation/deallocation (null for externally managed buffers). | |
size_t | size_ { 0 } |
The number of elements in the buffer. | |
A buffer for storing tensor data with configurable memory management.
TensorBuffer provides a generic container for tensor data that can be stored either in CPU or GPU memory based on the memory resource type. It supports both owned and externally managed memory.
TElementType | The type of the elements stored in the buffer. |
MR | The memory resource type that determines where and how memory is allocated. Must derive from Compute::MemoryResource. |
Example usage:
|
inlineexplicit |
Constructs a new TensorBuffer object with owned memory and optional value initialization.
This constructor allocates memory using the specified memory resource and optionally initializes all elements to the provided value.
size | The number of elements in the buffer. |
value | Value to initialize the buffer with (default: default-constructed TElementType). |
std::overflow_error | If size is too large, causing overflow in aligned size calculation. |
std::bad_alloc | If memory allocation fails. |
|
inline |
Constructs a new TensorBuffer object with externally managed memory.
This constructor does not allocate memory but instead wraps the provided pre-allocated memory. The buffer does not take ownership of this memory.
size | The number of elements in the buffer. |
data_ptr | Pointer to the pre-allocated memory. |
std::invalid_argument | If data_ptr is null. |
|
inline |
Destroys the TensorBuffer object and deallocates memory if owned.
If the buffer owns its memory (mr_ is not null), it will deallocate the memory using the memory resource. If the buffer uses externally managed memory, it will not deallocate anything.
|
delete |
Copy operations are explicitly deleted.
TensorBuffer does not support copying since it may involve deep copying of large memory blocks across different memory spaces.
|
inline |
Gets the aligned size of the buffer in bytes.
This is the actual memory size allocated, which may be larger than size() * sizeof(TPrecision) due to alignment padding.
|
inlineprivate |
Calculates the aligned size in bytes for a given number of elements.
num_elements | The number of elements. |
|
inlineprivate |
Creates a memory resource based on the template parameter.
|
inline |
Gets a pointer to the buffer's data.
|
inlineprivate |
Initializes the buffer with a specified value.
This method fills the buffer with the specified value, using different approaches depending on whether the buffer is in CPU or GPU memory.
value | Value to initialize the buffer with (default: default-constructed TElementType). |
|
inlinenoexcept |
Checks if the buffer's data pointer is properly aligned.
This method verifies that the buffer's data pointer is aligned according to the alignment requirements of the memory resource.
|
delete |
|
inline |
Resizes the buffer to a new size, preserving existing data when possible.
This method allocates a new buffer of the specified size, copies data from the old buffer to the new one (up to the smaller of the old and new sizes), and then deallocates the old buffer. If the new size is larger than the old size, the additional elements are default-initialized.
new_size | The new size of the buffer. Can be zero. |
std::runtime_error | If the buffer is externally managed. |
std::overflow_error | If new_size is too large, causing overflow in aligned size calculation. |
std::bad_alloc | If memory allocation fails. |
|
inline |
Gets the size of the buffer.
|
private |
The actual size in bytes of the allocated memory, including alignment padding.
|
staticconstexpr |
The alignment boundary for memory allocation based on the memory resource type.
This value is determined at compile time to ensure optimal memory access patterns for the specific memory resource type (CPU or GPU).
|
private |
A pointer to the allocated memory containing the buffer's data.
|
private |
Memory resource used for allocation/deallocation (null for externally managed buffers).
|
private |
The number of elements in the buffer.