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

PaddleBuf(size_t length)

PaddleBuf allocate memory internally, and manage it.

Parameters
  • [in] length: The length of data.

PaddleBuf(void *data, size_t length)

Set external memory, the PaddleBuf won’t manage it.

Parameters
  • [in] data: The start address of the external memory.

  • [in] length: The length of data.

PaddleBuf(const PaddleBuf &other)

Copy only available when memory is managed externally.

Parameters

void Resize(size_t length)

Resize the memory.

Parameters
  • [in] length: The length of data.

void Reset(void *data, size_t length)

Reset to external memory, with address and length set.

Parameters
  • [in] data: The start address of the external memory.

  • [in] length: The length of data.

bool empty() const

Tell whether the buffer is empty.

void *data() const

Get the data’s memory address.

size_t length() const

Get the memory length.

~PaddleBuf()
PaddleBuf &operator=(const PaddleBuf&)
PaddleBuf &operator=(PaddleBuf&&)
PaddleBuf() = default
PaddleBuf(PaddleBuf &&other)