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

HIAIMemory::HIAI_DMalloc

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

To allocate memory for DVPP on the device side, see HIAIMemory::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 deals with sending of large data between the host side and device side. You are advised not to use it as a common malloc API.
  • For the sake of performance, this API pre-allocates memory. The actual memory size is different from the allocated memory size. The running time of this API may also change.

Syntax

static HIAI_StatusT HIAIMemory::HIAI_DMalloc (const uint32_t dataSize, void*& dataBuffer, const uint32_t timeOut = MALLOC_DEFAULT_TIME_OUT, uint32_t flag = MEMORY_ATTR_AUTO_FREE)

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.

dataBuffer

Memory pointer

-

timeOut

Timeout when memory fails to be allocated. The default value is MALLOC_DEFAULT_TIME_OUT (indicating 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 HIAIMemory::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 HIAIMemory::HIAI_DFree API needs to be called to free the memory.
  • If flag is set to MEMORY_ATTR_MANUAL_FREE, the HIAIMemory::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

For details about the returned error codes, see "Error Codes."

Error Codes

No.

Error Code

Description

1

HIAI_OK

The running is OK.

2

HIAI_GRAPH_NOT_EXIST

The graph does not exist.

3

HIAI_GRAPH_MEMORY_POOL_NOT_EXISTED

The memory pool does not exist.

4

HIAI_GRAPH_MALLOC_LARGER

The buffer fails to be allocated because the size is larger than 256 MB.

5

HIAI_MEMORY_POOL_UPDATE_FAILED

The memory pool fails to be updated.

6

HIAI_GRAPH_SENDMSG_FAILED

The HDC module fails to send a message.

7

HIAI_GRAPH_MEMORY_POOL_INITED

The memory pool has been initialized.

8

HIAI_GRAPH_NO_MEMORY

There is no memory.