2_multi_stream
Description
This sample demonstrates task synchronization methods between multiple Streams, including Stream synchronization, Event synchronization, and Notify synchronization.
Product Support
This sample supports the following products:
| Product | Supported |
|---|---|
| Ascend 950PR/Ascend 950DT | Yes |
| Atlas A3 training series products/Atlas A3 inference series products | Yes |
| Atlas A2 training series products/Atlas A2 inference series products | Yes |
Compile and Run
- Download the sample code to the environment where CANN software is installed. Switch to the sample directory.
cd ${git_clone_path}/example/1_basic_features/stream/2_multi_stream
- Set environment variables.
# Replace ${install_root} with the CANN installation root directory. The default installation is in the `/usr/local/Ascend` directory.
source ${install_root}/cann/set_env.sh
export ASCEND_INSTALL_PATH=${install_root}/cann
# Set SOC_VERSION and ASCENDC_CMAKE_DIR
# -SOC_VERSION: Ascend AI processor model, such as Ascend910_9362, Ascend910B2, and so on
# -ASCENDC_CMAKE_DIR: The sample involves calling AscendC operators. Configure the AscendC compiler ascendc.cmake path, such as /usr/local/Ascend/cann/x86_64-linux/tikcpp/ascendc_kernel_cmake
source ${git_clone_path}/example/set_sample_env.sh
- Run the following command to execute the sample.
bash run.sh
CANN RUNTIME API
The key functionality points and their key interfaces involved in this sample are as follows:
- Initialization
- Call
aclInitinterface for initialization configuration. - Call
aclFinalizeinterface for deinitialization.
- Call
- Device Management
- Call
aclrtSetDeviceinterface to specify the Device for computation. - Call
aclrtResetDeviceForceinterface to forcefully reset the current computation Device and reclaim Device resources.
- Call
- Context Management
- Call
aclrtCreateContextinterface to create Context. - Call
aclrtDestroyContextinterface to destroy Context.
- Call
- Stream Management
- Call
aclrtCreateStreaminterface to create Stream. - Call
aclrtSynchronizeStreaminterface to block and wait for Stream tasks to complete. - Call
aclrtSetStreamFailureModeinterface to set the operation when Stream execution encounters errors. Default is continue on error, can be set to stop on error. - Call
aclrtDestroyStreamForceinterface to forcefully destroy Stream and discard all tasks. - Call
aclrtSynchronizeStreamWithTimeoutinterface for timeout waiting. If exceeding specified time, tasks will be discarded.
- Call
- Event Management
- Call
aclrtCreateEventinterface to create Event. - Call
aclrtRecordEventinterface to record Event. - Call
aclrtStreamWaitEventinterface to block specified Stream waiting for Event to complete. - Call
aclrtResetEventinterface to reset Event. - Call
aclrtSynchronizeEventinterface to block and wait for Event to complete. - Call
aclrtSynchronizeEventWithTimeoutinterface for timeout waiting. If exceeding specified time, tasks will be discarded.
- Call
- Notify Management
- Call
aclrtCreateNotifyinterface to create Notify. - Call
aclrtRecordNotifyinterface to record Notify. - Call
aclrtWaitAndResetNotifyinterface to block and wait for Notify and reset. - Call
aclrtDestroyNotifyinterface to destroy Notify.
- Call
- Memory Management
- Call
aclrtMallocinterface to allocate memory on Device. - Call
aclrtFreeinterface to release memory on Device.
- Call
- Data Transfer
- Call
aclrtMemcpyinterface to implement data transfer through memory copy.
- Call
Sample Output
[INFO] Use stream synchronize.
[INFO] Applied resource successfully, beging assigning task.
[INFO] Begin a long task, num += 1.
[INFO] Stream synchronize.
[INFO] Begin a short task, num *= 2.
[INFO] The answer is 2.
[INFO] Simulate timeout, wait 1000ms, the task will fail.
[ERROR] Operation failed: aclrtSynchronizeStreamWithTimeout(stream1, kWaitTimeMs) returned error code 507046
[INFO] Resource cleanup completed.
[INFO] Use event synchronize.
...
[INFO] Use notify synchronize.
[INFO] The answer is 2.
[INFO] Resource cleanup completed.