openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / scan_i32 / kernel.cl
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Launch 1: each work-group of 256 scans its slice in local memory (Hillis-Steele), writes the |
| 3 | // block-local inclusive prefix to y and the block total to bsum[g]. |
| 4 | // Launch 2: one work-group scans bsum in place (chunks of 256 with a running carry). |
| 5 | // Launch 3: every element of block g > 0 adds bsum[g - 1]. |
| 6 | |
| 7 | static void scan_tree(__local int *s, int l) |
| 8 | { |
| 9 | for (int off = 1; off < (int)get_local_size(0); off <<= 1) { |
| 10 | int t = l >= off ? s[l - off] : 0; |
| 11 | barrier(CLK_LOCAL_MEM_FENCE); |
| 12 | s[l] += t; |
| 13 | barrier(CLK_LOCAL_MEM_FENCE); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | __kernel void scan_blk(__global const int *x, __global int *y, __global int *bsum, const int n, __local int *s) |
| 18 | { |
| 19 | int l = get_local_id(0); |
| 20 | int i = get_global_id(0); |
| 21 | s[l] = i < n ? x[i] : 0; |
| 22 | barrier(CLK_LOCAL_MEM_FENCE); |
| 23 | scan_tree(s, l); |
| 24 | if (i < n) |
| 25 | y[i] = s[l]; |
| 26 | if (l == get_local_size(0) - 1) |
| 27 | bsum[get_group_id(0)] = s[l]; |
| 28 | } |
| 29 | |
| 30 | __kernel void scan_sums(__global int *bsum, const int nb, __local int *s) |
| 31 | { |
| 32 | int l = get_local_id(0); |
| 33 | int w = get_local_size(0); |
| 34 | int carry = 0; |
| 35 | for (int base = 0; base < nb; base += w) { |
| 36 | int j = base + l; |
| 37 | s[l] = j < nb ? bsum[j] : 0; |
| 38 | barrier(CLK_LOCAL_MEM_FENCE); |
| 39 | scan_tree(s, l); |
| 40 | if (j < nb) |
| 41 | bsum[j] = s[l] + carry; |
| 42 | carry += s[w - 1]; |
| 43 | barrier(CLK_LOCAL_MEM_FENCE); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | __kernel void scan_add(__global int *y, __global const int *bsum, const int n) |
| 48 | { |
| 49 | int i = get_global_id(0); |
| 50 | int g = get_group_id(0); |
| 51 | if (g > 0 && i < n) |
| 52 | y[i] += bsum[g - 1]; |
| 53 | } |