cuda内核调用是同步的还是异步的 [英] Are cuda kernel calls synchronous or asynchronous

查看:45
本文介绍了cuda内核调用是同步的还是异步的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到可以使用内核启动来同步不同的块,即,如果我希望所有块在进行操作 2 之前完成操作 1,我应该将操作 1 放在一个内核中,将操作 2 放在另一个内核中.这样,我可以实现块之间的全局同步.但是,cuda c 编程指南提到内核调用是异步的,即.CPU 不会等待第一个内核调用完成,因此 CPU 也可以在第一个内核完成之前调用第二个内核.但是,如果这是真的,那么我们就不能使用内核启动来同步块.请让我知道我哪里出错了

I read that one can use kernel launches to synchronize different blocks i.e., If i want all blocks to complete operation 1 before they go on to operation 2, I should place operation 1 in one kernel and operation 2 in another kernel. This way, I can achieve global synchronization between blocks. However, the cuda c programming guide mentions that kernel calls are asynchronous ie. the CPU does not wait for the first kernel call to finish and thus, the CPU can also call the second kernel before the 1st has finished. However, if this is true, then we cannot use kernel launches to synchronize blocks. Please let me know where i am going wrong

推荐答案

从 CPU 的角度来看,内核调用是异步的,因此如果您连续调用 2 个内核,则将调用第二个内核,而无需等待第一个内核调用结束.这只是意味着控制权立即返回给CPU.

Kernel calls are asynchronous from the point of view of the CPU so if you call 2 kernels in succession the second one will be called without waiting for the first one to finish. It only means that the control returns to the CPU immediately.

在 GPU 方面,如果您没有指定不同的流来执行内核,它们将按照它们被调用的顺序执行(如果您没有指定流,它们都会转到默认流并串行执行).只有在第一个内核完成后,第二个内核才会执行.

On the GPU side, if you haven't specified different streams to execute the kernel they will be executed by the order they were called (if you don't specify a stream they both go to the default stream and are executed serially). Only after the first kernel is finished the second one will execute.

此行为对于具有支持并发内核执行的计算能力 2.x 的设备有效.在其他设备上,即使内核调用仍然是异步的,内核执行始终是顺序的.

This behavior is valid for devices with compute capability 2.x which support concurrent kernel execution. On the other devices even though kernel calls are still asynchronous the kernel execution is always sequential.

查看第 3.2.5 节中的 CUDA C 编程指南,每个 CUDA 程序员都应该阅读.

Check the CUDA C programming guide on section 3.2.5 which every CUDA programmer should read.

这篇关于cuda内核调用是同步的还是异步的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆