SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Add.h
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Khaled Nasr
4  * Written (w) 2015 Sanuj Sharma
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #ifndef ADD_IMPL_H_
33 #define ADD_IMPL_H_
34 
35 #include <shogun/lib/config.h>
36 #include <shogun/lib/SGMatrix.h>
37 #include <shogun/lib/SGVector.h>
38 
39 #ifdef HAVE_EIGEN3
41 #endif // HAVE_EIGEN3
42 
43 #ifdef HAVE_VIENNACL
44 #include <shogun/lib/GPUMatrix.h>
45 #include <shogun/lib/GPUVector.h>
46 #include <viennacl/linalg/matrix_operations.hpp>
47 #include <viennacl/linalg/vector_operations.hpp>
48 #endif // HAVE_VIENNACL
49 
50 namespace shogun
51 {
52 
53 namespace linalg
54 {
55 
56 namespace implementation
57 {
58 
62 template <enum Backend, class Matrix>
63 struct add
64 {
66  typedef typename Matrix::Scalar T;
67 
76  static void compute(Matrix A, Matrix B, Matrix C, T alpha, T beta);
77 };
78 
82 template <class Matrix>
83 struct add<Backend::NATIVE, Matrix>
84 {
86  typedef typename Matrix::Scalar T;
87 
96  static SGMatrix<T> compute(SGMatrix<T> A, SGMatrix<T> B, T alpha=1, T beta=1)
97  {
98  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
99  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
100 
101  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
102  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
103  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
104 
105  SGMatrix<T> C(A.num_rows, A.num_cols);
106  compute(A.matrix, B.matrix, C.matrix, alpha, beta, A.num_rows*A.num_cols);
107 
108  return C;
109  }
110 
120  T alpha=1, T beta=1)
121  {
122  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
123  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
124  REQUIRE(C.matrix, "Matrix C is not initialized!\n");
125 
126  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
127  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
128  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
129  REQUIRE(A.num_rows == C.num_rows && A.num_cols == C.num_cols,
130  "Dimension mismatch! A(%d x %d) vs C(%d x %d)\n",
131  A.num_rows, A.num_cols, C.num_rows, C.num_cols);
132  compute(A.matrix, B.matrix, C.matrix, alpha, beta, A.num_rows*A.num_cols);
133  }
134 
143  static SGVector<T> compute(SGVector<T> A, SGVector<T> B, T alpha=1, T beta=1)
144  {
145  REQUIRE(A.vlen == B.vlen, "Vectors should have same length! "
146  "A(%d) vs B(%d)\n", A.vlen, B.vlen);
147 
148  SGVector<T> C(A.vlen);
149  compute(A.vector, B.vector, C.vector, alpha, beta, A.vlen);
150 
151  return C;
152  }
153 
163  T alpha=1, T beta=1)
164  {
165  REQUIRE(A.vlen == B.vlen, "Vectors should have same length! "
166  "A(%d) vs B(%d)\n", A.vlen, B.vlen);
167  REQUIRE(A.vlen == C.vlen, "Vectors should have same length! "
168  "A(%d) vs C(%d)\n", A.vlen, C.vlen);
169 
170  compute(A.vector, B.vector, C.vector, alpha, beta, A.vlen);
171  }
172 
182  static void compute(T* A, T* B, T* C, T alpha, T beta, index_t len)
183  {
184  for (int32_t i=0; i<len; i++)
185  C[i]=alpha*A[i]+beta*B[i];
186  }
187 };
188 
189 #ifdef HAVE_EIGEN3
190 
194 template <class Matrix>
195 struct add<Backend::EIGEN3, Matrix>
196 {
198  typedef typename Matrix::Scalar T;
199 
202 
205 
214  static SGMatrix<T> compute(SGMatrix<T> A, SGMatrix<T> B, T alpha=1, T beta=1)
215  {
216  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
217  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
218 
219  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
220  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
221  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
222 
223  SGMatrix<T> C(A.num_rows, A.num_cols);
224  compute(A, B, C, alpha, beta);
225 
226  return C;
227  }
228 
238  T alpha, T beta)
239  {
240  Eigen::Map<MatrixXt> A_eig=A;
241  Eigen::Map<MatrixXt> B_eig=B;
242  Eigen::Map<MatrixXt> C_eig=C;
243 
244  C_eig=alpha*A_eig+beta*B_eig;
245  }
246 
255  static SGVector<T> compute(SGVector<T> A, SGVector<T> B, T alpha=1, T beta=1)
256  {
257  REQUIRE(A.vlen == B.vlen, "Vectors should have same length! "
258  "A(%d) vs B(%d)\n", A.vlen, B.vlen);
259 
260  SGVector<T> C(A.vlen);
261  compute(A, B, C, alpha, beta);
262 
263  return C;
264  }
265 
275  T alpha, T beta)
276  {
277  Eigen::Map<VectorXt> A_eig=A;
278  Eigen::Map<VectorXt> B_eig=B;
279  Eigen::Map<VectorXt> C_eig=C;
280 
281  C_eig=alpha*A_eig+beta*B_eig;
282  }
283 };
284 #endif // HAVE_EIGEN3
285 
286 #ifdef HAVE_VIENNACL
287 
291 template <class Matrix>
292 struct add<Backend::VIENNACL, Matrix>
293 {
295  typedef typename Matrix::Scalar T;
296 
305  static CGPUMatrix<T> compute(CGPUMatrix<T> A, CGPUMatrix<T> B, T alpha=1, T beta=1)
306  {
307  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
308  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
309 
310  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
311  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
312  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
313 
314  CGPUMatrix<T> C(A.num_rows, A.num_cols);
315  compute(A, B, C, alpha, beta);
316 
317  return C;
318  }
319 
328  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
329  T alpha, T beta)
330  {
331  C.vcl_matrix()=alpha*A.vcl_matrix()+beta*B.vcl_matrix();
332  }
333 
342  static CGPUVector<T> compute(CGPUVector<T> A, CGPUVector<T> B, T alpha=1, T beta=1)
343  {
344  REQUIRE(A.vlen == B.vlen, "Vectors should have same length! "
345  "A(%d) vs B(%d)\n", A.vlen, B.vlen);
346 
347  CGPUVector<T> C(A.vlen);
348  compute(A, B, C, alpha, beta);
349 
350  return C;
351  }
352 
361  static void compute(CGPUVector<T> A, CGPUVector<T> B, CGPUVector<T> C,
362  T alpha, T beta)
363  {
364  C.vcl_vector()=alpha*A.vcl_vector()+beta*B.vcl_vector();
365  }
366 };
367 
368 #endif // HAVE_VIENNACL
369 
370 }
371 
372 }
373 
374 }
375 #endif // ADD_IMPL_H_
static SGMatrix< T > compute(SGMatrix< T > A, SGMatrix< T > B, T alpha=1, T beta=1)
Definition: Add.h:96
static SGVector< T > compute(SGVector< T > A, SGVector< T > B, T alpha=1, T beta=1)
Definition: Add.h:255
int32_t index_t
Definition: common.h:62
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
static void compute(SGVector< T > A, SGVector< T > B, SGVector< T > C, T alpha=1, T beta=1)
Definition: Add.h:162
index_t num_rows
Definition: SGMatrix.h:376
shogun matrix
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Add.h:201
index_t vlen
Definition: SGVector.h:494
shogun vector
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:65
Generic class which is specialized for different backends to perform addition.
Definition: Add.h:63
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
Definition: Add.h:204
static void compute(Matrix A, Matrix B, Matrix C, T alpha, T beta)
static void compute(SGVector< T > A, SGVector< T > B, SGVector< T > C, T alpha, T beta)
Definition: Add.h:274
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, T alpha, T beta)
Definition: Add.h:237
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static SGMatrix< T > compute(SGMatrix< T > A, SGMatrix< T > B, T alpha=1, T beta=1)
Definition: Add.h:214
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, T alpha=1, T beta=1)
Definition: Add.h:119
static void compute(T *A, T *B, T *C, T alpha, T beta, index_t len)
Definition: Add.h:182
static SGVector< T > compute(SGVector< T > A, SGVector< T > B, T alpha=1, T beta=1)
Definition: Add.h:143

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