E.V.E
v2023.02.15
 
Loading...
Searching...
No Matches

◆ reduce

eve::algo::reduce = function_with_traits<reduce_>[default_simple_algo_traits]
inlineconstexpr

#include <eve/module/algo/algo/reduce.hpp>

Configurable Callable Object performing a SIMD optimized version of the reduce By default, the operation will be unrolled by a factor of 4, align memory accesses and perform conversions if needed.

Due to the nature of how SIMD algorithms work, the reduce operation has to be paired with its, neutral element. For example, for add you pass {add, zero} as zero is the identity for add. Instead of zero it can be beneficial to pass eve's constants like eve::zero, eve::one because sometimes the implementation can be improved

Note
  • The interface differs from the standard to be homogeneous with eve::algo::transform_reduce.
  • Compilers can auto-vectorize reductions, especially with special options. Maybe you don't need a library implementation.

Alternative Header

#include <eve/algo.hpp>

Callable Signatures

namespace eve::algo
{
template <eve::algo::relaxed_range Rng, typename U>
U reduce(Rng&& rng, U init); // 1
template <eve::algo::relaxed_range Rng, typename Op, typename Zero, typename U>
U reduce(Rng&& rng, std::pair<Op, Zero> op_zero, U init) // 2
}
constexpr auto reduce
SIMD optimized version of std::reduce.
Definition: reduce.hpp:152
  1. Reduces the range rng along with the initial value init using regular addition.
  2. Reduces the range rng along with the initial value init over op_zero.

Parameters

  • rng: Relaxed range input range to process
  • init: Initial value. Also type of init matches the result type
  • op_zero: Pair of reduction operation (commutative/associative) and an identity (zero) for it. Default add_zero is {eve::add, eve::zero}.

Return value

  1. Sum of init and every elements of rng.
  2. Generalized sum of init and every elements of rng using op_zero.

Example

#include <eve/module/core.hpp>
#include <eve/module/algo.hpp>
#include <tts/tts.hpp>
#include <numeric>
#include <iostream>
#include <vector>
int main()
{
std::vector<double> v = {1.2,2.3,3.4,4.5,5.6,6.7};
std::cout << " -> v = "
<< tts::as_string(v)
<< "\n";
std::cout << " -> eve::algo::reduce(v, 0.) = "
<< eve::algo::reduce(v, 0.) << "\n";
std::cout << " -> eve::algo::reduce(v, std::pair{eve::mul, 1.}, 1.) = "
<< eve::algo::reduce(v, std::pair{eve::mul, 1.}, 1.) << "\n";
std::cout << " -> std::reduce(v.begin(), v.end(), 1., std::multiplies<>{}) = "
<< std::reduce(v.begin(), v.end(), 1., std::multiplies<>{}) << "\n";
return 0;
}
constexpr auto mul
tuple_callable` computing the product of its arguments.
Definition: mul.hpp:90