boostopenclgpgpuboost-compute

boost::compute stream compaction


How to do stream compaction with boost::compute?

E.g. if you want to perform heavy operation only on certain elements in the array. First you generate mask array with ones corresponding to elements for which you want to perform operation:

mask = [0 0 0 1 1 0 1 0 1]

Then perform exclusive scan (prefix sum) of mask array to get:

scan = [0 0 0 0 1 2 2 3 3]

Then compact this array with:

if (mask[i])
    inds[scan[i]] = i;

To get final array of compacted indices (inds):

[3 4 6 8]

Size of the final array is scan.last() + mask.last()


Solution

  • #include <boost/compute/algorithm/copy_if.hpp>
    
    using namespace boost::compute;
    
    detail::copy_index_if(mask.begin(), mask.end(), inds.begin(), _1 == 1, queue);