SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ElementwiseUnaryOperation.h
浏览该文件的文档.
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 
107 #ifdef HAVE_EIGEN3
108 
112 template <class Operand, class ReturnType, class UnaryOp>
113 struct elementwise_unary_operation<Backend::EIGEN3, Operand, ReturnType, UnaryOp>
114 {
116  using T = typename Operand::Scalar;
117 
119  using ST = typename UnaryOp::return_type;
120 
121 #ifdef HAVE_VIENNACL
122 
123  static_assert(std::is_same<SGMatrix<T>, Operand>::value
124  || std::is_same<SGVector<T>, Operand>::value,
125  "NATIVE backend not allowed for GPU operands! Use SGMatrix/SGVector "
126  "in order to use NATIVE or use VIENNACL backend instead.\n");
127 #endif // HAVE_VIENNACL
128 
136  static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
137  {
138  auto eigen_result=unary_op.compute_using_eigen3(operand);
139  std::copy(eigen_result.data(), eigen_result.data()+eigen_result.size(), result.data());
140  }
141 };
142 #endif // HAVE_EIGEN3
143 
144 #ifdef HAVE_VIENNACL
145 
151 template <class Operand, class UnaryOp>
152 struct elementwise_unary_operation<Backend::VIENNACL, Operand, Operand, UnaryOp>
153 {
155  using T = typename Operand::Scalar;
156 
158  static_assert(!std::is_same<T,complex128_t>::value,
159  "Complex numbers not supported!\n");
160 
162  static_assert(std::is_same<CGPUMatrix<T>, Operand>::value ||
163  std::is_same<CGPUVector<T>, Operand>::value,
164  "VIENNACL backend not allowed for CPU operands! Use CGPUMatrix/CGPUVector "
165  "in order to use VIENNACL or use NATIVE/EIGEN3 backend instead.\n");
166 
174  static void compute(Operand operand, Operand result, operations::ocl_operation unary_op)
175  {
176  const std::string operation=unary_op.get_operation();
177  std::hash<std::string> hash_fn;
178  const std::string hash=std::to_string(hash_fn(operation));
179  const std::string kernel_name="kernel_"+hash+"_"+ocl::get_type_string<T>();
180 
181  viennacl::ocl::kernel& kernel=
182  ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
183 
184  kernel.global_work_size(0, ocl::align_to_multiple_1d(operand.size()));
185 
186  viennacl::ocl::enqueue(kernel(operand.data(),
187  cl_int(operand.size()), cl_int(operand.offset),
188  result.data(), cl_int(result.offset)));
189  }
190 };
191 #endif // HAVE_VIENNACL
192 
193 }
194 
195 }
196 
197 }
198 #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 机器学习工具包 - 项目文档