interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / argmax / kernel.cl

52 lines · 1739 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// (value, index) pairs; (v2, i2) beats (v1, i1) when v2 > v1, or v2 == v1 and i2 < i1 (first maximum).
3// Pass 1: each work-group of 256 reduces its slice in local memory. Pass 2: one work-group reduces
4// the nb partials. Padding is (-INFINITY, INT_MAX), which never beats a real element.
5
6#define OAL_BEATS(v2, i2, v1, i1) ((v2) > (v1) || ((v2) == (v1) && (i2) < (i1)))
7
8static void amax_tree(__local float *sv, __local int *si, int l)
9{
10 for (int k = get_local_size(0) / 2; k > 0; k >>= 1) {
11 if (l < k && OAL_BEATS(sv[l + k], si[l + k], sv[l], si[l])) {
12 sv[l] = sv[l + k];
13 si[l] = si[l + k];
14 }
15 barrier(CLK_LOCAL_MEM_FENCE);
16 }
17}
18
19__kernel void amax_part(__global const float *x, __global float *pval, __global int *pidx, const int n,
20 __local float *sv, __local int *si)
21{
22 int l = get_local_id(0);
23 int i = get_global_id(0);
24 sv[l] = i < n ? x[i] : -INFINITY;
25 si[l] = i < n ? i : INT_MAX;
26 barrier(CLK_LOCAL_MEM_FENCE);
27 amax_tree(sv, si, l);
28 if (l == 0) {
29 pval[get_group_id(0)] = sv[0];
30 pidx[get_group_id(0)] = si[0];
31 }
32}
33
34__kernel void amax_fin(__global const float *pval, __global const int *pidx, __global int *y, const int nb,
35 __local float *sv, __local int *si)
36{
37 int l = get_local_id(0);
38 float bv = -INFINITY;
39 int bi = INT_MAX;
40 for (int j = l; j < nb; j += get_local_size(0)) {
41 if (OAL_BEATS(pval[j], pidx[j], bv, bi)) {
42 bv = pval[j];
43 bi = pidx[j];
44 }
45 }
46 sv[l] = bv;
47 si[l] = bi;
48 barrier(CLK_LOCAL_MEM_FENCE);
49 amax_tree(sv, si, l);
50 if (l == 0)
51 y[0] = si[0];
52}