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]);
112 template <
class Operand,
class ReturnType,
class UnaryOp>
116 using T =
typename Operand::Scalar;
119 using ST =
typename UnaryOp::return_type;
123 static_assert(std::is_same<
SGMatrix<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
136 static void compute(Operand operand, ReturnType result, UnaryOp unary_op)
138 auto eigen_result=unary_op.compute_using_eigen3(operand);
139 std::copy(eigen_result.data(), eigen_result.data()+eigen_result.size(), result.data());
142 #endif // HAVE_EIGEN3
151 template <
class Operand,
class UnaryOp>
152 struct elementwise_unary_operation<Backend::VIENNACL, Operand, Operand, UnaryOp>
155 using T =
typename Operand::Scalar;
158 static_assert(!std::is_same<T,complex128_t>::value,
159 "Complex numbers not supported!\n");
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");
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>();
181 viennacl::ocl::kernel& kernel=
182 ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
184 kernel.global_work_size(0, ocl::align_to_multiple_1d(operand.size()));
186 viennacl::ocl::enqueue(kernel(operand.data(),
187 cl_int(operand.size()), cl_int(operand.offset),
188 result.data(), cl_int(result.offset)));
191 #endif // HAVE_VIENNACL
198 #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