SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
38 
39 #ifdef HAVE_VIENNACL
40 #include <shogun/lib/GPUMatrix.h>
41 #include <viennacl/linalg/prod.hpp>
42 #include <viennacl/matrix.hpp>
43 #endif // HAVE_VIENNACL
44 
45 namespace shogun
46 {
47 
48 namespace linalg
49 {
50 
51 namespace implementation
52 {
53 
57 template <enum Backend, class Matrix>
59 {
61  typedef typename Matrix::Scalar T;
62 
73  static void compute(Matrix A, Matrix B, Matrix C,
74  bool transpose_A, bool transpose_B, bool overwrite);
75 };
76 
77 
79 template <class Matrix>
80 struct matrix_product<Backend::EIGEN3, Matrix>
81 {
83  typedef typename Matrix::Scalar T;
84 
87 
90 
99  static ReturnType compute(SGMatrix<T> A, SGMatrix<T> B,
100  bool transpose_A, bool transpose_B)
101  {
102  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
103  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
104  REQUIRE(A.num_cols == B.num_rows, "Number of columns for A (%d) and "
105  "number of rows for B (%d) should be equal!\n", A.num_cols, B.num_rows);
106 
107  ReturnType retMatrix(A.num_rows, B.num_cols);
108  compute(A, B, retMatrix, transpose_A, transpose_B, true);
109 
110  return retMatrix;
111  }
112 
124  bool transpose_A, bool transpose_B, bool overwrite)
125  {
126  Eigen::Map<MatrixXt> A_eig = A;
127  Eigen::Map<MatrixXt> B_eig = B;
128  Eigen::Map<MatrixXt> C_eig = C;
129 
130  if (overwrite)
131  {
132  if (transpose_A && transpose_B)
133  C_eig = A_eig.transpose() * B_eig.transpose();
134 
135  else if (transpose_A)
136  C_eig = A_eig.transpose() * B_eig;
137 
138  else if (transpose_B)
139  C_eig = A_eig * B_eig.transpose();
140 
141  else
142  C_eig = A_eig * B_eig;
143  }
144  else
145  {
146  if (transpose_A && transpose_B)
147  C_eig += A_eig.transpose() * B_eig.transpose();
148 
149  else if (transpose_A)
150  C_eig += A_eig.transpose() * B_eig;
151 
152  else if (transpose_B)
153  C_eig += A_eig * B_eig.transpose();
154 
155  else
156  C_eig += A_eig * B_eig;
157  }
158  }
159 };
160 
161 #ifdef HAVE_VIENNACL
162 
164 template <class Matrix>
165 struct matrix_product<Backend::VIENNACL, Matrix>
166 {
168  typedef typename Matrix::Scalar T;
169 
171  typedef CGPUMatrix<T> ReturnType;
172 
181  static ReturnType compute(CGPUMatrix<T> A, CGPUMatrix<T> B,
182  bool transpose_A, bool transpose_B)
183  {
184  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
185  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
186  REQUIRE(A.num_cols == B.num_rows, "Number of columns for A (%d) and "
187  "number of rows for B (%d) should be equal!\n", A.num_cols, B.num_rows);
188 
189  ReturnType retMatrix(A.num_rows, B.num_cols);
190  compute(A, B, retMatrix, transpose_A, transpose_B, true);
191 
192  return retMatrix;
193  }
194 
205  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
206  bool transpose_A, bool transpose_B, bool overwrite)
207  {
208  if (overwrite)
209  {
210  if (transpose_A && transpose_B)
211  C.vcl_matrix() = viennacl::linalg::prod(
212  viennacl::trans(A.vcl_matrix()), viennacl::trans(B.vcl_matrix()));
213 
214  else if (transpose_A)
215  C.vcl_matrix() = viennacl::linalg::prod(
216  viennacl::trans(A.vcl_matrix()), B.vcl_matrix());
217 
218  else if (transpose_B)
219  C.vcl_matrix() = viennacl::linalg::prod(
220  A.vcl_matrix(), viennacl::trans(B.vcl_matrix()));
221 
222  else
223  C.vcl_matrix() = viennacl::linalg::prod(A.vcl_matrix(), B.vcl_matrix());
224  }
225  else
226  {
227  if (transpose_A && transpose_B)
228  C.vcl_matrix() += viennacl::linalg::prod(
229  viennacl::trans(A.vcl_matrix()), viennacl::trans(B.vcl_matrix()));
230 
231  else if (transpose_A)
232  C.vcl_matrix() += viennacl::linalg::prod(
233  viennacl::trans(A.vcl_matrix()), B.vcl_matrix());
234 
235  else if (transpose_B)
236  C.vcl_matrix() += viennacl::linalg::prod(
237  A.vcl_matrix(), viennacl::trans(B.vcl_matrix()));
238 
239  else
240  C.vcl_matrix() += viennacl::linalg::prod(A.vcl_matrix(), B.vcl_matrix());
241  }
242  }
243 };
244 
245 #endif // HAVE_VIENNACL
246 
247 }
248 
249 }
250 
251 }
252 #endif // MATRIX_PRODUCT_IMPL_H_
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:376
index_t num_rows
Definition: SGMatrix.h:374
shogun matrix
static void compute(Matrix A, Matrix B, Matrix C, bool transpose_A, bool transpose_B, bool overwrite)
static ReturnType compute(SGMatrix< T > A, SGMatrix< T > B, bool transpose_A, bool transpose_B)
Definition: MatrixProduct.h:99
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:89
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, bool transpose_A, bool transpose_B, bool overwrite)

SHOGUN Machine Learning Toolbox - Documentation