interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / batched_gemm / kernel.cu

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