thrust::reduce_into
Defined in thrust/reduce.h
-
template<typename DerivedPolicy, typename InputIterator, typename OutputIterator>
void thrust::reduce_into(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, InputIterator first, InputIterator last, OutputIterator output) 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
uses0
as the initial value of the reduction.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 of the binary operation to parallelize the reduction.Note that
reduce_into
also assumes that the binary reduction operator (in this case operator+) is commutative. If the reduction operator is not commutative thenreduce_into
should not be used. Instead, one could useinclusive_scan
(which does not require commutativity) and select the last element of the output array.Unlike
reduce
,reduce_into
does not return the reduction result. Instead, it is written to*output
. Thus, whenexec
isthrust::cuda::par_nosync
, this algorithm does not wait for the work it launches to complete. Additionally, you can usereduce_into
to avoid copying the reduction result from device memory to host memory.The algorithm’s execution is parallelized as determined by
exec
.The following code snippet demonstrates how to use
reduce_into
to compute the sum of a sequence of integers using thethrust::device
execution policy for parallelization:#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(thrust::device, data.begin(), data.end(), output.begin()); // output[0] == 9
See also
reduce
- Parameters
exec – The execution policy to use for parallelization.
first – The beginning of the sequence.
last – The end of the sequence.
output – The location the reduction will be written to.
- Template Parameters
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator and if
x
andy
are objects ofInputIterator's
value_type
, thenx + y
is defined and is convertible toInputIterator's
value_type
. IfT
isInputIterator's
value_type
, thenT(0)
is defined.OutputIterator – is a model of Output Iterator and
OutputIterator's
value_type
is assignable fromInputIterator's
value_type
.