thrust::reduce_into
Defined in thrust/reduce.h
-
template<typename InputIterator, typename OutputIterator, typename T, typename BinaryFunction>
void thrust::reduce_into(InputIterator first, InputIterator last, OutputIterator output, T init, BinaryFunction binary_op) reduce_into
is a generalization of summation: it computes the sum (or some other binary operation) of all the elements in the range[first, last)
. This version ofreduce_into
usesinit
as the initial value of the reduction andbinary_op
as the binary function used for summation.reduce_into
is similar to the C++ Standard Template Library’sstd::accumulate
. The primary difference between the two functions is thatstd::accumulate
guarantees the order of summation, whilereduce_into
requires associativity ofbinary_op
to parallelize the reduction.Note that
reduce_into
also assumes that the binary reduction operator (in this casebinary_op
) is commutative. If the reduction operator is not commutative thenthrust::reduce_into
should not be used. Instead, one could useinclusive_scan
(which does not require commutativity) and select the last element of the output array.The following code snippet demonstrates how to use
reduce_into
to compute the maximum value of a sequence of integers.#include <cuda/functional> #include <thrust/reduce.h> #include <thrust/device_vector.h> #include <thrust/execution_policy.h> thrust::device_vector<int> data{1, 0, 2, 2, 1, 3}; thrust::device_vector<int> output(1); thrust::reduce_into(data.begin(), data.end(), output.begin(), -1, cuda::maximum{}); // output[0] == 3
See also
reduce
See also
transform_reduce
See also
transform_reduce_into
- Parameters
first – The beginning of the input sequence.
last – The end of the input sequence.
init – The initial value.
binary_op – The binary function used to ‘sum’ values.
- Template Parameters
InputIterator – is a model of Input Iterator and
InputIterator's
value_type
is convertible toT
.OutputIterator – is a model of Output Iterator and
OutputIterator's
value_type
is assignable fromT
.T – is a model of Assignable, and is convertible to
BinaryFunction's
first and second argument type.BinaryFunction – The function’s return type must be convertible to
OutputType
.
- Returns
The result of the reduction.