ViennaCL - The Vienna Computing Library: viennacl/linalg/opencl/kernels/hyb_matrix.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
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-2016, 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"
23 #include "viennacl/ocl/platform.hpp"
24 #include "viennacl/ocl/utils.hpp"
25
26 #include "viennacl/linalg/opencl/common.hpp"
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, bool with_alpha_beta)
43 {
44 if (with_alpha_beta)
45 source.append("__kernel void vec_mul_alpha_beta( \n");
46 else
47 source.append("__kernel void vec_mul( \n");
48 source.append(" const __global int* ell_coords, \n");
49 source.append(" const __global "); source.append(numeric_string); source.append("* ell_elements, \n");
50 source.append(" const __global uint* csr_rows, \n");
51 source.append(" const __global uint* csr_cols, \n");
52 source.append(" const __global "); source.append(numeric_string); source.append("* csr_elements, \n");
53 source.append(" const __global "); source.append(numeric_string); source.append(" * x, \n");
54 source.append(" uint4 layout_x, \n");
55 if (with_alpha_beta) { source.append(" "); source.append(numeric_string); source.append(" alpha, \n"); }
56 source.append(" __global "); source.append(numeric_string); source.append(" * result, \n");
57 source.append(" uint4 layout_result, \n");
58 if (with_alpha_beta) { source.append(" "); source.append(numeric_string); source.append(" beta, \n"); }
59 source.append(" unsigned int row_num, \n");
60 source.append(" unsigned int internal_row_num, \n");
61 source.append(" unsigned int items_per_row, \n");
62 source.append(" unsigned int aligned_items_per_row) \n");
63 source.append("{ \n");
64 source.append(" uint glb_id = get_global_id(0); \n");
65 source.append(" uint glb_sz = get_global_size(0); \n");
66
67 source.append(" for (uint row_id = glb_id; row_id < row_num; row_id += glb_sz) { \n");
68 source.append(" "); source.append(numeric_string); source.append(" sum = 0; \n");
69
70 source.append(" uint offset = row_id; \n");
71 source.append(" for (uint item_id = 0; item_id < items_per_row; item_id++, offset += internal_row_num) { \n");
72 source.append(" "); source.append(numeric_string); source.append(" val = ell_elements[offset]; \n");
73
74 source.append(" if (val != ("); source.append(numeric_string); source.append(")0) { \n");
75 source.append(" int col = ell_coords[offset]; \n");
76 source.append(" sum += (x[col * layout_x.y + layout_x.x] * val); \n");
77 source.append(" } \n");
78
79 source.append(" } \n");
80
81 source.append(" uint col_begin = csr_rows[row_id]; \n");
82 source.append(" uint col_end = csr_rows[row_id + 1]; \n");
83
84 source.append(" for (uint item_id = col_begin; item_id < col_end; item_id++) { \n");
85 source.append(" sum += (x[csr_cols[item_id] * layout_x.y + layout_x.x] * csr_elements[item_id]); \n");
86 source.append(" } \n");
87
88 if (with_alpha_beta)
89 source.append(" result[row_id * layout_result.y + layout_result.x] = alpha * sum + ((beta != 0) ? beta * result[row_id * layout_result.y + layout_result.x] : 0); \n");
90 else
91 source.append(" result[row_id * layout_result.y + layout_result.x] = sum; \n");
92 source.append(" } \n");
93 source.append("} \n");
94 }
95
96 namespace detail
97 {
98 template<typename StringT>
99 void generate_hyb_matrix_dense_matrix_mul(StringT & source, std::string const & numeric_string,
100 bool B_transposed, bool B_row_major, bool C_row_major)
101 {
102 source.append("__kernel void ");
103 source.append(viennacl::linalg::opencl::detail::sparse_dense_matmult_kernel_name(B_transposed, B_row_major, C_row_major));
104 source.append("( \n");
105 source.append(" const __global int* ell_coords, \n");
106 source.append(" const __global "); source.append(numeric_string); source.append("* ell_elements, \n");
107 source.append(" const __global uint* csr_rows, \n");
108 source.append(" const __global uint* csr_cols, \n");
109 source.append(" const __global "); source.append(numeric_string); source.append("* csr_elements, \n");
110 source.append(" unsigned int row_num, \n");
111 source.append(" unsigned int internal_row_num, \n");
112 source.append(" unsigned int items_per_row, \n");
113 source.append(" unsigned int aligned_items_per_row, \n");
114 source.append(" __global const "); source.append(numeric_string); source.append("* d_mat, \n");
115 source.append(" unsigned int d_mat_row_start, \n");
116 source.append(" unsigned int d_mat_col_start, \n");
117 source.append(" unsigned int d_mat_row_inc, \n");
118 source.append(" unsigned int d_mat_col_inc, \n");
119 source.append(" unsigned int d_mat_row_size, \n");
120 source.append(" unsigned int d_mat_col_size, \n");
121 source.append(" unsigned int d_mat_internal_rows, \n");
122 source.append(" unsigned int d_mat_internal_cols, \n");
123 source.append(" __global "); source.append(numeric_string); source.append(" * result, \n");
124 source.append(" unsigned int result_row_start, \n");
125 source.append(" unsigned int result_col_start, \n");
126 source.append(" unsigned int result_row_inc, \n");
127 source.append(" unsigned int result_col_inc, \n");
128 source.append(" unsigned int result_row_size, \n");
129 source.append(" unsigned int result_col_size, \n");
130 source.append(" unsigned int result_internal_rows, \n");
131 source.append(" unsigned int result_internal_cols) { \n");
132
133 source.append(" uint glb_id = get_global_id(0); \n");
134 source.append(" uint glb_sz = get_global_size(0); \n");
135
136 source.append(" for (uint result_col = 0; result_col < result_col_size; ++result_col) { \n");
137 source.append(" for (uint row_id = glb_id; row_id < row_num; row_id += glb_sz) { \n");
138 source.append(" "); source.append(numeric_string); source.append(" sum = 0; \n");
139
140 source.append(" uint offset = row_id; \n");
141 source.append(" for (uint item_id = 0; item_id < items_per_row; item_id++, offset += internal_row_num) { \n");
142 source.append(" "); source.append(numeric_string); source.append(" val = ell_elements[offset]; \n");
143
144 source.append(" if (val != ("); source.append(numeric_string); source.append(")0) { \n");
145 source.append(" int col = ell_coords[offset]; \n");
146 if (B_transposed && B_row_major)
147 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");
148 else if (B_transposed && !B_row_major)
149 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");
150 else if (!B_transposed && B_row_major)
151 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");
152 else
153 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");
154 source.append(" } \n");
155
156 source.append(" } \n");
157
158 source.append(" uint col_begin = csr_rows[row_id]; \n");
159 source.append(" uint col_end = csr_rows[row_id + 1]; \n");
160
161 source.append(" for (uint item_id = col_begin; item_id < col_end; item_id++) { \n");
162 if (B_transposed && B_row_major)
163 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");
164 else if (B_transposed && !B_row_major)
165 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");
166 else if (!B_transposed && B_row_major)
167 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");
168 else
169 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");
170 source.append(" } \n");
171
172 if (C_row_major)
173 source.append(" result[ (result_row_start + row_id * result_row_inc) * result_internal_cols + result_col_start + result_col * result_col_inc ] = sum; \n");
174 else
175 source.append(" result[ (result_row_start + row_id * result_row_inc) + (result_col_start + result_col * result_col_inc) * result_internal_rows ] = sum; \n");
176 source.append(" } \n");
177 source.append(" } \n");
178 source.append("} \n");
179 }
180 }
181
182 template<typename StringT>
183 void generate_hyb_matrix_dense_matrix_multiplication(StringT & source, std::string const & numeric_string)
184 {
185 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, false, false);
186 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, false, true);
187 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, true, false);
188 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, false, true, true);
189
190 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, false, false);
191 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, false, true);
192 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, true, false);
193 detail::generate_hyb_matrix_dense_matrix_mul(source, numeric_string, true, true, true);
194 }
195
197
198 // main kernel class
200 template<typename NumericT>
201 struct hyb_matrix
202 {
203 static std::string program_name()
204 {
205 return viennacl::ocl::type_to_string<NumericT>::apply() + "_hyb_matrix";
206 }
207
208 static void init(viennacl::ocl::context & ctx)
209 {
210 static std::map<cl_context, bool> init_done;
211 if (!init_done[ctx.handle().get()])
212 {
213 viennacl::ocl::DOUBLE_PRECISION_CHECKER<NumericT>::apply(ctx);
214 std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
215
216 std::string source;
217 source.reserve(1024);
218
219 viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
220
221 generate_hyb_vec_mul(source, numeric_string, true);
222 generate_hyb_vec_mul(source, numeric_string, false);
223 generate_hyb_matrix_dense_matrix_multiplication(source, numeric_string);
224
225 std::string prog_name = program_name();
226 #ifdef VIENNACL_BUILD_INFO
227 std::cout << "Creating program " << prog_name << std::endl;
228 #endif
229 ctx.add_program(source, prog_name);
230 init_done[ctx.handle().get()] = true;
231 } //if
232 } //init
233 };
234
235 } // namespace kernels
236 } // namespace opencl
237 } // namespace linalg
238 } // namespace viennacl
239 #endif
240
platform.hpp
Implements a OpenCL platform within ViennaCL.
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::linalg::opencl::kernels::generate_hyb_vec_mul
void generate_hyb_vec_mul(StringT &source, std::string const &numeric_string, bool with_alpha_beta)
Definition: hyb_matrix.hpp:42
viennacl::linalg::opencl::detail::sparse_dense_matmult_kernel_name
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
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
common.hpp
Common implementations shared by OpenCL-based operations.
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::hyb_matrix::program_name
static std::string program_name()
Definition: hyb_matrix.hpp:203
viennacl::linalg::opencl::kernels::detail::generate_hyb_matrix_dense_matrix_mul
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:99
kernel.hpp
Representation of an OpenCL kernel in ViennaCL.
viennacl::linalg::opencl::kernels::hyb_matrix::init
static void init(viennacl::ocl::context &ctx)
Definition: hyb_matrix.hpp:208
viennacl::ocl::type_to_string
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
viennacl::linalg::opencl::kernels::generate_hyb_matrix_dense_matrix_multiplication
void generate_hyb_matrix_dense_matrix_multiplication(StringT &source, std::string const &numeric_string)
Definition: hyb_matrix.hpp:183
viennacl::linalg::opencl::kernels::hyb_matrix
Main kernel class for generating OpenCL kernels for hyb_matrix.
Definition: hyb_matrix.hpp:201