多线程写入cuda内核中的顺序数组 [英] Multiple threads writing to sequential array in cuda kernel

查看:210
本文介绍了多线程写入cuda内核中的顺序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个cuda内核,它要求我在设备上分配一个对齐的数组 struct
我从我的计算得到正确的结果,我需要写入值到这个数组从索引 0

I am writing a cuda kernel which requires me to allocate an array of aligned struct on the device. I am getting the correct results from my computations and I need to write the values to this array starting from index 0.

当我尝试写入此数组并将结果显示回主机端时,一些答案显示为零。

When I try to write to this array and display the results back to host side, some of the answers are displayed as zero.

显然,我不是根据我的要求增加索引。我尝试使用计数器,我增加使用 atomicAdd(),但是我仍然得到一些值为零。

Clearly, I am not increasing the index as per my requirement. I tried using counter which I increase using atomicAdd(), however I still get some values as zero.

确切地说,我可以在内核中使用 1000 线程进行计算,但是我的输出分配数组可以有一个大小小于 100 或大于 10000

To be precise, I may use 1000 threads in my kernel for computations but my output allocated array can have a size less than 100 or more than 10000.

我的问题是,我如何使所有这些线程写入值到数组的正好一个位置(因为它们是计算),增加数组索引/计数器 1 它。

My question is, how do I make all these threads write the value to exactly one location of array ( as they are calculated ) and increment the array index/counter by 1 without overwriting it.

任何帮助将被赞赏。提前。

Any help will be appreciated.Thanks in advance.

推荐答案

您可以使用 atomicAdd()。它返回旧值,因此您使用该值作为索引:

You can use atomicAdd(). It returns the old value, so you use that value as the index:

old_i = atomicAdd(&i, 1);
out_array[old_i] = val

但是,线程写出结果,因为atomicAdd()将(间接)串行化所有写入。在这种情况下,应该让每个线程将其结果(如果有的话)写入为该线程留出的槽中,然后使用压缩算法(参见 thrust :: copy_if ) ,以收集结果。

However, you will get poor performance if many of your threads write out results, as the atomicAdd() will (indirectly) serialize all the writes. In that case, you should let each thread write its result,if any, to a slot set aside for that thread and then use a compaction algorithm (see thrust::copy_if), to gather up the results.

这篇关于多线程写入cuda内核中的顺序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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