Updated on 2022-03-13 GMT+08:00

HIAI_DMalloc

Allocates memory blocks on the host side or device side to support efficient data transmission. This API is defined in c_graph.h.

To allocate memory for DVPP on the device side, see HIAI_DVPP_DMalloc.

Application scenario:

With Matrix, this API is used to send large data from the host side to the device side. It must be used together with Macro: HIAI_REGISTER_SERIALIZE_FUNC.

For example, a 1080p or 4K image needs to be sent to the device side. To improve the data sending performance, you must register the structure serialization and deserialization functions through HIAI_REGISTER_SERIALIZE_FUNC. In addition, the large data block memory is allocated through HIAI_DMalloc. This greatly improves the data sending performance.

This API is used to send large data between the host side and the device side. If there is no performance requirement or no data sending is involved, this API is not recommended.

Syntax

void* HIAI_DMalloc (const uint32_t dataSize, const uint32_t timeOut, uint32_t flag)

Parameter Description

Parameter

Description

Value Range

dataSize

Size of a memory block

  • If this API is called to allocate memory on the host side, the value range is 0–256 MB, excluding 0. The recommended range is 256 KB–256 MB.
  • If this API is called to allocate memory on the device side, the value range is 0 to (256 MB – 96 bytes), excluding 0. The recommended range is (256 KB – 96 bytes) to (256 MB – 96 bytes). The model manager on the device side occupies 96 bytes.

timeOut

Timeout when memory fails to be allocated. The default value is 500 ms.

-

flag

  • If flag is set to MEMORY_ATTR_AUTO_FREE (default value), it indicates that if the memory is allocated and data is sent to the peer end using SendData, the HIAI_DFree API does not need to be called. After the programming is complete, the memory is automatically freed. If the memory is allocated but the data is not sent to the peer end using SendData, the HIAI_DFree API needs to be called to free the memory.
  • If flag is set to MEMORY_ATTR_MANUAL_FREE, the HIAI_DFree API must be called to free the memory in any case.
  • MEMORY_ATTR_NONE and MEMORY_ATTR_MAX are not used currently.

typedef enum {

MEMORY_ATTR_NONE = 0,

// The framework automatically frees the memory allocated by using DMalloc.

MEMORY_ATTR_AUTO_FREE = (0x1 << 1),

// DFree needs to be called manually to free the memory.

MEMORY_ATTR_MANUAL_FREE = (0x1 << 2),

MEMORY_ATTR_MAX

} HIAI_MEMORY_ATTR;

Return Value

Address of the memory allocated by using HIAI_DMalloc. If the memory fails to be allocated, a null pointer is returned.