Тяга: как вернуть индексы активных элементов массива - PullRequest
2 голосов
/ 16 марта 2012

Как я могу использовать 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 Ответ

3 голосов
/ 16 марта 2012

Объединение 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
...