Search code examples
cudathrust

collect position of specific value in an array using CUDA or thrust


I have an array like this:

array: 0 1 0 0 1 0 1

index: 0 1 2 3 4 5 6

and I want to collect all the position of "1" into result array like this:

1 4 6

thrust has a "counting" function but it can only count the number of "1"

so what's the fastest way of doing this? Does thrust have some function to handle this? If not, how to implement it by CUDA in an efficient way?

My own idea: first use prefix-sum to get an offset array, then add another kernel in which every thread write its id to resultArray[offset[id]] if its data array value is "1". I think my idea is ugly. Any better way?


Solution

  • Thrust has an example doing exactly that:

    https://github.com/thrust/thrust/blob/master/examples/counting_iterator.cu