00001 #ifndef _VIENNACL_MATRIX_COL_SOURCE_HPP_
00002 #define _VIENNACL_MATRIX_COL_SOURCE_HPP_
00003
00004 namespace viennacl
00005 {
00006 namespace linalg
00007 {
00008 namespace kernels
00009 {
00010 const char * const matrix_col_align1_unit_lower_triangular_substitute_inplace =
00011 "__kernel void unit_lower_triangular_substitute_inplace(\n"
00012 " __global const float * matrix,\n"
00013 " unsigned int matrix_rows,\n"
00014 " unsigned int matrix_cols,\n"
00015 " unsigned int matrix_internal_rows,\n"
00016 " unsigned int matrix_internal_cols,\n"
00017 " __global float * vector)\n"
00018 "{\n"
00019 " float temp;\n"
00020 " for (int row = 0; row < matrix_rows; ++row)\n"
00021 " {\n"
00022 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00023 " temp = vector[row];\n"
00024 " for (int elim = row + get_global_id(0) + 1; elim < matrix_rows; elim += get_global_size(0))\n"
00025 " vector[elim] -= temp * matrix[row * matrix_internal_rows + elim];\n"
00026 " }\n"
00027 "}\n"
00028 ;
00029
00030 const char * const matrix_col_align1_inplace_sub =
00031 "__kernel void inplace_sub(\n"
00032 " __global float * vec1,\n"
00033 " __global const float * vec2,\n"
00034 " unsigned int size) \n"
00035 "{ \n"
00036 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00037 " vec1[i] -= vec2[i];\n"
00038 "}\n"
00039 ;
00040
00041 const char * const matrix_col_align1_lower_triangular_substitute_inplace =
00042 "__kernel void lower_triangular_substitute_inplace(\n"
00043 " __global const float * matrix,\n"
00044 " unsigned int matrix_rows,\n"
00045 " unsigned int matrix_cols,\n"
00046 " unsigned int matrix_internal_rows,\n"
00047 " unsigned int matrix_internal_cols,\n"
00048 " __global float * vector)\n"
00049 "{\n"
00050 " float temp;\n"
00051 " for (int row = 0; row < matrix_rows; ++row)\n"
00052 " {\n"
00053 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00054 " if (get_global_id(0) == 0)\n"
00055 " vector[row] /= matrix[row+row*matrix_internal_rows];\n"
00056 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00057 " temp = vector[row];\n"
00058 " for (int elim = row + get_global_id(0) + 1; elim < matrix_rows; elim += get_global_size(0))\n"
00059 " vector[elim] -= temp * matrix[row * matrix_internal_rows + elim];\n"
00060 " }\n"
00061 "}\n"
00062 ;
00063
00064 const char * const matrix_col_align1_trans_vec_mul =
00065 "__kernel void trans_vec_mul(\n"
00066 " __global const float * matrix,\n"
00067 " unsigned int matrix_rows,\n"
00068 " unsigned int matrix_cols,\n"
00069 " unsigned int matrix_internal_rows,\n"
00070 " unsigned int matrix_internal_cols,\n"
00071 " __global const float * vector, \n"
00072 " __global float * result) \n"
00073 "{ \n"
00074 " //row and col indicate indices within transposed matrix\n"
00075 " for (unsigned int row = get_global_id(0); row < matrix_cols; row += get_global_size(0))\n"
00076 " {\n"
00077 " float dot_prod2 = 0.0f;\n"
00078 " for (unsigned int col = 0; col < matrix_rows; ++col)\n"
00079 " dot_prod2 += matrix[row * matrix_internal_rows + col] * vector[col];\n"
00080 " result[row] = dot_prod2;\n"
00081 " }\n"
00082 "}\n"
00083 ;
00084
00085 const char * const matrix_col_align1_rank1_update =
00086 "//perform a rank-1 update of the matrix, i.e. A += x * x^T\n"
00087 "__kernel void rank1_update(\n"
00088 " __global float * matrix,\n"
00089 " unsigned int matrix_rows,\n"
00090 " unsigned int matrix_cols,\n"
00091 " unsigned int matrix_internal_rows,\n"
00092 " unsigned int matrix_internal_cols,\n"
00093 " __global const float * vector1, \n"
00094 " __global const float * vector2) \n"
00095 "{ \n"
00096 " float tmp;\n"
00097 " for (unsigned int row= get_global_id(0); row < matrix_rows; row += get_global_size(0))\n"
00098 " {\n"
00099 " tmp = vector1[row];\n"
00100 " for (unsigned int col = 0; col < matrix_cols; ++col)\n"
00101 " matrix[row + col * matrix_internal_rows] += tmp * vector2[col];\n"
00102 " }\n"
00103 "}\n"
00104 ;
00105
00106 const char * const matrix_col_align1_sub =
00107 "__kernel void sub(\n"
00108 " __global const float * vec1,\n"
00109 " __global const float * vec2, \n"
00110 " __global float * result,\n"
00111 " unsigned int size)\n"
00112 "{ \n"
00113 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00114 " result[i] = vec1[i] - vec2[i];\n"
00115 "}\n"
00116 ;
00117
00118 const char * const matrix_col_align1_trans_unit_upper_triangular_substitute_inplace =
00119 "//transposed lower triangular matrix\n"
00120 "__kernel void trans_unit_upper_triangular_substitute_inplace(\n"
00121 " __global const float * matrix, \n"
00122 " unsigned int matrix_rows,\n"
00123 " unsigned int matrix_cols,\n"
00124 " unsigned int matrix_internal_rows,\n"
00125 " unsigned int matrix_internal_cols,\n"
00126 " __global float * vector) \n"
00127 "{ \n"
00128 " float temp; \n"
00129 " for (int row = matrix_rows-1; row > -1; --row) \n"
00130 " { \n"
00131 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00132 " \n"
00133 " temp = vector[row]; \n"
00134 " //eliminate column with index 'row' in parallel: \n"
00135 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00136 " vector[elim] -= temp * matrix[row + elim * matrix_internal_rows]; \n"
00137 " } \n"
00138 " \n"
00139 "}\n"
00140 ;
00141
00142 const char * const matrix_col_align1_lu_factorize =
00143 "__kernel void lu_factorize(\n"
00144 " __global float * matrix,\n"
00145 " unsigned int matrix_rows,\n"
00146 " unsigned int matrix_cols,\n"
00147 " unsigned int matrix_internal_rows,\n"
00148 " unsigned int matrix_internal_cols) \n"
00149 "{ \n"
00150 " float temp;\n"
00151 " for (unsigned int i=1; i<matrix_rows; ++i)\n"
00152 " {\n"
00153 " for (unsigned int k=0; k<i; ++k)\n"
00154 " {\n"
00155 " if (get_global_id(0) == 0)\n"
00156 " matrix[i + k*matrix_internal_rows] /= matrix[k + k*matrix_internal_rows];\n"
00157 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00158 " temp = matrix[i + k*matrix_internal_rows];\n"
00159 " \n"
00160 " //parallel subtraction:\n"
00161 " for (unsigned int j=k+1 + get_global_id(0); j<matrix_cols; j += get_global_size(0))\n"
00162 " matrix[i + j*matrix_internal_rows] -= temp * matrix[k + j*matrix_internal_rows];\n"
00163 " }\n"
00164 " }\n"
00165 "} \n"
00166 ;
00167
00168 const char * const matrix_col_align1_add =
00169 "__kernel void add(\n"
00170 " __global const float * vec1,\n"
00171 " __global const float * vec2, \n"
00172 " __global float * result,\n"
00173 " unsigned int size) \n"
00174 "{ \n"
00175 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00176 " result[i] = vec1[i] + vec2[i];\n"
00177 "}\n"
00178 ;
00179
00180 const char * const matrix_col_align1_vec_mul =
00181 "__kernel void vec_mul(\n"
00182 " __global const float * matrix,\n"
00183 " unsigned int matrix_rows,\n"
00184 " unsigned int matrix_cols,\n"
00185 " unsigned int matrix_internal_rows,\n"
00186 " unsigned int matrix_internal_cols,\n"
00187 " __global const float * vector, \n"
00188 " __global float * result) \n"
00189 "{ \n"
00190 " for (unsigned int row = get_global_id(0); row < matrix_rows; row += get_global_size(0))\n"
00191 " {\n"
00192 " float dot_prod = 0.0f;\n"
00193 " for (unsigned int col = 0; col < matrix_cols; ++col)\n"
00194 " dot_prod += matrix[row + col*matrix_internal_rows] * vector[col];\n"
00195 " result[row] = dot_prod;\n"
00196 " }\n"
00197 "}\n"
00198 ;
00199
00200 const char * const matrix_col_align1_trans_lower_triangular_substitute_inplace =
00201 "__kernel void trans_lower_triangular_substitute_inplace(\n"
00202 " __global const float * matrix,\n"
00203 " unsigned int matrix_rows,\n"
00204 " unsigned int matrix_cols,\n"
00205 " unsigned int matrix_internal_rows,\n"
00206 " unsigned int matrix_internal_cols,\n"
00207 " __global float * vector)\n"
00208 "{\n"
00209 " float temp;\n"
00210 " for (int row = 0; row < matrix_rows; ++row)\n"
00211 " {\n"
00212 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00213 " if (get_global_id(0) == 0)\n"
00214 " vector[row] /= matrix[row+row*matrix_internal_rows];\n"
00215 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00216 " temp = vector[row];\n"
00217 " for (int elim = row + get_global_id(0) + 1; elim < matrix_rows; elim += get_global_size(0))\n"
00218 " vector[elim] -= temp * matrix[elim * matrix_internal_rows + row];\n"
00219 " }\n"
00220 "}\n"
00221 ;
00222
00223 const char * const matrix_col_align1_inplace_divide =
00224 "__kernel void inplace_divide(\n"
00225 " __global float * vec,\n"
00226 " __global const float * fac, //note: CPU variant is mapped to prod_scalar\n"
00227 " unsigned int size) \n"
00228 "{ \n"
00229 " float factor = *fac;\n"
00230 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00231 " vec[i] /= factor;\n"
00232 "}\n"
00233 ;
00234
00235 const char * const matrix_col_align1_trans_upper_triangular_substitute_inplace =
00236 "//transposed lower triangular matrix\n"
00237 "__kernel void trans_upper_triangular_substitute_inplace(\n"
00238 " __global const float * matrix, \n"
00239 " unsigned int matrix_rows,\n"
00240 " unsigned int matrix_cols,\n"
00241 " unsigned int matrix_internal_rows,\n"
00242 " unsigned int matrix_internal_cols,\n"
00243 " __global float * vector) \n"
00244 "{ \n"
00245 " float temp; \n"
00246 " for (int row = matrix_rows-1; row > -1; --row) \n"
00247 " { \n"
00248 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00249 " if (get_global_id(0) == 0) \n"
00250 " vector[row] /= matrix[row + row*matrix_internal_rows]; \n"
00251 " \n"
00252 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00253 " temp = vector[row]; \n"
00254 " //eliminate column with index 'row' in parallel: \n"
00255 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00256 " vector[elim] -= temp * matrix[row + elim * matrix_internal_rows]; \n"
00257 " } \n"
00258 " \n"
00259 "}\n"
00260 ;
00261
00262 const char * const matrix_col_align1_unit_upper_triangular_substitute_inplace =
00263 "__kernel void unit_upper_triangular_substitute_inplace( \n"
00264 " __global const float * matrix, \n"
00265 " unsigned int matrix_rows,\n"
00266 " unsigned int matrix_cols,\n"
00267 " unsigned int matrix_internal_rows,\n"
00268 " unsigned int matrix_internal_cols,\n"
00269 " __global float * vector) \n"
00270 "{ \n"
00271 " float temp; \n"
00272 " for (int row = matrix_rows-1; row > -1; --row) \n"
00273 " { \n"
00274 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00275 " \n"
00276 " temp = vector[row]; \n"
00277 " //eliminate column with index 'row' in parallel: \n"
00278 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00279 " vector[elim] -= temp * matrix[elim + row * matrix_internal_rows]; \n"
00280 " } \n"
00281 " \n"
00282 "}\n"
00283 ;
00284
00285 const char * const matrix_col_align1_inplace_add =
00286 "__kernel void inplace_add(\n"
00287 " __global float * vec1,\n"
00288 " __global const float * vec2,\n"
00289 " unsigned int size) \n"
00290 "{ \n"
00291 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00292 " vec1[i] += vec2[i];\n"
00293 "}\n"
00294 ;
00295
00296 const char * const matrix_col_align1_trans_unit_lower_triangular_substitute_inplace =
00297 "\n"
00298 "__kernel void trans_unit_lower_triangular_substitute_inplace(\n"
00299 " __global const float * matrix,\n"
00300 " unsigned int matrix_rows,\n"
00301 " unsigned int matrix_cols,\n"
00302 " unsigned int matrix_internal_rows,\n"
00303 " unsigned int matrix_internal_cols,\n"
00304 " __global float * vector)\n"
00305 "{\n"
00306 " float temp;\n"
00307 " for (int row = 0; row < matrix_rows; ++row)\n"
00308 " {\n"
00309 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00310 "\n"
00311 " temp = vector[row];\n"
00312 "\n"
00313 " for (int elim = row + get_global_id(0) + 1; elim < matrix_rows; elim += get_global_size(0))\n"
00314 " vector[elim] -= temp * matrix[elim * matrix_internal_rows + row];\n"
00315 " }\n"
00316 "}\n"
00317 "\n"
00318 "\n"
00319 ;
00320
00321 const char * const matrix_col_align1_scaled_rank1_update =
00322 "__kernel void scaled_rank1_update(\n"
00323 " __global float * matrix,\n"
00324 " unsigned int matrix_rows,\n"
00325 " unsigned int matrix_cols,\n"
00326 " unsigned int matrix_internal_rows,\n"
00327 " unsigned int matrix_internal_cols,\n"
00328 " float val,\n"
00329 " __global const float * vector1, \n"
00330 " __global const float * vector2) \n"
00331 "{ \n"
00332 " float tmp;\n"
00333 " for (unsigned int row = get_global_id(0); row < matrix_rows; row += get_global_size(0))\n"
00334 " {\n"
00335 " tmp = val * vector1[row];\n"
00336 " for (unsigned int col = 0; col < matrix_cols; ++col)\n"
00337 " matrix[row + col*matrix_internal_rows] += tmp * vector2[col];\n"
00338 " }\n"
00339 "}\n"
00340 ;
00341
00342 const char * const matrix_col_align1_clear =
00343 "__kernel void clear(\n"
00344 " __global float * vec,\n"
00345 " unsigned int size) \n"
00346 "{ \n"
00347 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00348 " vec[i] = 0;\n"
00349 "}\n"
00350 ;
00351
00352 const char * const matrix_col_align1_cpu_inplace_mult =
00353 "__kernel void cpu_inplace_mult(\n"
00354 " __global float * vec,\n"
00355 " float factor, \n"
00356 " unsigned int size) \n"
00357 "{ \n"
00358 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00359 " vec[i] *= factor;\n"
00360 "}\n"
00361 ;
00362
00363 const char * const matrix_col_align1_inplace_mult =
00364 "__kernel void inplace_mult(\n"
00365 " __global float * vec,\n"
00366 " __global const float * fac, \n"
00367 " unsigned int size) \n"
00368 "{ \n"
00369 " float factor = *fac;\n"
00370 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00371 " vec[i] *= factor;\n"
00372 "}\n"
00373 ;
00374
00375 const char * const matrix_col_align1_upper_triangular_substitute_inplace =
00376 "__kernel void upper_triangular_substitute_inplace( \n"
00377 " __global const float * matrix, \n"
00378 " unsigned int matrix_rows,\n"
00379 " unsigned int matrix_cols,\n"
00380 " unsigned int matrix_internal_rows,\n"
00381 " unsigned int matrix_internal_cols,\n"
00382 " __global float * vector) \n"
00383 "{ \n"
00384 " float temp; \n"
00385 " for (int row = matrix_rows-1; row > -1; --row) \n"
00386 " { \n"
00387 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00388 " if (get_global_id(0) == 0) \n"
00389 " vector[row] /= matrix[row + row*matrix_internal_rows]; \n"
00390 " \n"
00391 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00392 " temp = vector[row]; \n"
00393 " //eliminate column with index 'row' in parallel: \n"
00394 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00395 " vector[elim] -= temp * matrix[elim + row * matrix_internal_rows]; \n"
00396 " } \n"
00397 " \n"
00398 "}\n"
00399 ;
00400
00401 }
00402 }
00403 }
00404 #endif