interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / gemm_nn / kernel.cu

33 lines · 1253 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2#include <cuda_runtime.h>
3
4#define TS 16
5
6// 16x16 tiles of A and B staged in static shared memory; out-of-range tile elements load as 0.0f,
7// which adds exact zeros to the accumulator.
8__global__ void gemm_nn(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];
12 int tx = threadIdx.x, ty = threadIdx.y;
13 int row = blockIdx.y * TS + ty, col = blockIdx.x * TS + tx;
14 float acc = 0.0f;
15 for (int k0 = 0; k0 < K; k0 += TS) {
16 As[ty][tx] = (row < M && k0 + tx < K) ? A[(long long)row * K + k0 + tx] : 0.0f;
17 Bs[ty][tx] = (k0 + ty < K && col < N) ? B[(long long)(k0 + ty) * N + col] : 0.0f;
18 __syncthreads();
19 for (int k = 0; k < TS; ++k)
20 acc += As[ty][k] * Bs[k][tx];
21 __syncthreads();
22 }
23 if (row < M && col < N)
24 C[(long long)row * N + col] = acc;
25}
26
27void oal_launch(const long long *p, void *const *buf)
28{
29 int M = (int)p[0], N = (int)p[1], K = (int)p[2];
30 dim3 grid((unsigned)((N + TS - 1) / TS), (unsigned)((M + TS - 1) / TS));
31 dim3 block(TS, TS);
32 gemm_nn<<<grid, block>>>((const float *)buf[0], (const float *)buf[1], (float *)buf[2], M, N, K);
33}