thrust::reduce#
Overloads#
reduce(exec, first, last)#
-
template<typename DerivedPolicy, typename InputIterator>
detail::it_value_t<InputIterator> thrust::reduce( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- 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 ofreduceuses0as the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires 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 thenthrust::reduceshould not be used. Instead, one could useinclusive_scan(which does not require commutativity) and select the last element of the output array.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
reduceto compute the sum of a sequence of integers using thethrust::hostexecution policy for parallelization:#include <thrust/reduce.h> #include <thrust/execution_policy.h> ... int data[6] = {1, 0, 2, 2, 1, 3}; int result = thrust::reduce(thrust::host, data, data + 6); // result == 9
- Parameters:
exec – The execution policy to use for parallelization.
first – The beginning of the sequence.
last – The end of the sequence.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator and if
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toInputIterator'svalue_type. IfTisInputIterator'svalue_type, thenT(0)is defined.
- Returns:
The result of the reduction.
reduce(first, last)#
-
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 ofreduceuses0as the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires 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 thenthrust::reduceshould 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
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
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toInputIterator'svalue_type. IfTisInputIterator'svalue_type, thenT(0)is defined.- Returns:
The result of the reduction.
reduce(exec, first, last, init)#
-
template<typename DerivedPolicy, typename InputIterator, typename T>
T thrust::reduce( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- InputIterator first,
- InputIterator last,
- T init,
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 ofreduceusesinitas the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires 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 thenthrust::reduceshould not be used. Instead, one could useinclusive_scan(which does not require commutativity) and select the last element of the output array.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
reduceto compute the sum of a sequence of integers including an initialization value using thethrust::hostexecution policy for parallelization:#include <thrust/reduce.h> #include <thrust/execution_policy.h> ... int data[6] = {1, 0, 2, 2, 1, 3}; int result = thrust::reduce(thrust::host, data, data + 6, 1); // result == 10
- Parameters:
exec – The execution policy to use for parallelization.
first – The beginning of the input sequence.
last – The end of the input sequence.
init – The initial value.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator and if
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toT.T – is convertible to
InputIterator'svalue_type.
- Returns:
The result of the reduction.
reduce(first, last, init)#
-
template<typename InputIterator, typename T>
T thrust::reduce( - InputIterator first,
- InputIterator last,
- T init,
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 ofreduceusesinitas the initial value of the reduction.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires 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 thenthrust::reduceshould 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
reduceto compute the sum of a sequence of integers including an initialization value.#include <thrust/reduce.h> ... int data[6] = {1, 0, 2, 2, 1, 3}; int result = thrust::reduce(data, data + 6, 1); // result == 10
- Parameters:
first – The beginning of the input sequence.
last – The end of the input sequence.
init – The initial value.
- Template Parameters:
InputIterator – is a model of Input Iterator and if
xandyare objects ofInputIterator'svalue_type, thenx + yis defined and is convertible toT.T – is convertible to
InputIterator'svalue_type.
- Returns:
The result of the reduction.
reduce(exec, first, last, init, binary_op)#
-
template<typename DerivedPolicy, typename InputIterator, typename T, typename BinaryFunction>
T thrust::reduce( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- InputIterator first,
- InputIterator last,
- T init,
- BinaryFunction binary_op,
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 ofreduceusesinitas the initial value of the reduction andbinary_opas the binary function used for summation.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity ofbinary_opto parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this casebinary_op) is commutative. If the reduction operator is not commutative thenthrust::reduceshould not be used. Instead, one could useinclusive_scan(which does not require commutativity) and select the last element of the output array.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
reduceto compute the maximum value of a sequence of integers using thethrust::hostexecution policy for parallelization:#include <thrust/reduce.h> #include <thrust/functional.h> #include <thrust/execution_policy.h> ... int data[6] = {1, 0, 2, 2, 1, 3}; int result = thrust::reduce(thrust::host, data, data + 6, -1, ::cuda::maximum<int>()); // result == 3
See also
- Parameters:
exec – The execution policy to use for parallelization.
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:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator and
InputIterator'svalue_typeis convertible toT.T – is a model of Assignable, and is convertible to
BinaryFunction'sfirst and second argument type.BinaryFunction – The function’s return type must be convertible to
OutputType.
- Returns:
The result of the reduction.
reduce(first, last, init, binary_op)#
-
template<typename InputIterator, typename T, typename BinaryFunction>
T thrust::reduce( - InputIterator first,
- InputIterator last,
- T init,
- BinaryFunction binary_op,
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 ofreduceusesinitas the initial value of the reduction andbinary_opas the binary function used for summation.reduceis similar to the C++ Standard Template Library’sstd::accumulate. The primary difference between the two functions is thatstd::accumulateguarantees the order of summation, whilereducerequires associativity ofbinary_opto parallelize the reduction.Note that
reducealso assumes that the binary reduction operator (in this casebinary_op) is commutative. If the reduction operator is not commutative thenthrust::reduceshould 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
reduceto compute the maximum value of a sequence of integers.#include <thrust/reduce.h> #include <thrust/functional.h> ... int data[6] = {1, 0, 2, 2, 1, 3}; int result = thrust::reduce(data, data + 6, -1, ::cuda::maximum<int>()); // result == 3
See also
- 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'svalue_typeis convertible toT.T – is a model of Assignable, and is convertible to
BinaryFunction'sfirst and second argument type.BinaryFunction – The function’s return type must be convertible to
OutputType.
- Returns:
The result of the reduction.