Function: ipc_mem_get_export_key

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

Atlas A2 training product/Atlas A2 inference product

Atlas training product

Atlas inference product

Atlas 200I/500 A2 inference product

x

Description

Sets the specified device memory to IPC shared memory in the current process and returns the shared memory key, so that the memory can be shared with other processes. This API must be used together with the following key APIs to implement memory sharing. The following uses processes A and B as an example to describe the process of calling the memory sharing API between two processes.

  1. In process A:

    a. If two processes on different devices share memory, call the acl.rt.device_enable_peer_access API to enable P2P.

    b. Call the acl.rt.malloc API to apply for the P2P memory (configured by the policy parameter of this API).

    c. Call acl.rt.ipc_mem_get_export_key to export the shared memory key.

    d. Obtain the ID of process B and call the acl.rt.ipc_mem_set_import_pid API to add the ID of process B to the whitelist.

    e. Call acl.rt.ipc_mem_close to disable IPC-based memory sharing. Only after process B calls acl.rt.ipc_mem_close to disable IPC-based memory sharing can process A do so. Otherwise, exceptions may occur.

    f. Call acl.rt.free to free the memory.

  2. In process B:

    a. If two processes on different devices share memory, call the acl.rt.device_enable_peer_access API to enable P2P.

    b. Call acl.rt.device_get_bare_tgid to obtain the ID of process B.

    This API adapts to both physical machines and VMs when obtaining the process ID. You only need to call this API to obtain the process ID and use the API with other APIs to share memory. If you do not call this API to obtain the process ID, an exception may occur when you use the process ID.

    c. Call aclrtIpcMemImportByKey to obtain the key information and return the device memory address pointer that can be used by the current process.

    Before calling acl.rt.ipc_mem_import_by_key, ensure that the memory to be shared exists and is not freed.

    d. Call acl.rt.ipc_mem_close to disable IPC-based memory sharing.

Prototype

  • C Prototype
    aclError aclrtIpcMemGetExportKey(void *devPtr, size_t size, char *key, size_t len, uint64_t flag)
  • Python Function
    1
    key, ret = acl.rt.ipc_mem_get_export_key(dev_ptr, size, len, flags)
    

Parameters

Parameter

Description

dev_ptr

Int, device memory address.

size

Int, memory size, in bytes.

len

Int, length of the key. The value is fixed at 65.

flags

Int, whether to enable process trustlist verification.

The options can be either of the following macros:

  • ACL_RT_IPC_MEM_EXPORT_FLAG_DEFAULT: Default value. It means enabling process trustlist verification.

    If you set the parameter to this value, call the acl.rt.ipc_mem_set_import_pid API separately to add the ID of the process that uses the shared memory key to the trustlist.

  • ACL_RT_IPC_MEM_EXPORT_FLAG_DISABLE_PID_VALIDATION: Disables process trustlist verification.

    If this macro is used, you do not need to call the acl.rt.ipc_mem_set_import_pid API.

The macros are defined as follows:

#define ACL_RT_IPC_MEM_EXPORT_FLAG_DEFAULT                0x0UL
#define ACL_RT_IPC_MEM_EXPORT_FLAG_DISABLE_PID_VALIDATION 0x1UL

Return Value

Return Value

Description

key

Str, shared memory key.

ret

Int, 0 on success; else failure.

Restrictions

When two processes on different devices share memory through IPC, as shown in the following figure, process A on device 0 shares the memory with process B on device 1 through IPC. When the shared memory address is used in process B:

  • This API must be used together with the acl.rt.device_enable_peer_access API to enable communication between two devices.
  • On the Atlas inference product, when the aclrtMalloc API is called to allocate device memory, policy must be set to a P2P type, for example, ACL_MEM_MALLOC_HUGE_FIRST_P2P.
  • During memory copy, the copy direction cannot be automatically determined based on the source and destination memory address pointers. Memory copy in the host -> device or device -> host direction is not supported, either synchronously or asynchronously. On the same device, asynchronous memory copy is supported, but synchronous memory copy is not.
  • Cross-chip access of Cube and Vector units is supported.

The preceding restrictions do not apply when two processes on the same device share memory through IPC.