Question

If I use

 float sum = thrust::transform_reduce(d_a.begin(), d_a.end(), conditional_operator(), 0.f, thrust::plus<float>());

I get the sum of all elements meeting a condition provided by conditional_operator(), as in Conditional reduction in CUDA.

But what can I sum only the elements d_a[0], d_a[2], d_a[4], d_a[6], ..... ?

I thought of changing the conditional operator, but it works on on elements in the array without any reference to the index.

What can I do for that?

Was it helpful?

Solution

There are two approaches I can think of for solving this sort of problem:

  1. Use the thrust zip operator to combine a counting iterator with the input data and modify your existing functor to accept tuples of (index, data). You can have the functor return the data when the index matches your criteria, and zero otherwise. This will work correctly with scan and reduction algorithms
  2. Use a thrust permutation iterator to gather the data which you want to sum and pass it to the standard reduce algorithm. The thrust developers have an example strided iterator which you can use to solve the problem of only processing every nth entry in an input iterator.

It might be worth implemented both and benchmarking them to see which approach is faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top