SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MatrixProduct.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Khaled Nasr
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
31 #ifndef MATRIX_PRODUCT_IMPL_H_
32 #define MATRIX_PRODUCT_IMPL_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
36 
37 #ifdef HAVE_EIGEN3
39 #endif // HAVE_EIGEN3
40 
41 #ifdef HAVE_VIENNACL
42 #include <shogun/lib/GPUMatrix.h>
43 #include <viennacl/linalg/prod.hpp>
44 #include <viennacl/matrix.hpp>
45 #endif // HAVE_VIENNACL
46 
47 namespace shogun
48 {
49 
50 namespace linalg
51 {
52 
53 namespace implementation
54 {
55 
59 template <enum Backend, class Matrix>
61 {
63  typedef typename Matrix::Scalar T;
64 
75  static void compute(Matrix A, Matrix B, Matrix C,
76  bool transpose_A, bool transpose_B, bool overwrite);
77 };
78 
79 #ifdef HAVE_EIGEN3
80 
82 template <> template <class Matrix>
83 struct matrix_product<Backend::EIGEN3, Matrix>
84 {
85  typedef typename Matrix::Scalar T;
87 
99  bool transpose_A, bool transpose_B, bool overwrite)
100  {
101  Eigen::Map<MatrixXt> A_eig = A;
102  Eigen::Map<MatrixXt> B_eig = B;
103  Eigen::Map<MatrixXt> C_eig = C;
104 
105  if (overwrite)
106  {
107  if (transpose_A && transpose_B)
108  C_eig = A_eig.transpose() * B_eig.transpose();
109 
110  else if (transpose_A)
111  C_eig = A_eig.transpose() * B_eig;
112 
113  else if (transpose_B)
114  C_eig = A_eig * B_eig.transpose();
115 
116  else
117  C_eig = A_eig * B_eig;
118  }
119  else
120  {
121  if (transpose_A && transpose_B)
122  C_eig += A_eig.transpose() * B_eig.transpose();
123 
124  else if (transpose_A)
125  C_eig += A_eig.transpose() * B_eig;
126 
127  else if (transpose_B)
128  C_eig += A_eig * B_eig.transpose();
129 
130  else
131  C_eig += A_eig * B_eig;
132  }
133  }
134 };
135 #endif // HAVE_EIGEN3
136 
137 #ifdef HAVE_VIENNACL
138 
140 template <> template <class Matrix>
141 struct matrix_product<Backend::VIENNACL, Matrix>
142 {
143  typedef typename Matrix::Scalar T;
144 
155  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
156  bool transpose_A, bool transpose_B, bool overwrite)
157  {
158  if (overwrite)
159  {
160  if (transpose_A && transpose_B)
161  C.vcl_matrix() = viennacl::linalg::prod(
162  viennacl::trans(A.vcl_matrix()), viennacl::trans(B.vcl_matrix()));
163 
164  else if (transpose_A)
165  C.vcl_matrix() = viennacl::linalg::prod(
166  viennacl::trans(A.vcl_matrix()), B.vcl_matrix());
167 
168  else if (transpose_B)
169  C.vcl_matrix() = viennacl::linalg::prod(
170  A.vcl_matrix(), viennacl::trans(B.vcl_matrix()));
171 
172  else
173  C.vcl_matrix() = viennacl::linalg::prod(A.vcl_matrix(), B.vcl_matrix());
174  }
175  else
176  {
177  if (transpose_A && transpose_B)
178  C.vcl_matrix() += viennacl::linalg::prod(
179  viennacl::trans(A.vcl_matrix()), viennacl::trans(B.vcl_matrix()));
180 
181  else if (transpose_A)
182  C.vcl_matrix() += viennacl::linalg::prod(
183  viennacl::trans(A.vcl_matrix()), B.vcl_matrix());
184 
185  else if (transpose_B)
186  C.vcl_matrix() += viennacl::linalg::prod(
187  A.vcl_matrix(), viennacl::trans(B.vcl_matrix()));
188 
189  else
190  C.vcl_matrix() += viennacl::linalg::prod(A.vcl_matrix(), B.vcl_matrix());
191  }
192  }
193 };
194 
195 #endif // HAVE_VIENNACL
196 
197 }
198 
199 }
200 
201 }
202 #endif // MATRIX_PRODUCT_IMPL_H_
shogun matrix
Definition: Parameter.h:26
static void compute(Matrix A, Matrix B, Matrix C, bool transpose_A, bool transpose_B, bool overwrite)
void matrix_product(Matrix A, Matrix B, Matrix C, bool transpose_A=false, bool transpose_B=false, bool overwrite=true)
Definition: Core.h:58
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: MatrixProduct.h:86
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, bool transpose_A, bool transpose_B, bool overwrite)
Definition: MatrixProduct.h:98

SHOGUN Machine Learning Toolbox - Documentation