1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_VECTOR_ELEMENT_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_VECTOR_ELEMENT_HPP
24 template <
typename StringT>
26 std::string
const & funcname, std::string
const & op, std::string
const & op_name)
28 source.append(
"__kernel void "); source.append(funcname); source.append(
"_"); source.append(op_name); source.append(
"(\n");
29 source.append(
" __global "); source.append(numeric_string); source.append(
" * vec1, \n");
30 source.append(
" uint4 size1, \n");
31 source.append(
" __global "); source.append(numeric_string); source.append(
" * vec2, \n");
32 source.append(
" uint4 size2) { \n");
33 source.append(
" for (unsigned int i = get_global_id(0); i < size1.z; i += get_global_size(0)) \n");
34 if (numeric_string[0] ==
'u' && funcname ==
"abs")
36 source.append(
" vec1[i*size1.y+size1.x] "); source.append(op); source.append(
" vec2[i*size2.y+size2.x]; \n");
40 source.append(
" vec1[i*size1.y+size1.x] "); source.append(op); source.append(
" "); source.append(funcname); source.append(
"(vec2[i*size2.y+size2.x]); \n");
42 source.append(
"} \n");
45 template <
typename StringT>
53 template <
typename StringT>
56 std::string kernel_name_suffix;
58 kernel_name_suffix =
"prod";
59 else if (op_type == 1)
60 kernel_name_suffix =
"div";
62 kernel_name_suffix =
"pow";
65 source.append(
"__kernel void element_" + kernel_name_suffix +
"(\n");
66 source.append(
" __global "); source.append(numeric_string); source.append(
" * vec1, \n");
67 source.append(
" unsigned int start1, \n");
68 source.append(
" unsigned int inc1, \n");
69 source.append(
" unsigned int size1, \n");
71 source.append(
" __global const "); source.append(numeric_string); source.append(
" * vec2, \n");
72 source.append(
" unsigned int start2, \n");
73 source.append(
" unsigned int inc2, \n");
75 source.append(
" __global const "); source.append(numeric_string); source.append(
" * vec3, \n");
76 source.append(
" unsigned int start3, \n");
77 source.append(
" unsigned int inc3, \n");
79 source.append(
" unsigned int op_type) \n");
80 source.append(
"{ \n");
81 source.append(
" for (unsigned int i = get_global_id(0); i < size1; i += get_global_size(0)) \n");
83 source.append(
" vec1[i*inc1+start1] = vec2[i*inc2+start2] * vec3[i*inc3+start3]; \n");
84 else if (op_type == 1)
85 source.append(
" vec1[i*inc1+start1] = vec2[i*inc2+start2] / vec3[i*inc3+start3]; \n");
86 else if (op_type == 2)
87 source.append(
" vec1[i*inc1+start1] = pow(vec2[i*inc2+start2], vec3[i*inc3+start3]); \n");
89 source.append(
"} \n");
96 template<
typename NumericT>
109 static std::map<cl_context, bool> init_done;
113 source.reserve(8192);
115 viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
118 if (numeric_string ==
"float" || numeric_string ==
"double")
145 if (numeric_string ==
"float" || numeric_string ==
"double")
149 #ifdef VIENNACL_BUILD_INFO
150 std::cout <<
"Creating program " << prog_name << std::endl;
152 ctx.add_program(source, prog_name);
153 init_done[ctx.handle().get()] =
true;
static std::string program_name()
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Provides OpenCL-related utilities.
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
static void apply(viennacl::ocl::context const &)
const OCL_TYPE & get() const
void generate_vector_unary_element_ops(StringT &source, std::string const &numeric_string, std::string const &funcname, std::string const &op, std::string const &op_name)
Main kernel class for generating OpenCL kernels for elementwise operations other than addition and su...
Representation of an OpenCL kernel in ViennaCL.
Helper class for converting a type to its string representation.
void generate_vector_binary_element_ops(StringT &source, std::string const &numeric_string, int op_type)
static void init(viennacl::ocl::context &ctx)