openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / gemm_nn / kernel.cl
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // 16x16 tiles of A and B staged in __local memory; out-of-range tile elements load as 0.0f, which |
| 3 | // adds exact zeros to the accumulator. global is the tile-rounded (N, M) grid, local (16, 16). |
| 4 | #define TS 16 |
| 5 | __kernel void gemm_nn(__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 tx = get_local_id(0), ty = get_local_id(1); |
| 11 | int row = get_group_id(1) * TS + ty, col = get_group_id(0) * TS + tx; |
| 12 | float acc = 0.0f; |
| 13 | for (int k0 = 0; k0 < K; k0 += TS) { |
| 14 | As[ty][tx] = (row < M && k0 + tx < K) ? A[row * K + k0 + tx] : 0.0f; |
| 15 | Bs[ty][tx] = (k0 + ty < K && col < N) ? B[(k0 + ty) * N + col] : 0.0f; |
| 16 | barrier(CLK_LOCAL_MEM_FENCE); |
| 17 | for (int k = 0; k < TS; ++k) |
| 18 | acc += As[ty][k] * Bs[k][tx]; |
| 19 | barrier(CLK_LOCAL_MEM_FENCE); |
| 20 | } |
| 21 | if (row < M && col < N) |
| 22 | C[row * N + col] = acc; |
| 23 | } |