aclrtMemAllocManaged

Note: This API is reserved and is not supported currently.

Function

Allocates unified virtual memory (UVM). The pointer to the allocated memory is returned through *ptr. The size of the allocated memory is rounded up to the nearest multiple of 2 MB based on the specified size. The memory allocated by this API can only be freed by calling aclrtFree.

The page table mapping between the virtual memory and physical memory is established only when the memory allocated by this API is actually accessed. If the memory access occurs on the host, the physical memory of the host is mapped. If the memory access occurs on the device, the physical memory of the device is mapped. When the memory is used later, each time the object that accesses the memory changes, for example, from the host to the device or from one device to another device, a page fault interrupt is triggered on the new access object. The memory data needs to be migrated to the new access object, and the page table mapping between the virtual memory and physical memory needs to be re-established. In this case, the page table mapping on the previous access object becomes invalid and the physical memory is released. If the access object is frequently changed, page faults are frequently triggered, memory data is frequently migrated, and the page table mapping is frequently re-established, affecting performance.

Prototype

1
aclError aclrtMemAllocManaged(void **ptr, uint64_t size, uint32_t flag)

Parameters

Parameter

Input/Output

Description

ptr

Output

Pointer to the allocated memory. Because the virtual addresses of the host and device are uniformly addressed, this parameter does not distinguish the allocation location.

size

Input

Memory size, in bytes

The value of size cannot be 0. A single application process can allocate a maximum of 3 TB UVM.

flag

Input

Memory Identifier.

Currently, the flag can only be set to ACL_RT_MEM_ATTACH_GLOBAL, which corresponds to the value 1. After this parameter is set to ACL_RT_MEM_ATTACH_GLOBAL, the memory allocated by this API can be accessed on both the device and host sides.

The macros are defined as follows:

#define ACL_RT_MEM_ATTACH_GLOBAL (0x01U)

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 its random numbers.