I am trying to perform minimal reduction on a zip iterator, but using a user-defined operator to only consider the second field from the tuple (the first field is the key and the second field is the value really relevant for the reduction)
However, I cannot get it to work, and currently calculates the result that is present in the vector
The following code reproduces the problem:
#include <thrust/device_vector.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/tuple.h>
#include <thrust/sequence.h>
typedef thrust::tuple<unsigned int, unsigned int> DereferencedIteratorTuple;
struct tuple_snd_min{
__host__ __device__
bool operator()(const DereferencedIteratorTuple& lhs,
const DereferencedIteratorTuple& rhs){
return (thrust::get<1>(lhs) < thrust::get<1>(rhs));
}
};
void f(){
thrust::device_vector<unsigned int> X(10);
thrust::device_vector<unsigned int> Y(10);
thrust::sequence(X.begin(), X.end());
thrust::sequence(Y.begin(), Y.end());
X[0] = 5;
Y[0] = 5;
X[1] = 50;
typedef thrust::device_vector<unsigned int>::iterator UIntIterator;
typedef thrust::tuple<UIntIterator, UIntIterator> IteratorTuple;
thrust::zip_iterator<IteratorTuple> first =
thrust::make_zip_iterator(thrust::make_tuple(X.begin(), Y.begin()));
thrust::tuple<unsigned int, unsigned int> init = first[0];
thrust::tuple<unsigned int, unsigned int> min =
thrust::reduce(first, first + 10, init, tuple_snd_min());
printf("(%d,%d)\n", thrust::get<0>(min), thrust::get<1>(min));
}
Thanks to Jared Hobrock's comment, I was able to fix it.
typedef thrust::tuple<unsigned int, unsigned int> DereferencedIteratorTuple;
struct tuple_snd_min{
__host__ __device__
const DereferencedIteratorTuple& operator()(const DereferencedIteratorTuple& lhs, const DereferencedIteratorTuple& rhs)
{
if(thrust::get<1>(lhs) < thrust::get<1>(rhs)) return lhs;
else return rhs;
}
};
source
share