Expression Templates Library (ETL)
sum.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 namespace etl::impl::standard {
16 
22 template <typename E>
23 value_t<E> sum(const E& input) {
24  using T = value_t<E>;
25 
26  T acc(0);
27 
28  auto acc_functor = [&acc](T value) { acc += value; };
29 
30  auto batch_fun = [](auto& sub) {
31  T acc(0);
32 
33  for (size_t i = 0; i < etl::size(sub); ++i) {
34  acc += sub[i];
35  }
36 
37  return acc;
38  };
39 
40  engine_dispatch_1d_acc_slice(input, batch_fun, acc_functor, sum_parallel_threshold);
41 
42  return acc;
43 }
44 
50 template <typename E>
51 value_t<E> asum(const E& input) {
52  using T = value_t<E>;
53 
54  T acc(0);
55 
56  auto acc_functor = [&acc](T value) { acc += value; };
57 
58  auto batch_fun = [](auto& sub) {
59  T acc(0);
60 
61  for (size_t i = 0; i < etl::size(sub); ++i) {
62  using std::abs;
63  acc += abs(sub[i]);
64  }
65 
66  return acc;
67  };
68 
69  engine_dispatch_1d_acc_slice(input, batch_fun, acc_functor, sum_parallel_threshold);
70 
71  return acc;
72 }
73 
74 } //end of namespace etl::impl::standard
void engine_dispatch_1d_acc_slice(E &&expr, Functor &&functor, AccFunctor &&acc_functor, [[maybe_unused]] size_t threshold, [[maybe_unused]] size_t n_threads=etl::threads)
Dispatch the elements of an ETL container in a parallel manner and use an accumulator functor to accu...
Definition: parallel_support.hpp:890
Definition: prob_pooling.hpp:10
auto abs(E &&value)
Apply absolute on each value of the given expression.
Definition: expression_builder.hpp:54
value_t< E > asum(E &&values)
Returns the sum of all the absolute values contained in the given expression.
Definition: expression_builder.hpp:637
constexpr size_t sum_parallel_threshold
The minimum number of elements before considering parallel acc implementation.
Definition: threshold.hpp:68
value_t< E > sum(E &&values)
Returns the sum of all the values contained in the given expression.
Definition: expression_builder.hpp:624
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81