boost::compute stream compaction

泪湿孤枕 提交于 2019-12-07 18:54:52

问题


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()


回答1:


#include <boost/compute/algorithm/copy_if.hpp>

using namespace boost::compute;

detail::copy_index_if(mask.begin(), mask.end(), inds.begin(), _1 == 1, queue);


来源:https://stackoverflow.com/questions/39402091/boostcompute-stream-compaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!