找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 256|回复: 3

DAY10:阅读CUDA异步并发执行中的Streams

[复制链接]
发表于 2018-5-13 12:01:11 | 显示全部楼层 |阅读模式
本文共计263个字,阅读时间10分钟

3.2.5.5.6. Callbacks
The runtime provides a way to insert【插入】 a callback at any point into a stream via cudaStreamAddCallback(). A callback is a function that is executed on the host once all commands issued to the stream before the callback have completed. Callbacks in stream 0 are executed once all preceding tasks and commands issued in all streams before the callback have completed.
The following code sample adds the callback function MyCallback to each of two streams after issuing a host-to-device memory copy, a kernel launch and a device-to-host memory copy into each stream. The callback will begin execution on the host after each of the device-to-host memory copies completes.
QQ图片20180511200536.png
The commands that are issued in a stream (or all commands issued to any stream if the callback is issued to stream 0) after a callback do not start executing before the callback has completed. The last parameter of cudaStreamAddCallback() is reserved for future use.
A callback must not make CUDA API calls (directly or indirectly), as it might end up waiting on itself if it makes such a call leading to a deadlock.
3.2.5.5.7. Stream Priorities
The relative priorities of streams can be specified at creation using cudaStreamCreateWithPriority(). The range of allowable priorities, ordered as [ highest priority【优先级】, lowest priority ] can be obtained【获得】 using the cudaDeviceGetStreamPriorityRange() function. At runtime, as blocks in low-priority schemes finish, waiting blocks in higher-priority streams are scheduled in their place.
The following code sample obtains the allowable range of priorities for the current device, and creates streams with the highest and lowest available priorities
微信图片_20180513130008.png

回复

使用道具 举报

 楼主| 发表于 2018-5-13 17:54:02 | 显示全部楼层
本文备注/经验分享:
A callback must not make CUDA API calls (directly or indirectly), as it might end up waiting on itself if it makes such a call leading to a deadlock.
回调函数不能调用任何CUDA API函数,无论是直接的,还是间接的调用。因为如果在回调函数中这样做了,调用CUDA函数的回调函数将自己等待自己,造成死锁。其实这很显然的,流中的下一个任务将需要等待流中的之前任务完成才能继续,因为CUDA Stream是顺序执行的, 而如果你一个流中的某回调函数,继续给某流发布了一个任务,很有可能该回调函数永远也等待不完下一个任务完成,因为等待下一个任务完成首先需要这回调函数先结束,而回调函数却在等待下一个任务完成....于是就死锁了。
回复 支持 反对

使用道具 举报

发表于 2018-5-14 10:13:38 | 显示全部楼层
回调函数是啥
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-14 12:15:06 | 显示全部楼层

回调函数顾名思义,是由被调用的CUDA Runtime,反过来调用的,用户的函数。用户将自己的函数提供给Runtime,然后Runtime则在恰当的实际,调用用户的函数。
因为这个行为和常规的用户---> Runtime的行为相反,是Runtime ---> 用户的,
所以叫回调(call back)



回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

快速回复 返回顶部 返回列表