interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / reduce_sum / kernel.cl

34 lines · 1118 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Pass 1: each work-group of 256 sums its slice in a local-memory tree and writes one partial.
3// Pass 2: one work-group sums the nb partials (strided, then a tree) into y[0].
4__kernel void rsum_part(__global const float *x, __global float *part, const int n, __local float *s)
5{
6 int l = get_local_id(0);
7 int i = get_global_id(0);
8 s[l] = i < n ? x[i] : 0.0f;
9 barrier(CLK_LOCAL_MEM_FENCE);
10 for (int k = get_local_size(0) / 2; k > 0; k >>= 1) {
11 if (l < k)
12 s[l] += s[l + k];
13 barrier(CLK_LOCAL_MEM_FENCE);
14 }
15 if (l == 0)
16 part[get_group_id(0)] = s[0];
17}
18
19__kernel void rsum_fin(__global const float *part, __global float *y, const int nb, __local float *s)
20{
21 int l = get_local_id(0);
22 float a = 0.0f;
23 for (int j = l; j < nb; j += get_local_size(0))
24 a += part[j];
25 s[l] = a;
26 barrier(CLK_LOCAL_MEM_FENCE);
27 for (int k = get_local_size(0) / 2; k > 0; k >>= 1) {
28 if (l < k)
29 s[l] += s[l + k];
30 barrier(CLK_LOCAL_MEM_FENCE);
31 }
32 if (l == 0)
33 y[0] = s[0];
34}