How to remove zero values from an array in parallel
To eliminate some elements from an array you may use Thrust Library's reordering operations. Given a predicate is_not_zero
, which returns false
for zero values, and true
for others, you may write the operation like this
thrust::copy_if(in_array, in_array + size, out_array, is_not_zero);
the output array will include only the values which are non-zero, because the predicate indicates so.
You may also use "remove_if" function with a reverse predicate which return true
for zeros, and false
for others..
thrust::remove_if(in_array, in_array + size, is_zero);
I suggest you taking a look at compaction examples of Thrust library, or general compaction concept.
https://github.com/thrust/thrust/blob/master/examples/stream_compaction.cu