vir-simd 0.4.189
Parallelism TS 2 extensions and simd fallback implementation
Loading...
Searching...
No Matches
Algorithm: transform_reduce

Applies transform_op to corresponding input elements and reduces the results along with the initial value init over reduce_op. More...

Functions

template<detail::simd_execution_policy ExecutionPolicy, detail::simd_execution_iterator It1, detail::simd_execution_iterator It2, typename T>
constexpr T vir::transform_reduce (ExecutionPolicy policy, It1 first1, It1 last1, It2 first2, T init)
 Inner product (iterator overload)
 
template<detail::simd_execution_policy ExecutionPolicy, detail::simd_execution_iterator It1, detail::simd_execution_iterator It2, typename T, typename BinaryReductionOp, typename BinaryTransformOp>
constexpr T vir::transform_reduce (ExecutionPolicy policy, It1 first1, It1 last1, It2 first2, T init, BinaryReductionOp reduce_op, BinaryTransformOp transform_op)
 As above but with user-provided transform and reduce operations (iterator overload)
 
template<detail::simd_execution_policy ExecutionPolicy, detail::simd_execution_iterator It, typename T, typename BinaryReductionOp, typename UnaryTransformOp>
constexpr T vir::transform_reduce (ExecutionPolicy policy, It first1, It last1, T init, BinaryReductionOp reduce_op, UnaryTransformOp transform_op)
 Transform one input range with subsequent reduction (iterator overload)
 
template<detail::simd_execution_policy ExecutionPolicy, detail::simd_execution_range Rng1, detail::simd_execution_range Rng2, typename T>
constexpr T vir::transform_reduce (ExecutionPolicy policy, Rng1 &&r1, Rng2 &&r2, T init)
 Inner product (range overload)
 
template<detail::simd_execution_policy ExecutionPolicy, detail::simd_execution_range Rng1, detail::simd_execution_range Rng2, typename T, typename BinaryReductionOp, typename BinaryTransformOp>
constexpr T vir::transform_reduce (ExecutionPolicy policy, Rng1 &&r1, Rng2 &&r2, T init, BinaryReductionOp reduce_op, BinaryTransformOp transform_op)
 As above but with user-provided transform and reduce operations (range overload)
 
template<detail::simd_execution_policy ExecutionPolicy, detail::simd_execution_range Rng, typename T, typename BinaryReductionOp, typename UnaryTransformOp>
constexpr T vir::transform_reduce (ExecutionPolicy policy, Rng &&r1, T init, BinaryReductionOp reduce_op, UnaryTransformOp transform_op)
 Transform one input range with subsequent reduction (range overload)
 
template<vir::detail::simd_execution_policy ExecutionPolicy, vir::detail::simd_execution_iterator It1, vir::detail::simd_execution_iterator It2, typename T>
constexpr T std::transform_reduce (ExecutionPolicy policy, It1 first1, It1 last1, It2 first2, T init)
 Overloads std::transform_reduce for vir::execution::simd.
 
template<vir::detail::simd_execution_policy ExecutionPolicy, vir::detail::simd_execution_iterator It1, vir::detail::simd_execution_iterator It2, typename T, typename BinaryReductionOp, typename BinaryTransformOp>
constexpr T std::transform_reduce (ExecutionPolicy policy, It1 first1, It1 last1, It2 first2, T init, BinaryReductionOp reduce_op, BinaryTransformOp transform_op)
 Overloads std::transform_reduce for vir::execution::simd.
 
template<vir::detail::simd_execution_policy ExecutionPolicy, vir::detail::simd_execution_iterator It, typename T, typename BinaryReductionOp, typename UnaryTransformOp>
constexpr T std::transform_reduce (ExecutionPolicy policy, It first1, It last1, T init, BinaryReductionOp reduce_op, UnaryTransformOp transform_op)
 Overloads std::transform_reduce for vir::execution::simd.
 

Detailed Description

Applies transform_op to corresponding input elements and reduces the results along with the initial value init over reduce_op.

These functions are drop-in replacements for std::transform_reduce. The only difference is that reduce_op and transform_op are called with simd<U, Abi> arguments instead of U (where U is the value-type of the corresponding input range).

Parameters
policyNeeds to be vir::execution::simd or one of the derived types returned from its modifiers. (vir::detail::simd_execution_policy)
first1,last1Iterator pair modelling vir::detail::simd_execution_iterator.
first2Begin iterator modelling vir::detail::simd_execution_iterator. std::distance (first1, last1) determines the size of this range.
r1,r2Input ranges modelling vir::detail::simd_execution_range.
initThe initial value determines the aggregation type and its value is included in the reduction.
reduce_opA binary operation that will be applied in unspecified order to the results of transform_op, the results of other reduce_op and init.
transform_opA unary or binary operation that will be applied to each element of the input range(s). The return type must be acceptable as input to reduce.
Returns
The result of the reduction over all transformed inputs.

Example

std::array<int> x = {1, 2, 3, 4, 2, 4, 3, 2};
std::array<int> y = {2, 3, 2, 1, 4, 1, 4, 0};
// inner product:
[](auto a, auto b) {
std::cout << "reduce(" << a << ',' << b << ") -> " << a + b << '\n';
return a + b;
},
[](auto a, auto b) {
std::cout << "transform(" << a << ',' << b << ") -> " << a * b << '\n';
return a * b;
});
assert(result == 42);
constexpr T transform_reduce(ExecutionPolicy policy, It1 first1, It1 last1, It2 first2, T init)
Inner product (iterator overload)
Definition simd_execution.h:1344
constexpr simd_policy simd
SIMD execution policy.
Definition simd_execution.h:528

A possible output (with suitably defined operator<< overloads) of the above example could be:

transform([1,2,3,4],[2,3,2,1]) -> [2,6,6,4]
transform([2,4,3,2],[4,1,4,0]) -> [8,4,12,0]
reduce([2,6,6,4],[8,4,12,0]) -> [10,10,18,4]
reduce([10,10],[18,4]) -> [28,14]
reduce([28],[14]) -> [42]
reduce([42],[0]) -> [42]
constexpr std::iter_value_t< It > reduce(ExecutionPolicy policy, It first, It last)
Sum the given range (iterator overload)
Definition simd_execution.h:1424
constexpr OutIt transform(ExecutionPolicy pol, It1 first1, It1 last1, OutIt d_first, UnaryOperation unary_op)
Unary transform (iterator overload)
Definition simd_execution.h:1222

Note that the final call to reduce adds the init value.