ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
ell_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_ELL_MATRIX_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_ELL_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_ell_vec_mul(StringT & source, std::string const & numeric_string)
43 {
44  source.append("__kernel void vec_mul( \n");
45  source.append(" __global const unsigned int * coords, \n");
46  source.append(" __global const "); source.append(numeric_string); source.append(" * elements, \n");
47  source.append(" __global const "); source.append(numeric_string); source.append(" * x, \n");
48  source.append(" uint4 layout_x, \n");
49  source.append(" __global "); source.append(numeric_string); source.append(" * result, \n");
50  source.append(" uint4 layout_result, \n");
51  source.append(" unsigned int row_num, \n");
52  source.append(" unsigned int col_num, \n");
53  source.append(" unsigned int internal_row_num, \n");
54  source.append(" unsigned int items_per_row, \n");
55  source.append(" unsigned int aligned_items_per_row) \n");
56  source.append("{ \n");
57  source.append(" uint glb_id = get_global_id(0); \n");
58  source.append(" uint glb_sz = get_global_size(0); \n");
59 
60  source.append(" for (uint row_id = glb_id; row_id < row_num; row_id += glb_sz) { \n");
61  source.append(" "); source.append(numeric_string); source.append(" sum = 0; \n");
62 
63  source.append(" uint offset = row_id; \n");
64  source.append(" for (uint item_id = 0; item_id < items_per_row; item_id++, offset += internal_row_num) { \n");
65  source.append(" "); source.append(numeric_string); source.append(" val = elements[offset]; \n");
66 
67  source.append(" if (val != 0.0f) { \n");
68  source.append(" int col = coords[offset]; \n");
69  source.append(" sum += (x[col * layout_x.y + layout_x.x] * val); \n");
70  source.append(" } \n");
71 
72  source.append(" } \n");
73 
74  source.append(" result[row_id * layout_result.y + layout_result.x] = sum; \n");
75  source.append(" } \n");
76  source.append("} \n");
77 }
78 
79 namespace detail
80 {
81  template<typename StringT>
82  void generate_ell_matrix_dense_matrix_mul(StringT & source, std::string const & numeric_string,
83  bool B_transposed, bool B_row_major, bool C_row_major)
84  {
85  source.append("__kernel void ");
86  source.append(viennacl::linalg::opencl::detail::sparse_dense_matmult_kernel_name(B_transposed, B_row_major, C_row_major));
87  source.append("( \n");
88  source.append(" __global const unsigned int * sp_mat_coords, \n");
89  source.append(" __global const "); source.append(numeric_string); source.append(" * sp_mat_elems, \n");
90  source.append(" unsigned int sp_mat_row_num, \n");
91  source.append(" unsigned int sp_mat_col_num, \n");
92  source.append(" unsigned int sp_mat_internal_row_num, \n");
93  source.append(" unsigned int sp_mat_items_per_row, \n");
94  source.append(" unsigned int sp_mat_aligned_items_per_row, \n");
95  source.append(" __global const "); source.append(numeric_string); source.append("* d_mat, \n");
96  source.append(" unsigned int d_mat_row_start, \n");
97  source.append(" unsigned int d_mat_col_start, \n");
98  source.append(" unsigned int d_mat_row_inc, \n");
99  source.append(" unsigned int d_mat_col_inc, \n");
100  source.append(" unsigned int d_mat_row_size, \n");
101  source.append(" unsigned int d_mat_col_size, \n");
102  source.append(" unsigned int d_mat_internal_rows, \n");
103  source.append(" unsigned int d_mat_internal_cols, \n");
104  source.append(" __global "); source.append(numeric_string); source.append(" * result, \n");
105  source.append(" unsigned int result_row_start, \n");
106  source.append(" unsigned int result_col_start, \n");
107  source.append(" unsigned int result_row_inc, \n");
108  source.append(" unsigned int result_col_inc, \n");
109  source.append(" unsigned int result_row_size, \n");
110  source.append(" unsigned int result_col_size, \n");
111  source.append(" unsigned int result_internal_rows, \n");
112  source.append(" unsigned int result_internal_cols) { \n");
113 
114  source.append(" uint glb_id = get_global_id(0); \n");
115  source.append(" uint glb_sz = get_global_size(0); \n");
116 
117  source.append(" for ( uint rc = glb_id; rc < (sp_mat_row_num * result_col_size); rc += glb_sz) { \n");
118  source.append(" uint row = rc % sp_mat_row_num; \n");
119  source.append(" uint col = rc / sp_mat_row_num; \n");
120 
121  source.append(" uint offset = row; \n");
122  source.append(" "); source.append(numeric_string); source.append(" r = ("); source.append(numeric_string); source.append(")0; \n");
123 
124  source.append(" for ( uint k = 0; k < sp_mat_items_per_row; k++, offset += sp_mat_internal_row_num) { \n");
125 
126  source.append(" uint j = sp_mat_coords[offset]; \n");
127  source.append(" "); source.append(numeric_string); source.append(" x = sp_mat_elems[offset]; \n");
128 
129  source.append(" if (x != ("); source.append(numeric_string); source.append(")0) { \n");
130  source.append(" "); source.append(numeric_string);
131  if (B_transposed && B_row_major)
132  source.append(" y = d_mat[ (d_mat_row_start + col * d_mat_row_inc) * d_mat_internal_cols + d_mat_col_start + j * d_mat_col_inc ]; \n");
133  else if (B_transposed && !B_row_major)
134  source.append(" y = d_mat[ (d_mat_row_start + col * d_mat_row_inc) + (d_mat_col_start + j * d_mat_col_inc) * d_mat_internal_rows ]; \n");
135  else if (!B_transposed && B_row_major)
136  source.append(" y = d_mat[ (d_mat_row_start + j * d_mat_row_inc) * d_mat_internal_cols + d_mat_col_start + col * d_mat_col_inc ]; \n");
137  else
138  source.append(" y = d_mat[ (d_mat_row_start + j * d_mat_row_inc) + (d_mat_col_start + col * d_mat_col_inc) * d_mat_internal_rows ]; \n");
139 
140  source.append(" r += x*y; \n");
141  source.append(" } \n");
142  source.append(" } \n");
143 
144  if (C_row_major)
145  source.append(" result[ (result_row_start + row * result_row_inc) * result_internal_cols + result_col_start + col * result_col_inc ] = r; \n");
146  else
147  source.append(" result[ (result_row_start + row * result_row_inc) + (result_col_start + col * result_col_inc) * result_internal_rows ] = r; \n");
148  source.append(" } \n");
149  source.append("} \n");
150 
151  }
152 }
153 
154 template<typename StringT>
155 void generate_ell_matrix_dense_matrix_multiplication(StringT & source, std::string const & numeric_string)
156 {
157  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, false, false, false);
158  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, false, false, true);
159  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, false, true, false);
160  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, false, true, true);
161 
162  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, true, false, false);
163  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, true, false, true);
164  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, true, true, false);
165  detail::generate_ell_matrix_dense_matrix_mul(source, numeric_string, true, true, true);
166 }
167 
169 
170 // main kernel class
172 template<typename NumericT>
174 {
175  static std::string program_name()
176  {
177  return viennacl::ocl::type_to_string<NumericT>::apply() + "_ell_matrix";
178  }
179 
180  static void init(viennacl::ocl::context & ctx)
181  {
182  static std::map<cl_context, bool> init_done;
183  if (!init_done[ctx.handle().get()])
184  {
186  std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
187 
188  std::string source;
189  source.reserve(1024);
190 
191  viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
192 
193  // fully parametrized kernels:
194  generate_ell_vec_mul(source, numeric_string);
195  generate_ell_matrix_dense_matrix_multiplication(source, numeric_string);
196 
197  std::string prog_name = program_name();
198  #ifdef VIENNACL_BUILD_INFO
199  std::cout << "Creating program " << prog_name << std::endl;
200  #endif
201  ctx.add_program(source, prog_name);
202  init_done[ctx.handle().get()] = true;
203  } //if
204  } //init
205 };
206 
207 } // namespace kernels
208 } // namespace opencl
209 } // namespace linalg
210 } // namespace viennacl
211 #endif
212 
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 kernel class for generating OpenCL kernels for ell_matrix.
Definition: ell_matrix.hpp:173
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
Representation of an OpenCL kernel in ViennaCL.
void generate_ell_matrix_dense_matrix_multiplication(StringT &source, std::string const &numeric_string)
Definition: ell_matrix.hpp:155
void generate_ell_matrix_dense_matrix_mul(StringT &source, std::string const &numeric_string, bool B_transposed, bool B_row_major, bool C_row_major)
Definition: ell_matrix.hpp:82
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
void generate_ell_vec_mul(StringT &source, std::string const &numeric_string)
Definition: ell_matrix.hpp:42
static void init(viennacl::ocl::context &ctx)
Definition: ell_matrix.hpp:180