SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
MatrixProduct.h
浏览该文件的文档.
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 <class Matrix>
83 struct matrix_product<Backend::EIGEN3, Matrix>
84 {
86  typedef typename Matrix::Scalar T;
87 
90 
93 
102  static ReturnType compute(SGMatrix<T> A, SGMatrix<T> B,
103  bool transpose_A, bool transpose_B)
104  {
105  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
106  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
107  REQUIRE(A.num_cols == B.num_rows, "Number of columns for A (%d) and "
108  "number of rows for B (%d) should be equal!\n", A.num_cols, B.num_rows);
109 
110  ReturnType retMatrix(A.num_rows, B.num_cols);
111  compute(A, B, retMatrix, transpose_A, transpose_B, true);
112 
113  return retMatrix;
114  }
115 
127  bool transpose_A, bool transpose_B, bool overwrite)
128  {
129  Eigen::Map<MatrixXt> A_eig = A;
130  Eigen::Map<MatrixXt> B_eig = B;
131  Eigen::Map<MatrixXt> C_eig = C;
132 
133  if (overwrite)
134  {
135  if (transpose_A && transpose_B)
136  C_eig = A_eig.transpose() * B_eig.transpose();
137 
138  else if (transpose_A)
139  C_eig = A_eig.transpose() * B_eig;
140 
141  else if (transpose_B)
142  C_eig = A_eig * B_eig.transpose();
143 
144  else
145  C_eig = A_eig * B_eig;
146  }
147  else
148  {
149  if (transpose_A && transpose_B)
150  C_eig += A_eig.transpose() * B_eig.transpose();
151 
152  else if (transpose_A)
153  C_eig += A_eig.transpose() * B_eig;
154 
155  else if (transpose_B)
156  C_eig += A_eig * B_eig.transpose();
157 
158  else
159  C_eig += A_eig * B_eig;
160  }
161  }
162 };
163 #endif // HAVE_EIGEN3
164 
165 #ifdef HAVE_VIENNACL
166 
168 template <class Matrix>
169 struct matrix_product<Backend::VIENNACL, Matrix>
170 {
172  typedef typename Matrix::Scalar T;
173 
175  typedef CGPUMatrix<T> ReturnType;
176 
185  static ReturnType compute(CGPUMatrix<T> A, CGPUMatrix<T> B,
186  bool transpose_A, bool transpose_B)
187  {
188  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
189  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
190  REQUIRE(A.num_cols == B.num_rows, "Number of columns for A (%d) and "
191  "number of rows for B (%d) should be equal!\n", A.num_cols, B.num_rows);
192 
193  ReturnType retMatrix(A.num_rows, B.num_cols);
194  compute(A, B, retMatrix, transpose_A, transpose_B, true);
195 
196  return retMatrix;
197  }
198 
209  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
210  bool transpose_A, bool transpose_B, bool overwrite)
211  {
212  if (overwrite)
213  {
214  if (transpose_A && transpose_B)
215  C.vcl_matrix() = viennacl::linalg::prod(
216  viennacl::trans(A.vcl_matrix()), viennacl::trans(B.vcl_matrix()));
217 
218  else if (transpose_A)
219  C.vcl_matrix() = viennacl::linalg::prod(
220  viennacl::trans(A.vcl_matrix()), B.vcl_matrix());
221 
222  else if (transpose_B)
223  C.vcl_matrix() = viennacl::linalg::prod(
224  A.vcl_matrix(), viennacl::trans(B.vcl_matrix()));
225 
226  else
227  C.vcl_matrix() = viennacl::linalg::prod(A.vcl_matrix(), B.vcl_matrix());
228  }
229  else
230  {
231  if (transpose_A && transpose_B)
232  C.vcl_matrix() += viennacl::linalg::prod(
233  viennacl::trans(A.vcl_matrix()), viennacl::trans(B.vcl_matrix()));
234 
235  else if (transpose_A)
236  C.vcl_matrix() += viennacl::linalg::prod(
237  viennacl::trans(A.vcl_matrix()), B.vcl_matrix());
238 
239  else if (transpose_B)
240  C.vcl_matrix() += viennacl::linalg::prod(
241  A.vcl_matrix(), viennacl::trans(B.vcl_matrix()));
242 
243  else
244  C.vcl_matrix() += viennacl::linalg::prod(A.vcl_matrix(), B.vcl_matrix());
245  }
246  }
247 };
248 
249 #endif // HAVE_VIENNACL
250 
251 }
252 
253 }
254 
255 }
256 #endif // MATRIX_PRODUCT_IMPL_H_
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
index_t num_rows
Definition: SGMatrix.h:376
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)
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:92
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, bool transpose_A, bool transpose_B, bool overwrite)

SHOGUN 机器学习工具包 - 项目文档