Skip to content

WIP: plan with absolute strides, block only when cache lines are reused - #106

Draft
Jutho wants to merge 1 commit into
jh/recursive-kernelfrom
jh/planning
Draft

Jutho wants to merge 1 commit into
jh/recursive-kernelfrom
jh/planning

Conversation

@Jutho

@Jutho Jutho commented Nov 29, 2026

Copy link
Copy Markdown
Member

Work in progress. Builds on #105 (and thereby #104), and targets its branch jh/recursive-kernel; it can be retargeted once those are merged.

An investigation of the planning of _mapreduce_block!, i.e. for inputs above MAXNAIVELENGTH. Running the planning itself costs 20–200 ns, at most 2% of the kernel at these sizes, so there is little to gain from a cheaper implementation. The decisions it takes mattered much more: on a set of 22 varied cases (maps with simple and complicated f, 3-input maps with mixed layouts, broadcasts, reductions, the native mul!), the blocking was often worse than not blocking at all, by up to 2.4×.

Changes so far:

  1. Negative strides. The memory region and the costs used signed strides: for a reversed array the estimated footprint came out tiny, so that transposes with it were not blocked, and the costs were negative. The planning now uses absolute strides, and reverses the loops along dimensions where the output (or, if it has stride zero, the first input that moves) has a negative stride, so that arrays reversed in the same way can be fused and use the unit-stride loop.
  2. Block only when cache lines are reused. Blocking only helps if an array comes back to cache lines after the inner loops moved on: through a smaller stride on an outer loop (permutations; always blocked, as before, since the footprint estimate misses cache set conflicts with power-of-two strides), or through a zero stride (reductions, broadcasts; blocked only if the reused data exceeds the budget). Arrays whose strides increase along the loops are no longer blocked, since that only shortened their contiguous runs.
  3. Costs ignore zero strides. A dimension got cost 1 as soon as any array had stride zero along it, so that the threads split the wrong dimension in the native mul!.
  4. Threads also split unblocked plans. The threads never split a dimension of at most 1024 that fit within a block, so that unblocked plans with such dimensions ran on a single thread.

The small-input path (_mapreduce_order!) is unchanged: the loops are only reversed inside _mapreduce_block!.

Benchmarks against #105 (Apple M4 Max, Julia 1.13.1; minimum of three interleaved runs):

case 1 thread 4 threads
sum over dims=(1,3), 64^4 0.49× 0.48×
sum over dims=(2,4), 64^4 0.93× 0.77×
transposed copy into a reversed destination, 1000² 0.64× 0.14×
copy, both arrays reversed, 1000² 0.46× 0.16×
copy into a reversed destination, 1000² 1.00× 0.29×
C .= A .+ b', 1000² 1.00× 0.62×
native mul! 200³ / 256×64×512 1.01× 0.50× / 0.26×

All other cases (transposes, 4D and 6D permutations, maps with 3 inputs, exp of a transpose, further reductions) are within ±8%, the run-to-run noise. Small inputs are unchanged.

Still to investigate:

  • Block shapes for permutations of 3 or more dimensions. For permutedims of 32^4 with p = (4, 3, 2, 1), the best blocks found by a search take 370 µs, against 830 µs for the chosen ones. For 2D transposes, the best blocks have one cache line along the innermost dimension and a large extent along the other one, whereas the heuristic chooses square-ish blocks. A rule along these lines helped transposes but made reductions and mul! 2–4× slower, so this needs a better cost model, without tuning for a single machine.
  • Loop order with zero strides. Zero strides rank as the smallest stride, which puts reduced dimensions early in the loop order even where the input is strided along them (sum over dims=(2,4) could take 2.2 instead of 3.3 ms). Not letting them vote at all breaks the accumulation in a register for reductions over dims=1.

🤖 Generated with Claude Code

- Dimensions along which the output (or, if it has stride zero, the first
  input that moves) has a negative stride are reversed before planning, so
  that arrays reversed along the same dimensions can be fused and traversed
  with unit strides.
- The costs and the memory region use absolute strides, and the costs ignore
  zero strides. With negative strides, the memory region came out tiny, so
  that transposes with a reversed array were not blocked, and the costs were
  negative; with zero strides, a dimension got cost 1 as soon as any array
  had stride zero along it.
- Inputs are only blocked if some array visits cache lines again after the
  inner loops moved on: through a smaller stride along an outer loop (always
  blocked), or through a stride zero with more reused data than fits in the
  block size. Arrays whose strides increase along the loops load every cache
  line once, and blocking only shortened their contiguous runs.
- The threads may split any dimension of size > 1 with a nonzero cost.
  Before, they never split a dimension of at most 1024 that fit within a
  block, so that unblocked plans with such dimensions ran on one thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mapreduce.jl 96.77% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/mapreduce.jl 94.40% <96.77%> (-0.72%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant