Synchronization

This section describes the usage examples and key APIs of devices, streams, and events in asynchronous scenarios.

Synchronization Mechanism

AscendCL provides the following synchronization methods:

Sample Code for Event Synchronization

After APIs are called, you need to add exception handling branches and record error logs and info logs. 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);
// ......

Sample Code for Task Synchronization Within a Stream

After APIs are called, you need to add exception handling branches and record error logs and info logs. 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 wait for the stream tasks to complete.
aclrtSynchronizeStream(stream);

//Explicitly destroy the stream after using the stream.
aclrtDestroyStream(stream);
// ......

API Call Sequence and Sample Code for Task Synchronization Between Streams

Synchronization between streams can be implemented by using an event. Call aclrtStreamWaitEvent to block the execution of a specified stream until the specified event is complete. Call aclrtRecordEvent before the aclrtStreamWaitEvent call.

For details about the model loading and execution workflow, see Model Inference.

For details about the operator loading and execution workflow, see Single-Operator Calling.

Figure 1 Synchronization between streams

After APIs are called, you need to add exception handling branches and record error logs and info logs. 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
#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, that is, the completion of s1.
//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);
// ......

Sample Code for Device Synchronization

After APIs are called, you need to add exception handling branches and record error logs and info logs. 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);