在回答我之前的问题时,有人给了我以下代码:
thrust::device_vector<bool> bools;
thrust::device_vector<float> values;
typedef thrust::device_vector<bool>::iterator BoolIterator;
typedef thrust::device_vector<float>::iterator ValueIterator;
typedef thrust::tuple<BoolIterator, ValueIterator> IteratorTuple;
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 IteratorTuple& lhs, const IteratorTuple& lhs)
{
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());
我想了解 Predicate 结构。如果运算符返回 false 会发生什么?选择哪个值?如果运算符返回 true 会发生什么?选择哪个值?