关于Stream间任务的同步等待(通过Notify实现)
多Stream之间任务的同步等待可以利用Notify实现,例如,若stream2的任务依赖stream1的任务,想保证stream1中的任务先完成,这时可创建一个Notify,调用acl.rt.record_notify接口将Notify插入到stream1中(通常称为Record Notify任务),调用acl.rt.wait_and_reset_notify接口在stream2中插入一个等待Notify完成的任务(通常称为Wait Notify任务)。
图1 同步等待流程_多Stream场景

上图中的模型加载与执行的流程请参见模型管理,算子加载与执行的流程请参见单算子调用。
调用接口后,需增加异常处理的分支,并记录报错日志、提示日志,此处不一一列举。以下是关键步骤的代码示例,不可以直接拷贝编译运行,仅供参考。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import acl # ...... # 创建一个notify notify, ret = acl.rt.create_notify(0) # 创建stream_1、stream_2 stream_1, ret = acl.rt.create_stream() stream_2, ret = acl.rt.create_stream() # 在stream_1末尾添加了一个notify ret = acl.rt.record_notify(notify, stream_1) # 阻塞stream_2运行,直到指定notify发生,也就是stream_1执行完成 # stream_1完成后,唤醒stream_2,继续执行stream_2的任务 ret = acl.rt.wait_and_reset_notify(notify, stream_2, 0) ret = acl.rt.synchronize_stream(stream_1) ret = acl.rt.synchronize_stream(stream_2) # 显式销毁资源 ret = acl.rt.destroy_stream(stream_2) ret = acl.rt.destroy_stream(stream_1) ret = acl.rt.destroy_notify(notify) # ...... |
父主题: 同步等待