SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ElementwiseProduct.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 ELEMENTWISE_PRODUCT_IMPL_H_
32 #define ELEMENTWISE_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/matrix_operations.hpp>
42 #endif // HAVE_VIENNACL
43 
44 namespace shogun
45 {
46 
47 namespace linalg
48 {
49 
50 namespace implementation
51 {
52 
56 template <enum Backend, class Matrix>
58 {
60  typedef typename Matrix::Scalar T;
61 
63  static void compute(Matrix A, Matrix B, Matrix C);
64 };
65 
66 
68 template <class Matrix>
69 struct elementwise_product<Backend::EIGEN3, Matrix>
70 {
72  typedef typename Matrix::Scalar T;
73 
76 
79 
90  static ReturnType compute(SGMatrix<T> A, SGMatrix<T> B)
91  {
92  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
93  REQUIRE(B.matrix, "Matrix A is not initialized!\n");
94 
95  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
96  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
97  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
98 
99  ReturnType retMatrix(A.num_rows, A.num_cols);
100  compute(A, B, retMatrix);
101 
102  return retMatrix;
103  }
104 
118  {
119  Eigen::Map<MatrixXt> A_eig = A;
120  Eigen::Map<MatrixXt> B_eig = B;
121  Eigen::Map<MatrixXt> C_eig = C;
122 
123  C_eig = A_eig.array() * B_eig.array();
124  }
125 };
126 
127 #ifdef HAVE_VIENNACL
128 
130 template <class Matrix>
131 struct elementwise_product<Backend::VIENNACL, Matrix>
132 {
134  typedef typename Matrix::Scalar T;
135 
137  typedef CGPUMatrix<T> ReturnType;
138 
149  static ReturnType compute(CGPUMatrix<T> A, CGPUMatrix<T> B)
150  {
151  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
152  REQUIRE(B.matrix, "Matrix A is not initialized!\n");
153 
154  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
155  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
156  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
157 
158  ReturnType retMatrix(A.num_rows, A.num_cols);
159  compute(A, B, retMatrix);
160 
161  return retMatrix;
162  }
163 
176  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C)
177  {
178  C.vcl_matrix() = viennacl::linalg::element_prod(A.vcl_matrix(), B.vcl_matrix());
179  }
180 };
181 
182 #endif // HAVE_VIENNACL
183 
184 }
185 
186 }
187 
188 }
189 #endif // ELEMENTWISE_PRODUCT_IMPL_H_
static void compute(Matrix A, Matrix B, Matrix C)
#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
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C)

SHOGUN Machine Learning Toolbox - Documentation