2

如何使用推力返回活动数组元素的索引,即返回数组元素等于 1 的索引向量?

对此进行扩展,在给定数组维度的多维索引的情况下,这将如何工作?

编辑:目前该功能看起来像这样

template<class VoxelType>
void VoxelVolumeT<VoxelType>::cudaThrustReduce(VoxelType *cuda_voxels)
{
    device_ptr<VoxelType> cuda_voxels_ptr(cuda_voxels);

    int active_voxel_count = thrust::count(cuda_voxels_ptr, cuda_voxels_ptr + dim.x*dim.y*dim.z, 1);

    device_vector<VoxelType> active_voxels;

    thrust::copy_if(make_counting_iterator(0), 
                    make_counting_iterator(dim.x*dim.y*dim.z),
                    cuda_voxels_ptr,
                    active_voxels.begin(),
                    _1 == 1);
}

哪个给出了错误

Error   15  error : no instance of overloaded function "thrust::copy_if" matches the argument list
4

1 回答 1

3

结合:counting_iterator_copy_if

#include <thrust/copy.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/functional.h>
...
using namespace thrust;
using namespace thrust::placeholders;

copy_if(make_counting_iterator<int>(0),
        make_counting_iterator<int>(array.size()), // indices from 0 to N
        array.begin(),                             // array data
        active_indices.begin(),                    // result will be written here
        _1 == 1);                                  // return when an element or array is equal to 1
于 2012-03-15T22:55:02.793 回答