1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_ELL_MATRIX_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_ELL_MATRIX_HPP
41 template<
typename StringT>
45 source.append(
"__kernel void vec_mul_alpha_beta( \n");
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");
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");
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");
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");
77 source.append(
" } \n");
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");
82 source.append(
" result[row_id * layout_result.y + layout_result.x] = sum; \n");
83 source.append(
" } \n");
84 source.append(
"} \n");
89 template<
typename StringT>
91 bool B_transposed,
bool B_row_major,
bool C_row_major)
93 source.append(
"__kernel void ");
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");
122 source.append(
" uint glb_id = get_global_id(0); \n");
123 source.append(
" uint glb_sz = get_global_size(0); \n");
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");
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");
132 source.append(
" for ( uint k = 0; k < sp_mat_items_per_row; k++, offset += sp_mat_internal_row_num) { \n");
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");
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");
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");
148 source.append(
" r += x*y; \n");
149 source.append(
" } \n");
150 source.append(
" } \n");
153 source.append(
" result[ (result_row_start + row * result_row_inc) * result_internal_cols + result_col_start + col * result_col_inc ] = r; \n");
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");
162 template<
typename StringT>
180 template<
typename NumericT>
190 static std::map<cl_context, bool> init_done;
197 source.reserve(1024);
199 viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
207 #ifdef VIENNACL_BUILD_INFO
208 std::cout <<
"Creating program " << prog_name << std::endl;
210 ctx.add_program(source, prog_name);
211 init_done[ctx.handle().get()] =
true;
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...
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Provides OpenCL-related utilities.
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Common implementations shared by OpenCL-based operations.
Main kernel class for generating OpenCL kernels for ell_matrix.
void generate_ell_vec_mul(StringT &source, std::string const &numeric_string, bool with_alpha_beta)
static std::string program_name()
static void apply(viennacl::ocl::context const &)
const OCL_TYPE & get() const
Representation of an OpenCL kernel in ViennaCL.
void generate_ell_matrix_dense_matrix_multiplication(StringT &source, std::string const &numeric_string)
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)
Helper class for converting a type to its string representation.
static void init(viennacl::ocl::context &ctx)