aclrtMallocHost

Applicable Products

Product

Supported

Ascend 950PR / Ascend 950DT

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference products

Atlas training products

Function

Allocates host memory (page-locked memory). The system ensures that the start address of the memory is 64-byte aligned.

The memory allocated by this API can only be freed by calling aclrtFreeHost or aclrtFreeHostWithDevSync.

Prototype

1
aclError aclrtMallocHost(void **hostPtr, size_t size)

Parameters

Parameter

Input/Output

Description

hostPtr

Output

Pointer to the pointer to the allocated memory.

size

Input

Memory size in bytes to allocate.

size cannot be 0.

Returns

0 on success; otherwise, failure. For details, see aclError.

Restrictions

  • The memory allocated by this API is not initialized. Before using the memory, call aclrtMemset to initialize it and clear unpredictable contents.
  • This API does not perform implicit device synchronization or stream synchronization. The memory allocation result, either success or failure, is returned immediately.
  • If aclrtMallocHost is called to allocate too much page-locked memory, the physical memory used by the operating system for paging decreases, and the overall system performance deteriorates.
  • Performance deterioration will be caused by too frequent calls to aclrtMallocHost and aclrtFreeHost. You are advised to allocate or manage memory in advance to avoid unnecessary memory allocation and deallocation.
  • For the Ascend RC form and Open Ctrl CPU form, the host and device are integrated. Therefore, allocating host memory is equivalent to allocating device memory. In addition, memory is allocated based on normal pages. If the start address needs to be 64-byte aligned, you need to handle the alignment problem by yourself.
  • For the Ascend RC form and Ctrl CPU open form, if you use this API to allocate a large memory block and divide and manage the memory, each memory segment must meet the requirements listed below. len indicates the size of a memory segment. ALIGN_UP[len,k] indicates rounding up to a multiple of k bytes as in this formula: ((len – 1)/k + 1) x k.
    • The memory size is rounded up to the nearest multiple of 32 plus 32 bytes (m = ALIGN_UP[len,32] + 32 bytes).
    • The memory start address must be 64-byte aligned (ALIGN_UP[m,64]).

API Call Example

For the API call example, see Host Page-Locked Memory Usage.