Explicit Synchronization

For asynchronous task APIs, the API call by the host thread only indicates that tasks are delivered, but does not indicate that the tasks are complete. You need to explicitly call device synchronization, stream synchronization, and other explicit synchronization APIs to wait until the tasks are complete. After such explicit synchronization APIs are called, the host thread is blocked until the related tasks are complete.

Device Synchronization: aclrtSynchronizeDevice

Block the current host thread until all explicitly or implicitly created streams in the current context of the current device complete all delivered tasks.

The following is a code sample for device synchronization, 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
// Specify a device.
aclrtSetDevice(0);

// Create a stream.
aclrtStream stream;
aclrtCreateStream(&stream);

// Deliver tasks to the stream.
......

// Block application execution until the compute device has completed all preceding requested tasks.
aclrtSynchronizeDevice();

// Destroy resources.
aclrtDestroyStream(stream);
aclrtResetDevice(0);

Stream Synchronization: aclrtSynchronizeStream

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

The following is a code sample for stream synchronization, 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
// Create a stream.
aclrtStream stream;
aclrtCreateStream(&stream);

// Deliver tasks to the stream.
......

// Call aclrtSynchronizeStream to block the application until all tasks in the specified stream are complete.
aclrtSynchronizeStream(stream);

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

In addition, you can use aclrtStreamQuery to check whether all tasks on the stream are complete.