thrust::transform_reduce#
Overloads#
transform_reduce(exec, first, last, unary_op, init, binary_op)#
-
template<typename DerivedPolicy, typename InputIterator, typename UnaryFunction, typename OutputType, typename BinaryFunction>
OutputType thrust::transform_reduce( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- InputIterator first,
- InputIterator last,
- UnaryFunction unary_op,
- OutputType init,
- BinaryFunction binary_op,
transform_reducefuses thetransformandreduceoperations.transform_reduceis equivalent to performing a transformation defined byunary_opinto a temporary sequence and then performingreduceon the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.transform_reduceperforms a reduction on the transformation of the sequence[first, last)according tounary_op. Specifically,unary_opis applied to each element of the sequence and then the result is reduced to a single value withbinary_opusing the initial valueinit. Note that the transformationunary_opis not applied to the initial valueinit. The order of reduction is not specified, sobinary_opmust be both commutative and associative.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
transform_reduceto compute the maximum value of the absolute value of the elements of a range using thethrust::hostexecution policy for parallelization:#include <thrust/transform_reduce.h> #include <thrust/functional.h> #include <thrust/execution_policy.h> template<typename T> struct absolute_value { __host__ __device__ T operator()(const T &x) const { return x < T(0) ? -x : x; } }; ... int data[6] = {-1, 0, -2, -2, 1, -3}; int result = thrust::transform_reduce(thrust::host, data, data + 6, absolute_value<int>(), 0, ::cuda::maximum<int>()); // result == 3See also
See also
- Parameters:
exec – The execution policy to use for parallelization.
first – The beginning of the sequence.
last – The end of the sequence.
unary_op – The function to apply to each element of the input sequence.
init – The result is initialized to this value.
binary_op – The reduction operation.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toUnaryFunction'sargument type.UnaryFunction – The function’s return type must be convertible to
OutputType.OutputType – 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 transformed reduction.
transform_reduce(first, last, unary_op, init, binary_op)#
-
template<typename InputIterator, typename UnaryFunction, typename OutputType, typename BinaryFunction>
OutputType thrust::transform_reduce( - InputIterator first,
- InputIterator last,
- UnaryFunction unary_op,
- OutputType init,
- BinaryFunction binary_op,
transform_reducefuses thetransformandreduceoperations.transform_reduceis equivalent to performing a transformation defined byunary_opinto a temporary sequence and then performingreduceon the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.transform_reduceperforms a reduction on the transformation of the sequence[first, last)according tounary_op. Specifically,unary_opis applied to each element of the sequence and then the result is reduced to a single value withbinary_opusing the initial valueinit. Note that the transformationunary_opis not applied to the initial valueinit. The order of reduction is not specified, sobinary_opmust be both commutative and associative.The following code snippet demonstrates how to use
transform_reduceto compute the maximum value of the absolute value of the elements of a range.#include <thrust/transform_reduce.h> #include <thrust/functional.h> template<typename T> struct absolute_value { __host__ __device__ T operator()(const T &x) const { return x < T(0) ? -x : x; } }; ... int data[6] = {-1, 0, -2, -2, 1, -3}; int result = thrust::transform_reduce(data, data + 6, absolute_value<int>(), 0, ::cuda::maximum<int>()); // result == 3See also
See also
- Parameters:
first – The beginning of the sequence.
last – The end of the sequence.
unary_op – The function to apply to each element of the input sequence.
init – The result is initialized to this value.
binary_op – The reduction operation.
- Template Parameters:
InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toUnaryFunction'sargument type.UnaryFunction – The function’s return type must be convertible to
OutputType.OutputType – 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 transformed reduction.