interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / scan_i32 / kernel.cu

65 lines · 1987 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Launch 1: each block of 256 scans its slice in shared memory (Hillis-Steele), writes the
3// block-local inclusive prefix to y and the block total to bsum[g].
4// Launch 2: one block 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#include <cuda_runtime.h>
7
8#define OAL_B 256
9
10__global__ void scan_blk(const int *x, int *y, int *bsum, long long n)
11{
12 __shared__ int s[OAL_B];
13 int l = threadIdx.x;
14 long long i = (long long)blockIdx.x * OAL_B + l;
15 s[l] = i < n ? x[i] : 0;
16 __syncthreads();
17 for (int off = 1; off < OAL_B; off <<= 1) {
18 int t = l >= off ? s[l - off] : 0;
19 __syncthreads();
20 s[l] += t;
21 __syncthreads();
22 }
23 if (i < n)
24 y[i] = s[l];
25 if (l == OAL_B - 1)
26 bsum[blockIdx.x] = s[l];
27}
28
29__global__ void scan_sums(int *bsum, long long nb)
30{
31 __shared__ int s[OAL_B];
32 int l = threadIdx.x;
33 int carry = 0;
34 for (long long base = 0; base < nb; base += OAL_B) {
35 long long j = base + l;
36 s[l] = j < nb ? bsum[j] : 0;
37 __syncthreads();
38 for (int off = 1; off < OAL_B; off <<= 1) {
39 int t = l >= off ? s[l - off] : 0;
40 __syncthreads();
41 s[l] += t;
42 __syncthreads();
43 }
44 if (j < nb)
45 bsum[j] = s[l] + carry;
46 carry += s[OAL_B - 1];
47 __syncthreads();
48 }
49}
50
51__global__ void scan_add(int *y, const int *bsum, long long n)
52{
53 long long i = (long long)blockIdx.x * OAL_B + threadIdx.x;
54 if (blockIdx.x > 0 && i < n)
55 y[i] += bsum[blockIdx.x - 1];
56}
57
58void oal_launch(const long long *p, void *const *buf)
59{
60 long long n = p[0], nb = p[1];
61 int *x = (int *)buf[0], *y = (int *)buf[1], *bsum = (int *)buf[2];
62 scan_blk<<<(unsigned)nb, OAL_B>>>(x, y, bsum, n);
63 scan_sums<<<1, OAL_B>>>(bsum, nb);
64 scan_add<<<(unsigned)nb, OAL_B>>>(y, bsum, n);
65}