Function: malloc

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Supported
  • Atlas inference products: Supported
  • Atlas training products: Supported

Function

Allocates linear memory of the specified size on the device, returns the pointer to the allocated memory through dev\_ptr, and ensures that the start address of the memory is 64-byte aligned. The memory allocated by calling this API needs to be freed by calling acl.rt.free.

The allocated memory is byte-aligned. The alignment policy varies depending on the product model.

  • For Ascend 950PR&950DT products, the allocation size is the input size rounded up to the nearest multiple of 32 bytes.
  • For the product models below, the memory allocated by this API is byte-aligned. The size bytes requested by users are rounded up to the nearest multiple of 32 bytes, with an additional 32 bytes added. However, for huge pages with the memory allocation granularity of 1 GB, to save huge pages, this API only rounds up the size requested by users to an integer multiple of 32 bytes, but does not add an extra 32 bytes.
    • Atlas A3 products
    • Atlas A2 products
    • Atlas 200I/500 A2 inference products
    • Atlas inference products
    • Atlas training products

Prototype

  • C Prototype

    aclError aclrtMalloc(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
    
  • Python Function

    dev_ptr, ret = acl.rt.malloc(size, policy)
    

Parameters

Parameter Description
size Int, size of the memory to be allocated, in bytes. The value cannot be 0.
policy Int, memory allocation policy.
If the configured memory allocation rule exceeds the value range in aclrtMemMallocPolicy and the size is greater than or equal to 2 MB, the huge page memory is allocated. Otherwise, common page memory is allocated.

Return Values

Return Value Description
dev_ptr Int, address of the pointer to the allocated device memory.
ret Int, error code. If 0 is returned, the operation is successful. If other values are returned, the operation fails.

Constraints

  • Media data processing has higher requirements on the memory for storing the input and output data (for example, the start address of the memory must be 128-byte aligned). Therefore, the following dedicated memory allocation APIs are required:

  • The memory allocated by this API is not initialized. Call acl.rt.memset to initialize the memory and clear random numbers in the memory before using the memory.

  • This API does not perform implicit device synchronization or stream synchronization. The memory allocation result, either success or failure, is returned immediately.

  • The value of policy can only be a single enumeration item and cannot be multiple enumeration items combined with bitwise OR.

  • For the ACL_MEM_MALLOC_HUGE_FIRST_P2P and ACL_MEM_MALLOC_HUGE_ONLY_P2P memory allocation rules, you are advised to use the acl.rt.malloc_with_cfg API. Otherwise, performance issues may occur or memory may fail to be allocated.

  • The memory allocated by calling the acl.rt.malloc API needs to be freed by calling the acl.rt.free API.

  • Performance deterioration will be caused by the frequent calling of acl.rt.malloc to allocate memory and acl.rt.free to free memory. You are advised to allocate or manage memory in advance to avoid frequent memory allocation and deallocation.

  • The size of memory allocated by calling acl.rt.malloc is the input size rounded up to the nearest multiple of 32, plus 32 bytes.

    If you use this API to allocate a large memory block, and divide and manage the memory, each memory segment must meet the following requirements:

    • 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]).

      Note

      len indicates the size of a memory segment, and ALIGN\_UP\[len, k\] indicates rounding up to the nearest multiple of k bytes: ((len - 1) / k + 1) x k.