Skip to main content
General matrix multiply (GEMM) is the canonical high-performance GPU kernel. This example shows how Enigma’s layout algebra replaces manual index arithmetic with composable layout transformations.

Problem

Compute C = A × B where:
  • A is (M, K), row-major
  • B is (K, N), row-major
  • C is (M, N), row-major

Approach: tiled outer product

Each threadgroup computes one tile of C. Threads within the threadgroup cooperate by loading shared tiles of A and B into threadgroup memory, then computing the partial dot products.

Naive kernel (no shared memory)

A simpler starting point — each thread computes one output element:
Dispatch:

Tiled kernel with layout algebra

For larger matrices, tile with layout algebra to enable shared memory reuse:

Performance tips

  • Tile size: 16×16 or 32×32 threadgroup tiles hit L1/L2 cache reuse sweet spots.
  • vec_width=4: Vectorize loads with float4 to improve memory bandwidth.
  • Simdgroup matrix: On M3+ hardware, use simdgroup_multiply_accumulate for 8×8 hardware-accelerated matrix multiply.

See also