Class PaddleBuf

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:

To have the PaddleBuf allocate and manage the memory:

Usage:

Let PaddleBuf manage the memory internally.

const int num_elements = 128;
PaddleBuf buf(num_elements/// sizeof(float));

Or

PaddleBuf buf;
buf.Resize(num_elements/// sizeof(float));
Works the exactly the same.

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 &operator=(const PaddleBuf&)
PaddleBuf &operator=(PaddleBuf&&)
PaddleBuf() = default
PaddleBuf(PaddleBuf &&other)