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
matrix_element.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_MATRIX_ELEMENT_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_MATRIX_ELEMENT_HPP
3 
7 #include "viennacl/ocl/utils.hpp"
9 
12 namespace viennacl
13 {
14 namespace linalg
15 {
16 namespace opencl
17 {
18 namespace kernels
19 {
20 
22 
23 
24 //generate code for C = op1(A) * op2(B), where A, B, C can have different storage layouts and opX(D) = D or trans(D)
25 template <typename StringType>
26 void generate_matrix_unary_element_ops(StringType & source, std::string const & numeric_string,
27  std::string const & funcname, std::string const & op, std::string const & op_name, bool is_row_major)
28 {
29  source.append("__kernel void "); source.append(funcname); source.append("_"); source.append(op_name); source.append("(\n");
30  source.append(" __global "); source.append(numeric_string); source.append(" * A, \n");
31  source.append(" unsigned int A_start1, unsigned int A_start2, \n");
32  source.append(" unsigned int A_inc1, unsigned int A_inc2, \n");
33  source.append(" unsigned int A_size1, unsigned int A_size2, \n");
34  source.append(" unsigned int A_internal_size1, unsigned int A_internal_size2, \n");
35 
36  source.append(" __global const "); source.append(numeric_string); source.append(" * B, \n");
37  source.append(" unsigned int B_start1, unsigned int B_start2, \n");
38  source.append(" unsigned int B_inc1, unsigned int B_inc2, \n");
39  source.append(" unsigned int B_internal_size1, unsigned int B_internal_size2) { \n");
40 
41  if (is_row_major)
42  {
43  source.append(" unsigned int row_gid = get_global_id(0) / get_local_size(0); \n");
44  source.append(" unsigned int col_gid = get_global_id(0) % get_local_size(0); \n");
45 
46  source.append(" for (unsigned int row = row_gid; row < A_size1; row += get_num_groups(0)) \n");
47  source.append(" for (unsigned int col = col_gid; col < A_size2; col += get_local_size(0)) \n");
48  source.append(" A[(row * A_inc1 + A_start1) * A_internal_size2 + col * A_inc2 + A_start2] \n");
49  source.append(" "); source.append(op); source.append(" "); source.append(funcname); source.append("(B[(row * B_inc1 + B_start1) * B_internal_size2 + col * B_inc2 + B_start2]); \n");
50  }
51  else
52  {
53  source.append(" unsigned int row_gid = get_global_id(0) % get_local_size(0); \n");
54  source.append(" unsigned int col_gid = get_global_id(0) / get_local_size(0); \n");
55 
56  source.append(" for (unsigned int col = col_gid; col < A_size2; col += get_num_groups(0)) \n");
57  source.append(" for (unsigned int row = row_gid; row < A_size1; row += get_local_size(0)) \n");
58  source.append(" A[(row * A_inc1 + A_start1) + (col * A_inc2 + A_start2) * A_internal_size1] \n");
59  source.append(" "); source.append(op); source.append(" "); source.append(funcname); source.append("(B[(row * B_inc1 + B_start1) + (col * B_inc2 + B_start2) * B_internal_size1]); \n");
60  }
61  source.append("} \n");
62 }
63 
64 template <typename StringType>
65 void generate_matrix_unary_element_ops(StringType & source, std::string const & numeric_string, std::string const & funcname, bool is_row_major)
66 {
67  generate_matrix_unary_element_ops(source, numeric_string, funcname, "=", "assign", is_row_major);
68  //generate_matrix_unary_element_ops(source, numeric_string, funcname, "+=", "plus", is_row_major);
69  //generate_matrix_unary_element_ops(source, numeric_string, funcname, "-=", "minus", is_row_major);
70 }
71 
73 
74 // main kernel class
76 template <typename NumericT, typename F>
78 {
79  static std::string program_name()
80  {
82  }
83 
84  static void init(viennacl::ocl::context & ctx)
85  {
87  std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
88 
89  static std::map<cl_context, bool> init_done;
90  if (!init_done[ctx.handle().get()])
91  {
92  std::string source;
93  source.reserve(8192);
95 
96  viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
97 
98  // unary operations
99  if (numeric_string == "float" || numeric_string == "double")
100  {
101  generate_matrix_unary_element_ops(source, numeric_string, "acos", is_row_major);
102  generate_matrix_unary_element_ops(source, numeric_string, "asin", is_row_major);
103  generate_matrix_unary_element_ops(source, numeric_string, "atan", is_row_major);
104  generate_matrix_unary_element_ops(source, numeric_string, "ceil", is_row_major);
105  generate_matrix_unary_element_ops(source, numeric_string, "cos", is_row_major);
106  generate_matrix_unary_element_ops(source, numeric_string, "cosh", is_row_major);
107  generate_matrix_unary_element_ops(source, numeric_string, "exp", is_row_major);
108  generate_matrix_unary_element_ops(source, numeric_string, "fabs", is_row_major);
109  generate_matrix_unary_element_ops(source, numeric_string, "floor", is_row_major);
110  generate_matrix_unary_element_ops(source, numeric_string, "log", is_row_major);
111  generate_matrix_unary_element_ops(source, numeric_string, "log10", is_row_major);
112  generate_matrix_unary_element_ops(source, numeric_string, "sin", is_row_major);
113  generate_matrix_unary_element_ops(source, numeric_string, "sinh", is_row_major);
114  generate_matrix_unary_element_ops(source, numeric_string, "sqrt", is_row_major);
115  generate_matrix_unary_element_ops(source, numeric_string, "tan", is_row_major);
116  generate_matrix_unary_element_ops(source, numeric_string, "tanh", is_row_major);
117  }
118  else
119  {
120  generate_matrix_unary_element_ops(source, numeric_string, "abs", is_row_major);
121  }
122 
123  std::string prog_name = program_name();
124  #ifdef VIENNACL_BUILD_INFO
125  std::cout << "Creating program " << prog_name << std::endl;
126  #endif
127  ctx.add_program(source, prog_name);
128  init_done[ctx.handle().get()] = true;
129  } //if
130  } //init
131 };
132 
133 } // namespace kernels
134 } // namespace opencl
135 } // namespace linalg
136 } // namespace viennacl
137 #endif
138 
Implements a OpenCL platform within ViennaCL.
static void init(viennacl::ocl::context &ctx)
Helper class for checking whether a matrix has a row-major layout.
Definition: forwards.h:484
Various little tools used here and there in ViennaCL.
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
Provides OpenCL-related utilities.
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Definition: context.hpp:611
Main kernel class for generating OpenCL kernels for elementwise-operations such as element_sin() on/w...
static void apply(viennacl::ocl::context const &)
Definition: utils.hpp:40
const OCL_TYPE & get() const
Definition: handle.hpp:191
void generate_matrix_unary_element_ops(StringType &source, std::string const &numeric_string, std::string const &funcname, std::string const &op, std::string const &op_name, bool is_row_major)
Representation of an OpenCL kernel in ViennaCL.
std::string type_to_string(viennacl::row_major)
Definition: matrix.hpp:917
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
Runtime generation of OpenCL kernels for matrix operations.