Threadgroup Memory
Threadgroup memory is the on-core scratch space shared by one threadgroup (Metal's shared memory), capped at 32 KB per threadgroup.
CUDA equivalent: shared memory. Same purpose (cooperative staging, cross-thread
communication within a block), same declaration style (threadgroup float tile[N]; vs __shared__), same lifetime. The difference is quantity and role:
you're used to 48-228 KB of configurable shared memory per SM; here it's 32 KB
per threadgroup (~60 KB physically per core), against a register
file almost as large as an SM's.
That ratio demotes threadgroup memory from "where tiles live" to "how tiles pass through." The house pattern, visible in every GEMM and attention kernel in this glossary:
- A cooperative load stages a slab of device memory into threadgroup memory: coalesced, vectorized, every thread carrying an equal share.
- Each simdgroup immediately pulls fragments of that slab into
registers via
simdgroup_loadand does all real work there. - The slab is overwritten by the next one. Nothing lingers.
Doubling your threadgroup-memory footprint halves how many threadgroups can be resident, which cuts occupancy. That's the standard CUDA trade, and the exact cost of double buffering, which allocates two staging buffers to overlap loading with math.
Two behavioral notes that differ from a modern SM:
- Barriers are almost free.
threadgroup_barriercosts ~2 cycles. The CUDA reflex of restructuring algorithms to avoid__syncthreads()buys nothing here; see synchronization. - Scattered access is comparatively expensive. Bank behavior is less forgiving than an SM's; the fast pattern is straight-line, contiguous, vectorized access. Apple publishes no bank mapping but does give a rule (opens in a new tab): align threadgroup allocations to 16 bytes, have neighboring threads in each quad touch neighboring elements, and avoid many threads reading one location. Combined with cheap barriers, this is why Metal kernels look the way they do: simple coalesced staging, frequent syncs, no clever scatter tricks.
There is no cudaFuncAttributePreferredSharedMemoryCarveout equivalent. Apple's
Feature Set Tables (opens in a new tab)
fix the cap at 32 KB per threadgroup on every Apple-silicon GPU family, counting
static threadgroup arrays and host-set setThreadgroupMemoryLength blocks
(each a multiple of 16 bytes) together; query maxThreadgroupMemoryLength at
run time rather than hard-coding it. The same tables give 1,024 threads per
threadgroup as a theoretical maximum (a built pipeline reports its own, lower
limit), 31 buffer argument slots, and 4 KB for setBytes. When a working set
won't fit, the answer is almost always "keep more of it in registers," not "get
more shared memory."