SHOGUN  4.0.0
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 <> template <class Matrix>
92 struct elementwise_square<Backend::EIGEN3,Matrix>
93 {
94  typedef typename Matrix::Scalar T;
96 
98 
107  {
108  SGMatrix<T> result(m.num_rows, m.num_cols);
109  compute(m, result);
110  return result;
111  }
112 
121  {
122  SGMatrix<T> result(b.m_row_size, b.m_col_size);
123  compute(b, result);
124  return result;
125  }
126 
133  static void compute(SGMatrix<T> mat, SGMatrix<T> result)
134  {
135  Eigen::Map<MatrixXt> m = mat;
136  Eigen::Map<MatrixXt> r = result;
137 
138  r = m.array().template square();
139  }
140 
147  static void compute(Block<SGMatrix<T> > b, SGMatrix<T> result)
148  {
149  Eigen::Map<MatrixXt> map = b.m_matrix;
150  Eigen::Map<MatrixXt> r = result;
151 
152  Eigen::Block< Eigen::Map<MatrixXt> > m = map.block(
153  b.m_row_begin, b.m_col_begin,
154  b.m_row_size, b.m_col_size);
155 
156  r = m.array().template square();
157  }
158 };
159 
160 #endif // HAVE_EIGEN3
161 
162 #ifdef HAVE_VIENNACL
163 
166 template <> template <class Matrix>
167 struct elementwise_square<Backend::VIENNACL,Matrix>
168 {
169  typedef typename Matrix::Scalar T;
170  typedef CGPUMatrix<T> ReturnType;
171 
172 
180  static CGPUMatrix<T> compute(CGPUMatrix<T> m)
181  {
182  CGPUMatrix<T> result(m.num_rows, m.num_cols);
183  compute(m, result);
184  return result;
185  }
186 
194  static CGPUMatrix<T> compute(Block<CGPUMatrix<T> > b)
195  {
196  SG_SERROR("The operation elementwise_square() on a matrix block is currently not supported\n");
197  return CGPUMatrix<T>();
198  }
199 
206  static void compute(CGPUMatrix<T> mat, CGPUMatrix<T> result)
207  {
208  const std::string operation = "return element*element;";
209 
210  std::string kernel_name = "elementwise_square_" + ocl::get_type_string<T>();
211  viennacl::ocl::kernel& kernel =
212  ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
213 
214  kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_rows*mat.num_cols));
215 
216  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
217  cl_int(mat.num_rows*mat.num_cols), cl_int(mat.offset),
218  result.vcl_matrix(), cl_int(result.offset)));
219  }
220 
227  static void compute(Block<SGMatrix<T> > b, SGMatrix<T> result)
228  {
229  SG_SERROR("The operation elementwise_square() on a matrix block is currently not supported\n");
230  }
231 };
232 
233 #endif // HAVE_VIENNACL
234 
235 }
236 
237 }
238 
239 }
240 #endif // ELEMENTWISESQUARE_IMPL_H_
Generic class square which provides a static compute method. This class is specialized for different ...
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
implementation::elementwise_square< backend, Matrix >::ReturnType elementwise_square(Matrix m)
Definition: Core.h:104
index_t num_rows
Definition: SGMatrix.h:329
index_t num_cols
Definition: SGMatrix.h:331
shogun matrix
Definition: Parameter.h:26
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