Thrust:如何返回活动数组元素的索引

时间:2012-03-15 21:38:59

标签: cuda thrust

如何使用推力返回有源数组元素的索引,即返回索引向量,其中数组元素等于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

1 个答案:

答案 0 :(得分:3)

counting_iteratorcopy_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