00001 #ifndef VIENNACL_LINALG_KERNELS_MATRIX_ROW_SOURCE_HPP_
00002 #define VIENNACL_LINALG_KERNELS_MATRIX_ROW_SOURCE_HPP_
00003
00004 namespace viennacl
00005 {
00006 namespace linalg
00007 {
00008 namespace kernels
00009 {
00010 const char * const matrix_row_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[elim * matrix_internal_cols + row];\n"
00026 " }\n"
00027 "}\n"
00028 ;
00029
00030 const char * const matrix_row_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_row_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_cols];\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[elim * matrix_internal_cols + row];\n"
00060 " }\n"
00061 "}\n"
00062 ;
00063
00064 const char * const matrix_row_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 + col*matrix_internal_cols] * vector[col];\n"
00080 " result[row] = dot_prod2;\n"
00081 " }\n"
00082 "}\n"
00083 ;
00084
00085 const char * const matrix_row_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 " unsigned int offset;\n"
00098 " for (unsigned int row = get_global_id(0); row < matrix_rows; row += get_global_size(0))\n"
00099 " {\n"
00100 " tmp = vector1[row];\n"
00101 " offset = row*matrix_internal_cols;\n"
00102 " for (unsigned int col = 0; col < matrix_cols; ++col)\n"
00103 " matrix[offset+col] += tmp * vector2[col];\n"
00104 " }\n"
00105 "}\n"
00106 ;
00107
00108 const char * const matrix_row_align1_sub =
00109 "__kernel void sub(\n"
00110 " __global const float * vec1,\n"
00111 " __global const float * vec2, \n"
00112 " __global float * result,\n"
00113 " unsigned int size)\n"
00114 "{ \n"
00115 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00116 " result[i] = vec1[i] - vec2[i];\n"
00117 "}\n"
00118 ;
00119
00120 const char * const matrix_row_align1_trans_unit_upper_triangular_substitute_inplace =
00121 "//transposed lower triangular matrix\n"
00122 "__kernel void trans_unit_upper_triangular_substitute_inplace(\n"
00123 " __global const float * matrix, \n"
00124 " unsigned int matrix_rows,\n"
00125 " unsigned int matrix_cols,\n"
00126 " unsigned int matrix_internal_rows,\n"
00127 " unsigned int matrix_internal_cols,\n"
00128 " __global float * vector) \n"
00129 "{ \n"
00130 " float temp; \n"
00131 " for (int row = matrix_rows-1; row > -1; --row) \n"
00132 " { \n"
00133 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00134 " temp = vector[row]; \n"
00135 " //eliminate column with index 'row' in parallel: \n"
00136 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00137 " vector[elim] -= temp * matrix[row * matrix_internal_cols + elim]; \n"
00138 " } \n"
00139 " \n"
00140 "}\n"
00141 ;
00142
00143 const char * const matrix_row_align1_lu_factorize =
00144 "__kernel void lu_factorize(\n"
00145 " __global float * matrix,\n"
00146 " unsigned int matrix_rows,\n"
00147 " unsigned int matrix_cols,\n"
00148 " unsigned int matrix_internal_rows,\n"
00149 " unsigned int matrix_internal_cols) \n"
00150 "{ \n"
00151 " float temp;\n"
00152 " unsigned rowi;\n"
00153 " unsigned rowk;\n"
00154 " for (unsigned int i=1; i<matrix_rows; ++i)\n"
00155 " {\n"
00156 " rowi = i * matrix_internal_cols;\n"
00157 " for (unsigned int k=0; k<i; ++k)\n"
00158 " {\n"
00159 " rowk = k * matrix_internal_cols;\n"
00160 " if (get_global_id(0) == 0)\n"
00161 " matrix[rowi + k] /= matrix[rowk + k];\n"
00162 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00163 " temp = matrix[rowi + k];\n"
00164 " \n"
00165 " //parallel subtraction:\n"
00166 " for (unsigned int j=k+1 + get_global_id(0); j<matrix_rows; j += get_global_size(0))\n"
00167 " matrix[rowi + j] -= temp * matrix[rowk + j];\n"
00168 " }\n"
00169 " }\n"
00170 "} \n"
00171 ;
00172
00173 const char * const matrix_row_align1_add =
00174 "__kernel void add(\n"
00175 " __global const float * vec1,\n"
00176 " __global const float * vec2, \n"
00177 " __global float * result,\n"
00178 " unsigned int size) \n"
00179 "{ \n"
00180 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00181 " result[i] = vec1[i] + vec2[i];\n"
00182 "}\n"
00183 ;
00184
00185 const char * const matrix_row_align1_fft_direct =
00186 "// naive fourier transform (quadratic complexity, use for reference only)\n"
00187 "__kernel void fft_direct(__global float2* input,\n"
00188 " __global float2* output,\n"
00189 " unsigned int size,\n"
00190 " unsigned int stride,\n"
00191 " unsigned int batch_num,\n"
00192 " float sign) {\n"
00193 "// unsigned int base_offset = 0;\n"
00194 " const float NUM_PI = 3.14159265358979323846;\n"
00195 " \n"
00196 " for(unsigned int batch_id = 0; batch_id < batch_num; batch_id++) {\n"
00197 " for(unsigned int k = get_global_id(0); k < size; k += get_global_size(0)) {\n"
00198 " float2 f = 0.0f;\n"
00199 " for(unsigned int n = 0; n < size; n++) {\n"
00200 " float2 in = input[batch_id * stride + n]; //input index here\n"
00201 " float sn, cs;\n"
00202 " float arg = sign * 2 * NUM_PI * k / size * n;\n"
00203 " sn = sincos(arg, &cs);\n"
00204 " float2 ex = (float2)(cs, sn);\n"
00205 " f = f + (float2)(in.x * ex.x - in.y * ex.y, in.x * ex.y + in.y * ex.x);\n"
00206 " }\n"
00207 " output[batch_id * stride + k] = f;// output index here\n"
00208 " }\n"
00209 "// base_offset += stride;\n"
00210 " }\n"
00211 "}\n"
00212 ;
00213
00214 const char * const matrix_row_align1_vec_mul =
00215 "\n"
00216 "\n"
00217 "\n"
00218 "__kernel void vec_mul(\n"
00219 " __global const float * matrix,\n"
00220 " unsigned int matrix_rows,\n"
00221 " unsigned int matrix_cols,\n"
00222 " unsigned int matrix_internal_rows,\n"
00223 " unsigned int matrix_internal_cols,\n"
00224 " __global const float * vector, \n"
00225 " __global float * result) \n"
00226 "{ \n"
00227 " for (unsigned int row = get_global_id(0); row < matrix_rows; row += get_global_size(0))\n"
00228 " {\n"
00229 " float dot_prod = 0.0f;\n"
00230 " for (unsigned int col = 0; col < matrix_cols; ++col)\n"
00231 " dot_prod += matrix[row*matrix_internal_cols + col] * vector[col];\n"
00232 " result[row] = dot_prod;\n"
00233 " }\n"
00234 "}\n"
00235 "\n"
00236 "\n"
00237 ;
00238
00239 const char * const matrix_row_align1_fft_radix2_local =
00240 "unsigned int get_reorder_num(unsigned int v, unsigned int bit_size) {\n"
00241 " v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);\n"
00242 " v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);\n"
00243 " v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);\n"
00244 " v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);\n"
00245 " v = (v >> 16) | (v << 16);\n"
00246 " v = v >> (32 - bit_size);\n"
00247 " return v;\n"
00248 "}\n"
00249 "__kernel void fft_radix2_local(__global float2* input,\n"
00250 " __local float2* lcl_input,\n"
00251 " unsigned int bit_size,\n"
00252 " unsigned int size,\n"
00253 " unsigned int stride,\n"
00254 " unsigned int batch_num,\n"
00255 " float sign) {\n"
00256 " unsigned int grp_id = get_group_id(0);\n"
00257 " unsigned int grp_num = get_num_groups(0);\n"
00258 " unsigned int lcl_sz = get_local_size(0);\n"
00259 " unsigned int lcl_id = get_local_id(0);\n"
00260 " const float NUM_PI = 3.14159265358979323846;\n"
00261 " for(unsigned int batch_id = grp_id; batch_id < batch_num; batch_id += grp_num) {\n"
00262 " //unsigned int base_offset = stride * batch_id;\n"
00263 " //copy chunk of global memory to local\n"
00264 " for(unsigned int p = lcl_id; p < size; p += lcl_sz) {\n"
00265 " unsigned int v = get_reorder_num(p, bit_size);\n"
00266 " lcl_input[v] = input[batch_id * stride + p];//index\n"
00267 " }\n"
00268 " barrier(CLK_LOCAL_MEM_FENCE);\n"
00269 " \n"
00270 " //performs Cooley-Tukey FFT on local array\n"
00271 " for(unsigned int s = 0; s < bit_size; s++) {\n"
00272 " unsigned int ss = 1 << s;\n"
00273 " float cs, sn;\n"
00274 " for(unsigned int tid = lcl_id; tid < size; tid += lcl_sz) {\n"
00275 " unsigned int group = (tid & (ss - 1));\n"
00276 " unsigned int pos = ((tid >> s) << (s + 1)) + group;\n"
00277 " float2 in1 = lcl_input[pos];\n"
00278 " float2 in2 = lcl_input[pos + ss];\n"
00279 " float arg = group * sign * NUM_PI / ss;\n"
00280 " sn = sincos(arg, &cs);\n"
00281 " float2 ex = (float2)(cs, sn);\n"
00282 " float2 tmp = (float2)(in2.x * ex.x - in2.y * ex.y, in2.x * ex.y + in2.y * ex.x);\n"
00283 " lcl_input[pos + ss] = in1 - tmp;\n"
00284 " lcl_input[pos] = in1 + tmp;\n"
00285 " }\n"
00286 " barrier(CLK_LOCAL_MEM_FENCE);\n"
00287 " }\n"
00288 " \n"
00289 " //copy local array back to global memory\n"
00290 " for(unsigned int p = lcl_id; p < size; p += lcl_sz) {\n"
00291 " input[batch_id * stride + p] = lcl_input[p];//index\n"
00292 " }\n"
00293 " }\n"
00294 "}\n"
00295 ;
00296
00297 const char * const matrix_row_align1_trans_lower_triangular_substitute_inplace =
00298 "__kernel void trans_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 " if (get_global_id(0) == 0)\n"
00311 " vector[row] /= matrix[row+row*matrix_internal_cols];\n"
00312 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00313 " temp = vector[row];\n"
00314 " for (int elim = row + get_global_id(0) + 1; elim < matrix_rows; elim += get_global_size(0))\n"
00315 " vector[elim] -= temp * matrix[row * matrix_internal_cols + elim];\n"
00316 " }\n"
00317 "}\n"
00318 ;
00319
00320 const char * const matrix_row_align1_inplace_divide =
00321 "__kernel void inplace_divide(\n"
00322 " __global float * vec,\n"
00323 " __global const float * fac, //note: CPU variant is mapped to prod_scalar\n"
00324 " unsigned int size) \n"
00325 "{ \n"
00326 " float factor = *fac;\n"
00327 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00328 " vec[i] /= factor;\n"
00329 "}\n"
00330 ;
00331
00332 const char * const matrix_row_align1_trans_upper_triangular_substitute_inplace =
00333 "//transposed lower triangular matrix\n"
00334 "__kernel void trans_upper_triangular_substitute_inplace(\n"
00335 " __global const float * matrix, \n"
00336 " unsigned int matrix_rows,\n"
00337 " unsigned int matrix_cols,\n"
00338 " unsigned int matrix_internal_rows,\n"
00339 " unsigned int matrix_internal_cols,\n"
00340 " __global float * vector) \n"
00341 "{ \n"
00342 " float temp; \n"
00343 " for (int row = matrix_rows-1; row > -1; --row) \n"
00344 " { \n"
00345 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00346 " if (get_global_id(0) == 0) \n"
00347 " vector[row] /= matrix[row*matrix_internal_cols + row]; \n"
00348 " \n"
00349 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00350 " temp = vector[row]; \n"
00351 " //eliminate column with index 'row' in parallel: \n"
00352 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00353 " vector[elim] -= temp * matrix[row * matrix_internal_cols + elim]; \n"
00354 " } \n"
00355 " \n"
00356 "}\n"
00357 ;
00358
00359 const char * const matrix_row_align1_unit_upper_triangular_substitute_inplace =
00360 "__kernel void unit_upper_triangular_substitute_inplace( \n"
00361 " __global const float * matrix, \n"
00362 " unsigned int matrix_rows,\n"
00363 " unsigned int matrix_cols,\n"
00364 " unsigned int matrix_internal_rows,\n"
00365 " unsigned int matrix_internal_cols,\n"
00366 " __global float * vector) \n"
00367 "{ \n"
00368 " float temp; \n"
00369 " for (int row = matrix_rows-1; row > -1; --row) \n"
00370 " { \n"
00371 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00372 " temp = vector[row]; \n"
00373 " //eliminate column with index 'row' in parallel: \n"
00374 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00375 " vector[elim] -= temp * matrix[elim * matrix_internal_cols + row]; \n"
00376 " } \n"
00377 " \n"
00378 "}\n"
00379 ;
00380
00381 const char * const matrix_row_align1_inplace_add =
00382 "__kernel void inplace_add(\n"
00383 " __global float * A,\n"
00384 " unsigned int A_row_start,\n"
00385 " unsigned int A_col_start,\n"
00386 " unsigned int A_row_size,\n"
00387 " unsigned int A_col_size,\n"
00388 " unsigned int A_internal_rows,\n"
00389 " unsigned int A_internal_cols,\n"
00390 " __global const float * B, \n"
00391 " unsigned int B_row_start,\n"
00392 " unsigned int B_col_start,\n"
00393 " unsigned int B_row_size,\n"
00394 " unsigned int B_col_size,\n"
00395 " unsigned int B_internal_rows,\n"
00396 " unsigned int B_internal_cols)\n"
00397 "{ \n"
00398 " if ( get_global_id(0) < A_row_size\n"
00399 " && get_global_id(1) < A_col_size\n"
00400 " )\n"
00401 " A[ (get_global_id(0) + A_row_start) * A_internal_cols\n"
00402 " + (get_global_id(1) + A_col_start)] \n"
00403 " += B[ (get_global_id(0) + B_row_start) * B_internal_cols\n"
00404 " + (get_global_id(1) + B_col_start)];\n"
00405 "}\n"
00406 ;
00407
00408 const char * const matrix_row_align1_trans_unit_lower_triangular_substitute_inplace =
00409 "\n"
00410 "__kernel void trans_unit_lower_triangular_substitute_inplace(\n"
00411 " __global const float * matrix,\n"
00412 " unsigned int matrix_rows,\n"
00413 " unsigned int matrix_cols,\n"
00414 " unsigned int matrix_internal_rows,\n"
00415 " unsigned int matrix_internal_cols,\n"
00416 " __global float * vector)\n"
00417 "{\n"
00418 " float temp;\n"
00419 " for (int row = 0; row < matrix_rows; ++row)\n"
00420 " {\n"
00421 " barrier(CLK_GLOBAL_MEM_FENCE);\n"
00422 "\n"
00423 " temp = vector[row];\n"
00424 "\n"
00425 " for (int elim = row + get_global_id(0) + 1; elim < matrix_rows; elim += get_global_size(0))\n"
00426 " vector[elim] -= temp * matrix[row * matrix_internal_cols + elim];\n"
00427 " }\n"
00428 "}\n"
00429 "\n"
00430 "\n"
00431 ;
00432
00433 const char * const matrix_row_align1_scaled_rank1_update =
00434 "__kernel void scaled_rank1_update(\n"
00435 " __global float * matrix,\n"
00436 " unsigned int matrix_rows,\n"
00437 " unsigned int matrix_cols,\n"
00438 " unsigned int matrix_internal_rows,\n"
00439 " unsigned int matrix_internal_cols,\n"
00440 " float val,\n"
00441 " __global const float * vector1, \n"
00442 " __global const float * vector2) \n"
00443 "{ \n"
00444 " float tmp;\n"
00445 " unsigned int offset;\n"
00446 " for (unsigned int row = get_global_id(0); row < matrix_rows; row += get_global_size(0))\n"
00447 " {\n"
00448 " tmp = val * vector1[row];\n"
00449 " offset = row * matrix_internal_cols;\n"
00450 " for (unsigned int col = 0; col < matrix_cols; ++col)\n"
00451 " matrix[offset+col] += tmp * vector2[col];\n"
00452 " }\n"
00453 "}\n"
00454 ;
00455
00456 const char * const matrix_row_align1_clear =
00457 "__kernel void clear(\n"
00458 " __global float * vec,\n"
00459 " unsigned int size) \n"
00460 "{ \n"
00461 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00462 " vec[i] = 0;\n"
00463 "}\n"
00464 ;
00465
00466 const char * const matrix_row_align1_fft_radix2 =
00467 "__kernel void fft_radix2(__global float2* input,\n"
00468 " unsigned int s,\n"
00469 " unsigned int bit_size,\n"
00470 " unsigned int size,\n"
00471 " unsigned int stride,\n"
00472 " unsigned int batch_num,\n"
00473 " float sign) {\n"
00474 " unsigned int ss = 1 << s;\n"
00475 " unsigned int half_size = size >> 1;\n"
00476 " float cs, sn;\n"
00477 " const float NUM_PI = 3.14159265358979323846;\n"
00478 " unsigned int glb_id = get_global_id(0);\n"
00479 " unsigned int glb_sz = get_global_size(0);\n"
00480 " \n"
00481 "// unsigned int base_offset = 0;\n"
00482 " \n"
00483 " for(unsigned int batch_id = 0; batch_id < batch_num; batch_id++) {\n"
00484 " for(unsigned int tid = glb_id; tid < half_size; tid += glb_sz) {\n"
00485 " unsigned int group = (tid & (ss - 1));\n"
00486 " unsigned int pos = ((tid >> s) << (s + 1)) + group;\n"
00487 " unsigned int offset = batch_id * stride + pos;\n"
00488 " float2 in1 = input[offset];//index\n"
00489 " float2 in2 = input[offset + ss];//index\n"
00490 " float arg = group * sign * NUM_PI / ss;\n"
00491 " sn = sincos(arg, &cs);\n"
00492 " //sn = native_sin(arg);\n"
00493 " //cs = native_cos(arg);\n"
00494 " float2 ex = (float2)(cs, sn);\n"
00495 " float2 tmp = (float2)(in2.x * ex.x - in2.y * ex.y, in2.x * ex.y + in2.y * ex.x);\n"
00496 " input[offset + ss] = in1 - tmp;//index\n"
00497 " input[offset] = in1 + tmp;//index\n"
00498 " }\n"
00499 "// base_offset += stride;\n"
00500 " }\n"
00501 "}\n"
00502 ;
00503
00504 const char * const matrix_row_align1_cpu_inplace_mult =
00505 "__kernel void cpu_inplace_mult(\n"
00506 " __global float * vec,\n"
00507 " float factor, \n"
00508 " unsigned int size) \n"
00509 "{ \n"
00510 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00511 " vec[i] *= factor;\n"
00512 "}\n"
00513 ;
00514
00515 const char * const matrix_row_align1_fft_reorder =
00516 "/*\n"
00517 "* Performs reordering of input data in bit-reversal order\n"
00518 "* Probably it's better to do in host side,\n"
00519 "*/\n"
00520 "unsigned int get_reorder_num_2(unsigned int v, unsigned int bit_size) {\n"
00521 " v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);\n"
00522 " v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);\n"
00523 " v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);\n"
00524 " v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);\n"
00525 " v = (v >> 16) | (v << 16);\n"
00526 " v = v >> (32 - bit_size);\n"
00527 " return v;\n"
00528 "}\n"
00529 "__kernel void fft_reorder(__global float2* input,\n"
00530 " unsigned int bit_size,\n"
00531 " unsigned int size,\n"
00532 " unsigned int stride,\n"
00533 " int batch_num) {\n"
00534 " //unsigned int base_offset = 0;\n"
00535 " unsigned int glb_id = get_global_id(0);\n"
00536 " unsigned int glb_sz = get_global_size(0);\n"
00537 " \n"
00538 " for(unsigned int batch_id = 0; batch_id < batch_num; batch_id++) {\n"
00539 " for(unsigned int i = glb_id; i < size; i += glb_sz) {\n"
00540 " unsigned int v = get_reorder_num_2(i, bit_size);\n"
00541 " if(i < v) {\n"
00542 " float2 tmp = input[batch_id * stride + i]; // index\n"
00543 " input[batch_id * stride + i] = input[batch_id * stride + v]; //index\n"
00544 " input[batch_id * stride + v] = tmp; //index\n"
00545 " }\n"
00546 " }\n"
00547 " //base_offset += stride;\n"
00548 " }\n"
00549 "}\n"
00550 ;
00551
00552 const char * const matrix_row_align1_inplace_mult =
00553 "__kernel void inplace_mult(\n"
00554 " __global float * vec,\n"
00555 " __global const float * fac, \n"
00556 " unsigned int size) \n"
00557 "{ \n"
00558 " float factor = *fac;\n"
00559 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
00560 " vec[i] *= factor;\n"
00561 "}\n"
00562 ;
00563
00564 const char * const matrix_row_align1_upper_triangular_substitute_inplace =
00565 "__kernel void upper_triangular_substitute_inplace( \n"
00566 " __global const float * matrix, \n"
00567 " unsigned int matrix_rows,\n"
00568 " unsigned int matrix_cols,\n"
00569 " unsigned int matrix_internal_rows,\n"
00570 " unsigned int matrix_internal_cols,\n"
00571 " __global float * vector) \n"
00572 "{ \n"
00573 " float temp; \n"
00574 " for (int row = matrix_rows-1; row > -1; --row) \n"
00575 " { \n"
00576 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00577 " if (get_global_id(0) == 0) \n"
00578 " vector[row] /= matrix[row*matrix_internal_cols + row]; \n"
00579 " \n"
00580 " barrier(CLK_GLOBAL_MEM_FENCE); \n"
00581 " temp = vector[row]; \n"
00582 " //eliminate column with index 'row' in parallel: \n"
00583 " for (int elim = get_global_id(0); elim < row; elim += get_global_size(0)) \n"
00584 " vector[elim] -= temp * matrix[elim * matrix_internal_cols + row]; \n"
00585 " } \n"
00586 " \n"
00587 "}\n"
00588 ;
00589
00590 }
00591 }
00592 }
00593 #endif