Event Waiting

Synchronous wait of tasks between streams can be implemented using events. For example, if the tasks in stream 2 depend on the tasks in stream 1 and you want to ensure that the tasks in stream 1 are complete first, you can create an event, call aclrtRecordEvent to insert the event into stream 1 (usually called an Event Record task), and call aclrtStreamWaitEvent to insert a task that waits for the event to complete into stream 2 (usually called an Event Wait task).

The following is the sample code for calling aclrtStreamWaitEvent, which is for reference only and cannot be directly copied for compilation and running:

 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
31
32
33
34
35
36
// Create an event.
aclrtEvent event;
aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC);

// Create stream 1.
aclrtStream stream1;
aclrtCreateStream(&stream1);

// Create stream 2.
aclrtStream stream2;
aclrtCreateStream(&stream2);

// Deliver tasks to stream 1.
......

// Append an event to stream 1.
aclrtRecordEvent(event, stream1);

// Deliver tasks that do not depend on the completion of stream 1 to stream 2.
......

// Block stream 2 until the specified event is complete, which means that stream 1 has been executed.
aclrtStreamWaitEvent(stream2, event);

// Deliver tasks that depend on the completion of stream 1 to stream 2.
......

// Block the application running until all tasks in stream 1 and stream 2 are complete.
aclrtSynchronizeStream(stream1);
aclrtSynchronizeStream(stream2);

// Explicitly destroy resources.
aclrtDestroyStream(stream1);
aclrtDestroyStream(stream2);
aclrtDestroyEvent(event);
......