2012-03-15 9 views
2

推力を使用してアクティブな配列要素のインデックスを返す、つまり配列要素が1に等しいインデックスのベクトルを返すにはどうすればよいですか?推力:アクティブな配列要素のインデックスを返す方法

これを拡張すると、配列の寸法が与えられた多次元インデックスの場合、どのように機能しますか?

編集:現在の機能はcopy_ifcounting_iteratorを組み合わせてエラーに

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

答えて

3

を与えている。この

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); 
} 

次のようになります。

#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 
+0

さて、私はそれを試してみました、エラーが発生しています 'エラーエラー:オーバーロードされた関数のインスタンスがありません" thrust :: copy_if "は引数リストと一致します – ragnar

+0

クールな、それは動作しているようです。 '推理::プレースホルダー'がどのように機能するかに関する文書はありますか?また、1次元イテレータと一緒に反復する3次元イテレータを作成する最良の方法は何ですか?すなわち、私は3次元座標を知りたいが、単純に線形メモリにアクセスするために単一のインデックスに戻すことは無駄に思える。 – ragnar

+0

@ragnar:おそらく、 'transform_iterator'を使って線形インデックスを多次元座標に変換することができます。 –

関連する問題