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 #ifdef ETL_BLAS_MODE
16 #include "cblas.h" //For ddot/sdot
17 #endif
18 
19 /*
20  * Unfortunately, BLAS library are retarded and do not offer sum,
21  * but only asum (absolute sum), therefore, the onyl solution is to
22  * use a vector of ones and a dot product
23  */
24 
25 namespace etl::impl::blas {
26 
27 #ifdef ETL_BLAS_MODE
28 
34 template <typename A>
35 value_t<A> sum(const A& a) {
36  if constexpr (is_dma<A>) {
37  etl::dyn_vector<value_t<A>> ones(etl::size(a));
38  ones = value_t<A>(1);
39 
40  a.ensure_cpu_up_to_date();
41 
42  [[maybe_unused]] const auto* m_a = a.memory_start();
43  [[maybe_unused]] const auto* m_b = ones.memory_start();
44 
45  if constexpr (is_single_precision<A>) {
46  return cblas_sdot(etl::size(a), m_a, 1, m_b, 1);
47  } else if constexpr (is_double_precision<A>) {
48  return cblas_ddot(etl::size(a), m_a, 1, m_b, 1);
49  } else {
50  cpp_unreachable("BLAS not enabled/available");
51  }
52  } else {
53  cpp_unreachable("BLAS not enabled/available");
54  }
55 }
56 
62 template <typename A>
63 value_t<A> asum(const A& a) {
64  if constexpr (is_dma<A>) {
65  a.ensure_cpu_up_to_date();
66 
67  if constexpr (is_single_precision<A>) {
68  return cblas_sasum(etl::size(a), a.memory_start(), 1);
69  } else if constexpr (is_double_precision<A>) {
70  return cblas_dasum(etl::size(a), a.memory_start(), 1);
71  } else {
72  cpp_unreachable("BLAS not enabled/available");
73  }
74  } else {
75  cpp_unreachable("BLAS not enabled/available");
76  }
77 }
78 
79 #else
80 
81 //COVERAGE_EXCLUDE_BEGIN
82 
86 template <typename A>
87 value_t<A> sum(const A& /*a*/) {
88  cpp_unreachable("BLAS not enabled/available");
89  return 0.0;
90 }
91 
95 template <typename A>
96 value_t<A> asum(const A& /*a*/) {
97  cpp_unreachable("BLAS not enabled/available");
98  return 0.0;
99 }
100 
101  //COVERAGE_EXCLUDE_END
102 
103 #endif
104 
105 } //end of namespace etl::impl::blas
value_t< E > asum(E &&values)
Returns the sum of all the absolute values contained in the given expression.
Definition: expression_builder.hpp:637
Matrix with run-time fixed dimensions.
Definition: dyn.hpp:26
value_t< E > sum(E &&values)
Returns the sum of all the values contained in the given expression.
Definition: expression_builder.hpp:624
Definition: dot.hpp:19
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81