Expression Templates Library (ETL)
dot.hpp
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 //Include the implementations
16 #include "etl/impl/std/dot.hpp"
17 #include "etl/impl/blas/dot.hpp"
18 #include "etl/impl/cublas/dot.hpp"
19 #include "etl/impl/vec/dot.hpp"
20 
21 namespace etl::detail {
22 
32 template <typename A, typename B>
33 constexpr etl::dot_impl select_default_dot_impl() {
34  if (all_dma<A, B> && cblas_enabled) {
35  return etl::dot_impl::BLAS;
36  }
37 
38  if (vec_enabled && all_vectorizable<vector_mode, A, B> && std::same_as<default_intrinsic_type<value_t<A>>, default_intrinsic_type<value_t<B>>>) {
39  return etl::dot_impl::VEC;
40  }
41 
42  return etl::dot_impl::STD;
43 }
44 
45 #ifdef ETL_MANUAL_SELECT
46 
53 template <typename A, typename B>
54 etl::dot_impl select_dot_impl() {
55  if (local_context().dot_selector.forced) {
56  auto forced = local_context().dot_selector.impl;
57 
58  switch (forced) {
59  //CUBLAS cannot always be used
60  case dot_impl::CUBLAS:
61  if (!cublas_enabled || !all_dma<A, B>) {
62  std::cerr << "Forced selection to CUBLAS dot implementation, but not possible for this expression" << std::endl;
63  return select_default_dot_impl<A, B>();
64  }
65 
66  return forced;
67 
68  //BLAS cannot always be used
69  case dot_impl::BLAS:
70  if (!cblas_enabled || !all_dma<A, B>) {
71  std::cerr << "Forced selection to BLAS dot implementation, but not possible for this expression" << std::endl;
72  return select_default_dot_impl<A, B>();
73  }
74 
75  return forced;
76 
77  //VEC cannot always be used
78  case dot_impl::VEC:
79  if (!vec_enabled || !decay_traits<A>::template vectorizable<vector_mode> || !decay_traits<B>::template vectorizable<vector_mode>) {
80  std::cerr << "Forced selection to VEC dot implementation, but not possible for this expression" << std::endl;
81  return select_default_dot_impl<A, B>();
82  }
83 
84  return forced;
85 
86  //In other cases, simply use the forced impl
87  default:
88  return forced;
89  }
90  }
91 
92  return select_default_dot_impl<A, B>();
93 }
94 
95 #else
96 
103 template <typename A, typename B>
104 constexpr etl::dot_impl select_dot_impl() {
105  return select_default_dot_impl<A, B>();
106 }
107 
108 #endif
109 
113 struct dot_impl {
120  template <typename A, typename B>
121  static value_t<A> apply(const A& a, const B& b) {
122  constexpr_select auto impl = select_dot_impl<A, B>();
123 
124  if
125  constexpr_select(impl == etl::dot_impl::BLAS) {
126  inc_counter("impl:blas");
127  return etl::impl::blas::dot(a, b);
128  }
129  else if
130  constexpr_select(impl == etl::dot_impl::CUBLAS) {
131  inc_counter("impl:cublas");
132  return etl::impl::cublas::dot(a, b);
133  }
134  else if
135  constexpr_select(impl == etl::dot_impl::VEC) {
136  inc_counter("impl:vec");
137  return etl::impl::vec::dot(a, b);
138  }
139  else {
140  inc_counter("impl:std");
141  return etl::impl::standard::dot(a, b);
142  }
143  }
144 };
145 
146 } //end of namespace etl::detail
Standard implementation.
CUBLAS implementation of the dot product.
constexpr bool vec_enabled
Indicates if vectorization is available in any format.
Definition: config.hpp:220
VEC implementation.
BLAS implementation.
Definition: expression_builder.hpp:699
Standard implementation of the "dot" reduction.
dot_impl
Enumeration describing the different implementations of dot.
Definition: dot_impl.hpp:20
BLAS implementation.
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
constexpr bool cublas_enabled
Indicates if the NVIDIA CUBLAS library is available for ETL.
Definition: config.hpp:99
static value_t< A > apply(const A &a, const B &b)
Apply the functor to a and b.
Definition: dot.hpp:121
typename default_intrinsic_traits< T >::intrinsic_type default_intrinsic_type
Helper to get the intrinsic corresponding type of a vectorizable type.
Definition: vectorization.hpp:256
Unified vectorized implementation of the "dot" reduction.
Functor for dot product.
Definition: dot.hpp:113
constexpr bool cblas_enabled
Indicates if a BLAS library is available for ETL.
Definition: config.hpp:76
BLAS implementation of the "dot" reduction.
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
void inc_counter([[maybe_unused]] const char *name)
Increase the given counter.
Definition: counters.hpp:25