compute-acceleration
GEMM and sum reduction across serial, OpenMP and CUDA
Loading...
Searching...
No Matches
gemm_tiled.cuh
1// Copyright (c) 2025 yanghuafang
2// SPDX-License-Identifier: MIT
3
4#ifndef ACCEL_CUDA_GEMM_TILED_CUH_
5#define ACCEL_CUDA_GEMM_TILED_CUH_
6
7#include <cuda_runtime.h>
8
9#include "core/gemm_shape.h"
10
11namespace accel {
12
13// A 32x32 block is exactly one warp per row, so the cooperative loads are
14// warp-aligned, and each staged tile costs 4 KiB of shared memory. Changing
15// this means re-checking both properties.
16inline constexpr int kGemmTileDim = 32;
17
18// Shared-memory tiled GEMM, C = A * B, row-major throughout and overwriting C
19// rather than accumulating.
20//
21// Each block computes one tile of C by marching along K, cooperatively staging
22// one tile of A and one of B per step. Every staged element is then read
23// kGemmTileDim times by different threads, which is the whole
24// arithmetic-intensity gain over direct global loads.
25//
26// Out-of-range loads yield zero rather than being skipped, keeping the inner
27// product fixed-trip and uniform across the block: a divergent tail costs more
28// than the wasted multiply-adds.
29//
30// Must be launched with blockDim == (kGemmTileDim, kGemmTileDim) -- shared
31// memory is indexed by threadIdx, so any other shape reads out of bounds.
32// Extents need not be multiples of the tile. `c` must not alias `a` or `b`.
33__global__ void GemmTiledKernel(const float* __restrict__ a,
34 const float* __restrict__ b,
35 float* __restrict__ c, int m, int n, int k);
36
37// Validates `shape` and launches on `stream`. Asynchronous: check completion
38// with CUDA_CHECK_LAUNCH or by synchronising, which the benchmark driver
39// defers until after its timing loop.
40//
41// Throws std::invalid_argument if the shape is degenerate, CudaError if the
42// launch is rejected.
43void LaunchGemmTiled(const float* a, const float* b, float* c,
44 const GemmShape& shape, cudaStream_t stream = nullptr);
45
46} // namespace accel
47
48#endif // ACCEL_CUDA_GEMM_TILED_CUH_