openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / gemm_nt / kernel.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | #include <cuda_runtime.h> |
| 3 | |
| 4 | #define TS 16 |
| 5 | |
| 6 | // C = A B^T. 16x16 tiles of A and of B^T staged in static shared memory; out-of-range tile elements |
| 7 | // load as 0.0f, which adds exact zeros to the accumulator. |
| 8 | __global__ void gemm_nt(const float *A, const float *B, float *C, int M, int N, int K) |
| 9 | { |
| 10 | __shared__ float As[TS][TS]; |
| 11 | __shared__ float Bs[TS][TS]; // Bs[k][n] = B[n][k] |
| 12 | int tx = threadIdx.x, ty = threadIdx.y; |
| 13 | int row = blockIdx.y * TS + ty, col = blockIdx.x * TS + tx; |
| 14 | int bn = blockIdx.x * TS + ty; // B row this thread stages |
| 15 | float acc = 0.0f; |
| 16 | for (int k0 = 0; k0 < K; k0 += TS) { |
| 17 | As[ty][tx] = (row < M && k0 + tx < K) ? A[(long long)row * K + k0 + tx] : 0.0f; |
| 18 | Bs[tx][ty] = (bn < N && k0 + tx < K) ? B[(long long)bn * K + k0 + tx] : 0.0f; |
| 19 | __syncthreads(); |
| 20 | for (int k = 0; k < TS; ++k) |
| 21 | acc += As[ty][k] * Bs[k][tx]; |
| 22 | __syncthreads(); |
| 23 | } |
| 24 | if (row < M && col < N) |
| 25 | C[(long long)row * N + col] = acc; |
| 26 | } |
| 27 | |
| 28 | void oal_launch(const long long *p, void *const *buf) |
| 29 | { |
| 30 | int M = (int)p[0], N = (int)p[1], K = (int)p[2]; |
| 31 | dim3 grid((unsigned)((N + TS - 1) / TS), (unsigned)((M + TS - 1) / TS)); |
| 32 | dim3 block(TS, TS); |
| 33 | gemm_nt<<<grid, block>>>((const float *)buf[0], (const float *)buf[1], (float *)buf[2], M, N, K); |
| 34 | } |