Synchronous Wait
This section describes the usage examples and key APIs of devices, streams, events, and Notify in asynchronous scenarios.
Synchronization Mechanisms
|
Call aclrtSynchronizeEvent to block the app and wait until the event is complete. |
|
|
Call aclrtSynchronizeStream to block the app until all tasks in the specified stream are complete. |
|
|
The following methods are supported:
The difference between Notify and events is that after Notify Wait is complete, the Notify status is automatically reset. Therefore, a Notify Record task can notify only one Notify Wait task. However, Event Wait does not automatically reset the event status. Therefore, an Event Record task can notify one or more Event Wait tasks. |
|
|
Call aclrtSynchronizeDevice to block the app until the device has completed all requested tasks. In multi-device scenarios, this API is called to wait for the device of the current context. |
Synchronous Wait of an Event
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include "acl/acl.h" // ...... //Create an event. aclrtEvent event; aclrtCreateEventExWithFlag(&event, ACL_EVENT_CAPTURE_STREAM_PROGRESS); //Explicitly create a stream. aclrtStream stream; aclrtCreateStream(&stream); //Append an event to a stream. aclrtRecordEvent(event, stream); //Block the app execution and wait until the event is complete, that is, the completion of the stream. //The stream completion event wakes up the control flow, starting the app execution. aclrtSynchronizeEvent(event); //Explicitly destroy allocations. aclrtDestroyStream(stream); aclrtDestroyEvent(event); // ...... |
Synchronous Wait of Tasks in a Stream
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include "acl/acl.h" // ...... //Explicitly create a stream. aclrtStream stream; aclrtCreateStream(&stream); //Pass the stream argument to the task triggering call. aclrtMemcpyAsync(dstPtr, dstSize, srcPtr, srcSize, ACL_MEMCPY_HOST_TO_DEVICE, stream); //Call aclrtSynchronizeStream to block the app until all tasks in the specified stream are complete. aclrtSynchronizeStream(stream); //Explicitly destroy the stream after using the stream. aclrtDestroyStream(stream); // ...... |
Synchronous Wait of Tasks Between Streams (Implemented Using Events)
Synchronous wait of tasks between streams can be implemented using events. For example, if the tasks in stream2 depend on the tasks in stream1 and you want to ensure that the tasks in stream1 are complete first, you can create an event, call aclrtRecordEvent to insert the event into stream1 (usually called an Event Record task), and call aclrtStreamWaitEvent to insert a task that waits for the event completion into stream2 (usually called an Event Wait task).
For details about the loading and execution process of models and operators, see Model Management and Single-Operator Calling, respectively.
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include "acl/acl.h" // ...... //Create an event. aclrtEvent event; aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC); //Create stream1. aclrtStream s1; aclrtCreateStream(&s1); //Create stream2. aclrtStream s2; aclrtCreateStream(&s2); //Append an event to s1. aclrtRecordEvent(event, s1); //Block s2 until the event is complete, which means that s1 has been executed. //Wake up s2 for execution after s1 is complete. aclrtStreamWaitEvent(s2, event); aclrtSynchronizeStream(s1); aclrtSynchronizeStream(s2); //Explicitly destroy allocations. aclrtDestroyStream(s2); aclrtDestroyStream(s1); aclrtDestroyEvent(event); // ...... |
Synchronous Wait of Tasks Between Streams (Implemented Using Notify)
Synchronous wait of tasks between streams can be implemented using Notify. For example, if the tasks in stream2 depend on the tasks in stream1 and you want to ensure that the tasks in stream1 are complete first, you can create a Notify object, call aclrtRecordNotify to insert the Notify object into stream1 (usually called a Notify Record task), and call aclrtWaitAndResetNotify to insert a task that waits for the completion of Notify into stream2 (usually called a Notify Wait task).
For details about the loading and execution process of models and operators, see Model Management and Single-Operator Calling, respectively.
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include "acl/acl.h" // ...... //Create a Notify object. aclrtNotify notify; aclrtCreateNotify(¬ify, ACL_NOTIFY_FLAG_DEFAULT); //Create stream1. aclrtStream s1; aclrtCreateStream(&s1); //Create stream2. aclrtStream s2; aclrtCreateStream(&s2); //Append the Notify object to s1. aclrtRecordNotify(notify, s1); //Block s2 until the completion of Notify, which means that s1 is complete. //Wake up s2 for execution after s1 is complete. aclrtWaitAndResetNotify(notify, s2, 0); aclrtSynchronizeStream(s1); aclrtSynchronizeStream(s2); //Explicitly destroy allocations. aclrtDestroyStream(s2); aclrtDestroyStream(s1); aclrtDestroyNotify(notify); // ...... |
Synchronous Wait of a Device
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include "acl/acl.h" // ...... //Specify a device. aclrtSetDevice(0); //Create a stream. aclrtStream stream; aclrtCreateStream(&stream); //Block app execution until the compute device has completed all preceding requested tasks. aclrtSynchronizeDevice(); //Destroy allocations. aclrtDestroyStream(stream); aclrtResetDevice(0); |