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.cu

47 lines · 1326 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Pass 1: each block of 256 sums its slice in a shared-memory tree and writes one partial.
3// Pass 2: one block sums the nb partials (strided, then a tree) into y[0].
4#include <cuda_runtime.h>
5
6#define OAL_B 256
7
8__global__ void rsum_part(const float *x, float *part, long long n)
9{
10 __shared__ float s[OAL_B];
11 int l = threadIdx.x;
12 long long i = (long long)blockIdx.x * OAL_B + l;
13 s[l] = i < n ? x[i] : 0.0f;
14 __syncthreads();
15 for (int k = OAL_B / 2; k > 0; k >>= 1) {
16 if (l < k)
17 s[l] += s[l + k];
18 __syncthreads();
19 }
20 if (l == 0)
21 part[blockIdx.x] = s[0];
22}
23
24__global__ void rsum_fin(const float *part, float *y, long long nb)
25{
26 __shared__ float s[OAL_B];
27 int l = threadIdx.x;
28 float a = 0.0f;
29 for (long long j = l; j < nb; j += OAL_B)
30 a += part[j];
31 s[l] = a;
32 __syncthreads();
33 for (int k = OAL_B / 2; k > 0; k >>= 1) {
34 if (l < k)
35 s[l] += s[l + k];
36 __syncthreads();
37 }
38 if (l == 0)
39 y[0] = s[0];
40}
41
42void oal_launch(const long long *p, void *const *buf)
43{
44 long long n = p[0], nb = p[1];
45 rsum_part<<<(unsigned)nb, OAL_B>>>((const float *)buf[0], (float *)buf[2], n);
46 rsum_fin<<<1, OAL_B>>>((const float *)buf[2], (float *)buf[1], nb);
47}