dot.hpp
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief -
6  *
7  * \author O. Krause
8  * \date 2013
9  *
10  *
11  * \par Copyright 1995-2015 Shark Development Team
12  *
13  * <BR><HR>
14  * This file is part of Shark.
15  * <http://image.diku.dk/shark/>
16  *
17  * Shark is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Lesser General Public License as published
19  * by the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * Shark is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 //===========================================================================
32 #ifndef SHARK_LINALG_BLAS_KERNELS_CBLAS_DOT_HPP
33 #define SHARK_LINALG_BLAS_KERNELS_CBLAS_DOT_HPP
34 
35 #include "cblas_inc.hpp"
36 
37 namespace shark {namespace blas {namespace bindings {
38 
39 inline void dot(int N,
40  float const* x, int strideX,
41  float const* y, int strideY,
42  float& result
43 ) {
44  result = cblas_sdot(N, x, strideX, y, strideY);
45 }
46 
47 inline void dot(int N,
48  double const* x, int strideX,
49  double const* y, int strideY,
50  double& result
51 ) {
52  result = cblas_ddot(N, x, strideX, y, strideY);
53 }
54 
55 inline void dot(int N,
56  std::complex<float> const* x, int strideX,
57  std::complex<float> const* y, int strideY,
58  std::complex<float>& result
59 ) {
60  cblas_cdotu_sub(N,
61  reinterpret_cast<cblas_float_complex_type const* >(x), strideX,
62  reinterpret_cast<cblas_float_complex_type const* >(y), strideY,
63  reinterpret_cast<cblas_float_complex_type*>(&result)
64  );
65 }
66 
67 inline void dot(int N,
68  std::complex<double> const* x, int strideX,
69  std::complex<double> const* y, int strideY,
70  std::complex<double>& result
71 ) {
72  cblas_zdotu_sub(N,
73  reinterpret_cast<cblas_double_complex_type const* >(x), strideX,
74  reinterpret_cast<cblas_double_complex_type const* >(y), strideY,
75  reinterpret_cast<cblas_double_complex_type*>(&result)
76  );
77 }
78 
79 
80 // y <- alpha* op (A)* x + beta* y
81 // op (A) == A || A^T || A^H
82 template <typename VectorX, typename VectorY>
83 void dot(
84  vector_expression<VectorX> const& x,
85  vector_expression<VectorY> const& y,
86  typename VectorX::value_type& result,
87  boost::mpl::true_
88 ){
89  SIZE_CHECK(x().size() == y().size());
90 
91  dot(
92  x().size(),
93  traits::storage(x), traits::stride(x),
94  traits::storage(y), traits::stride(y),
95  result
96  );
97 }
98 
99 template<class Storage1, class Storage2, class T1, class T2, class T3>
100 struct optimized_dot_detail{
101  typedef boost::mpl::false_ type;
102 };
103 template<>
104 struct optimized_dot_detail<
105  dense_tag, dense_tag,
106  double, double, double
107 >{
108  typedef boost::mpl::true_ type;
109 };
110 template<>
111 struct optimized_dot_detail<
112  dense_tag, dense_tag,
113  float, float, float
114 >{
115  typedef boost::mpl::true_ type;
116 };
117 
118 template<>
119 struct optimized_dot_detail<
120  dense_tag, dense_tag,
121  std::complex<double>, std::complex<double>, std::complex<double>
122 >{
123  typedef boost::mpl::true_ type;
124 };
125 template<>
126 struct optimized_dot_detail<
127  dense_tag, dense_tag,
128  std::complex<float>, std::complex<float>, std::complex<float>
129 >{
130  typedef boost::mpl::true_ type;
131 };
132 
133 template<class V1, class V2, class result_type>
134 struct has_optimized_dot
135 : public optimized_dot_detail<
136  typename V1::storage_category,
137  typename V2::storage_category,
138  typename V1::value_type,
139  typename V2::value_type,
140  result_type
141 >{};
142 
143 }}}
144 #endif