matmul — Matrix Multiply on the VPE
A GEMM C = A · B (A is M×K, B is K×N) is M·N dot products of length K: C[i][j] = dot(row i of A, column j of B). The VPE is a vector reduction engine, so dot()/dot2() express that directly, with one row and one or two columns in SRAM while the matrices stay in DRAM. B is supplied transposed (Bt, shape N×K) so column j of B is the contiguous row j of Bt.
The point of matmul/main.cpp is that the context binds no dimension, so one kernel can compute a batch of differently-sized matrices without touching its VpeContext.
void matmulRows(mu::vdma::VpeContext& ctx, const float* a, const float* bt, float* c,
uint32_t n, uint32_t k, uint32_t rowStart, uint32_t rowEnd,
uint64_t aBuf, uint64_t b0Buf, uint64_t b1Buf)
{
for (uint32_t i = rowStart; i < rowEnd; ++i)
{
ctx.load(a + static_cast<uint64_t>(i) * k, aBuf, k); // A row i, shared operand
uint32_t j = 0;
for (; j + 1 < n; j += 2) // two columns per step via dot2
{
ctx.load(bt + static_cast<uint64_t>(j) * k, b0Buf, k);
ctx.load(bt + static_cast<uint64_t>(j + 1) * k, b1Buf, k);
auto r = ctx.dot2(aBuf, b0Buf, b1Buf, k);
c[static_cast<uint64_t>(i) * n + j] = r.r1;
c[static_cast<uint64_t>(i) * n + j + 1] = r.r2;
}
if (j < n) // odd trailing column
{
ctx.load(bt + static_cast<uint64_t>(j) * k, b0Buf, k);
c[static_cast<uint64_t>(i) * n + j] = ctx.dot2(aBuf, b0Buf, b0Buf, k).r1;
}
}
}
The odd trailing column also uses dot2, with the same operand supplied twice. Keep this loop on the dot2 path when tasks share a VPE.
All four kernels are built on that loop:
| Kernel | What it adds |
|---|---|
matmul | One matrix, output rows split across tasks (as in knn). |
matmulBatch | A list of square matrices of differing sizes. Buffers are allocated once, sized for the largest matrix; each matmul then runs at its own per-call dimension, so a new size costs one more loop iteration, not a new context. One task, so one VPE. |
matmulBatchParallel | Flattens the batch into (matrix, row-block) units and takes every taskCount-th one. The enumeration is deterministic on every task, so ownership needs no shared state and no locking. |
matmulBatchParallelTiled | Keeps XARITH_MATMUL_ROWTILE A rows resident and reuses each loaded B column across all of them. |
Task placement and VPE assignment
A subsystem has 128 MU hardware threads and two VPEs, each with an independent SRAM pool. Using 48 tasks on a 24-SUB device does not by itself occupy all 48 VPEs. Three choices determine how the parallel kernels reach them:
| Choice | What it controls | Example default |
|---|---|---|
| PXL batch size | Tasks in one scheduled batch; large batches can leave SUBs idle at small task counts. | batchSize(1) |
| PXL locality | Task placement on SUBs and MU clusters. Compact groups nearby task IDs; Spread distributes them across SUBs. | SpreadMode (explicit) |
| XArith VPE strategy | A context’s local VPE: ByThreadId selects threadId & 1; ByClusterId selects clusterId & 1. | ByClusterId |
Compare locality modes and task counts for your matrix sizes. Several tasks can share a VPE to hide load latency. Increase XARITH_MATMUL_REPS to amortize dispatch overhead.
Round-robin assignment avoids locks but can leave work unevenly distributed when matrix sizes differ.
Memory-bound vs compute-bound
matmulRows streams all of B past one resident A row, so it re-reads B once per A row: an arithmetic intensity of only ~0.5 FLOP/byte, which means throughput tracks DRAM bandwidth, not the VPEs. The tiled kernel reads B once per tile of A rows instead, lifting intensity to ~0.5 · XARITH_MATMUL_ROWTILE:
// One B column, loaded once, reduced against every resident A row of the tile.
for (uint32_t j = 0; j < n; ++j)
{
ctx.load(bt + static_cast<uint64_t>(j) * k, bColBuf, k); // B column j: one load
for (uint32_t r = 0; r + 1 < rows; r += 2) // reuse across the tile
{
auto res = ctx.dot2(bColBuf, aBufs[r], aBufs[r + 1], k);
c[(rowStart + r) * n + j] = res.r1;
c[(rowStart + r + 1) * n + j] = res.r2;
}
}
The trade-off is SRAM: a bigger tile holds more buffers per task, so fewer tasks fit per VPE, and concurrency is what hides load latency. The kernel clamps XARITH_MATMUL_ROWTILE to what the pool can hold and prints the value used.
ByClusterId can relieve allocation contention by distributing tasks across the independent VPE pools. Each task still allocates from one pool, so changing the strategy does not increase its maximum row tile. Actual concurrency also depends on task placement and allocation availability.
A larger tile reduces B loads but can be slower when too few tasks fit at once. Sweep row tiles and task counts together after setting the launch geometry.
Both parallel tests print estimated DRAM GB/s: access-pattern traffic divided by dispatch time, not a hardware bandwidth measurement. Cache reuse can make it exceed physical bandwidth. XARITH_MATMUL_PEAKBW=<GB/s> reports its ratio to a reference peak. Low estimated bandwidth alone does not identify a compute limit: SRAM waits, placement, small matrices, and uneven work can also cause it.
Allocation (bytes) vs per-call dimension (elements)
- Footprint (SRAM held, hence how many tasks fit on a VPE) follows the bytes you allocate.
- Work (elements processed) follows the per-call
dimensionyou pass to each op.
A smaller dimension does not shrink the footprint or free a VPE for another task; the buffers still occupy what you allocated. To fit more concurrent tasks, allocate fewer bytes. Only vectors ever live in SRAM: even the largest matmul in the batch holds a few K-length rows, never the matrices themselves.
./bin/xarith_matmul
XARITH_MATMUL_REPS=24 PXL_TASK_COUNT=384 XARITH_MATMUL_SIZES=256 ./bin/xarith_matmul
Environment Variables:
| Variable | Default | Description |
|---|---|---|
PXL_DEVICE_ID | 0 | Device ID |
PXL_NUM_SUB | 24 | Number of subsystems |
PXL_TASK_COUNT | 48 | Number of subtasks (raised to PXL_NUM_SUB if lower) |
XARITH_MATMUL_BATCH | 1 | Parallel tasks per PXL batch; 0 omits batchSize() to compare the SDK default |
XARITH_MATMUL_LOCALITY | 1 | Parallel placement: 0 = CompactMode, 1 = SpreadMode |
XARITH_MATMUL_VPE_STRATEGY | 1 | Parallel VPE assignment: 0 = ByThreadId, 1 = ByClusterId |
XARITH_MATMUL_REPS | 4 | Times XARITH_MATMUL_SIZES is cycled into the batch |
XARITH_MATMUL_SIZES | 64,128,48,256,96,192 | Square matrix sizes making up the batch |
XARITH_MATMUL_ROWTILE | 8 | A rows resident in the tiled kernel |
XARITH_MATMUL_USEDOT | 0 | 1 reduces the tiled kernel with single dot() instead of fused dot2() |
XARITH_MATMUL_PEAKBW | (unset) | Device peak DRAM bandwidth in GB/s, to print estimated bandwidth as a percentage |
The batch, locality, and strategy options affect only the two parallel kernels, which print their selected settings. The single-task demonstrations keep their original settings. Locality and strategy accept only 0 or 1; the full environment contract is in the top-of-file comments of matmul/main.cpp.
# Compare Compact with the default Spread placement.
XARITH_MATMUL_REPS=24 XARITH_MATMUL_LOCALITY=0 ./bin/xarith_matmul
# Compare the previous SDK-default batch, Compact placement, and thread-based VPE selection.
XARITH_MATMUL_REPS=24 XARITH_MATMUL_BATCH=0 XARITH_MATMUL_LOCALITY=0 XARITH_MATMUL_VPE_STRATEGY=0 ./bin/xarith_matmul
Use matmul/sweep.sh from the example package root to compare task counts and row-tile sizes. It inherits batch, locality, and VPE strategy as fixed settings per sweep; choose separate XARITH_MATMUL_OUTDIR directories when comparing them to retain all logs. Run ./matmul/sweep.sh --help for settings and defaults.