SHOGUN  4.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Add.h
Go to the documentation of this file.
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 
97  T alpha=1, T beta=1)
98  {
99  REQUIRE(A.num_rows == B.num_rows && B.num_rows == C.num_rows,
100  "Matrices should have same number of rows!\n");
101  REQUIRE(A.num_cols == B.num_cols && B.num_cols == C.num_cols,
102  "Matrices should have same number of columns!\n");
103  compute(A.matrix, B.matrix, C.matrix, alpha, beta, A.num_rows*A.num_cols);
104  }
105 
115  T alpha=1, T beta=1)
116  {
117  REQUIRE(A.vlen == B.vlen, "Vectors should have same length!\n");
118  compute(A.vector, B.vector, C.vector, alpha, beta, A.vlen);
119  }
120 
130  static void compute(T* A, T* B, T* C, T alpha, T beta, index_t len)
131  {
132  for (int32_t i=0; i<len; i++)
133  C[i]=alpha*A[i]+beta*B[i];
134  }
135 };
136 
137 #ifdef HAVE_EIGEN3
138 
142 template <class Matrix>
143 struct add<Backend::EIGEN3, Matrix>
144 {
146  typedef typename Matrix::Scalar T;
147 
150 
153 
163  T alpha, T beta)
164  {
165  Eigen::Map<MatrixXt> A_eig=A;
166  Eigen::Map<MatrixXt> B_eig=B;
167  Eigen::Map<MatrixXt> C_eig=C;
168 
169  C_eig=alpha*A_eig+beta*B_eig;
170  }
171 
181  T alpha, T beta)
182  {
183  Eigen::Map<VectorXt> A_eig=A;
184  Eigen::Map<VectorXt> B_eig=B;
185  Eigen::Map<VectorXt> C_eig=C;
186 
187  C_eig=alpha*A_eig+beta*B_eig;
188  }
189 };
190 #endif // HAVE_EIGEN3
191 
192 #ifdef HAVE_VIENNACL
193 
197 template <class Matrix>
198 struct add<Backend::VIENNACL, Matrix>
199 {
201  typedef typename Matrix::Scalar T;
202 
211  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
212  T alpha, T beta)
213  {
214  C.vcl_matrix()=alpha*A.vcl_matrix()+beta*B.vcl_matrix();
215  }
216 
225  static void compute(CGPUVector<T> A, CGPUVector<T> B, CGPUVector<T> C,
226  T alpha, T beta)
227  {
228  C.vcl_vector()=alpha*A.vcl_vector()+beta*B.vcl_vector();
229  }
230 };
231 
232 #endif // HAVE_VIENNACL
233 
234 }
235 
236 }
237 
238 }
239 #endif // ADD_IMPL_H_
int32_t index_t
Definition: common.h:62
#define REQUIRE(x,...)
Definition: SGIO.h:206
static void compute(SGVector< T > A, SGVector< T > B, SGVector< T > C, T alpha=1, T beta=1)
Definition: Add.h:114
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Add.h:149
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:58
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:152
static void compute(Matrix A, Matrix B, Matrix C, T alpha, T beta)
index_t num_rows
Definition: SGMatrix.h:329
shogun vector
Definition: Parameter.h:28
static void compute(SGVector< T > A, SGVector< T > B, SGVector< T > C, T alpha, T beta)
Definition: Add.h:180
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, T alpha, T beta)
Definition: Add.h:162
index_t num_cols
Definition: SGMatrix.h:331
shogun matrix
Definition: Parameter.h:26
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, T alpha=1, T beta=1)
Definition: Add.h:96
static void compute(T *A, T *B, T *C, T alpha, T beta, index_t len)
Definition: Add.h:130
index_t vlen
Definition: SGVector.h:481

SHOGUN Machine Learning Toolbox - Documentation