22.3 Cache-blocked, packed, batch-invariant matmul in C
The naive triple-loop matmul from Vectors, matrices, row-major layout, and matmul in Mathematics is easy to understand and slow on large matrices, because it keeps fetching values that sit far apart in memory. This section rewrites the same function, tl_matmul_f32, with the same signature and contract, as a high-performance kernel: Goto’s loop nest, packed operands, a register micro-kernel, and threads from the thread pool. At it is 10 to 20 times faster than the triple loop while doing exactly the same floating-point operations.
Concepts from other sections (6)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- decode step: one forward pass with a single new query per sequence, at the newest position. Read more: 22.6 Paged attention for decode in C
- memory-bound: limited by the rate at which data can be read, not by arithmetic throughput. Read more: 22.7 Fused int4 and int8 dequantize-matmul in C
- greedy decoding: taking the single most likely token at every step; a beam of size 1. Read more: 10.4 Beam search, generic over a step function
- fused projection: an engine-side parameter such as
qkv_projorgate_up_projthat concatenates several checkpoint tensors into one GEMM. Read more: 20.3.4 Portability and load triage - logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- RMSNorm: normalization by followed by a learned per-channel gain. Read more: 22.8 Elementwise kernels in C: RMSNorm, RoPE, SiLU-mul, embedding, add, argmax
Speed is only half of the story. The kernel also makes a promise most fast libraries do not: batch invariance. Row of a product has the same bits whether it was computed alone (, a decode step) or inside a batch of 37 (a prefill), at any position, on any number of threads. That promise is what lets the C backend generate the same tokens as numpy, only faster, and it comes entirely from fixing the order of the reduction. This optional kernel demonstrates both optimizations; the Python and Rust engine paths keep their own implementations.
You will use the error slot from C runtime support and an arena allocator with marks and tl_parallel_for from the thread pool. Helpful background from Mathematics: the naive version of this very file in the matmul section above, the error bound in Error analysis, condition numbers, and tolerance budgets, and FLOP counting in Counting: parameters, FLOPs, KV bytes, and memory plans. The contract is matmul.h under the rules of c/ABI.md (rule 10 is batch invariance), and the reference solution is matmul.c, with the helpers column_strip, pack_b, pack_a, micro_kernel, and store_tile. As you read, try to predict each step before you see it.
Learning objectives
- Use the roofline model to explain why the triple loop is memory-bound and how blocking raises arithmetic intensity.
- Describe the five loops of Goto’s nest and what each keeps in which level of the memory hierarchy.
- Pack operands into micro-kernel order with zero padding, absorbing
trans_band leading dimensions.- Explain why a constant k-block size, zero padding, and
FP_CONTRACT OFFmake each row’s bits independent of the batch.- Apply exactly once across k-blocks and parallelize over column strips without changing a bit.
Why the triple loop is slow: the roofline
Section titled “Why the triple loop is slow: the roofline”This section uses the following quantities throughout:
| Symbol | Meaning | Type / shape |
|---|---|---|
, (or with trans_b), , row-major with leading dimensions | float* | |
| the micro-tile held in registers: | constants MR, NR | |
| k values per block, the fixed reduction step: 128 | KC | |
| rows of packed at once: 64 | MC | |
| columns per strip, one thread’s unit of work: 128 | NC | |
| partial sum of block : , from 0, in increasing | float | |
| arithmetic intensity: FLOPs per byte moved from memory | FLOP/byte |
A core can do floating-point operations per second only if its operands arrive fast enough. Memory delivers bytes per second, so a loop that does FLOPs per byte it loads runs at most at . That ceiling is the roofline.
In the naive loop, each inner step loads one float of and one of (8 bytes) for 2 FLOPs, so , and walking down a column of touches a new 64-byte cache line on every step. Yet a product of matrices does FLOPs on only distinct numbers, so the data could be reused times; the naive loop reloads it instead. Blocking is the art of keeping a piece of data in a fast level (registers, L1, L2) while it is used many times.
Goto’s loop nest
Section titled “Goto’s loop nest”Goto’s design uses five loops, each sized for one level of the memory hierarchy. The outermost loop walks column strips of columns of (and of ); one strip is one unit of work for a thread. Inside a strip, the second loop walks k-blocks of and packs the block of into a buffer bp, which stays in L2 while every row block of passes it. The third loop walks row blocks of and packs the block of into ap, which stays in L1/L2 across the strip. The fourth loop walks micro-tiles: each -wide panel of bp against each -tall panel of ap. And the fifth, the micro-kernel, computes an block of partial sums in 64 accumulators, reading one column of values of and one row of values of per : loads for FLOPs.
The micro-kernel’s intensity is what makes the whole thing fast: 6.4 FLOPs per float loaded instead of 1, with every load from the packed buffers sequential. The compiler turns acc[r][c] += a[r] * b[c] with constant into vector registers (16 floats of a row is four 128-bit NEON or two 256-bit AVX registers).
Packing
Section titled “Packing”The micro-kernel’s sequential loads only exist because of packing: copying a block into the order the micro-kernel will read it. pack_b writes panel (columns to ) as consecutive rows of floats; pack_a writes panel (rows to ) as consecutive columns of floats.
Two more jobs happen during the copy. First, trans_b and the leading dimensions disappear. The packer reads at B[k*ldb + j] or B[j*ldb + k] and at A[i*lda + k]; the micro-kernel only ever sees contiguous panels. Second, edges are padded with zeros. When is not a multiple of 4 or not a multiple of 16, the last panel is filled with zeros, so the micro-kernel always computes a full tile. A zero row contributes nothing, and the store writes only the real corner back into through ldc. One code path for every tile is what makes batch invariance possible, as the next part shows.
The buffers live on the stack: floats, 96 KiB. Rule 1 of the ABI forbids a kernel to allocate, and this signature has no arena to borrow from.
The reduction order and batch invariance
Section titled “The reduction order and batch invariance”Floating-point addition is not associative: and can differ in the last bit, as the matmul section in Mathematics shows. So “the same sum” really means “the same sum in the same order”. In this kernel, element is
Every quantity in that expression depends only on row of , column of , , , , and the constant . It does not depend on , on which micro-tile row landed in, on the zero padding, or on which thread computed the strip. That is batch invariance (ABI rule 10): row of a product computed with equals row computed with , bit for bit.
Real libraries lose this property in three ways, all for speed. One is a block size that depends on the shape, such as a “GEMV path” for that sums all of in one block; the decode step () and the prefill () then round differently, and the worked example below shows exactly how. Another is splitting across threads (“split-K”) and adding the thread partials in completion order. The third is using different instructions for edge tiles, for example a fused multiply-add in the vectorized body and a separate multiply and add in a scalar remainder. A fused multiply-add rounds once where the pair rounds twice. Zero padding gives every element the same code path, and #pragma STDC FP_CONTRACT OFF forbids the compiler to fuse, so the arithmetic is the same on every path and every machine.
Threads
Section titled “Threads”Parallelism follows the same rule: never split a sum. tl_parallel_for(tp, n_strips, 1, strips, &args) runs each column strip exactly once, on some worker. Strips write disjoint columns of and only read and , so there is no race, and a strip’s arithmetic does not depend on the worker, so the result has the same bits with 1 or 8 threads. With tp == NULL the strips run serially on the caller.
What it costs
Section titled “What it costs”The packed kernel still does FLOPs. Packing costs copies per call and packing costs copies per strip, small next to when the matrices are large. For (decode) the micro-kernel computes a tile of which one row is real, so three quarters of the arithmetic is wasted on padding. That is the price of invariance here; the quantized GEMV in Fused int4 and int8 dequantize-matmul in C is where decode gets its own kernel.
Working it by hand
Section titled “Working it by hand”Start with the small product from the matmul section in Mathematics, pushed through the tiles: , , so . There is one strip (columns 0 and 1), one k-block (), and one row block.
pack_b writes one 16-wide panel of 3 rows: {7, 8, 0, ..., 0 | 9, 10, 0, ..., 0 | 11, 12, 0, ..., 0}, with 14 zeros of padding per row. pack_a writes one 4-tall panel of 3 columns: {1, 4, 0, 0 | 2, 5, 0, 0 | 3, 6, 0, 0}. The micro-kernel’s accumulator after each is:
| adds for , | acc row 0 | acc row 1 | |
|---|---|---|---|
| 0 | , | (7, 8) | (28, 32) |
| 1 | , | (25, 28) | (73, 82) |
| 2 | , | (58, 64) | (139, 154) |
Rows 2 and 3 of the accumulator and columns 2 to 15 are zero (padding). The store writes only the corner: , the same result as the triple loop.
Next, see why the block size must not depend on . Take one output whose four products are, in order, (float32). With the sum in one block, : float32 spacing near is 8, so adding 1 or 3 rounds back to each time, and the result is . With blocks of 2, , which rounds up to . Same numbers, different grouping, different bits. If the kernel used one block for and blocks of 2 otherwise, a decode step and a prefill would disagree in exactly this way.
Finally, across blocks. With and , element goes through three stores: , then , then . appears once.
The interface
Section titled “The interface”The signature and contract are unchanged from the naive version; what is new is the promise of rule 10:
tl_status tl_matmul_f32(const float *A, const float *B, float *C, int64_t M, int64_t N, int64_t K, int64_t lda, int64_t ldb, int64_t ldc, float alpha, float beta, int trans_b, tl_pool *tp);/* From L9.1 the k order is fixed and independent of M and of the row's position: row i computed with M = 1 equals row i computed with any M, bit for bit. tp may be NULL (serial); with a pool, column strips run in parallel. */The solution takes over the naive version’s file, c/src/kernels/matmul.c: same signature, new body. Every contract case of the naive version still holds.
Building it step by step
Section titled “Building it step by step”The arithmetic is fixed at ; the cost is memory traffic. So the design question is how many times each loaded value is reused before it leaves a fast level of the hierarchy. Registers give the most reuse per load: an block of accumulators reuses each loaded value times and each value times, and accumulators fit the vector registers of NEON and AVX2 and give 6.4 FLOPs per float loaded. The micro-kernel needs its operands contiguous, so you pack: copy a block of and an block of into micro-kernel order. Packing is also the one place that has to know about trans_b and leading dimensions.
Batch invariance needs one code path and a fixed grouping of the sum. Zero padding to whole tiles gives every element the same path, a constant fixes the grouping, and FP_CONTRACT OFF stops the compiler fusing on some paths and not others. belongs to the first k-block only, because is overwritten by the first block’s store and accumulated into afterwards. Threads must split independent outputs, never a sum, so the unit of work is a column strip of , scheduled by the pool. And there is no allocation in a kernel: packing buffers sized by the block constants live on the stack (96 KiB, below the 512 KiB of a macOS worker thread).
Block constants and the arithmetic rule
Section titled “Block constants and the arithmetic rule”/* A fused multiply-add rounds once where a multiply then an add rounds * twice. If the compiler fused on one code path and not on another, an * element's bits would depend on where it sits in a tile. */#pragma STDC FP_CONTRACT OFF
#define MR 4 /* rows of a micro-tile */#define NR 16 /* columns of a micro-tile */#define KC 128 /* k values per block: the fixed reduction step */#define MC 64 /* rows per packed block of A (a multiple of MR) */#define NC 128 /* columns per strip (a multiple of NR) */Packing
Section titled “Packing”Both packers pad with zeros past the real edge, and both resolve the strides so the micro-kernel never sees them:
/* panel p holds bp[p * kc * NR + k * NR + c] = op(B)[k0 + k][j0 + p * NR + c] */static void pack_b(const mm_args *a, int64_t k0, int64_t kc, int64_t j0, int64_t nc, float *bp) { for (int64_t jp = 0; jp < nc; jp += NR) { for (int64_t k = 0; k < kc; k++) { for (int64_t c = 0; c < NR; c++) { int64_t j = j0 + jp + c; float v = 0.0f; /* zero past column nc */ if (jp + c < nc) v = a->trans_b ? a->B[j * a->ldb + (k0 + k)] /* B is N x K */ : a->B[(k0 + k) * a->ldb + j]; /* B is K x N */ *bp++ = v; } } }}
/* panel p holds ap[p * kc * MR + k * MR + r] = A[i0 + p * MR + r][k0 + k] */static void pack_a(const mm_args *a, int64_t i0, int64_t mc, int64_t k0, int64_t kc, float *ap) { for (int64_t ip = 0; ip < mc; ip += MR) for (int64_t k = 0; k < kc; k++) for (int64_t r = 0; r < MR; r++) *ap++ = ip + r < mc ? a->A[(i0 + ip + r) * a->lda + (k0 + k)] : 0.0f; /* lda, not K */}The micro-kernel
Section titled “The micro-kernel”Constant bounds let the compiler keep acc in vector registers; each partial sum starts at 0 and adds in increasing :
static void micro_kernel(int64_t kc, const float *ap, const float *bp, float acc[MR][NR]) { for (int r = 0; r < MR; r++) for (int c = 0; c < NR; c++) acc[r][c] = 0.0f; /* P_b starts at 0 */ for (int64_t k = 0; k < kc; k++) { const float *av = ap + k * MR, *bv = bp + k * NR; /* sequential reads */ for (int r = 0; r < MR; r++) for (int c = 0; c < NR; c++) acc[r][c] += av[r] * bv[c]; /* 20 loads, 128 FLOPs */ }}The store
Section titled “The store”Only the real corner is written, through ldc, and is applied only in the first block:
static void store_tile(const mm_args *a, int64_t i, int64_t j, int64_t mr, int64_t nr, float acc[MR][NR], int first) { for (int64_t r = 0; r < mr; r++) { /* mr <= MR: no write into padding */ float *c = a->C + (i + r) * a->ldc + j; for (int64_t q = 0; q < nr; q++) { float v = a->alpha * acc[r][q]; if (!first) c[q] = c[q] + v; /* later k-blocks add */ else c[q] = a->beta == 0.0f ? v : v + a->beta * c[q]; /* beta once; never read C if 0 */ } }}The loop nest for one strip
Section titled “The loop nest for one strip”static void column_strip(const mm_args *a, int64_t j0, int64_t nc) { float bp[KC * NC], ap[MC * KC]; /* 96 KiB, on the stack */ for (int64_t k0 = 0; k0 < a->K; k0 += KC) { /* k-blocks, increasing */ int64_t kc = a->K - k0 < KC ? a->K - k0 : KC; pack_b(a, k0, kc, j0, nc, bp); /* stays in L2 for every row block */ for (int64_t i0 = 0; i0 < a->M; i0 += MC) { int64_t mc = a->M - i0 < MC ? a->M - i0 : MC; pack_a(a, i0, mc, k0, kc, ap); for (int64_t jr = 0; jr < nc; jr += NR) for (int64_t ir = 0; ir < mc; ir += MR) { float acc[MR][NR]; micro_kernel(kc, ap + ir * kc, bp + jr * kc, acc); store_tile(a, i0 + ir, j0 + jr, mc - ir < MR ? mc - ir : MR, nc - jr < NR ? nc - jr : NR, acc, k0 == 0); } } }}Notice what the block boundaries depend on. k0 steps by the constant KC, never by anything derived from M; i0 and ir decide only where a row is packed, and the padded rows contribute nothing to any real row.
The entry point
Section titled “The entry point”The entry point keeps the naive version’s contract and adds the strips:
tl_status tl_matmul_f32(const float *A, const float *B, float *C, int64_t M, int64_t N, int64_t K, int64_t lda, int64_t ldb, int64_t ldc, float alpha, float beta, int trans_b, tl_pool *tp) { /* 1. Validate everything before touching C (the M03.1 contract): * negative dims, ldc < N, C NULL; and when K > 0, lda < K, * ldb < (trans_b ? K : N), A or B NULL: TL_EINVAL. M or N == 0: TL_OK. */ // ... /* 2. K == 0: no k-block runs, and the empty sum is 0, so C = beta * C. */ if (K == 0) { for (int64_t i = 0; i < M; i++) for (int64_t j = 0; j < N; j++) C[i * ldc + j] = beta == 0.0f ? 0.0f : beta * C[i * ldc + j]; return TL_OK; } /* 3. One range per column strip, on whichever worker. */ mm_args a = {A, B, C, M, N, K, lda, ldb, ldc, alpha, beta, trans_b}; int64_t n_strips = (N + NC - 1) / NC; if (tp == NULL) { strips(&a, 0, n_strips, 0); return TL_OK; } return tl_parallel_for(tp, n_strips, 1, strips, &a);}strips is the pool’s range function: for each strip index in its range it calls column_strip on columns . On the worked example, column_strip runs once with kc = 3, mc = 2, nc = 2, one micro-tile, and store_tile writes mr = 2, nr = 2: .
The full solution is matmul.c, against the contract matmul.h.
Why this design
Section titled “Why this design”The cost is FLOPs as before, plus copies per call to pack and copies per strip to pack , small next to for large matrices. Measured against the triple loop at , the solution is about 19 times faster (about 50 GFLOP/s on one Apple M-series core); Apple’s Accelerate (cblas_sgemm, on the AMX units) is several times faster again.
Each rejected alternative trades away something this kernel needs. A GEMV fast path for would avoid the three wasted rows of a tile during decode, but it sums in a different grouping, so decode and prefill disagree in the last bit and greedy decoding can flip tokens on near-ties. Here invariance wins, and the quantized kernel gives decode its own path. Split-K threading parallelizes small- products better but adds partial sums in completion order, which changes bits with timing; column strips never split a sum. Hand-written intrinsics or assembly would be faster per core but tie the kernel to one ISA, while constant-bound C loops vectorize well on both NEON and AVX2 and keep the arithmetic auditable. The tradeoff accepted is that fixed block sizes are not tuned per machine, and FP_CONTRACT OFF gives up the speed of fused multiply-adds, in exchange for identical bits on every path and every machine.
What can go wrong
Section titled “What can go wrong”A correct solution has to handle every tile edge and every carried-over rule of the naive contract:
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| The hand example | unit, smoke | the worked example through one padded micro-tile | the definition, and the edge store |
| Shapes across tile edges | differential | around 4, 16, 64, 128, up to 257, both trans_b, against float64 within | every tile edge |
| applies once across k-blocks | unit | , , ; then over NaN | accumulation across k-blocks |
| Views, and padding untouched | unit | lda, ldb, ldc larger than the rows; ‘s other columns unchanged | views into fused projections |
| Empty and invalid inputs, as in the naive version | boundary | empty dims, gives , TL_EINVAL leaves untouched | the contract carries over |
| Batch-invariant rows | property | 16 rows of a Linear layer (): each alone vs inside every batch at every offset, bitwise | batched decode in continuous batching |
| Batch invariance at | property | the catalog’s , bitwise | prefill vs decode |
| Pool result equals serial, bitwise | property | 4 threads vs serial, three strips (the last partial), under TSan too | threads never change bits |
| Row-major and transposed | unit | original row-major and transposed-B contract cases | the original interface remains compatible |
| and | unit | alpha/beta behavior, including no read of C when beta is zero | preserves the original arithmetic contract |
| Empty shapes, bad arguments, leading dimensions | boundary | empty shapes, invalid arguments, and padded leading dimensions | preserves the original shape and view rules |
Most bugs in a blocked matmul come from the store and the packers. Applying in every k-block gives for , and with only the last block survives; store_tile applies only when first (k0 == 0). Later k-blocks that overwrite instead of adding leave only the last products; the solution writes c[q] = c[q] + v when not first. Storing the whole micro-tile at an edge writes past or into its padding (ASan, or a neighbour’s columns); the store writes only the real mr x nr corner. Storing with instead of ldc scrambles views of ; rows of are addressed with ldc.
The packers have their own traps. Packing without honoring trans_b makes Linear layers multiply by the wrong matrix; pack_b reads B[j * ldb + k] when trans_b. Packing with instead of lda is right on packed matrices and wrong on every view; pack_a indexes with lda. Packing buffers sized for the full matrix on the stack overflow worker threads (512 KiB on macOS); size them by the block constants, 96 KiB.
Warning: Reading when lets a NaN in a fresh buffer reach the logits. Use
beta == 0.0f ? v : ..., and do the same in the path.
Rewrites tend to lose details of the old contract. Returning early for leaves with old values instead of ; an explicit path writes . Dropping the naive version’s argument checks lets a short lda read past the buffer; validate every argument before touching .
Warning: A “GEMV fast path” that sums all of at once when makes decode and prefill differ in the last bit, and greedy tokens flip on near-ties. Keep one code path for every , with constant
KC.
Finally, a partition that drops or repeats a strip is wrong only with a pool, which makes it easy to miss; use strips, one pool range each.
Where this leads
Section titled “Where this leads”This kernel relies on the error slot behind TL_EINVAL from the runtime support layer and on tl_parallel_for from the thread pool to run the column strips. Looking forward, Fused int4 and int8 dequantize-matmul in C uses it as both its benchmark and its float32 baseline, running this kernel on the dequantized weight.
Production libraries push each idea further:
| This piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
| 4 x 16 C micro-kernel | BLIS and OpenBLAS assembly micro-kernels | hand-scheduled FMA pipelines, prefetching, per-microarchitecture block sizes | BLIS kernels/armv8a/, kernels/haswell/ |
| column-strip threads | BLIS multithreading | parallelism in several loops at once (the and loops), sized by the cache topology | Smith et al., “Anatomy of High-Performance Many-Threaded Matrix Multiplication” (2014) |
| fixed for invariance | batch-invariant kernels for LLM serving | invariant matmul, RMSNorm, and attention as a PyTorch library | Thinking Machines batch_invariant_ops |
| float32 CPU GEMM | Apple Accelerate / AMX, cuBLAS | dedicated matrix units, several times the vector throughput | cblas_sgemm; cuBLASLt |
| one kernel for every dtype | llama.cpp ggml_mul_mat | dispatch to quantized dot-product kernels per weight format | ggml/src/ggml-cpu/ggml-cpu.c |
Further reading
Section titled “Further reading”- Goto and van de Geijn, “Anatomy of High-Performance Matrix Multiplication” (2008).
- Van Zee and van de Geijn, “BLIS: A Framework for Rapidly Instantiating BLAS Functionality” (2015).
- Williams, Waterman, and Patterson, “Roofline” (2009).
- He et al., “Defeating Nondeterminism in LLM Inference” (Thinking Machines, 2025).
Key terms
Section titled “Key terms”- roofline: the bound on a loop’s speed from peak compute , memory bandwidth , and arithmetic intensity .
- arithmetic intensity: FLOPs performed per byte moved from memory.
- micro-kernel: the innermost loop that computes an tile of partial sums in registers.
- packing: copying a block of an operand into the exact, contiguous order the micro-kernel reads, padded with zeros to whole tiles.
- k-block: a run of consecutive k values whose products are summed from 0 before being added to .
- batch invariance: the property that a row’s output bits do not depend on the batch size, its position in the batch, or the threads.
- split-K: parallelizing a matmul by splitting the reduction dimension across threads, which breaks bitwise reproducibility.
Summary
Section titled “Summary”- The triple loop is slow because of memory, not arithmetic. Blocking reuses each loaded value many times from cache and registers; the packed version is 10 to 20 times faster at with the same operations.
- Packing copies a block into the exact order the micro-kernel reads it, padded with zeros to whole micro-tiles, so the inner loop is unit-stride and every output element goes through the same code path.
- Batch invariance is a property of the reduction order. Each is a sum over k-blocks of fixed size , each a sum from 0 in increasing : nothing depends on , on the row’s position, or on the threads, so a row has the same bits alone or in a batch of 37.
- applies once, in the first k-block, and later blocks add; still never reads .
- Threads split columns, never a sum, so 1 and 4 threads give identical bits.
Review questions
Section titled “Review questions”- Why does zero padding make the result independent of where a row sits in a micro-tile?
Answer
Every row, real or padding, goes through the same full $4 \times 16$ micro-kernel and the same sequence of multiplies and adds. A real row's products and their order depend only on its own row of $A$ and the column of $B$; padding rows are zero and are never stored. So the code path, and the bits, are the same at offset 0 or offset 3 of a tile. - Why must be a constant rather than chosen from the shape?
Answer
The k-block boundaries decide how the sum is grouped, and float addition is not associative. A shape-dependent block size (for example all of $K$ when $M = 1$) groups the same products differently and changes the last bits, as the $1, 10^8, 3, 3$ example shows. - With and , how many times is applied, and where?
Answer
Once, in `store_tile` with `first` true, which happens for `k0 == 0`. The second and third k-blocks add $\alpha P_1$ and $\alpha P_2$ to the stored value. Applying $\beta$ in every block would give $\beta^3 C$. - Why is it safe to run strips on different threads without locks?
Answer
Each strip writes a disjoint set of columns of $C$ and only reads $A$ and $B$; packing buffers are local to the strip's stack frame. There is no shared mutable state, so no race, and the strip's arithmetic does not depend on the worker. - Why are the packing buffers on the stack and not from an arena or
malloc?Answer
ABI rule 1 forbids a kernel to allocate, and this signature carries no arena. Their size depends only on the block constants (96 KiB), not on the matrix, so the stack is safe even on worker threads with 512 KiB stacks. - What is the arithmetic intensity of the micro-kernel, measured in FLOPs per float loaded, and how does it compare with the naive loop?
Answer
Each $k$ step loads $M_R + N_R = 20$ floats and does $2 M_R N_R = 128$ FLOPs, which is 6.4 FLOPs per float. The naive loop loads two floats for 2 FLOPs, 1 FLOP per float (0.25 FLOPs per byte).