26. C++ Extension APIs

This chapter describes C++ extension APIs, including APIs for function overloading and template encapsulation, which simplify API calls in C++ scenarios and provide type-safe memory operations.





aclrtSynchronizeDevice

aclError aclrtSynchronizeDevice(int32_t timeout)

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

Description

Blocks the current thread until the device corresponding to the context bound to the current thread completes computation.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtSynchronizeDeviceWithTimeout is called.

Parameters

Parameter Input/Output Description
timeout Input Timeout of the API.
Value description:
- -1: Permanent wait.
- > 0: A specific timeout, in milliseconds.

Return Values

0: success; others: failure. For details, see aclError.




aclrtSynchronizeStream

aclError aclrtSynchronizeStream(aclrtStream stream, int32_t timeout)

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

Description

Blocks the current thread on the host until all tasks in the specified stream are complete. This API also allows you to set a timeout so that it can exit on its own when the application becomes abnormal.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtSynchronizeStreamWithTimeout is called.

Parameters

Parameter Input/Output Description
stream Input Stream for which all tasks need to be completed. See aclrtStream for the type definition.
timeout Input Timeout of the API.
Value description:
- -1: Permanent wait.
- > 0: A specific timeout, in milliseconds.

Return Values

0: success; others: failure. For details, see aclError.




aclrtSynchronizeEvent

aclError aclrtSynchronizeEvent(aclrtEvent event, int32_t timeout)

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

Description

Blocks the current thread until all tasks captured by the event are complete. This API also allows you to wait permanently or configure a specific timeout. If a specific timeout is configured, the application can exit on its own based on the configured timeout when an exception occurs.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtSynchronizeEventWithTimeout is called.

Parameters

Parameter Input/Output Description
event Input Event to wait for. See aclrtEvent for the type definition.
timeout Input Timeout of the API.
Value description:
- -1: Permanent wait.
- > 0: A specific timeout, in milliseconds.

Return Values

0: success; others: failure. For details, see aclError.




aclrtStreamWaitEvent

aclError aclrtStreamWaitEvent(aclrtStream stream, aclrtEvent event, int32_t timeout)

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

Description

Blocks the execution of the specified stream until the specified event is complete. This is an asynchronous API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtStreamWaitEventWithTimeout is called.

Parameters

Parameter Input/Output Description
stream Input Specified stream. See aclrtStream for the type definition.
In a multi-stream synchronous wait scenario, for example, when Stream2 waits for Stream1, set this parameter to Stream2.
event Input Event to wait for. See aclrtEvent for the type definition.
timeout Input Timeout period.
Value description:
- 0: No timeout.
- > 0: A specific timeout, in milliseconds.

Return Values

0: success; others: failure. For details, see aclError.




aclrtCreateStream

aclError aclrtCreateStream(aclrtStream *stream, uint32_t priority, uint32_t flag)

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

Description

Creates a stream in the current process or thread.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtCreateStreamWithConfig is called.

Parameters

Parameter Input/Output Description
stream Output Pointer to the stream. See aclrtStream for the type definition.
priority Input Priority.
The value range is [0, 7], supporting up to 8 priorities in total. A smaller number indicates a higher priority, where 0 is the highest and 7 is the lowest. If the configured priority exceeds the value range, it is corrected to the nearest boundary value.
flag Input Flag of the stream pointer.
For the flag value description, see the "flag value description" in the C API aclrtCreateStreamWithConfig.

Return Values

0: success; others: failure. For details, see aclError.




aclrtSetOpExecuteTimeOut

aclError aclrtSetOpExecuteTimeOut(uint64_t timeout, uint64_t *actualTimeout)

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

Description

Sets the timeout for operator execution, in microseconds. If a timeout is specified when the operator is delivered, that timeout takes priority over the timeout set by this API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtSetOpExecuteTimeOutV2 is called.

Parameters

Parameter Input/Output Description
timeout Input Sets the timeout, in microseconds.
Setting this parameter to 0 indicates the maximum timeout.
actualTimeout Output Returns the timeout that actually takes effect, in microseconds.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtSetOpExecuteTimeOutV2.




aclrtCreateEvent

aclError aclrtCreateEvent(aclrtEvent *event, uint32_t flag)

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

Description

Creates an event with flags. Events with different flags serve different purposes. You can specify multiple flags when creating an event (by performing a bitwise OR operation) to enable the corresponding functions simultaneously. When creating an event, event resources are not limited by hardware.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtCreateEventExWithFlag is called.

Parameters

Parameter Input/Output Description
event Output Event pointer. See aclrtEvent for the type definition.
flag Input Flag of the event pointer.
flag is a bitmap. You can set flag to a single macro or perform an OR operation on multiple macros.
Currently, flag can be set to the following macros:
- ACL_EVENT_TIME_LINE: This bit indicates that the created event needs to record timestamp information. Note: Using the timestamp function affects the performance of event-related APIs.
- ACL_EVENT_SYNC: This bit indicates that the created event supports synchronization across multiple streams.
- ACL_EVENT_CAPTURE_STREAM_PROGRESS: This bit indicates that the created event is used to track the task execution progress of a stream.
- ACL_EVENT_IPC: This bit indicates that the created event is used for inter-process communication. For details, see aclrtIpcGetEventHandle. Note: On Ascend 950DT products, this flag is not supported for creating an event. This flag cannot be ORed with other flags. An event created with this flag cannot be used in the following APIs or scenarios: aclrtResetEvent, aclrtQueryEvent, aclrtQueryEventWaitStatus, aclrtEventElapsedTime, aclrtEventGetTimestamp, aclrtGetEventId, and model capture scenarios (see the description in aclmdlRICaptureBegin). Otherwise, an error is returned.


The macros are defined as follows:
#define ACL_EVENT_TIME_LINE 0x00000008U
#define ACL_EVENT_SYNC 0x00000001U
#define ACL_EVENT_CAPTURE_STREAM_PROGRESS 0x00000002U
#define ACL_EVENT_IPC 0x00000040U

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtCreateEventExWithFlag.




aclrtMemcpyBatch

template <typename T, typename U>
aclError aclrtMemcpyBatch(T **dsts, size_t *destMaxs, U **srcs, size_t *sizes, size_t numBatches, aclrtMemcpyBatchAttr attr)

template <typename T, typename U>
aclError aclrtMemcpyBatch(T **dsts, size_t *destMaxs, U **srcs, size_t *sizes, size_t numBatches, aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs)

Applicable Products

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

Description

Performs batch memory copy.

The host memory in this API supports page-locked memory (for example, memory allocated through the aclrtMallocHost API) and non-page-locked memory (memory allocated through the malloc API).

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpyBatchV2 is called.

Parameters

Parameter Input/Output Description
dsts Input Destination memory address array.
destMaxs Input Memory copy maximum length array, used to store the maximum length of each memory segment to be copied, in bytes.
srcs Input Source memory address array.
sizes Input Memory copy length array, used to store the size of each memory segment to be copied, in bytes.
numBatches Input Length of the dsts, srcs, and sizes arrays.
attr Input Memory copy attribute. The same memory copy attribute is used for all memory.
See aclrtMemcpyBatchAttr for the type definition.
attrs Input Memory copy attribute array. The corresponding memory copy attribute is used for each memory segment.
See aclrtMemcpyBatchAttr for the type definition.
attrsIndexes Input Memory copy attribute index array, used to specify the copy range to which each entry in the attrs array applies. The attribute specified in attrs[k] applies to copy operations from attrsIndexes[k] to attrsIndexes[k+1] - 1, and attrs[numAttrs-1] applies to copy operations from attrsIndexes[numAttrs-1] to numBatches - 1.
numAttrs Input Length of the attrs and attrsIndexes arrays.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemcpyBatchV2.




aclrtMemcpyBatchAsync

template <typename T, typename U>
aclError aclrtMemcpyBatchAsync(T **dsts, size_t *destMaxs, U **srcs, size_t *sizes, size_t numBatches, aclrtMemcpyBatchAttr attr, aclrtStream stream)

template <typename T, typename U>
aclError aclrtMemcpyBatchAsync(T **dsts, size_t *destMaxs, U **srcs, size_t *sizes, size_t numBatches, aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs, aclrtStream stream)

Applicable Products

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

Description

Performs batch memory copy.

The host memory in this API supports page-locked memory (for example, memory allocated through the aclrtMallocHost API) and non-page-locked memory (memory allocated through the malloc API). When the host memory is non-page-locked memory, this API returns only after the memory copy task is complete. When the host memory is page-locked memory, this API is an asynchronous API. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory copy task has been completed.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpyBatchAsyncV2 is called.

Parameters

Parameter Input/Output Description
dsts Input Destination memory address array.
destMaxs Input Memory copy maximum length array, used to store the maximum length of each memory segment to be copied, in bytes.
srcs Input Source memory address array.
sizes Input Memory copy length array, used to store the size of each memory segment to be copied, in bytes.
numBatches Input Length of the dsts, srcs, and sizes arrays.
attr Input Memory copy attribute. The same memory copy attribute is used for all memory.
See aclrtMemcpyBatchAttr for the type definition.
attrs Input Memory copy attribute array. The corresponding memory copy attribute is used for each memory segment.
See aclrtMemcpyBatchAttr for the type definition.
attrsIndexes Input Memory copy attribute index array, used to specify the copy range to which each entry in the attrs array applies. The attribute specified in attrs[k] applies to copy operations from attrsIndexes[k] to attrsIndexes[k+1] - 1, and attrs[numAttrs-1] applies to copy operations from attrsIndexes[numAttrs-1] to numBatches - 1.
numAttrs Input Length of the attrs and attrsIndexes arrays.
stream Input Stream on which the memory copy task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemcpyBatchAsyncV2.




aclrtPointerGetAttributes

template <typename T>
aclError aclrtPointerGetAttributes(const T *ptr, aclrtPtrAttributes *attributes)

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

Description

Obtains memory attribute information, including whether the memory resides on the host or device, the page table size, and other details.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtPointerGetAttributes is called.

Parameters

Parameter Input/Output Description
ptr Input Memory address.
Do not pass a device address mapped through the aclrtHostRegister API or a device address obtained through the aclrtHostGetDevicePointer API; otherwise, undefined behavior occurs.
attributes Output Memory attribute information. See aclrtPtrAttributes for the type definition.

Return Values

0: success; others: failure. For details, see aclError.




aclrtHostRegister

template <typename T>
aclError aclrtHostRegister(T *ptr, uint64_t size, aclrtHostRegisterType type, T **devPtr)

template <typename T>
aclError aclrtHostRegister(T *ptr, uint64_t size, uint32_t flag)

Applicable Products

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

Description

Registers a host memory address. To unregister, call the aclrtHostUnregister API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, it calls the C API aclrtHostRegister or aclrtHostRegisterV2.

Parameters

Parameter Input/Output Description
ptr Input Host memory address.
You can directly pass a host memory address allocated through the aclrtMallocHost or aclrtMallocHostWithCfg API, or pass the address after offsetting it. However, the offset address must meet the 4 KB page alignment requirement.
When the OS kernel version is 5.10 or earlier, using non-page-locked memory causes exceptions. Therefore, you must call the aclrtMallocHost or aclrtMallocHostWithCfg API to allocate host page-locked memory.
When the OS kernel version is later than 5.10, non-page-locked host memory is supported. Therefore, you can either call the aclrtMallocHost or aclrtMallocHostWithCfg API to allocate host page-locked memory, or use the malloc API to allocate host non-page-locked memory. Host memory allocated through the malloc API does not need to meet the 4 KB page alignment requirement.
size Input Memory size, in bytes.
type Input Memory registration type. See aclrtHostRegisterType for the type definition.
devPtr Output Device-accessible memory address to which the host memory is mapped.
This address can be accessed only on the device, for example, as a kernel function parameter for the AI Core on the device to access. For memory processing on the host side, use the original host memory address.
flag Input Memory registration type.
The value is one of the following macros. You can configure a single macro or a bitwise OR of multiple macros (for example, ACL_HOST_REG_MAPPED | ACL_HOST_REG_PINNED).

- ACL_HOST_REG_MAPPED: Registers the host memory mapping as a memory address accessible by the device, and then calls the aclrtHostGetDevicePointer API to obtain the mapped device memory address.
- ACL_HOST_REG_IOMEMORY: Registers the I/O space (registers and cache) mapping of a third-party PCIe device on the host as accessible by the device, including read and write. Page-locked memory is not supported when this macro is used, so this macro cannot be combined with ACL_HOST_REG_PINNED.
- ACL_HOST_REG_READONLY: Registers the host memory mapping as read-only for the device.
- ACL_HOST_REG_PINNED: Registers the host page-unlocked memory as page-locked memory. Host page-unlocked memory can be requested through C/C++ standard library functions (such as malloc, calloc, and new) or the default mmap system call. This macro applies only to host page-unlocked memory; if host page-locked memory is passed, the API returns an error.


The macro definitions are as follows:
#define ACL_HOST_REG_MAPPED 0x2UL
#define ACL_HOST_REG_IOMEMORY 0x4UL
#define ACL_HOST_REG_READONLY 0x8UL
#define ACL_HOST_REG_PINNED 0X10000000UL

Return Values

0: success; others: failure. For details, see aclError.




aclrtHostGetDevicePointer

template <typename T>
aclError aclrtHostGetDevicePointer(T *pHost, T **pDevice, uint32_t flag)

Applicable Products

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

Description

Obtains the device memory address mapped by the aclrtHostRegister API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtHostGetDevicePointer is called.

Parameters

Parameter Input/Output Description
pHost Input Host memory address mapped by the aclrtHostRegister API.
pDevice Output Device memory address to which the host memory is mapped.
flag Input Reserved parameter. Currently fixed to 0.

Return Values

0: success; others: failure. For details, see aclError.




aclrtHostUnregister

template <typename T>
aclError aclrtHostUnregister(T *ptr)

Applicable Products

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

Description

Unregisters the host memory.

This API is used in pairs with the aclrtHostRegister API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, it calls the C API aclrtHostUnregister.

Parameters

Parameter Input/Output Description
ptr Input Host-side memory address.

Return Values

0: success; others: failure. For details, see aclError.




aclrtMemAllocManaged

template <typename T>
aclError aclrtMemAllocManaged(T **devPtr, size_t size, uint32_t flags = ACL_RT_MEM_ATTACH_GLOBAL)

Applicable Products

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

Description

Allocates Unified Virtual Memory (UVM) and returns the pointer to the allocated memory through *ptr. The allocated memory size is aligned upward to 2 MB based on the size you specify. To release the memory allocated by this API, call aclrtFree.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemAllocManaged is called.

Parameters

Parameter Input/Output Description
devPtr Output Pointer to the pointer to the allocated memory. Because host and device virtual addresses are unified, this parameter does not distinguish the allocation location.
size Input Memory size, in bytes.
size cannot be 0. A single application process can allocate up to 3 TB of UVM virtual memory.
flag Input Memory flag.
Currently, flag supports only ACL_RT_MEM_ATTACH_GLOBAL, whose value is 1. When ACL_RT_MEM_ATTACH_GLOBAL is set, the memory allocated by this API can be accessed on both the device and host sides.
The macro is defined as follows:
#define ACL_RT_MEM_ATTACH_GLOBAL (0x01U)

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemAllocManaged.




aclrtMemManagedPrefetchAsync

template <typename T>
aclError aclrtMemManagedPrefetchAsync(const T *ptr, size_t size, aclrtMemManagedLocation location, uint32_t flags, aclrtStream stream)

Applicable Products

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

Description

Performs prefetching for UVM.

The memory operated by this API must be allocated through the aclrtMemAllocManaged API. This API is asynchronous. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory prefetch task has been executed.

This API is an encapsulated API applicable only to C++ programs. Inside the API, it calls the C API aclrtMemManagedPrefetchAsync.

Parameters

Parameter Input/Output Description
ptr Input Memory address to be prefetched. The address range must be within the UVM memory range, that is, [0x90000000000ULL, 0x90000000000ULL+3TB).
size Input Length of the memory to be prefetched, in bytes. It must be 2 MB aligned. The value range is (0, 3TB].
location Input Location information of the physical memory. The location parameter contains two members: id and type. See aclrtMemManagedLocation for the type definition.
flags Input Reserved parameter. Currently fixed to 0.
stream Input Stream on which the memory prefetch task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.




aclrtMemManagedPrefetchBatchAsync

template <typename T>
aclError aclrtMemManagedPrefetchBatchAsync(const T **ptrs, size_t *sizes, size_t count, aclrtMemManagedLocation prefetchLoc, uint64_t flags, aclrtStream stream)

template <typename T>
aclError aclrtMemManagedPrefetchBatchAsync(const T **ptrs, size_t *sizes, size_t count, aclrtMemManagedLocation *prefetchLocs, size_t *prefetchLocIdxs, size_t numPrefetchLocs, uint64_t flags, aclrtStream stream)

Applicable Products

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

Description

Performs batch prefetch of UVM.

The memory operated by this API must be allocated through the aclrtMemAllocManaged API. This API is asynchronous. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory prefetch task has been executed.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemManagedPrefetchBatchAsync is called.

Parameters

Parameter Input/Output Description
ptrs Input Memory address array to be prefetched. The range of each address must be within the UVM memory range, that is, [0x90000000000ULL, 0x90000000000ULL+3TB).
sizes Input Memory prefetch length array, used to store the length of each UVM memory segment to be prefetched, in bytes. Each segment length must be 2 MB aligned, with a value range of (0, 3TB].
count Input Length of the ptrs and sizes arrays.
prefetchLoc Input Physical memory location information. Each location contains two members: id and type. The same physical memory location information is used for all memory to be prefetched.
See aclrtMemManagedLocation for the type definition.
prefetchLocs Input Physical memory location information array. Each location contains two members: id and type. The corresponding physical memory location information is used for each memory segment to be prefetched.
See aclrtMemManagedLocation for the type definition.
prefetchLocIdxs Input Physical memory prefetch information index array, used to specify the prefetch range applicable to each physical address in the prefetchLocs array. For the physical address specified by prefetchLocs[k], the UVM memory addresses of the elements from index prefetchLocIdxs[k] to index prefetchLocIdxs[k+1] – 1 in the ptrs array are prefetched. For the physical address specified by prefetchLocs[numPrefetchLocs -1], the UVM memory addresses of the elements from index prefetchLocIdxs[numPrefetchLocs -1] to index count - 1 in the ptrs array are prefetched.
numPrefetchLocs Input Length of the prefetchLocs and prefetchLocIdxs arrays.
flags Input Reserved parameter. Currently fixed to 0.
stream Input Stream on which the memory prefetch task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemManagedPrefetchBatchAsync.




aclrtMalloc

template <typename T>
aclError aclrtMalloc(T **devPtr, size_t size, aclrtMallocConfig *cfg = nullptr)

template <typename T>
aclError aclrtMalloc(T **devPtr, size_t size, aclrtMemMallocPolicy policy, aclrtMallocConfig *cfg = nullptr)

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

Description

Allocates linear memory of the specified size on the device and returns the pointer to the allocated memory through *devPtr. The start address of the memory is 64-byte aligned.

Memory allocated by this API must be released using the aclrtFree API or the aclrtFreeWithDevSync API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMallocWithCfg is called.

Parameters

Parameter Input/Output Description
devPtr Output Pointer to the pointer to the allocated memory on the device.
size Input Size of the memory to allocate, in bytes.
size cannot be 0.
policy Input Memory allocation policy. See aclrtMemMallocPolicy for the type definition.
If the configured memory allocation policy exceeds the value range of aclrtMemMallocPolicy, memory is allocated as large pages when size ≥ 2 MB; otherwise, memory is allocated as normal pages.
cfg Input Memory configuration information. See aclrtMallocConfig for the type definition.

Return Values

0: success; others: failure. For details, see aclError.




aclrtMallocHost

template <typename T>
aclError aclrtMallocHost(T **hostPtr, size_t size, aclrtMallocConfig *cfg = nullptr)

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

Description

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

Memory allocated through this API must be released through the aclrtFreeHost API or the aclrtFreeHostWithDevSync API.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMallocHostWithCfg is called.

Parameters

Parameter Input/Output Description
hostPtr Output Pointer to the pointer of the allocated memory.
size Input Size of the memory to allocate, in bytes.
size cannot be 0.
cfg Input Memory configuration information. See aclrtMallocConfig for the type definition.

Return Values

0: success; others: failure. For details, see aclError.




aclrtMemcpy

template <typename T, typename U>
aclError aclrtMemcpy(T *dst, size_t destMax, const U *src, size_t count, aclrtMemcpyKind kind)

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

Description

Performs memory copy.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpy is called.

Parameters

Parameter Input/Output Description
dst Input Destination memory address pointer.
destMax Input Maximum memory length of the destination memory address, in bytes.
src Input Source memory address pointer.
count Input Length of the memory copy, in bytes.
kind Input Type of the memory copy. This is a reserved parameter. Configuring a value from the enumeration is invalid. The system determines, based on the source memory address pointer and the destination memory address pointer, whether data at the source address can be copied to the destination address. If not, the system returns an error.
See aclrtMemcpyKind for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemcpy.




aclrtMemcpyAsync

template <typename T, typename U>
aclError aclrtMemcpyAsync(T *dst, size_t destMax, const U *src, size_t count, aclrtMemcpyKind kind, aclrtStream stream)

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

Description

Performs memory copy.

The host memory in this API supports page-locked memory (for example, memory allocated through the aclrtMallocHost API) and non-page-locked memory (memory allocated through the malloc API). When the host memory is page-locked memory, this API is an asynchronous API. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory copy task has been executed. When the host memory is non-page-locked memory, this API returns only after the memory copy task is complete.

This API is an encapsulated API applicable only to C++ programs. Inside the API, it calls the C API aclrtMemcpyAsync.

Parameters

Parameter Input/Output Description
dst Input Destination memory address pointer.
destMax Input Maximum memory length of the destination memory address, in bytes.
src Input Source memory address pointer.
count Input Length of the memory copy, in bytes.
kind Input Type of the memory copy. See aclrtMemcpyKind for the type definition.
stream Input Stream on which the memory copy task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemcpyAsync.




aclrtMemcpy2d

template <typename T, typename U>
aclError aclrtMemcpy2d(T *dst, size_t dpitch, const U *src, size_t spitch, size_t width, size_t height, aclrtMemcpyKind kind)

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

Description

Performs synchronous memory copy, mainly for copying matrix data.

This API is an encapsulated API applicable only to C++ programs. Inside the API, it calls the C API aclrtMemcpy2d.

Parameters

Parameter Input/Output Description
dst Input Destination memory address pointer.
dpitch Input Address distance between two adjacent column vectors in the destination memory.
src Input Source memory address pointer.
spitch Input Address distance between two adjacent column vectors in the source memory.
width Input Width of the data to be copied.
height Input Height of the data to be copied.
kind Input Memory copy type. See aclrtMemcpyKind for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemcpy2d.




aclrtMemcpy2dAsync

template <typename T, typename U>
aclError aclrtMemcpy2dAsync(T *dst, size_t dpitch, const U *src, size_t spitch, size_t width, size_t height, aclrtMemcpyKind kind, aclrtStream stream)

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

Description

Performs asynchronous memory copy, mainly for copying matrix data. This is an asynchronous API.

The host memory in this API supports page-locked memory (for example, memory allocated through the aclrtMallocHost API) and non-page-locked memory (memory allocated through the malloc API). When the host memory is non-page-locked memory, this API returns only after the memory copy task is complete. When the host memory is page-locked memory, this API is an asynchronous API. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory copy task has been executed.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpy2dAsync is called.

Parameters

Parameter Input/Output Description
dst Input Destination memory address pointer.
dpitch Input Address distance between two adjacent column vectors in the destination memory.
src Input Source memory address pointer.
spitch Input Address distance between two adjacent column vectors in the source memory.
width Input Width of the data to be copied.
The maximum value of width is 5000000, and it must be less than or equal to dpitch and spitch.
height Input Height of the data to be copied.
The maximum value of height is 5 × 1024 × 1024 = 5242880. Otherwise, the API returns a failure value.
kind Input Type of memory copy. See aclrtMemcpyKind for the type definition.
stream Input Stream on which the memory copy task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the C API aclrtMemcpy2dAsync.




aclrtGetSymbolAddress

template <typename T>
aclError aclrtGetSymbolAddress(const T &symbol, void **devPtr)

Applicable Products

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

Description

Obtains the address of a device variable.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtGetSymbolAddress is called.

Parameters

Parameter Input/Output Description
symbol Input Device variable name. Pass the variable name declared with __gm__ here.
devPtr Output Memory address pointer of the device variable.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

  • This API is applicable only to C++ programs.
  • This API applies only to the scenario where you develop custom operators in Ascend C and use the Bisheng compiler for mixed host and device code compilation.
  • The device variable address is valid only on the current device. After switching the device, you must re-obtain the address.
  • Only device variables in AI Core operators are supported, with the following constraints:
    • Device variables defined in the file where the main function resides are supported (for example, __gm__ float convWeights).
    • Cross-file references to device variables through the extern keyword are supported. For example, if __gm__ float convWeights is defined in file A, it can be declared and referenced in file B through extern __gm__ float convWeights. The compilation requirement must be satisfied: use the -dc mode of the Bisheng compiler to compile multiple source files into a single operator binary file.
    • Basic data types, function pointers, structures, and arrays are supported. The class type is not supported. Note that function pointers support only functions pointing to pure Scalar, and cannot effectively distinguish between Cube and Vector function logic.



aclrtGetSymbolSize

template <typename T>
aclError aclrtGetSymbolSize(const T &symbol, size_t *size)

Applicable Products

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

Description

Obtains the memory size occupied by a device variable.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtGetSymbolSize is called.

Parameters

Parameter Input/Output Description
symbol Input Device variable name. Pass the variable name declared with __gm__.
size Output Size of the device variable, in bytes.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the aclrtGetSymbolAddress API.




aclrtMemcpyFromSymbol

template <typename T>
aclError aclrtMemcpyFromSymbol(void *dst, size_t dstMax, const T &symbol, size_t count,
                               size_t offset, aclrtMemcpyKind kind)

Applicable Products

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

Description

Performs synchronous memory copy from a device variable to the host. It is used to read the data of a device variable.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpyFromSymbol is called.

Parameters

Parameter Input/Output Description
dst Input Destination memory address pointer.
dstMax Input Maximum length of the destination memory, in bytes. The condition dstMax ≥ Count must be satisfied.
symbol Input Device variable name. Pass the variable name declared with __gm__ here.
count Input Length of the memory copy, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
offset Input Device variable address offset, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
kind Input Copy type. See aclrtMemcpyKind for the type definition. This API supports only ACL_MEMCPY_DEVICE_TO_HOST and ACL_MEMCPY_DEFAULT.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the aclrtGetSymbolAddress API.




aclrtMemcpyFromSymbolAsync

template <typename T>
aclError aclrtMemcpyFromSymbolAsync(void *dst, size_t dstMax, const T &symbol, size_t count,
                                    size_t offset, aclrtMemcpyKind kind, aclrtStream stream)

Applicable Products

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

Description

Performs asynchronous memory copy of device variable data to the host. It is used to read the data of a device variable.

The host memory in this API supports page-locked memory (for example, memory allocated through the aclrtMallocHost API) and non-page-locked memory (memory allocated through the malloc API). When the host memory is page-locked memory, this API is an asynchronous API. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory copy task has been completed. When the host memory is page-locked memory, this API is an asynchronous API. When the host memory is non-page-locked memory, this API returns only after the memory copy task is complete.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpyFromSymbolAsync is called.

Parameters

Parameter Input/Output Description
dst Input Destination memory address pointer.
dstMax Input Maximum length of the destination memory, in bytes. The condition dstMax ≥ Count must be satisfied.
symbol Input Device variable name. Pass the variable name declared with __gm__ here.
count Input Length of the memory copy, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
offset Input Address offset of the device variable, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
kind Input Copy type. See aclrtMemcpyKind for the type definition. This API supports only ACL_MEMCPY_DEVICE_TO_HOST and ACL_MEMCPY_DEFAULT.
stream Input Stream on which the memory copy task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

  • Same as the constraints of the aclrtGetSymbolAddress API.
  • This API is asynchronous. After calling it, you need to synchronously wait for the copy to complete.



aclrtMemcpyToSymbol

template <typename T>
aclError aclrtMemcpyToSymbol(const T &symbol, const void *src, size_t count, size_t offset, aclrtMemcpyKind kind)

Applicable Products

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

Description

Performs synchronous memory copy from host data to a device variable. It is used to write data to a device variable.

This API is an encapsulated API applicable only to C++ programs. Inside the API, the C API aclrtMemcpyToSymbol is called.

Parameters

Parameter Input/Output Description
symbol Input Device variable name. Pass the variable name declared with __gm__ here.
src Input Source memory address pointer.
count Input Length of the memory copy, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
offset Input Device variable address offset, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
kind Input Copy type. See aclrtMemcpyKind for the type definition. This API supports only ACL_MEMCPY_HOST_TO_DEVICE and ACL_MEMCPY_DEFAULT.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

Same as the constraints of the aclrtGetSymbolAddress API.




aclrtMemcpyToSymbolAsync

template <typename T>
aclError aclrtMemcpyToSymbolAsync(const T &symbol, const void *src, size_t count, size_t offset,
                                  aclrtMemcpyKind kind, aclrtStream stream)

Applicable Products

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

Description

Performs asynchronous memory copy from host data to a device variable. It is used to write data to a device variable.

The host memory in this API supports page-locked memory (for example, memory allocated through the aclrtMallocHost API) and non-page-locked memory (memory allocated through the malloc API). When the host memory is page-locked memory, this API is an asynchronous API. A successful call only indicates that the task has been delivered, not that the task has been executed. After calling this API, call a synchronous wait API (for example, aclrtSynchronizeStream) to ensure that the memory copy task has been executed. When the host memory is page-locked memory, this API is an asynchronous API. When the host memory is non-page-locked memory, this API returns only after the memory copy task is complete.

This API is an encapsulated API applicable only to C++ programs. Inside the API, it calls the C API aclrtMemcpyToSymbolAsync.

Parameters

Parameter Input/Output Description
symbol Input Device variable name. Pass the variable name declared with __gm__ here.
src Input Source memory address pointer.
count Input Length of the memory copy, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
offset Input Device variable address offset, in bytes. The condition Offset + Count ≤ Device variable size must be satisfied. The device variable size can be obtained via the aclrtGetSymbolSize API.
kind Input Copy type. See aclrtMemcpyKind for the type definition. This API supports only ACL_MEMCPY_HOST_TO_DEVICE and ACL_MEMCPY_DEFAULT.
stream Input Stream on which the memory copy task is executed. See aclrtStream for the type definition.

Return Values

0: success; others: failure. For details, see aclError.

Constraints

  • Same as the constraints of the aclrtGetSymbolAddress API.
  • This API is asynchronous. After calling it, you must synchronously wait for the copy to complete.