推力计数发生 [英] thrust count occurence
问题描述
可能重复:
计算cuda数组中出现的数字
有没有办法使用thrust或cuda来计算数组中重复项的出现次数?
is there a way to use thrust or cuda to count occurrence for the duplicates in an array?
例如
如果我有一个设备向量{ 11,9,1,3,11,1,2,9,1,11}
我应该得到1:3 2:1 3:1 9:2,11:4
for example if I have a device vector { 11, 11, 9, 1, 3, 11, 1, 2, 9, 1, 11} I should get 1 :3 2:1 3:1 9:2, 11:4
如果推力不能做,那我该如何使用内核呢?
if thrust cannot do that, How can I use a kernel to do that?
谢谢!我在做浓度计算。这就是为什么我问这个问题。
假设在域中存在100000个具有nx X ny X nz个单元格的粒子,我需要计算每个单元格的浓度(每个单元格中有多少粒子)
Thanks! I am doing concentration calculation. that's why I am asking this question. assume there are 100000 particles in the domain which has nx X ny X nz cells, i need to calculate the concentration of each cell(how many particles in each cell)
我的内核是这个
__global__ void concentration_kernel(float3* posPtr, uint* device_cons)
{
__shared__ uint cache[256];
uint x = threadIdx.x + blockIdx.x * blockDim.x;
uint y = threadIdx.y + blockIdx.y * blockDim.y;
uint offset = x + y * blockDim.x * gridDim.x;
float3 posf3 = posPtr[offset];//make_float3(43.5,55,0.66);//
uint cellIndex = (uint)(posf3.z+1)*153*110 + (uint)(posf3.y)*153 + (uint)posf3.x;
cache[threadIdx.x] = device_cons[cellIndex];
__syncthreads();
uint a = cache[threadIdx.x];
a++;
cache[threadIdx.x] = a;
__syncthreads();
device_cons[cellIndex] = cache[threadIdx.x];
}
推荐答案
使用推荐::排序,然后使用 thrust :: reduce_by_key 。但是,您还需要在排序后创建1的新向量(称为值)(并且与排序后的向量长度相同)。这些值将被相加以获得计数:
You can first sort the vector using thrust::sort and then use thrust::reduce_by_key. However, you also need to create a new vector (called values) of 1's (and of the same length as your sorted vector) after sort. These values will be added up to get the counts:
reduce_by_key is a generalization of reduce to key-value pairs.
For each group of consecutive keys in the range [keys_first, keys_last)
that are equal, reduce_by_key copies the first element of the group to
the keys_output. The corresponding values in the range are reduced using
the plus and the result copied to values_output.
这篇关于推力计数发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!