interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / controls / scan_exclusive / kernel.cl

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