31 #ifndef ELEMENTWISE_OPERATION_H_
32 #define ELEMENTWISE_OPERATION_H_
42 #endif // HAVE_VIENNACL
45 #include <type_traits>
53 namespace implementation
61 template <enum 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
102 for (decltype(operand.size()) i=0; i<operand.size(); ++i)
103 result.data()[i]=unary_op(operand.data()[i]);
111 template <
class Operand,
class ReturnType,
class UnaryOp>
115 using T =
typename Operand::Scalar;
118 using ST =
typename UnaryOp::return_type;
122 static_assert(std::is_same<
SGMatrix<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
135 static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
137 auto eigen_result=unary_op.compute_using_eigen3(operand);
138 std::copy(eigen_result.data(), eigen_result.data()+eigen_result.size(), result.data());
149 template <
class Operand,
class UnaryOp>
150 struct elementwise_unary_operation<Backend::VIENNACL, Operand, Operand, UnaryOp>
153 using T =
typename Operand::Scalar;
156 static_assert(!std::is_same<T,complex128_t>::value,
157 "Complex numbers not supported!\n");
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");
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>();
179 viennacl::ocl::kernel& kernel=
180 ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
182 kernel.global_work_size(0, ocl::align_to_multiple_1d(operand.size()));
184 viennacl::ocl::enqueue(kernel(operand.data(),
185 cl_int(operand.size()), cl_int(operand.offset),
186 result.data(), cl_int(result.offset)));
189 #endif // HAVE_VIENNACL
196 #endif // ELEMENTWISE_OPERATION_H_
typename UnaryOp::return_type ST
class ocl_operation for element-wise unary OpenCL operations for GPU-types (CGPUMatrix/CGPUVector).
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
std::string get_operation() const
static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
typename Operand::Scalar T