Class PaddleBuf¶
Defined in File paddle_api.h
Class Documentation¶
-
class paddle::PaddleBuf¶
Memory manager for PaddleTensor.
The PaddleBuf holds a buffer for data input or output. The memory can be allocated by user or by PaddleBuf itself, but in any case, the PaddleBuf should be reused for better performance.
For user allocated memory, the following API can be used:
PaddleBuf(void* data, size_t length) to set an external memory by specifying the memory address and length.
Reset(void* data, size_t length) to reset the PaddleBuf with an external memory. ATTENTION, for user allocated memory, deallocation should be done by users externally after the program finished. The PaddleBuf won’t do any allocation or deallocation.
To have the PaddleBuf allocate and manage the memory:
PaddleBuf(size_t length) will allocate a memory of size
length
.Resize(size_t length) resize the memory to no less than
length
, ATTENTION if the allocated memory is larger thanlength
, nothing will done.
Usage:
Let PaddleBuf manage the memory internally.
const int num_elements = 128; PaddleBuf buf(num_elements/// sizeof(float));
Or
Works the exactly the same.PaddleBuf buf; buf.Resize(num_elements/// sizeof(float));
One can also make the
PaddleBuf
use the external memory.PaddleBuf buf; void* external_memory = new float[num_elements]; buf.Reset(external_memory, num_elements*sizeof(float)); ... delete[] external_memory; // manage the memory lifetime outside.
Public Functions
-
inline explicit PaddleBuf(size_t length)¶
PaddleBuf allocate memory internally, and manage it.
- Parameters
length – [in] The length of data.
-
inline PaddleBuf(void *data, size_t length)¶
Set external memory, the PaddleBuf won’t manage it.
- Parameters
data – [in] The start address of the external memory.
length – [in] The length of data.
-
explicit PaddleBuf(const PaddleBuf &other)¶
Copy only available when memory is managed externally.
- Parameters
other – [in] another
PaddleBuf
-
void Resize(size_t length)¶
Resize the memory.
- Parameters
length – [in] The length of data.
-
void Reset(void *data, size_t length)¶
Reset to external memory, with address and length set.
- Parameters
data – [in] The start address of the external memory.
length – [in] The length of data.
-
inline bool empty() const¶
Tell whether the buffer is empty.
-
inline void *data() const¶
Get the data’s memory address.
-
inline size_t length() const¶
Get the memory length.
-
inline ~PaddleBuf()¶
-
PaddleBuf() = default¶