SHOGUN  4.2.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 
40 
41 #ifdef HAVE_VIENNACL
43 #include <shogun/lib/GPUMatrix.h>
44 #endif
45 
46 namespace shogun
47 {
48 
49 namespace linalg
50 {
51 
55 namespace implementation
56 {
57 
63 template <enum Backend,class Matrix>
65 {
73  static Matrix compute(Matrix m);
74 
82  static Matrix compute(Block<Matrix> b);
83 };
84 
88 template <class Matrix>
89 struct elementwise_square<Backend::EIGEN3,Matrix>
90 {
92  typedef typename Matrix::Scalar T;
93 
96 
99 
108  {
109  SGMatrix<T> result(m.num_rows, m.num_cols);
110  compute(m, result);
111  return result;
112  }
113 
122  {
123  SGMatrix<T> result(b.m_row_size, b.m_col_size);
124  compute(b, result);
125  return result;
126  }
127 
134  static void compute(SGMatrix<T> mat, SGMatrix<T> result)
135  {
136  Eigen::Map<MatrixXt> m = mat;
137  Eigen::Map<MatrixXt> r = result;
138 
139  r = m.array().template square();
140  }
141 
148  static void compute(Block<SGMatrix<T> > b, SGMatrix<T> result)
149  {
150  Eigen::Map<MatrixXt> map = b.m_matrix;
151  Eigen::Map<MatrixXt> r = result;
152 
153  Eigen::Block< Eigen::Map<MatrixXt> > m = map.block(
154  b.m_row_begin, b.m_col_begin,
155  b.m_row_size, b.m_col_size);
156 
157  r = m.array().template square();
158  }
159 };
160 
161 
162 #ifdef HAVE_VIENNACL
163 
166 template <class Matrix>
167 struct elementwise_square<Backend::VIENNACL,Matrix>
168 {
170  typedef typename Matrix::Scalar T;
171 
173  typedef CGPUMatrix<T> ReturnType;
174 
182  static CGPUMatrix<T> compute(CGPUMatrix<T> m)
183  {
184  CGPUMatrix<T> result(m.num_rows, m.num_cols);
185  compute(m, result);
186  return result;
187  }
188 
196  static CGPUMatrix<T> compute(Block<CGPUMatrix<T> > b)
197  {
198  SG_SERROR("The operation elementwise_square() on a matrix block is currently not supported\n");
199  return CGPUMatrix<T>();
200  }
201 
208  static void compute(CGPUMatrix<T> mat, CGPUMatrix<T> result)
209  {
210  const std::string operation = "return element*element;";
211 
212  std::string kernel_name = "elementwise_square_" + ocl::get_type_string<T>();
213  viennacl::ocl::kernel& kernel =
214  ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
215 
216  kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_rows*mat.num_cols));
217 
218  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
219  cl_int(mat.num_rows*mat.num_cols), cl_int(mat.offset),
220  result.vcl_matrix(), cl_int(result.offset)));
221  }
222 
229  static void compute(Block<SGMatrix<T> > b, SGMatrix<T> result)
230  {
231  SG_SERROR("The operation elementwise_square() on a matrix block is currently not supported\n");
232  }
233 };
234 
235 #endif // HAVE_VIENNACL
236 
237 }
238 
239 }
240 
241 }
242 #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:376
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:374
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