Possible Duplicate:
Counting occurences of numbers in cuda array
is there a way to use thrust or cuda to count occurrence for the duplicates in an array?
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?
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)
My kernel is this
__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];
}
You could use a combination of
thrust::unique
andthrust::binary_search
to find duplicates. You won't be able to do it in place using this approach, but it can be done just using thrust code.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: