Synchronous and Asynchronous APIs

CANN supports several explicit synchronization types. After these types of APIs are called, the host thread blocks until the related tasks are completed.

  • Device synchronization: for example, aclrtSynchronizeDevice

    The host thread blocks until all explicitly or implicitly created streams on the device complete all previously delivered tasks. Minimize the use of this function to avoid delaying host-side execution.

  • Stream synchronization: for example, aclrtSynchronizeStream

    The host thread blocks until the specified stream completes all delivered tasks.

  • Event synchronization: for example, aclrtSynchronizeEvent

    The host thread blocks until the specified event is completed. This is finer-grained synchronization.

For asynchronous APIs, calling an API only means that the task has been delivered. It does not indicate successful execution. Before the task completes, the asynchronous API already returns success to the host thread. You need to call the preceding synchronous APIs explicitly to block the host thread and wait until the tasks are completed. Otherwise, service exceptions (such as training or inference exceptions) or unknown situations (such as device link or card disconnections) may occur.