openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / tsoftmax16 / kernel.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | #include <cuda_runtime.h> |
| 3 | |
| 4 | // One block of 32 threads per row; max and sum by a shared-memory tree, every thread reaches every barrier. |
| 5 | __global__ void tsoftmax16(const float *x, float *y) |
| 6 | { |
| 7 | __shared__ float sh[32]; |
| 8 | long long r = blockIdx.x; |
| 9 | int j = threadIdx.x; |
| 10 | float v = x[r * 32 + j]; |
| 11 | sh[j] = v; |
| 12 | __syncthreads(); |
| 13 | for (int w = 16; w > 0; w /= 2) { |
| 14 | float o = 0.0f; |
| 15 | if (j < w) |
| 16 | o = fmaxf(sh[j], sh[j + w]); |
| 17 | __syncthreads(); |
| 18 | if (j < w) |
| 19 | sh[j] = o; |
| 20 | __syncthreads(); |
| 21 | } |
| 22 | float m = sh[0]; |
| 23 | __syncthreads(); |
| 24 | float e = expf(v - m); |
| 25 | sh[j] = e; |
| 26 | __syncthreads(); |
| 27 | for (int w = 16; w > 0; w /= 2) { |
| 28 | float o = 0.0f; |
| 29 | if (j < w) |
| 30 | o = sh[j] + sh[j + w]; |
| 31 | __syncthreads(); |
| 32 | if (j < w) |
| 33 | sh[j] = o; |
| 34 | __syncthreads(); |
| 35 | } |
| 36 | y[r * 32 + j] = e / sh[0]; |
| 37 | } |
| 38 | |
| 39 | void oal_launch(const long long *p, void *const *buf) |
| 40 | { |
| 41 | long long tiles = p[0]; |
| 42 | tsoftmax16<<<(unsigned)(tiles * 16), 32>>>((const float *)buf[0], (float *)buf[1]); |
| 43 | } |