#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
namespace eve::algo
{
template <eve::algo::relaxed_range Rng, typename U>
template <eve::algo::relaxed_range Rng, typename Op, typename Zero, typename U>
U
reduce(Rng&& rng, std::pair<Op, Zero> op_zero, U init)
}
constexpr auto reduce
SIMD optimized version of std::reduce.
Definition reduce.hpp:152
- Reduces the range
rng
along with the initial value init
using regular addition.
- 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
- Sum of
init
and every elements of rng
.
- Generalized sum of
init
and every elements of rng
using op_zero
.
#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.) = "
std::cout << " -> eve::algo::reduce(v, std::pair{eve::mul, 1.}, 1.) = "
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_callablecomputing the product of its arguments. ! ! @groupheader{Header file}...
Definition mul.hpp:119