openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / dot / kernel.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Pass 1: each block of 256 forms a[i]*b[i] and sums its slice in a shared-memory tree. |
| 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 dot_part(const float *a, const float *b, 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 ? a[i] * b[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 dot_fin(const float *part, float *y, long long nb) |
| 25 | { |
| 26 | __shared__ float s[OAL_B]; |
| 27 | int l = threadIdx.x; |
| 28 | float acc = 0.0f; |
| 29 | for (long long j = l; j < nb; j += OAL_B) |
| 30 | acc += part[j]; |
| 31 | s[l] = acc; |
| 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 | |
| 42 | void oal_launch(const long long *p, void *const *buf) |
| 43 | { |
| 44 | long long n = p[0], nb = p[1]; |
| 45 | dot_part<<<(unsigned)nb, OAL_B>>>((const float *)buf[0], (const float *)buf[1], (float *)buf[3], n); |
| 46 | dot_fin<<<1, OAL_B>>>((const float *)buf[3], (float *)buf[2], nb); |
| 47 | } |