thrust::discard_iterator

Defined in thrust/iterator/discard_iterator.h

template<typename System = use_default>
class discard_iterator

discard_iterator is an iterator which represents a special kind of pointer that ignores values written to it upon dereference. This iterator is useful for ignoring the output of certain algorithms without wasting memory capacity or bandwidth. discard_iterator may also be used to count the size of an algorithm’s output which may not be known a priori.

The following code snippet demonstrates how to use discard_iterator to ignore one of the output ranges of reduce_by_key

#include <thrust/iterator/discard_iterator.h>
#include <thrust/reduce.h>
#include <thrust/device_vector.h>

int main()
{
  thrust::device_vector<int> keys{1, 3, 3, 3, 2, 2, 1};
  thrust::device_vector<int> values{9, 8, 7, 6, 5, 4, 3};

  thrust::device_vector<int> result(4);

  // we are only interested in the reduced values
  // use discard_iterator to ignore the output keys
  thrust::reduce_by_key(keys.begin(), keys.end(),
                        values.begin(),
                        thrust::make_discard_iterator(),
                        result.begin());

  // result is now [9, 21, 9, 3]

  return 0;
}

See also

make_discard_iterator