Pebbling and checkpointing
Import std.pebble explicitly to plan and execute schedules for a chain
of steps x_i = step(i, x_(i-1)) from a permanent input x_0. The library
provides reversible chain pebbling for clean XOR computation and Revolve
binomial checkpointing for an adjoint sweep. Both planners produce plain
Action data at compile time or at run time.
Bennett reversible chain pebbling
A work slot starts at zero. Placing a pebble for node i XORs
step(i, x_(i-1)) into its slot. Removing it repeats the XOR in that same
slot. Both operations require node i-1 to be present, with x_0 always
available. The step can be non-injective. Reevaluating it from the same
predecessor gives the bits needed to clear the slot.
With k work slots, max_visit(k) is 2^k - 1. For k >= 1,
max_singleton(k) is 2^(k-1), the farthest vertex that can be left
pebbled by itself. The limits saturate at the largest i64 vertex.
Length zero refers to the permanent input and needs no slots.
visit_and_clear(L, k)finds the shortest route to any occupancy containingL. If that takesDtoggles, it emits thoseDtoggles, XORs the result into a separate output accumulator, then retraces the exact slot assignments in reverse order. The total cost is2D + 1actions.singleton(L, k)leaves justLpresent. It uses exact breadth-first search throughBFS_LIMIT, and Knill’s exact recurrence beyond it.knill_cost(L, k)gives the recurrence cost directly. For a split atm, the three legs placem, placeLwithmheld, and removemwithLheld. MinimizeF(m,k) + F(L-m,k-1) + F(m,k-1)over all feasible splits. Knill, An Analysis of Bennett’s Pebble Game
Both plans expose actions, toggles, steps and peak_slots.
At L = 7, three work slots reach the target in 13 toggles. The clean
plan takes 26 toggles plus one output XOR. A singleton first becomes
possible with four slots and takes 19 toggles.
run_bennett runs a visit-and-clear plan with a compile-time step
closure and zero-filled caller-supplied Vec(u64) registers. It checks
the schedule’s occupancy and verifies the actual bits in every register
at the end. The caller can start the output accumulator at any value.
Running the same plan twice with the same input and step restores that
value. An invalid action or a step that fails to clear a register can
return InvalidPlan after executing part or all of the schedule.
Measured search budget
BFS uses two preallocated flat tables with 2^L entries each. The queue
stores masks and occupancy counts. The predecessor table is indexed by
mask and recovers the sequence of toggles before the planner assigns
concrete work slots. A feasible visit beyond BFS_LIMIT = 9
returns SearchLimit. Knill’s table and witness are bounded by
KNILL_LIMIT = 512. Infeasible space bounds return Unreachable.
The following are checked x86-64 interpreter measurements for individual
evaluations materializing [Action] with --comptime-report. Allocation
counts include interpreter storage for function locals. The BFS itself
uses preallocated queue and predecessor tables throughout neighbor
enumeration.
| Evaluation | MIR steps | Allocations | Cumulative allocation bytes |
|---|---|---|---|
Bennett visit and clear, L = 7, k = 3 |
50,936 | 5,445 | 51,125 |
Bennett visit and clear, L = 9, k = 6 |
237,844 | 20,730 | 171,445 |
Bennett singleton, L = 9, k = 9 |
559,504 | 48,831 | 358,850 |
Knill singleton, L = 10, k = 7 |
76,945 | 10,063 | 87,026 |
Revolve, L = 7, c = 3 |
15,603 | 2,347 | 29,793 |
A probe with the BFS bound temporarily raised reached the interpreter’s
1,000,000-step cap on the L = 10, k = 7 singleton. The corresponding
public query uses Knill’s recurrence. Compiler limits apply to an entire
evaluation, so group multiple plans into separate comptime expressions
when their combined cost exceeds the budget.
To reproduce the example measurements, run
jihoo run examples/pebbling.jh --comptime-report /tmp/pebbling.json.
For the length-nine cases, evaluate
comptime pebble.visit_and_clear(9, 6).unwrap().into_actions() and
comptime pebble.singleton(9, 9).unwrap().into_actions() separately.
Revolve binomial checkpointing
c >= 1 counts saved checkpoints, including the initial checkpoint.
The primal cursor and adjoint live separately. binomial_capacity(c,t)
computes B(c,t) = C(c+t,c). revolve_cost(L,c) finds the smallest
t >= 0 with B(c,t) >= L. The optimal extra forward work is
P(L,c) = t*L - C(c+t,t-1), with zero extra work at t = 0.
There are L + P(L,c) forward steps and L adjoint turns.
Griewank and Walther, Algorithm 799: Revolve
revolve(L,c) constructs a witness by choosing a binomial split,
saving the suffix’s start in a free slot, walking the suffix and
releasing that slot before restoring the prefix checkpoint. The plan
uses Advance(node), Save(slot), Restore(slot), Turn(node)
and Release(slot). An Advance can batch several forward steps.
A Turn(i) computes x_i from x_(i-1) and immediately calls the
adjoint closure with the index, both endpoints and the incoming adjoint.
Turns run in the order L, L-1, ..., 1. run_revolve returns the
final primal and the input’s adjoint. Witness generation is bounded by
REVOLVE_LIMIT = 1_000_000. Larger lengths can still query
revolve_cost.
The unit tests compare this cost and the executed witness against a
memoized split recurrence for every length through 40 and every
checkpoint count from one through five. They also compare the primal
and adjoint to a full tape. For the runnable squaring chain modulo
2^64, the step is x*x + 2i + 1 and its formal adjoint multiplies by
2*x, where x is the step’s input.
import std.pebble
const PLAN: [pebble.Action] = comptime pebble.visit_and_clear(7, 3).unwrap().into_actions()
fn main() {
var scratch = Vec(u64).filled(3, 0)
var output: u64 = 0
pebble.run_bennett(PLAN, 13, &scratch, &output, fn(i, x) {
x *% x +% (2 * i + 1) as u64
}).unwrap()
assert(output == 5675873914096352600, "final primal")
}