1

thrust::reduce配列 A の最大値を見つけるために使用したい のですがA[i]、別の配列 B の特定のブール条件も満たす場合にのみ、最大値として選択する必要があります。たとえば、B[i] は true である必要があります。これを行うのは、thrust::reduce のバージョンですか。ドキュメントを調べたところ、次の API しか見つかりませんでした。

thrust::reduce(begin,end, default value, operator)

しかし、私は彼らのバージョンが私の問題により適しているのか知りたいと思いましたか?

編集: コンパイルは最後の行で失敗します!

      typedef thrust::device_ptr<int> IntIterator;
      typedef thrust::device_ptr<float> FloatIterator;
      typedef thrust::tuple<IntIterator,FloatIterator> IteratorTuple;
      typedef thrust::zip_iterator<IteratorTuple> myZipIterator;
      thrust::device_ptr<int> deviceNBMInt(gpuNBMInt);
    thrust::device_ptr<int> deviceIsActive(gpuIsActive);
    thrust::device_ptr<float> deviceNBMSim(gpuNBMSim);

    myZipIterator iter_begin = thrust::make_zip_iterator(thrust::make_tuple(deviceIsActive,deviceNBMSim));
    myZipIterator iter_end = thrust::make_zip_iterator(thrust::make_tuple(deviceIsActive + numRow,deviceNBMSim + numRow));
    myZipIterator result =  thrust::max_element(iter_begin, iter_end, Predicate());
4

1 に答える 1

0

はいあります。Extrema And Zip iteratorを見てみるべきだと思います

このような方法でうまくいくはずです(このコードがすぐに機能するかどうかはわかりません):

typedef thrust::device_ptr<bool>  BoolIterator;
typedef thrust::device_ptr<float>  ValueIterator;

BoolIterator bools_begin, bools_end;
ValueIterator values_begin, values_end; 
// initialize these pointers
// ...

typedef thrust::tuple<BoolIterator, ValueIterator> IteratorTuple;
typedef thrust::tuple<bool, value> DereferencedIteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;

ZipIterator iter_begin(thrust::make_tuple(bools_begin, values_begin));
ZipIterator iter_end(thrust::make_tuple(bools_end, values_end));

struct Predicate
{
  __host__ __device__ bool operator () 
                      (const DereferencedIteratorTuple& lhs, 
                       const DereferencedIteratorTuple& lhs) 
  {
    using thrust::get;
    if (get<0>(lhs) && get<0>(rhs) ) return get<1>(lhs) < get<1>(rhs); else
    return ! get<0>(lhs) ;
  }
};

ZipIterator result =  thrust::max_element(iter_begin, iter_end, Predicate());

または、 zip イテレータを使用して同様の手法を検討することもできますthrust::reduce。または、inner_productで試すことができます。何がより速く動作するかはわかりません。

于 2012-12-21T11:13:16.443 に答える