ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
hyb_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_HYB_MATRIX_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_HYB_MATRIX_HPP
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
21 #include "viennacl/tools/tools.hpp"
22 #include "viennacl/ocl/kernel.hpp"
24 #include "viennacl/ocl/utils.hpp"
25 
27 
30 namespace viennacl
31 {
32 namespace linalg
33 {
34 namespace opencl
35 {
36 namespace kernels
37 {
38 
40 
41 template<typename StringT>
42 void generate_hyb_vec_mul(StringT & source, std::string const & numeric_string)
43 {
44  source.append("__kernel void vec_mul( \n");
45  source.append(" const __global int* ell_coords, \n");
46  source.append(" const __global "); source.append(numeric_string); source.append("* ell_elements, \n");
47  source.append(" const __global uint* csr_rows, \n");
48  source.append(" const __global uint* csr_cols, \n");
49  source.append(" const __global "); source.append(numeric_string); source.append("* csr_elements, \n");
50  source.append(" const __global "); source.append(numeric_string); source.append(" * x, \n");
51  source.append(" uint4 layout_x, \n");
52  source.append(" __global "); source.append(numeric_string); source.append(" * result, \n");
53  source.append(" uint4 layout_result, \n");
54  source.append(" unsigned int row_num, \n");
55  source.append(" unsigned int internal_row_num, \n");
56  source.append(" unsigned int items_per_row, \n");
57  source.append(" unsigned int aligned_items_per_row) \n");
58  source.append("{ \n");
59  source.append(" uint glb_id = get_global_id(0); \n");
60  source.append(" uint glb_sz = get_global_size(0); \n");
61 
62  source.append(" for (uint row_id = glb_id; row_id < row_num; row_id += glb_sz) { \n");
63  source.append(" "); source.append(numeric_string); source.append(" sum = 0; \n");
64 
65  source.append(" uint offset = row_id; \n");
66  source.append(" for (uint item_id = 0; item_id < items_per_row; item_id++, offset += internal_row_num) { \n");
67  source.append(" "); source.append(numeric_string); source.append(" val = ell_elements[offset]; \n");
68 
69  source.append(" if (val != ("); source.append(numeric_string); source.append(")0) { \n");
70  source.append(" int col = ell_coords[offset]; \n");
71  source.append(" sum += (x[col * layout_x.y + layout_x.x] * val); \n");
72  source.append(" } \n");
73 
74  source.append(" } \n");
75 
76  source.append(" uint col_begin = csr_rows[row_id]; \n");
77  source.append(" uint col_end = csr_rows[row_id + 1]; \n");
78 
79  source.append(" for (uint item_id = col_begin; item_id < col_end; item_id++) { \n");
80  source.append(" sum += (x[csr_cols[item_id] * layout_x.y + layout_x.x] * csr_elements[item_id]); \n");
81  source.append(" } \n");
82 
83  source.append(" result[row_id * layout_result.y + layout_result.x] = sum; \n");
84  source.append(" } \n");
85  source.append("} \n");
86 }
87 
88 namespace detail
89 {
90  template<typename StringT>
91  void generate_hyb_matrix_dense_matrix_mul(StringT & source, std::string const & numeric_string,
92  bool B_transposed, bool B_row_major, bool C_row_major)
93  {
94  source.append("__kernel void ");
95  source.append(viennacl::linalg::opencl::detail::sparse_dense_matmult_kernel_name(B_transposed, B_row_major, C_row_major));
96  source.append("( \n");
97  source.append(" const __global int* ell_coords, \n");
98  source.append(" const __global "); source.append(numeric_string); source.append("* ell_elements, \n");
99  source.append(" const __global uint* csr_rows, \n");
100  source.append(" const __global uint* csr_cols, \n");
101  source.append(" const __global "); source.append(numeric_string); source.append("* csr_elements, \n");
102  source.append(" unsigned int row_num, \n");
103  source.append(" unsigned int internal_row_num, \n");
104  source.append(" unsigned int items_per_row, \n");
105  source.append(" unsigned int aligned_items_per_row, \n");
106  source.append(" __global const "); source.append(numeric_string); source.append("* d_mat, \n");
107  source.append(" unsigned int d_mat_row_start, \n");
108  source.append(" unsigned int d_mat_col_start, \n");
109  source.append(" unsigned int d_mat_row_inc, \n");
110  source.append(" unsigned int d_mat_col_inc, \n");
111  source.append(" unsigned int d_mat_row_size, \n");
112  source.append(" unsigned int d_mat_col_size, \n");
113  source.append(" unsigned int d_mat_internal_rows, \n");
114  source.append(" unsigned int d_mat_internal_cols, \n");
115  source.append(" __global "); source.append(numeric_string); source.append(" * result, \n");
116  source.append(" unsigned int result_row_start, \n");
117  source.append(" unsigned int result_col_start, \n");
118  source.append(" unsigned int result_row_inc, \n");
119  source.append(" unsigned int result_col_inc, \n");
120  source.append(" unsigned int result_row_size, \n");
121  source.append(" unsigned int result_col_size, \n");
122  source.append(" unsigned int result_internal_rows, \n");
123  source.append(" unsigned int result_internal_cols) { \n");
124 
125  source.append(" uint glb_id = get_global_id(0); \n");
126  source.append(" uint glb_sz = get_global_size(0); \n");
127 
128  source.append(" for (uint result_col = 0; result_col < result_col_size; ++result_col) { \n");
129  source.append(" for (uint row_id = glb_id; row_id < row_num; row_id += glb_sz) { \n");
130  source.append(" "); source.append(numeric_string); source.append(" sum = 0; \n");
131 
132  source.append(" uint offset = row_id; \n");
133  source.append(" for (uint item_id = 0; item_id < items_per_row; item_id++, offset += internal_row_num) { \n");
134  source.append(" "); source.append(numeric_string); source.append(" val = ell_elements[offset]; \n");
135 
136  source.append(" if (val != ("); source.append(numeric_string); source.append(")0) { \n");
137  source.append(" int col = ell_coords[offset]; \n");
138  if (B_transposed && B_row_major)
139  source.append(" sum += d_mat[ (d_mat_row_start + result_col * d_mat_row_inc) * d_mat_internal_cols + d_mat_col_start + col * d_mat_col_inc ] * val; \n");
140  else if (B_transposed && !B_row_major)
141  source.append(" sum += d_mat[ (d_mat_row_start + result_col * d_mat_row_inc) + (d_mat_col_start + col * d_mat_col_inc) * d_mat_internal_rows ] * val; \n");
142  else if (!B_transposed && B_row_major)
143  source.append(" sum += d_mat[ (d_mat_row_start + col * d_mat_row_inc) * d_mat_internal_cols + d_mat_col_start + result_col * d_mat_col_inc ] * val; \n");
144  else
145  source.append(" sum += d_mat[ (d_mat_row_start + col * d_mat_row_inc) + (d_mat_col_start + result_col * d_mat_col_inc) * d_mat_internal_rows ] * val; \n");
146  source.append(" } \n");
147 
148  source.append(" } \n");
149 
150  source.append(" uint col_begin = csr_rows[row_id]; \n");
151  source.append(" uint col_end = csr_rows[row_id + 1]; \n");
152 
153  source.append(" for (uint item_id = col_begin; item_id < col_end; item_id++) { \n");
154  if (B_transposed && B_row_major)
155  source.append(" sum += d_mat[ (d_mat_row_start + result_col * d_mat_row_inc) * d_mat_internal_cols + d_mat_col_start + csr_cols[item_id] * d_mat_col_inc ] * csr_elements[item_id]; \n");
156  else if (B_transposed && !B_row_major)
157  source.append(" sum += d_mat[ (d_mat_row_start + result_col * d_mat_row_inc) + (d_mat_col_start + csr_cols[item_id] * d_mat_col_inc) * d_mat_internal_rows ] * csr_elements[item_id]; \n");
158  else if (!B_transposed && B_row_major)
159  source.append(" sum += d_mat[ (d_mat_row_start + csr_cols[item_id] * d_mat_row_inc) * d_mat_internal_cols + d_mat_col_start + result_col * d_mat_col_inc ] * csr_elements[item_id]; \n");
160  else
161  source.append(" sum += d_mat[ (d_mat_row_start + csr_cols[item_id] * d_mat_row_inc) + (d_mat_col_start + result_col * d_mat_col_inc) * d_mat_internal_rows ] * csr_elements[item_id]; \n");
162  source.append(" } \n");
163 
164  if (C_row_major)
165  source.append(" result[ (result_row_start + row_id * result_row_inc) * result_internal_cols + result_col_start + result_col * result_col_inc ] = sum; \n");
166  else
167  source.append(" result[ (result_row_start + row_id * result_row_inc) + (result_col_start + result_col * result_col_inc) * result_internal_rows ] = sum; \n");
168  source.append(" } \n");
169  source.append(" } \n");
170  source.append("} \n");
171  }
172 }
173 
174 template<typename StringT>
175 void generate_hyb_matrix_dense_matrix_multiplication(StringT & source, std::string const & numeric_string)
176 {
177  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, false, false);
178  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, false, true);
179  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, true, false);
180  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, true, true);
181 
182  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, false, false);
183  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, false, true);
184  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, true, false);
185  detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, true, true);
186 }
187 
189 
190 // main kernel class
192 template<typename NumericT>
194 {
195  static std::string program_name()
196  {
197  return viennacl::ocl::type_to_string<NumericT>::apply() + "_hyb_matrix";
198  }
199 
200  static void init(viennacl::ocl::context & ctx)
201  {
202  static std::map<cl_context, bool> init_done;
203  if (!init_done[ctx.handle().get()])
204  {
206  std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
207 
208  std::string source;
209  source.reserve(1024);
210 
211  viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
212 
213  generate_hyb_vec_mul(source, numeric_string);
214  generate_hyb_matrix_dense_matrix_multiplication(source, numeric_string);
215 
216  std::string prog_name = program_name();
217  #ifdef VIENNACL_BUILD_INFO
218  std::cout << "Creating program " << prog_name << std::endl;
219  #endif
220  ctx.add_program(source, prog_name);
221  init_done[ctx.handle().get()] = true;
222  } //if
223  } //init
224 };
225 
226 } // namespace kernels
227 } // namespace opencl
228 } // namespace linalg
229 } // namespace viennacl
230 #endif
231 
Implements a OpenCL platform within ViennaCL.
Various little tools used here and there in ViennaCL.
std::string sparse_dense_matmult_kernel_name(bool B_transposed, bool B_row_major, bool C_row_major)
Returns the OpenCL kernel string for the operation C = A * B with A sparse, B, C dense matrices...
Definition: common.hpp:49
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
Common implementations shared by OpenCL-based operations.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
static void apply(viennacl::ocl::context const &)
Definition: utils.hpp:40
const OCL_TYPE & get() const
Definition: handle.hpp:189
Definition: blas3.hpp:36
void generate_hyb_matrix_dense_matrix_mul(StringT &source, std::string const &numeric_string, bool B_transposed, bool B_row_major, bool C_row_major)
Definition: hyb_matrix.hpp:91
Representation of an OpenCL kernel in ViennaCL.
static void init(viennacl::ocl::context &ctx)
Definition: hyb_matrix.hpp:200
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
void generate_hyb_vec_mul(StringT &source, std::string const &numeric_string)
Definition: hyb_matrix.hpp:42
void generate_hyb_matrix_dense_matrix_multiplication(StringT &source, std::string const &numeric_string)
Definition: hyb_matrix.hpp:175
Main kernel class for generating OpenCL kernels for hyb_matrix.
Definition: hyb_matrix.hpp:193