Skip to main content
Enigma uses Metal’s explicit grid-and-threadgroup execution model. You control the full launch geometry at dispatch time — Enigma never infers it from data shapes.

Grid and threadgroups

A kernel launch is defined by two parameters:
  • grid=(gx, gy, gz) — total number of threads in each dimension across the entire dispatch
  • threads=(tx, ty, tz) — number of threads per threadgroup in each dimension
Metal schedules grid / threads threadgroups. Each threadgroup runs concurrently on a single GPU core, sharing threadgroup (shared) memory.

Thread index queries

Inside @enigma.kernel, use these functions to obtain thread coordinates as IRValue objects:

Global position (most common)

Threadgroup-relative position

SIMD group queries

Launch patterns

1D — elementwise

For kernels where each thread handles one element:
Dispatch:

2D — matrix kernels

For kernels operating on 2D data, use y for rows and x for columns (standard convention):
Dispatch:

Vectorized — vec_width

When you compile with vec_width=4, each thread processes 4 elements. Divide the grid accordingly:

Launch configuration rules

  • Every logical element must be covered by exactly one thread.
  • output_size is in bytes, not elements. For f32: elements × 4.
  • Threadgroup size should be a multiple of the SIMD group size (32) for best occupancy.
  • For padded domains, use predicated stores (store_if) to avoid writing out-of-bounds.

Threadgroup size guidelines

Device limits

Query capabilities at runtime: