ViennaCL - The Vienna Computing Library: viennacl/linalg/opencl/kernels/vector_element.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector_element.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_VECTOR_ELEMENT_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_VECTOR_ELEMENT_HPP
3
4 #include "viennacl/tools/tools.hpp"
5 #include "viennacl/ocl/kernel.hpp"
6 #include "viennacl/ocl/platform.hpp"
7 #include "viennacl/ocl/utils.hpp"
8
11 namespace viennacl
12 {
13 namespace linalg
14 {
15 namespace opencl
16 {
17 namespace kernels
18 {
19
21
22
23 //generate code for C = op1(A) * op2(B), where A, B, C can have different storage layouts and opX(D) = D or trans(D)
24 template <typename StringT>
25 void generate_vector_unary_element_ops(StringT & source, std::string const & numeric_string,
26 std::string const & funcname, std::string const & op, std::string const & op_name)
27 {
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") // abs() on unsigned does not work on MacOS X 10.6.8, so we use the identity:
35 {
36 source.append(" vec1[i*size1.y+size1.x] "); source.append(op); source.append(" vec2[i*size2.y+size2.x]; \n");
37 }
38 else
39 {
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");
41 }
42 source.append("} \n");
43 }
44
45 template <typename StringT>
46 void generate_vector_unary_element_ops(StringT & source, std::string const & numeric_string, std::string const & funcname)
47 {
48 generate_vector_unary_element_ops(source, numeric_string, funcname, "=", "assign");
49 //generate_vector_unary_element_ops(source, numeric_string, funcname, "+=", "plus");
50 //generate_vector_unary_element_ops(source, numeric_string, funcname, "-=", "minus");
51 }
52
53 template <typename StringT>
54 void generate_vector_binary_element_ops(StringT & source, std::string const & numeric_string, int op_type) //op_type: {0: product, 1: division, 2: power}
55 {
56 std::string kernel_name_suffix;
57 if (op_type == 0)
58 kernel_name_suffix = "prod";
59 else if (op_type == 1)
60 kernel_name_suffix = "div";
61 else
62 kernel_name_suffix = "pow";
63
64 // generic kernel for the vector operation v1 = alpha * v2 + beta * v3, where v1, v2, v3 are not necessarily distinct vectors
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");
70
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");
74
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");
78
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");
82 if (op_type == 0)
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");
88
89 source.append("} \n");
90 }
91
93
94 // main kernel class
96 template<typename NumericT>
97 struct vector_element
98 {
99 static std::string program_name()
100 {
101 return viennacl::ocl::type_to_string<NumericT>::apply() + "_vector_element";
102 }
103
104 static void init(viennacl::ocl::context & ctx)
105 {
106 viennacl::ocl::DOUBLE_PRECISION_CHECKER<NumericT>::apply(ctx);
107 std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
108
109 static std::map<cl_context, bool> init_done;
110 if (!init_done[ctx.handle().get()])
111 {
112 std::string source;
113 source.reserve(8192);
114
115 viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
116
117 // unary operations
118 if (numeric_string == "float" || numeric_string == "double")
119 {
120 generate_vector_unary_element_ops(source, numeric_string, "acos");
121 generate_vector_unary_element_ops(source, numeric_string, "asin");
122 generate_vector_unary_element_ops(source, numeric_string, "atan");
123 generate_vector_unary_element_ops(source, numeric_string, "ceil");
124 generate_vector_unary_element_ops(source, numeric_string, "cos");
125 generate_vector_unary_element_ops(source, numeric_string, "cosh");
126 generate_vector_unary_element_ops(source, numeric_string, "exp");
127 generate_vector_unary_element_ops(source, numeric_string, "fabs");
128 generate_vector_unary_element_ops(source, numeric_string, "floor");
129 generate_vector_unary_element_ops(source, numeric_string, "log");
130 generate_vector_unary_element_ops(source, numeric_string, "log10");
131 generate_vector_unary_element_ops(source, numeric_string, "sin");
132 generate_vector_unary_element_ops(source, numeric_string, "sinh");
133 generate_vector_unary_element_ops(source, numeric_string, "sqrt");
134 generate_vector_unary_element_ops(source, numeric_string, "tan");
135 generate_vector_unary_element_ops(source, numeric_string, "tanh");
136 }
137 else
138 {
139 generate_vector_unary_element_ops(source, numeric_string, "abs");
140 }
141
142 // binary operations
143 generate_vector_binary_element_ops(source, numeric_string, 0);
144 generate_vector_binary_element_ops(source, numeric_string, 1);
145 if (numeric_string == "float" || numeric_string == "double")
146 generate_vector_binary_element_ops(source, numeric_string, 2);
147
148 std::string prog_name = program_name();
149 #ifdef VIENNACL_BUILD_INFO
150 std::cout << "Creating program " << prog_name << std::endl;
151 #endif
152 ctx.add_program(source, prog_name);
153 init_done[ctx.handle().get()] = true;
154 } //if
155 } //init
156 };
157
158 } // namespace kernels
159 } // namespace opencl
160 } // namespace linalg
161 } // namespace viennacl
162 #endif
163
platform.hpp
Implements a OpenCL platform within ViennaCL.
viennacl::linalg::opencl::kernels::vector_element::program_name
static std::string program_name()
Definition: vector_element.hpp:99
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
utils.hpp
Provides OpenCL-related utilities.
viennacl::ocl::context::handle
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Definition: context.hpp:611
viennacl::ocl::DOUBLE_PRECISION_CHECKER::apply
static void apply(viennacl::ocl::context const &)
Definition: utils.hpp:40
viennacl::ocl::handle::get
const OCL_TYPE & get() const
Definition: handle.hpp:191
viennacl::linalg::opencl::kernels::generate_vector_unary_element_ops
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)
Definition: vector_element.hpp:25
viennacl::linalg::opencl::kernels::vector_element
Main kernel class for generating OpenCL kernels for elementwise operations other than addition and su...
Definition: vector_element.hpp:97
kernel.hpp
Representation of an OpenCL kernel in ViennaCL.
viennacl::ocl::type_to_string
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
viennacl::linalg::opencl::kernels::generate_vector_binary_element_ops
void generate_vector_binary_element_ops(StringT &source, std::string const &numeric_string, int op_type)
Definition: vector_element.hpp:54
viennacl::linalg::opencl::kernels::vector_element::init
static void init(viennacl::ocl::context &ctx)
Definition: vector_element.hpp:104