Synchronous and Asynchronous APIs

CANN supports the explicit synchronization types below. After calling these types of APIs, the host thread is blocked until related tasks are completed.

  • Device synchronization: for example, aclrtSynchronizeDevice

    Blocks the current host thread until all explicitly or implicitly created streams on the device complete all previously delivered tasks. Avoid using this function as much as possible, because it delays host running.

  • Stream synchronization: for example, aclrtSynchronizeStream

    Blocks the current host thread until the specified stream completes all delivered tasks.

  • Event synchronization: for example, aclrtSynchronizeEvent

    Blocks the current host thread until the specified event is completed. It is a finer-grained synchronization.

For asynchronous APIs, the API call only indicates that tasks are delivered, not that the tasks are executed successfully. The asynchronous APIs return a success response to the host thread before the tasks are completed. 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.