ocl_kernels_vector.hpp 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
// **************************************************************************
//
//    PARALUTION   www.paralution.com
//
//    Copyright (C) 2015  PARALUTION Labs UG (haftungsbeschränkt) & Co. KG
//                        Am Hasensprung 6, 76571 Gaggenau
//                        Handelsregister: Amtsgericht Mannheim, HRA 706051
//                        Vertreten durch:
//                        PARALUTION Labs Verwaltungs UG (haftungsbeschränkt)
//                        Am Hasensprung 6, 76571 Gaggenau
//                        Handelsregister: Amtsgericht Mannheim, HRB 721277
//                        Geschäftsführer: Dimitar Lukarski, Nico Trost
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// **************************************************************************



// PARALUTION version 1.1.0 


#ifndef PARALUTION_OCL_KERNELS_VECTOR_HPP_
#define PARALUTION_OCL_KERNELS_VECTOR_HPP_

namespace paralution {

const char *ocl_kernels_vector =
	"__kernel void kernel_scale(const IndexType size, const ValueType alpha, __global ValueType *x) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    x[gid] = alpha * x[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_scaleadd(         const IndexType size,\n"
	"                                       const ValueType alpha,\n"
	"                              __global const ValueType *x,\n"
	"                              __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] = alpha * out[gid] + x[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_scaleaddscale(         const IndexType size,\n"
	"                                            const ValueType alpha,\n"
	"                                            const ValueType beta, \n"
	"                                   __global const ValueType *x,\n"
	"                                   __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] = alpha * out[gid] + beta * x[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_scaleaddscale_offset(         const IndexType size,\n"
	"                                                   const IndexType src_offset,\n"
	"                                                   const IndexType dst_offset, \n"
	"                                                   const ValueType alpha,\n"
	"                                                   const ValueType beta, \n"
	"                                          __global const ValueType *x,\n"
	"                                          __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid+dst_offset] = alpha * out[gid+dst_offset] + beta * x[gid+src_offset];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_scaleadd2(         const IndexType size,\n"
	"                                        const ValueType alpha,\n"
	"                                        const ValueType beta,\n"
	"                                        const ValueType gamma,\n"
	"                               __global const ValueType *x,\n"
	"                               __global const ValueType *y,\n"
	"                               __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] = alpha * out[gid] + beta * x[gid] + gamma * y[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_pointwisemult(         const IndexType size,\n"
	"                                   __global const ValueType *x,\n"
	"                                   __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] = out[gid] * x[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_pointwisemult2(         const IndexType size,\n"
	"                                    __global const ValueType *x,\n"
	"                                    __global const ValueType *y,\n"
	"                                    __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] = y[gid] * x[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_copy_offset_from(         const IndexType size,\n"
	"                                               const IndexType src_offset,\n"
	"                                               const IndexType dst_offset,\n"
	"                                      __global const ValueType *in,\n"
	"                                      __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid+dst_offset] = in[gid+src_offset];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_permute(         const IndexType size,\n"
	"                             __global const IndexType *permute,\n"
	"                             __global const ValueType *in,\n"
	"                             __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[permute[gid]] = in[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_permute_backward(         const IndexType size,\n"
	"                                      __global const IndexType *permute,\n"
	"                                      __global const ValueType *in,\n"
	"                                      __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] = in[permute[gid]];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_dot(         const IndexType  size,\n"
	"                         __global const ValueType *x,\n"
	"                         __global const ValueType *y,\n"
	"                         __global       ValueType *out,\n"
	"                                  const IndexType GROUP_SIZE,\n"
	"                                  const IndexType  LOCAL_SIZE) {\n"
	"\n"
	"    IndexType tid = get_local_id(0);\n"
	"\n"
	"    __local ValueType sdata[BLOCK_SIZE];\n"
	"    sdata[tid] = (ValueType) 0;\n"
	"\n"
	"    IndexType group_id = GROUP_SIZE * get_group_id(0);\n"
	"    IndexType gid = group_id + tid;\n"
	"\n"
	"    for (IndexType i = 0; i < LOCAL_SIZE; ++i, gid += BLOCK_SIZE) {\n"
	"\n"
	"      if (gid < size)\n"
	"        sdata[tid] += x[gid] * y[gid];\n"
	"      else\n"
	"        i = LOCAL_SIZE;\n"
	"\n"
	"    }\n"
	"\n"
	"    barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    for (IndexType i = BLOCK_SIZE/2; i > 0; i /= 2) {\n"
	"\n"
	"      if (tid < i)\n"
	"        sdata[tid] += sdata[tid + i];\n"
	"\n"
	"      barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    }\n"
	"\n"
	"    if (tid == 0)\n"
	"      out[get_group_id(0)] = sdata[tid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_dotc(         const IndexType size,\n"
	"                          __global const ValueType *x,\n"
	"                          __global const ValueType *y,\n"
	"                          __global       ValueType *out,\n"
	"                                   const IndexType GROUP_SIZE,\n"
	"                                   const IndexType LOCAL_SIZE) {\n"
	"\n"
	"    IndexType tid = get_local_id(0);\n"
	"\n"
	"    __local ValueType sdata[BLOCK_SIZE];\n"
	"    sdata[tid] = (ValueType) 0;\n"
	"\n"
	"    IndexType group_id = GROUP_SIZE * get_group_id(0);\n"
	"    IndexType gid = group_id + tid;\n"
	"\n"
	"    for (IndexType i = 0; i < LOCAL_SIZE; ++i, gid += BLOCK_SIZE) {\n"
	"\n"
	"      if (gid < size)\n"
	"        sdata[tid] += x[gid] * y[gid];\n"
	"      else\n"
	"        i = LOCAL_SIZE;\n"
	"\n"
	"    }\n"
	"\n"
	"    barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    for (IndexType i = BLOCK_SIZE/2; i > 0; i /= 2) {\n"
	"\n"
	"      if (tid < i)\n"
	"        sdata[tid] += sdata[tid + i];\n"
	"\n"
	"      barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    }\n"
	"\n"
	"    if (tid == 0)\n"
	"      out[get_group_id(0)] = sdata[tid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_norm(        const IndexType size,\n"
	"                         __global const ValueType *x,\n"
	"                         __global       ValueType *out,\n"
	"                                  const IndexType GROUP_SIZE,\n"
	"                                  const IndexType  LOCAL_SIZE) {\n"
	"\n"
	"    IndexType tid = get_local_id(0);\n"
	"    __local ValueType sdata[BLOCK_SIZE];\n"
	"    sdata[tid] = (ValueType) 0;\n"
	"\n"
	"    IndexType group_id = GROUP_SIZE * get_group_id(0);\n"
	"    IndexType gid = group_id + tid;\n"
	"\n"
	"    for (IndexType i = 0; i < LOCAL_SIZE; ++i, gid += BLOCK_SIZE) {\n"
	"\n"
	"      if (gid < size)\n"
	"        sdata[tid] += x[gid] * x[gid];\n"
	"      else\n"
	"        i = LOCAL_SIZE;\n"
	"\n"
	"    }\n"
	"\n"
	"    barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    for (IndexType i = BLOCK_SIZE/2; i > 0; i /= 2) {\n"
	"\n"
	"      if (tid < i)\n"
	"        sdata[tid] += sdata[tid + i];\n"
	"\n"
	"      barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    }\n"
	"\n"
	"    if (tid == 0)\n"
	"      out[get_group_id(0)] = sdata[tid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_axpy(         const IndexType size,\n"
	"                                   const ValueType alpha,\n"
	"                          __global const ValueType *x,\n"
	"                          __global       ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < size)\n"
	"    out[gid] += alpha * x[gid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_reduce(         const IndexType size,\n"
	"                            __global const ValueType *data,\n"
	"                            __global       ValueType *out,\n"
	"                                     const IndexType GROUP_SIZE,\n"
	"                                     const IndexType LOCAL_SIZE) {\n"
	"\n"
	"    IndexType tid = get_local_id(0);\n"
	"    __local ValueType sdata[BLOCK_SIZE];\n"
	"    sdata[tid] = (ValueType) 0;\n"
	"\n"
	"    IndexType gid = GROUP_SIZE * get_group_id(0) + tid;\n"
	"\n"
	"    for (IndexType i = 0; i < LOCAL_SIZE; ++i, gid += BLOCK_SIZE) {\n"
	"\n"
	"      if (gid < size)\n"
	"        sdata[tid] += data[gid];\n"
	"      else\n"
	"        i = LOCAL_SIZE;\n"
	"\n"
	"    }\n"
	"\n"
	"    barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    for (IndexType i = BLOCK_SIZE/2; i > 0; i /= 2) {\n"
	"\n"
	"      if ( tid < i )\n"
	"        sdata[tid] += sdata[tid + i];\n"
	"\n"
	"      barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    }\n"
	"\n"
	"    if (tid == 0)\n"
	"      out[get_group_id(0)] = sdata[tid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_asum(         const IndexType size,\n"
	"                          __global const ValueType *data,\n"
	"                          __global       ValueType *out,\n"
	"                                   const IndexType GROUP_SIZE,\n"
	"                                   const IndexType LOCAL_SIZE) {\n"
	"\n"
	"    IndexType tid = get_local_id(0);\n"
	"    __local ValueType sdata[BLOCK_SIZE];\n"
	"    sdata[tid] = (ValueType) 0;\n"
	"\n"
	"    IndexType gid = GROUP_SIZE * get_group_id(0) + tid;\n"
	"\n"
	"    for (IndexType i = 0; i < LOCAL_SIZE; ++i, gid += BLOCK_SIZE) {\n"
	"\n"
	"      if (gid < size)\n"
	"        sdata[tid] += fabs(data[gid]);\n"
	"      else\n"
	"        i = LOCAL_SIZE;\n"
	"\n"
	"    }\n"
	"\n"
	"    barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    for (IndexType i = BLOCK_SIZE/2; i > 0; i /= 2) {\n"
	"\n"
	"      if (tid < i)\n"
	"        sdata[tid] += sdata[tid + i];\n"
	"\n"
	"      barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    }\n"
	"\n"
	"    if (tid == 0)\n"
	"      out[get_group_id(0)] = sdata[tid];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_amax(         const IndexType size,\n"
	"                          __global const ValueType *data,\n"
	"                          __global       ValueType *out,\n"
	"                          __global       IndexType *iout,\n"
	"                                   const IndexType GROUP_SIZE,\n"
	"                                   const IndexType LOCAL_SIZE) {\n"
	"\n"
	"    IndexType tid = get_local_id(0);\n"
	"    __local IndexType idata[BLOCK_SIZE];\n"
	"    __local ValueType sdata[BLOCK_SIZE];\n"
	"    sdata[tid] = (ValueType) 0;\n"
	"    idata[tid] = 0;\n"
	"\n"
	"    IndexType gid = GROUP_SIZE * get_group_id(0) + tid;\n"
	"\n"
	"    for (IndexType i = 0; i < LOCAL_SIZE; ++i, gid += BLOCK_SIZE) {\n"
	"\n"
	"      if (gid < size) {\n"
	"        ValueType tmp = data[gid];\n"
	"        if (fabs(tmp) > fabs(sdata[tid])) {\n"
	"          sdata[tid] = fabs(tmp);\n"
	"          idata[tid] = gid;\n"
	"        }\n"
	"      }\n"
	"\n"
	"    }\n"
	"\n"
	"    barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    for (IndexType i = BLOCK_SIZE/2; i > 0; i /= 2) {\n"
	"\n"
	"      if (tid < i) {\n"
	"        ValueType tmp = sdata[tid+i];\n"
	"        if (fabs(tmp) > fabs(sdata[tid])) {\n"
	"          sdata[tid] = fabs(tmp);\n"
	"          idata[tid] = idata[tid+i];\n"
	"        }\n"
	"      }\n"
	"\n"
	"      barrier(CLK_LOCAL_MEM_FENCE);\n"
	"\n"
	"    }\n"
	"\n"
	"    if (tid == 0) {\n"
	"      out[get_group_id(0)] = sdata[tid];\n"
	"      iout[get_group_id(0)] = idata[tid];\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_power(const IndexType n, const double power, __global ValueType *out) {\n"
	"\n"
	"  IndexType gid = get_global_id(0);\n"
	"\n"
	"  if (gid < n)\n"
	"    out[gid] = pow(out[gid], (ValueType) power);\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_copy_from_float(const IndexType n, __global const float *in, __global ValueType *out) {\n"
	"\n"
	"  IndexType ind = get_global_id(0);\n"
	"\n"
	"  if (ind < n)\n"
	"    out[ind] = (ValueType) in[ind];\n"
	"\n"
	"}\n"
	"\n"
	"__kernel void kernel_copy_from_double(const IndexType n, __global const double *in, __global ValueType *out) {\n"
	"\n"
	"  IndexType ind = get_global_id(0);\n"
	"\n"
	"  if (ind < n)\n"
	"    out[ind] = (ValueType) in[ind];\n"
	"\n"
	"}\n"
	"\n"
;
}

#endif // PARALUTION_OCL_KERNELS_VECTOR_HPP_