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.
namespace eve::algo
{
template <eve::algo::relaxed_range Rng, typename MapOp, typename U>
template< eve::algo::relaxed_range Rng, typename MapOp
, typename AddOp, typename Zero, typename U
>
U
transform_reduce(Rng&& rng, MapOp map_op, std::pair<AddOp, Zero> add_zero, U init);
}
constexpr auto transform_reduce
SIMD version of std::transform_reduce for a single range.
Definition transform_reduce.hpp:177
#include <eve/module/core.hpp>
#include <eve/module/algo.hpp>
#include <tts/tts.hpp>
#include <iostream>
#include <vector>
int main()
{
std::vector<float> v = {1.0f, 2.0f, 3.0f, 4.0f, -1.0f, -2.0f, -3.0f, -4.0f, 1.0f, 2.0f, 3.0f, 4.0f};
std::cout << " -> v = "
<< tts::as_string(v)
<< "\n";
std::cout << " -> eve::algo::transform_reduce(v, [](auto x) { return x + x }, 0.f) = "
std::cout << " -> std::transform_reduce(v.begin(), v.end(), std::plus<>{}, 0.f, [](auto x) { return x + x }) = "
<< std::transform_reduce(v.begin(), v.end(), 0., std::plus<>{}, [](auto x) { return x + x; }) << "\n";
std::cout << " -> eve::algo::reduce(eve::views::map(v, [](auto x) { return x + x }), 0.f) = "
std::cout << " -> eve::algo::transform_reduce(v, [](auto x) { return x + x }, std::pair{eve::mul, eve::one}, 1.f) = "
std::cout << " -> eve::algo::transform_reduce[eve::algo::fuse_operations](v, [](auto x, auto sum) { return eve::fma(x, 2.f, sum); }, 0.f) = "
}
constexpr auto fuse_operations
Some algorithms (for example transform_reduce) can be implemented more efficient if you fuse multiple...
Definition traits.hpp:305
constexpr auto reduce
SIMD optimized version of std::reduce.
Definition reduce.hpp:152
constexpr auto mul
tuple_callable computing the product of its arguments.
Definition mul.hpp:128
constexpr auto one
Computes the constant .
Definition one.hpp:66
constexpr auto fma
strict_elementwise_callable computing the fused multiply add of its three parameters.
Definition fma.hpp:100