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
39#endif
40
41#include "../proxy_expressions.hpp"
42
43namespace remora{
44
45namespace 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 auto transposedM = trans(m);
69 typedef typename Orientation1::transposed_orientation transpO1;
70 typedef typename Orientation2::transposed_orientation transpO2;
71 gemm(trans(e2),trans(e1),transposedM,alpha,row_major(),transpO2(),transpO1(), Tag2(),Tag1());
72 }
73}
74
75
76namespace kernels{
77
78///\brief Well known GEneral Matrix-Matrix product kernel M+=alpha*E1*E2.
79///
80/// If bindings are included and the matrix combination allow for a specific binding
81/// to be applied, the binding is called automatically from {binding}/gemm.h
82/// otherwise default/gemm.h is used which is fully implemented for all dense/sparse combinations.
83/// if a combination is optimized, bindings::has_optimized_gemm<M,E1,E2>::type evaluates to std::true_type
84/// The kernels themselves are implemented in bindings::gemm.
85template<class M, class E1, class E2>
86void gemm(
87 matrix_expression<E1, cpu_tag> const& e1,
88 matrix_expression<E2, cpu_tag> const& e2,
89 matrix_expression<M, cpu_tag>& m,
90 typename M::value_type alpha
91) {
92 REMORA_SIZE_CHECK(m().size1() == e1().size1());
93 REMORA_SIZE_CHECK(m().size2() == e2().size2());
94 REMORA_SIZE_CHECK(e1().size2() == e2().size1());
95
96 typedef typename M::orientation ResultOrientation;
97 typedef typename E1::orientation E1Orientation;
98 typedef typename E2::orientation E2Orientation;
99 typedef typename E1::evaluation_category::tag E1Tag;
100 typedef typename E2::evaluation_category::tag E2Tag;
101
102 bindings::gemm(e1, e2, m ,alpha,
103 ResultOrientation(), E1Orientation(), E2Orientation(),
104 E1Tag(),E2Tag()
105 );
106}
107
108}}
109
110#ifdef REMORA_USE_CLBLAST
111#include "clBlast/gemm.hpp"
112#elif defined REMORA_USE_GPU
113#include "gpu/gemm.hpp"
114#endif
115#endif