gemm.hpp
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief matrix-matrix multiplication kernel
5  *
6  * \author O. Krause
7  * \date 2012
8  *
9  *
10  * \par Copyright 1995-2015 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://image.diku.dk/shark/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 
31 #ifndef REMORA_KERNELS_GEMM_HPP
32 #define REMORA_KERNELS_GEMM_HPP
33 
34 #include "default/gemm.hpp"
35 #ifdef REMORA_USE_CBLAS
36 #include "cblas/dense_gemm.hpp"
37 #else
38 #include "default/dense_gemm.hpp"
39 #endif
40 
41 #include "../detail/matrix_proxy_classes.hpp"
42 
43 namespace remora{
44 
45 namespace bindings{
46  //-- Dense gemm
47  template <class E1, class E2, class Mat, class Orientation1, class Orientation2>
48  void gemm(
49  matrix_expression<E1, cpu_tag> const& e1,
50  matrix_expression<E2, cpu_tag> const& e2,
51  matrix_expression<Mat, cpu_tag>& m,
52  typename Mat::value_type alpha,
53  row_major, Orientation1, Orientation2,
54  dense_tag, dense_tag
55  ){
56  dense_gemm(e1,e2,m,alpha);
57  }
58  //column major result is transformed to row_major using A=B*C <=> A^T = C^T B^T
59  template<class M, class E1, class E2, class Orientation1, class Orientation2, class Tag1, class Tag2>
60  void gemm(
61  matrix_expression<E1, cpu_tag> const& e1,
62  matrix_expression<E2, cpu_tag> const& e2,
63  matrix_expression<M, cpu_tag>& m,
64  typename M::value_type alpha,
65  column_major, Orientation1, Orientation2,
66  Tag1, Tag2
67  ){
68  matrix_transpose<M> transposedM(m());
69  typedef typename Orientation1::transposed_orientation transpO1;
70  typedef typename Orientation2::transposed_orientation transpO2;
71  matrix_transpose<E1 const> e1trans(e1());
72  matrix_transpose<E2 const> e2trans(e2());
73  gemm(e2trans,e1trans,transposedM,alpha,row_major(),transpO2(),transpO1(), Tag2(),Tag1());
74  }
75 }
76 
77 
78 namespace kernels{
79 
80 ///\brief Well known GEneral Matrix-Matrix product kernel M+=alpha*E1*E2.
81 ///
82 /// If bindings are included and the matrix combination allow for a specific binding
83 /// to be applied, the binding is called automatically from {binding}/gemm.h
84 /// otherwise default/gemm.h is used which is fully implemented for all dense/sparse combinations.
85 /// if a combination is optimized, bindings::has_optimized_gemm<M,E1,E2>::type evaluates to std::true_type
86 /// The kernels themselves are implemented in bindings::gemm.
87 template<class M, class E1, class E2>
88 void gemm(
89  matrix_expression<E1, cpu_tag> const& e1,
90  matrix_expression<E2, cpu_tag> const& e2,
91  matrix_expression<M, cpu_tag>& m,
92  typename M::value_type alpha
93 ) {
94  REMORA_SIZE_CHECK(m().size1() == e1().size1());
95  REMORA_SIZE_CHECK(m().size2() == e2().size2());
96  REMORA_SIZE_CHECK(e1().size2() == e2().size1());
97 
98  typedef typename M::orientation ResultOrientation;
99  typedef typename E1::orientation E1Orientation;
100  typedef typename E2::orientation E2Orientation;
101  typedef typename E1::evaluation_category::tag E1Tag;
102  typedef typename E2::evaluation_category::tag E2Tag;
103 
104  bindings::gemm(e1, e2, m ,alpha,
105  ResultOrientation(), E1Orientation(), E2Orientation(),
106  E1Tag(),E2Tag()
107  );
108 }
109 
110 }}
111 
112 #ifdef REMORA_USE_CLBLAST
113 #include "clBlast/gemm.hpp"
114 #elif REMORA_USE_GPU
115 #include "gpu/gemm.hpp"
116 #endif
117 #endif