thrust::reduce
Defined in thrust/reduce.h
- 
template<typename InputIterator>
 detail::it_value_t<InputIterator> thrust::reduce(InputIterator first, InputIterator last)
- reduceis a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range- [first, last). This version of- reduceuses- 0as the initial value of the reduction.- reduceis similar to the C++ Standard Template Library’s- std::accumulate. The primary difference between the two functions is that- std::accumulateguarantees the order of summation, while- reducerequires associativity of the binary operation to parallelize the reduction.- Note that - reducealso assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative then- thrust::reduceshould not be used. Instead, one could use- inclusive_scan(which does not require commutativity) and select the last element of the output array.- The following code snippet demonstrates how to use - reduceto compute the sum of a sequence of integers.- #include <thrust/reduce.h> ... int data[6] = {1, 0, 2, 2, 1, 3}; int result = thrust::reduce(data, data + 6); // result == 9 - Parameters
- first – The beginning of the sequence. 
- last – The end of the sequence. 
 
- Template Parameters
- InputIterator – is a model of Input Iterator and if - xand- yare objects of- InputIterator's- value_type, then- x + yis defined and is convertible to- InputIterator's- value_type. If- Tis- InputIterator's- value_type, then- T(0)is defined.
- Returns
- The result of the reduction.