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