ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
matrix_solve.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_MATRIX_SOLVE_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_MATRIX_SOLVE_HPP
3 
7 #include "viennacl/ocl/utils.hpp"
8 
10 
13 namespace viennacl
14 {
15 namespace linalg
16 {
17 namespace opencl
18 {
19 namespace kernels
20 {
21 
22 template<typename StringT>
23 void generate_matrix_solve_blas3(StringT & source, std::string const & numeric_string,
24  bool row_major_A, bool row_major_B,
25  bool upper_solve, bool unit_diagonal)
26 {
27  //start OpenCL code:
28  source.append("__kernel void ");
29  if (unit_diagonal)
30  source.append("unit_");
31  if (upper_solve)
32  source.append("upper_");
33  else
34  source.append("lower_");
35  source.append("solve");
36 
37  source.append("( \n");
38  source.append(" __global const "); source.append(numeric_string); source.append(" * A, \n");
39  source.append(" unsigned int A_start1, unsigned int A_start2, \n");
40  source.append(" unsigned int A_inc1, unsigned int A_inc2, \n");
41  source.append(" unsigned int A_size1, unsigned int A_size2, \n");
42  source.append(" unsigned int A_internal_size1, unsigned int A_internal_size2, \n");
43  source.append(" __global "); source.append(numeric_string); source.append(" * B, \n");
44  source.append(" unsigned int B_start1, unsigned int B_start2, \n");
45  source.append(" unsigned int B_inc1, unsigned int B_inc2, \n");
46  source.append(" unsigned int B_size1, unsigned int B_size2, \n");
47  source.append(" unsigned int B_internal_size1, unsigned int B_internal_size2) { \n");
48  source.append(" "); source.append(numeric_string); source.append(" temp; \n");
49  if (upper_solve)
50  {
51  //Note: A is square, thus A_rows == A_cols and no dispatch for transposedness needed
52  source.append(" for (unsigned int row_cnt = 0; row_cnt < A_size1; ++row_cnt) \n");
53  source.append(" { \n");
54  source.append(" unsigned int row = A_size1 - 1 - row_cnt; \n");
55  }
56  else //lower triangular solve
57  {
58  source.append(" for (unsigned int row = 0; row < A_size1; ++row) \n");
59  source.append(" { \n");
60  }
61 
62  if (!unit_diagonal)
63  {
64  source.append(" barrier(CLK_GLOBAL_MEM_FENCE); \n");
65  source.append(" if (get_local_id(0) == 0) \n");
66  //Note: A is square, thus A_internal_rows == A_internal_cols and no dispatch for transposedness needed
67  if (row_major_B)
68  source.append(" B[(row * B_inc1 + B_start1) * B_internal_size2 + (get_group_id(0) * B_inc2 + B_start2)] /= ");
69  else
70  source.append(" B[(row * B_inc1 + B_start1) + (get_group_id(0) * B_inc2 + B_start2) * B_internal_size1] /= ");
71 
72  if (row_major_A)
73  source.append("A[(row * A_inc1 + A_start1) * A_internal_size2 + (row * A_inc2 + A_start2)]; \n");
74  else
75  source.append("A[(row * A_inc1 + A_start1) + (row * A_inc2 + A_start2)*A_internal_size1]; \n");
76  }
77 
78  source.append(" barrier(CLK_GLOBAL_MEM_FENCE); \n");
79 
80  if (row_major_B)
81  source.append(" temp = B[(row * B_inc1 + B_start1) * B_internal_size2 + (get_group_id(0) * B_inc2 + B_start2)]; \n");
82  else
83  source.append(" temp = B[(row * B_inc1 + B_start1) + (get_group_id(0) * B_inc2 + B_start2) * B_internal_size1]; \n");
84 
85  source.append(" //eliminate column of op(A) with index 'row' in parallel: \n");
86  if (upper_solve)
87  source.append(" for (unsigned int elim = get_local_id(0); elim < row; elim += get_local_size(0)) \n");
88  else
89  source.append(" for (unsigned int elim = row + get_local_id(0) + 1; elim < A_size1; elim += get_local_size(0)) \n");
90 
91  if (row_major_B)
92  source.append(" B[(elim * B_inc1 + B_start1) * B_internal_size2 + (get_group_id(0) * B_inc2 + B_start2)] -= temp * ");
93  else
94  source.append(" B[(elim * B_inc1 + B_start1) + (get_group_id(0) * B_inc2 + B_start2) * B_internal_size1] -= temp * ");
95 
96  if (row_major_A)
97  source.append("A[(elim * A_inc1 + A_start1) * A_internal_size2 + (row * A_inc2 + A_start2)]; \n");
98  else
99  source.append("A[(elim * A_inc1 + A_start1) + (row * A_inc2 + A_start2) * A_internal_size1]; \n");
100 
101  source.append(" } \n");
102  source.append("} \n");
103 }
104 
105 
106 // main kernel class
112 template<typename NumericT, typename LayoutT1, typename LayoutT2>
114 {
115  static std::string program_name()
116  {
117  return viennacl::ocl::type_to_string<NumericT>::apply() + "_matrix_solve_" + detail::type_to_string(LayoutT1()) + detail::type_to_string(LayoutT2());
118  }
119 
120  static void init(viennacl::ocl::context & ctx)
121  {
122  static std::map<cl_context, bool> init_done;
123  if (!init_done[ctx.handle().get()])
124  {
126  std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
127  bool matrix_row_major = viennacl::is_row_major<LayoutT1>::value;
128  bool rhs_row_major = viennacl::is_row_major<LayoutT2>::value;
129 
130  std::string source;
131  source.reserve(8192);
132 
133  viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
134 
135  // only generate for floating points (forces error for integers)
136  if (numeric_string == "float" || numeric_string == "double")
137  {
138  generate_matrix_solve_blas3(source, numeric_string, matrix_row_major, rhs_row_major,
139  false, false);
140  generate_matrix_solve_blas3(source, numeric_string, matrix_row_major, rhs_row_major,
141  false, true);
142  generate_matrix_solve_blas3(source, numeric_string, matrix_row_major, rhs_row_major,
143  true, false);
144  generate_matrix_solve_blas3(source, numeric_string, matrix_row_major, rhs_row_major,
145  true, true);
146  }
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 
Implements a OpenCL platform within ViennaCL.
Helper class for checking whether a matrix has a row-major layout.
Definition: forwards.h:483
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:54
Provides OpenCL-related utilities.
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Definition: context.hpp:613
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
static void apply(viennacl::ocl::context const &)
Definition: utils.hpp:40
const OCL_TYPE & get() const
Definition: handle.hpp:189
void generate_matrix_solve_blas3(StringT &source, std::string const &numeric_string, bool row_major_A, bool row_major_B, bool upper_solve, bool unit_diagonal)
Main kernel class for the generation of matrix solve kernels.
Representation of an OpenCL kernel in ViennaCL.
std::string type_to_string(viennacl::row_major)
Definition: matrix.hpp:464
static void init(viennacl::ocl::context &ctx)
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
Runtime generation of OpenCL kernels for matrix operations.