SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <enum 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  for (decltype(operand.size()) i=0; i<operand.size(); ++i)
103  result.data()[i]=unary_op(operand.data()[i]);
104  }
105 };
106 
111 template <class Operand, class ReturnType, class UnaryOp>
112 struct elementwise_unary_operation<Backend::EIGEN3, Operand, ReturnType, UnaryOp>
113 {
115  using T = typename Operand::Scalar;
116 
118  using ST = typename UnaryOp::return_type;
119 
120 #ifdef HAVE_VIENNACL
121 
122  static_assert(std::is_same<SGMatrix<T>, Operand>::value
123  || std::is_same<SGVector<T>, Operand>::value,
124  "NATIVE backend not allowed for GPU operands! Use SGMatrix/SGVector "
125  "in order to use NATIVE or use VIENNACL backend instead.\n");
126 #endif // HAVE_VIENNACL
127 
135  static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
136  {
137  auto eigen_result=unary_op.compute_using_eigen3(operand);
138  std::copy(eigen_result.data(), eigen_result.data()+eigen_result.size(), result.data());
139  }
140 };
141 
142 #ifdef HAVE_VIENNACL
143 
149 template <class Operand, class UnaryOp>
150 struct elementwise_unary_operation<Backend::VIENNACL, Operand, Operand, UnaryOp>
151 {
153  using T = typename Operand::Scalar;
154 
156  static_assert(!std::is_same<T,complex128_t>::value,
157  "Complex numbers not supported!\n");
158 
160  static_assert(std::is_same<CGPUMatrix<T>, Operand>::value ||
161  std::is_same<CGPUVector<T>, Operand>::value,
162  "VIENNACL backend not allowed for CPU operands! Use CGPUMatrix/CGPUVector "
163  "in order to use VIENNACL or use NATIVE/EIGEN3 backend instead.\n");
164 
172  static void compute(Operand operand, Operand result, operations::ocl_operation unary_op)
173  {
174  const std::string operation=unary_op.get_operation();
175  std::hash<std::string> hash_fn;
176  const std::string hash=std::to_string(hash_fn(operation));
177  const std::string kernel_name="kernel_"+hash+"_"+ocl::get_type_string<T>();
178 
179  viennacl::ocl::kernel& kernel=
180  ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
181 
182  kernel.global_work_size(0, ocl::align_to_multiple_1d(operand.size()));
183 
184  viennacl::ocl::enqueue(kernel(operand.data(),
185  cl_int(operand.size()), cl_int(operand.offset),
186  result.data(), cl_int(result.offset)));
187  }
188 };
189 #endif // HAVE_VIENNACL
190 
191 }
192 
193 }
194 
195 }
196 #endif // ELEMENTWISE_OPERATION_H_
class ocl_operation for element-wise unary OpenCL operations for GPU-types (CGPUMatrix/CGPUVector).
shogun matrix
shogun vector
Template struct elementwise_unary_operation. This struct is specialized for computing element-wise op...
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation