31 #ifndef ELEMENTWISE_OPERATION_H_ 32 #define ELEMENTWISE_OPERATION_H_ 34 #include <shogun/lib/config.h> 42 #endif // HAVE_VIENNACL 45 #include <type_traits> 53 namespace implementation
61 template <Backend backend,
class Operand,
class ReturnType,
class UnaryOp>
70 template <
class Operand,
class ReturnType,
class UnaryOp>
74 using T =
typename Operand::Scalar;
77 using ST =
typename ReturnType::Scalar;
81 static_assert(std::is_same<
SGMatrix<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 95 static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
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");
101 #pragma omp parallel for 103 for (std::make_signed<decltype(operand.size())>::type i=0; i<operand.size(); ++i)
105 for (decltype(operand.size()) i=0; i<operand.size(); ++i)
107 result.data()[i]=unary_op(operand.data()[i]);
115 template <
class Operand,
class ReturnType,
class UnaryOp>
119 using T =
typename Operand::Scalar;
122 using ST =
typename UnaryOp::return_type;
126 static_assert(std::is_same<
SGMatrix<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 139 static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
141 auto eigen_result=unary_op.compute_using_eigen3(operand);
142 std::copy(eigen_result.data(), eigen_result.data()+eigen_result.size(), result.data());
153 template <
class Operand,
class UnaryOp>
157 using T =
typename Operand::Scalar;
160 static_assert(!std::is_same<T,complex128_t>::value,
161 "Complex numbers not supported!\n");
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");
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>();
183 viennacl::ocl::kernel& kernel=
184 ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
186 kernel.global_work_size(0, ocl::align_to_multiple_1d(operand.size()));
188 viennacl::ocl::enqueue(kernel(operand.data(),
189 cl_int(operand.size()), cl_int(operand.offset),
190 result.data(), cl_int(result.offset)));
193 #endif // HAVE_VIENNACL 200 #endif // ELEMENTWISE_OPERATION_H_
std::string get_operation() const
typename UnaryOp::return_type ST
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 ...
static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
typename Operand::Scalar T
Template struct elementwise_unary_operation. This struct is specialized for computing element-wise op...
typename ReturnType::Scalar ST
all of classes and functions are contained in the shogun namespace
static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
typename Operand::Scalar T