SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ElementwiseSquare.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Soumyajit De
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 ELEMENTWISESQUARE_IMPL_H_
32 #define ELEMENTWISESQUARE_IMPL_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
37 #include <shogun/io/SGIO.h>
38 
39 #ifdef HAVE_EIGEN3
41 #endif // HAVE_EIGEN3
42 
43 #ifdef HAVE_VIENNACL
45 #include <shogun/lib/GPUMatrix.h>
46 #endif
47 
48 namespace shogun
49 {
50 
51 namespace linalg
52 {
53 
57 namespace implementation
58 {
59 
65 template <enum Backend,class Matrix>
67 {
75  static Matrix compute(Matrix m);
76 
84  static Matrix compute(Block<Matrix> b);
85 };
86 
87 #ifdef HAVE_EIGEN3
88 
91 template <class Matrix>
92 struct elementwise_square<Backend::EIGEN3,Matrix>
93 {
95  typedef typename Matrix::Scalar T;
96 
99 
102 
111  {
112  SGMatrix<T> result(m.num_rows, m.num_cols);
113  compute(m, result);
114  return result;
115  }
116 
125  {
126  SGMatrix<T> result(b.m_row_size, b.m_col_size);
127  compute(b, result);
128  return result;
129  }
130 
137  static void compute(SGMatrix<T> mat, SGMatrix<T> result)
138  {
139  Eigen::Map<MatrixXt> m = mat;
140  Eigen::Map<MatrixXt> r = result;
141 
142  r = m.array().template square();
143  }
144 
151  static void compute(Block<SGMatrix<T> > b, SGMatrix<T> result)
152  {
153  Eigen::Map<MatrixXt> map = b.m_matrix;
154  Eigen::Map<MatrixXt> r = result;
155 
156  Eigen::Block< Eigen::Map<MatrixXt> > m = map.block(
157  b.m_row_begin, b.m_col_begin,
158  b.m_row_size, b.m_col_size);
159 
160  r = m.array().template square();
161  }
162 };
163 
164 #endif // HAVE_EIGEN3
165 
166 #ifdef HAVE_VIENNACL
167 
170 template <class Matrix>
171 struct elementwise_square<Backend::VIENNACL,Matrix>
172 {
174  typedef typename Matrix::Scalar T;
175 
177  typedef CGPUMatrix<T> ReturnType;
178 
186  static CGPUMatrix<T> compute(CGPUMatrix<T> m)
187  {
188  CGPUMatrix<T> result(m.num_rows, m.num_cols);
189  compute(m, result);
190  return result;
191  }
192 
200  static CGPUMatrix<T> compute(Block<CGPUMatrix<T> > b)
201  {
202  SG_SERROR("The operation elementwise_square() on a matrix block is currently not supported\n");
203  return CGPUMatrix<T>();
204  }
205 
212  static void compute(CGPUMatrix<T> mat, CGPUMatrix<T> result)
213  {
214  const std::string operation = "return element*element;";
215 
216  std::string kernel_name = "elementwise_square_" + ocl::get_type_string<T>();
217  viennacl::ocl::kernel& kernel =
218  ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
219 
220  kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_rows*mat.num_cols));
221 
222  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
223  cl_int(mat.num_rows*mat.num_cols), cl_int(mat.offset),
224  result.vcl_matrix(), cl_int(result.offset)));
225  }
226 
233  static void compute(Block<SGMatrix<T> > b, SGMatrix<T> result)
234  {
235  SG_SERROR("The operation elementwise_square() on a matrix block is currently not supported\n");
236  }
237 };
238 
239 #endif // HAVE_VIENNACL
240 
241 }
242 
243 }
244 
245 }
246 #endif // ELEMENTWISESQUARE_IMPL_H_
Generic class square which provides a static compute method. This class is specialized for different ...
index_t num_cols
Definition: SGMatrix.h:378
Generic class Block which wraps a matrix class and contains block specific information, providing a uniform way to deal with matrix blocks for all supported backend matrices.
Definition: Block.h:49
index_t num_rows
Definition: SGMatrix.h:376
shogun matrix
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SERROR(...)
Definition: SGIO.h:179
static void compute(Block< SGMatrix< T > > b, SGMatrix< T > result)

SHOGUN Machine Learning Toolbox - Documentation