假设我们有两个输入,第一个是数组,第二个是位图
thrust::device_vector<point_t> points;
Bitset bits; // Imagine this can be accessed within the kernel.
我想要做的是将有效点复制到输出向量。
thrust::device<point_t> output;
要访问
Bitset
,我需要给出一个恰好是点数组索引的数字。
逻辑如下:
for(size_t i = 0; i < points.size(); i++) {
if (bits.is_active(i)) {
output.push_back(points[i]);
}
}
我相信这可以通过
copy_if
、make_transform_iterator
、make_zip_iterator
等的组合来实现。但是我需要 copy_if
的谓词来在转换之前访问该值。我该如何让它发挥作用?
auto get_point =
[] __device__(const thrust::tuple<size_t, point_t>& t) {
return thrust::get<1>(t);
};
auto it1 = thrust::make_transform_iterator(
thrust::make_zip_iterator(thrust::make_tuple(
thrust::counting_iterator<size_t>(0), points.begin())),
get_point);
auto it2 = thrust::make_transform_iterator(
thrust::make_zip_iterator(thrust::make_tuple(
thrust::counting_iterator<size_t>(points.size()), points.end())),
get_point);
thrust::copy_if(
thrust::cuda::par.on(stream), it1, it2,
output.begin(),
[=] __device__(const thrust::tuple<size_t, point_t>& t) {
auto index = thrust::get<0>(t);
return bits.is_active(index);
});
感谢 Abator,他的解决方案有效。
auto end = thrust::copy_if(
thrust::cuda::par.on(stream),
points.begin(),
points.end(),
thrust::counting_iterator<size_t>(0),
output.begin(),
[=] __device__(size_t i) { return bits.is_active(i); }
);