openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / batched_gemm / kernel.cl
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // One work-group per 16x16 tile of one batch member (dimension 2 is the batch index). Tiles of A and B |
| 3 | // are staged in __local memory; out-of-range tile elements load as 0.0f, adding exact zeros. |
| 4 | #define TS 16 |
| 5 | __kernel void batched_gemm(__global const float *A, __global const float *B, __global float *C, |
| 6 | const int M, const int N, const int K) |
| 7 | { |
| 8 | __local float As[TS][TS]; |
| 9 | __local float Bs[TS][TS]; |
| 10 | int bt = get_global_id(2); |
| 11 | __global const float *Ab = A + bt * M * K; |
| 12 | __global const float *Bb = B + bt * K * N; |
| 13 | int tx = get_local_id(0), ty = get_local_id(1); |
| 14 | int row = get_group_id(1) * TS + ty, col = get_group_id(0) * TS + tx; |
| 15 | float acc = 0.0f; |
| 16 | for (int k0 = 0; k0 < K; k0 += TS) { |
| 17 | As[ty][tx] = (row < M && k0 + tx < K) ? Ab[row * K + k0 + tx] : 0.0f; |
| 18 | Bs[ty][tx] = (k0 + ty < K && col < N) ? Bb[(k0 + ty) * N + col] : 0.0f; |
| 19 | barrier(CLK_LOCAL_MEM_FENCE); |
| 20 | for (int k = 0; k < TS; ++k) |
| 21 | acc += As[ty][k] * Bs[k][tx]; |
| 22 | barrier(CLK_LOCAL_MEM_FENCE); |
| 23 | } |
| 24 | if (row < M && col < N) |
| 25 | C[(bt * M + row) * N + col] = acc; |
| 26 | } |