SHOGUN  6.0.0
ElementwiseUnaryOperation.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 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 ELEMENTWISE_OPERATION_H_
32 #define ELEMENTWISE_OPERATION_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
36 
37 #ifdef HAVE_VIENNACL
38 #include <shogun/lib/GPUMatrix.h>
42 #endif // HAVE_VIENNACL
43 
44 #include <algorithm>
45 #include <type_traits>
46 
47 namespace shogun
48 {
49 
50 namespace linalg
51 {
52 
53 namespace implementation
54 {
55 
61 template <Backend backend, class Operand, class ReturnType, class UnaryOp>
63 {
64 };
65 
70 template <class Operand, class ReturnType, class UnaryOp>
71 struct elementwise_unary_operation<Backend::NATIVE, Operand, ReturnType, UnaryOp>
72 {
74  using T = typename Operand::Scalar;
75 
77  using ST = typename ReturnType::Scalar;
78 
79 #ifdef HAVE_VIENNACL
80 
81  static_assert(std::is_same<SGMatrix<T>, Operand>::value
82  || std::is_same<SGVector<T>, Operand>::value,
83  "NATIVE backend not allowed for GPU operands! Use SGMatrix/SGVector "
84  "in order to use NATIVE or use VIENNACL backend instead.\n");
85 #endif // HAVE_VIENNACL
86 
95  static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
96  {
97  static_assert(std::is_same<ST,decltype(unary_op(operand.data()[0]))>::value,
98  "The return type of the unary operator and the scalar types of the "
99  "result must be the same!\n");
100 
101 #pragma omp parallel for
102 #ifdef _WIN32
103  for (std::make_signed<decltype(operand.size())>::type i=0; i<operand.size(); ++i)
104 #else
105  for (decltype(operand.size()) i=0; i<operand.size(); ++i)
106 #endif
107  result.data()[i]=unary_op(operand.data()[i]);
108  }
109 };
110 
115 template <class Operand, class ReturnType, class UnaryOp>
116 struct elementwise_unary_operation<Backend::EIGEN3, Operand, ReturnType, UnaryOp>
117 {
119  using T = typename Operand::Scalar;
120 
122  using ST = typename UnaryOp::return_type;
123 
124 #ifdef HAVE_VIENNACL
125 
126  static_assert(std::is_same<SGMatrix<T>, Operand>::value
127  || std::is_same<SGVector<T>, Operand>::value,
128  "NATIVE backend not allowed for GPU operands! Use SGMatrix/SGVector "
129  "in order to use NATIVE or use VIENNACL backend instead.\n");
130 #endif // HAVE_VIENNACL
131 
139  static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
140  {
141  auto eigen_result=unary_op.compute_using_eigen3(operand);
142  std::copy(eigen_result.data(), eigen_result.data()+eigen_result.size(), result.data());
143  }
144 };
145 
146 #ifdef HAVE_VIENNACL
147 
153 template <class Operand, class UnaryOp>
154 struct elementwise_unary_operation<Backend::VIENNACL, Operand, Operand, UnaryOp>
155 {
157  using T = typename Operand::Scalar;
158 
160  static_assert(!std::is_same<T,complex128_t>::value,
161  "Complex numbers not supported!\n");
162 
164  static_assert(std::is_same<CGPUMatrix<T>, Operand>::value ||
165  std::is_same<CGPUVector<T>, Operand>::value,
166  "VIENNACL backend not allowed for CPU operands! Use CGPUMatrix/CGPUVector "
167  "in order to use VIENNACL or use NATIVE/EIGEN3 backend instead.\n");
168 
176  static void compute(Operand operand, Operand result, operations::ocl_operation unary_op)
177  {
178  const std::string operation=unary_op.get_operation();
179  std::hash<std::string> hash_fn;
180  const std::string hash=std::to_string(hash_fn(operation));
181  const std::string kernel_name="kernel_"+hash+"_"+ocl::get_type_string<T>();
182 
183  viennacl::ocl::kernel& kernel=
184  ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
185 
186  kernel.global_work_size(0, ocl::align_to_multiple_1d(operand.size()));
187 
188  viennacl::ocl::enqueue(kernel(operand.data(),
189  cl_int(operand.size()), cl_int(operand.offset),
190  result.data(), cl_int(result.offset)));
191  }
192 };
193 #endif // HAVE_VIENNACL
194 
195 }
196 
197 }
198 
199 }
200 #endif // ELEMENTWISE_OPERATION_H_
class ocl_operation for element-wise unary OpenCL operations for GPU-types (CGPUMatrix/CGPUVector).
Backend
All currently supported linear algebra backend libraries, with a default backend, which will be used ...
Definition: linalg.h:74
Template struct elementwise_unary_operation. This struct is specialized for computing element-wise op...
shogun vector
shogun matrix
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation