SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
40 
41 #ifdef HAVE_VIENNACL
42 #include <shogun/lib/GPUMatrix.h>
43 #include <shogun/lib/GPUVector.h>
44 #include <viennacl/linalg/matrix_operations.hpp>
45 #include <viennacl/linalg/vector_operations.hpp>
46 #endif // HAVE_VIENNACL
47 
48 namespace shogun
49 {
50 
51 namespace linalg
52 {
53 
54 namespace implementation
55 {
56 
60 template <enum Backend, class Matrix>
61 struct add
62 {
64  typedef typename Matrix::Scalar T;
65 
74  static void compute(Matrix A, Matrix B, Matrix C, T alpha, T beta);
75 };
76 
80 template <class Matrix>
81 struct add<Backend::EIGEN3, Matrix>
82 {
84  typedef typename Matrix::Scalar T;
85 
88 
91 
100  static SGMatrix<T> compute(SGMatrix<T> A, SGMatrix<T> B, T alpha=1, T beta=1)
101  {
102  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
103  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
104 
105  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
106  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
107  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
108 
109  SGMatrix<T> C(A.num_rows, A.num_cols);
110  compute(A, B, C, alpha, beta);
111 
112  return C;
113  }
114 
124  T alpha, T beta)
125  {
126  Eigen::Map<MatrixXt> A_eig=A;
127  Eigen::Map<MatrixXt> B_eig=B;
128  Eigen::Map<MatrixXt> C_eig=C;
129 
130  C_eig=alpha*A_eig+beta*B_eig;
131  }
132 
141  static SGVector<T> compute(SGVector<T> A, SGVector<T> B, T alpha=1, T beta=1)
142  {
143  REQUIRE(A.vlen == B.vlen, "Vectors should have same length! "
144  "A(%d) vs B(%d)\n", A.vlen, B.vlen);
145 
146  SGVector<T> C(A.vlen);
147  compute(A, B, C, alpha, beta);
148 
149  return C;
150  }
151 
161  T alpha, T beta)
162  {
163  Eigen::Map<VectorXt> A_eig=A;
164  Eigen::Map<VectorXt> B_eig=B;
165  Eigen::Map<VectorXt> C_eig=C;
166 
167  C_eig=alpha*A_eig+beta*B_eig;
168  }
169 };
170 
171 #ifdef HAVE_VIENNACL
172 
176 template <class Matrix>
177 struct add<Backend::VIENNACL, Matrix>
178 {
180  typedef typename Matrix::Scalar T;
181 
190  static CGPUMatrix<T> compute(CGPUMatrix<T> A, CGPUMatrix<T> B, T alpha=1, T beta=1)
191  {
192  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
193  REQUIRE(B.matrix, "Matrix B is not initialized!\n");
194 
195  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
196  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
197  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
198 
199  CGPUMatrix<T> C(A.num_rows, A.num_cols);
200  compute(A, B, C, alpha, beta);
201 
202  return C;
203  }
204 
213  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
214  T alpha, T beta)
215  {
216  C.vcl_matrix()=alpha*A.vcl_matrix()+beta*B.vcl_matrix();
217  }
218 
227  static CGPUVector<T> compute(CGPUVector<T> A, CGPUVector<T> B, T alpha=1, T beta=1)
228  {
229  REQUIRE(A.vlen == B.vlen, "Vectors should have same length! "
230  "A(%d) vs B(%d)\n", A.vlen, B.vlen);
231 
232  CGPUVector<T> C(A.vlen);
233  compute(A, B, C, alpha, beta);
234 
235  return C;
236  }
237 
246  static void compute(CGPUVector<T> A, CGPUVector<T> B, CGPUVector<T> C,
247  T alpha, T beta)
248  {
249  C.vcl_vector()=alpha*A.vcl_vector()+beta*B.vcl_vector();
250  }
251 };
252 
253 #endif // HAVE_VIENNACL
254 
255 }
256 
257 }
258 
259 }
260 #endif // ADD_IMPL_H_
static SGVector< T > compute(SGVector< T > A, SGVector< T > B, T alpha=1, T beta=1)
Definition: Add.h:141
#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
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Add.h:87
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:66
Generic class which is specialized for different backends to perform addition.
Definition: Add.h:61
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
Definition: Add.h:90
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:160
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C, T alpha, T beta)
Definition: Add.h:123
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:100

SHOGUN Machine Learning Toolbox - Documentation