CUDA:将不同线程中的向量堆栈到一维向量
我在 CUDA 中的每个线程都有一个推力向量,我想按顺序堆叠向量(线程 0 中的向量,线程 1 中的向量,......以及线程 n 中的向量)以创建 1d 向量并发回 CPU 。有没有好的方法可以做到这一点?任何帮助表示赞赏。谢谢。
I have a thrust vector for each thread in CUDA, and I want to stack vectors by orders (vector in thread 0, vector in thread 1,.... and vector in thread n) to create a 1d vector and send back to CPU. Is there a good way to do this? Any help is appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将多个线程中的项目存储到单个向量中的最高效方法是线程交错。假设 4 个线程 (t0-t3) 中的每一个都有 4 个要存储的元素 (e0-e3)。最有效的最终存储模式将是:
执行此操作的代码如下所示:
在您的问题中,您似乎想要这个顺序:
该存储模式通常效率较低,但您可以这样实现:
这两种情况下的存储效率差异是未合并和合并行为之间的差异,
cuda
SO 标签上的许多问题都涵盖了这一点,例如 这个。The most performant way to store items from several threads into a single vector will be thread-interleaved. Suppose each of 4 threads (t0-t3) has 4 elements to store (e0-e3). The final storage pattern which will be most efficient will be:
The code to do that would look like this:
In your question it seems that you want this order:
That storage pattern will generally be less efficient, but you could achieve it like this:
The storage efficiency difference in these two cases is the difference between uncoalesced and coalesced behavior, which is covered in numerous questions here on the
cuda
SO tag, such as this one.