paralever

Verifying Equivalence of Parallel Program and its Sequential Counterpart — in Lean 4

ISP - I Presentation

Overview

  • Motivation

  • Independence and Wellformedness of Parallel Programs

  • Paralever Language

  • Proof of Equivalence

  • Parallel Programs are Faster

  • Examples

  • Autoparallelization

  • Races

  • MLIR

  • Performance of Paralever

  • Limitations

a BMW R 1250 GS, whose rear end is a Paralever

Paralever is BMW's rear drive architecture of most modern BMW shaft-driven motorcycles… But it runs sequentially…

BMW R 1250 GS · Cjp24, CC BY-SA 4.0, via Wikimedia Commons

Motivation

  • Consider the two jobs run sequentially separately or parallely(in arbitrary order). Does the memory come out the same?

[5] := 1
∥
[8] := [7]
→touch different locations
one heap
[5] := 1
∥
[8] := [5]
→share location 5
two heaps
left first
[5] := 1[5] = 1  [8] = 0
→
[8] := [5][5] = 1  [8] = 1
right first
[8] := [5][5] = 0  [8] = 0
→
[5] := 1[5] = 1  [8] = 0
  • Clearly the two parallel executions do not yield the same heap.

  • Under what conditions can we guarantee that the parallel execution is equivalent to the sequential execution?

Problem Statement

p : ParM α    q : ParM β    σ : State      WellFormed (par p q) σ
∀ r σ'.   Reaches (par p q, σ) (pure r, σ')  ⇔  Reaches (seq p q, σ) (pure r, σ')
par_seq_eq — the conclusion is ParSeqEq (par p q) (seq p q) σ, unfolded

We define every symbol the statement uses.

l : LocA Memory Location. Locations are numbered 0, 1, 2, …, etc.
v : ValA value. What a single location holds.
σ : Loc → ValA heap. One value for every location at once; σ l is what location l holds.
p : ParM αA program. Built from four constructors: a finished value, a read, write and a parallel composition of finitely many programs. Running it returns a value of type α.
cA configuration. The whole state of a run: a pair (p, σ) — the program still to run, and the heap as it stands.
wrongThe racy outcome. The one other configuration. A race steps to it; it is terminal and absorbing, so nothing leaves it.
c → c'One step. c' is one of the finitely many successors of c. A read or a write has exactly one; a parallel composition has one per branch ready to move, and that choice is the only nondeterminism in the model.
Reaches c c'An interleaving. A finite chain c → c₁ → ⋯ → c'. To quantify over interleavings is to quantify over these chains.
seq p qSequential composition. Run p to completion, then q. Exactly one chain, no choice anywhere.
par p qParallel composition. Run both branches, interleaved in any order, and join once each has finished. Many chains.
R(p), W(p)The footprint. The locations p reads, and the locations p writes.
WellFormed p σThe hypothesis. At every parallel composition reached while running p from σ, the branches' footprints are pairwise independent, and each branch is itself well formed.

Independence and Wellformedness of Parallel Programs

def Indep (f₁ f₂ : Footprint) : Prop := Disjoint f₁.writes f₂.writes ∧ Disjoint f₁.writes f₂.reads ∧ Disjoint f₂.writes f₁.reads
W₁ R₁ W₂ R₂ C1 · W₁ ∩ W₂ = ∅ C2 · W₁ ∩ R₂ = ∅ C3 · W₂ ∩ R₁ = ∅ no clause on R₁ ∩ R₂ — shared reads are permitted
  • The footprint of a program is a pair of finite sets of locations: R(p), those it reads, and W(p), those it writes. It is computed from the program, never supplied by hand.

  • Two programs are independent when the three clauses above hold of their footprints. This is Bernstein's condition, and it is decidable.

Scroll Down ↓ for more details

A program is well formed when every parallel pair in it is independent

def WellFormed {τ : Type} : ParM τ → State → Prop | .pure _, _ => True | .read l k, σ => WellFormed (k (σ l)) σ | .write l v k, σ => WellFormed (k ()) (σ.update l v) | .par cs k, σ => let r := runChilds cs σ IndepAll (childFootprints cs σ) ∧ WellFormedChilds cs σ ∧ WellFormed (k r.1) r.2
  • p is well formed from heap σ if, at every parallel composition reached while running p from σ, its branches are pairwise independent, each branch is itself well formed, and what follows the join is well formed too.

  • Each branch is checked against the heap the branch before it left during the execution.

par write C[0] par write C[1] write C[2] IndepAll checked here and here the outermost check alone is unsound

Paralever Language

buf A[2, 3] = 1, 2, 3, 4, 5, 6 buf out[3] %k := 10 parallel { %s := 0 for j in 3 { %v := A[0, j] if %v > 2 { %s := %s + %v * %k } else { %s := %s + %v } } %k := 99 out[0] := %s } | { parallel { %a := A[1, 0] out[1] := %a } | { %b := A[1, 2] out[2] := %b * %k } } print out, %k -- %k = 10, not 99
  • Paralever provides a small imperative language which has shared buffers, private registers, loops, branches, parallel composition, synchronizations and other features needed for realistic programs.

  • Every program denotes two executions — a parallel one and a sequential one.

  • Well-formedness is decided on the program as written, and it implies the two executions agree.

  • DESIGN PRINCIPLE: to allow ease to parallel sequential equivalence, and mimic hardware and compiler behaviors.

    • Example 1: Registers are firstprivate, which means any changes to registers within parallel threads are discarded at the join.

    • Example 2: Synchronizations are used to ensure that certain operations are completed before others, mimicking the behavior of real hardware.

Scroll Down ↓ for more details

A program is an element of an inductive type

inductive ParM : Type → Type 1 where | pure {α : Type} : α → ParM α | read {α : Type} : Loc → (Val → ParM α) → ParM α | write {α : Type} : Loc → Val → (Unit → ParM α) → ParM α /-- Fork every child at once, then continue with all their results. The only constructor whose meaning Lean does not already fix: `run` interprets it by interleaving, which is what makes the sequentialization theorem non-vacuous. -/ | par {β : Type} {αs : List Type} : Childs ParM αs → (Results αs → ParM β) → ParM β
write x[0] 5 par read x[0] v read x[1] w write z[0] 0 write y[0] 2*v write y[1] w+1 pure () pure () pure () read y[0] r pure r join k the three children join, and k runs on their results

We decide independence and report a verdict

buf A[2, 3] = 1, 2, 3, 4, 5, 6 buf out[3] %k := 10 parallel { %s := 0 for j in 3 { %v := A[0, j] if %v > 2 { %s := %s + %v * %k } else { %s := %s + %v } } %k := 99 out[0] := %s } | { parallel { %a := A[1, 0] out[1] := %a } | { %b := A[1, 2] out[2] := %b * %k } } print out, %k
reads {A[0,0] … A[1,2]} writes {out[0], out[1], out[2]} threads 2 rounds 4 parallel, 8 sequential verdict every heap Proved: Yes out = [33, 4, 60] 10← 10, not 99
  • Buffers are shared and registers are not: out[i] := %s is what Indep measures, %s and %k are invisible to it.

  • Registers are firstprivate, at every depth. All four threads read %k = 10 — which is why out[2] is 60. Thread 0 sets %k := 99 and the join discards it.

  • if … else branches per location; print is erased from both interpreters, and is a barrier the parallelizer may not cross.

The semantics is a step relation on configurations

def step {α : Type} : ParM α → State → List (Conf α) | .pure _, _ => [] | .read l k, σ => [.run (k (σ l)) σ] | .write l v k, σ => [.run (k ()) (σ.update l v)] | .par cs k, σ => if racingAll (nextAccsChilds cs) then [.wrong] else match allPure cs with | some vs => [.run (k vs) σ] | none => (stepChilds cs σ).map fun r => match r with | some x => .run (.par x.1 k) x.2 | none => .wrong
inductive Childs (Node : Type → Type 1) : List Type → Type 1 where | nil : Childs Node [] | cons {α : Type} {αs : List Type} : Node α → Childs Node αs → Childs Node (α :: αs)
parPair p q = .par Π[ p q ] (fun r => .pure (r.1, r.2.1)) seqPair p q = bind p (fun a => bind q (fun b => .pure (a, b))) parAll ps = .par (childsOf ps) (fun x => .pure (resultsToList ps x)) seqAll [] = .pure [] seqAll (p :: ps) = bind (seqPair p (seqAll ps)) (fun r => .pure (r.1 :: r.2))parPair is parAll at two children, not the other way round
  • step returns a list: empty is terminal, a singleton is deterministic, and a fork returns one entry per ready child. That list is where every schedule comes from.

  • racingAll is checked before the children move, so a fork with two conflicting next actions goes to wrong and stays there.

The ParSeqEq theorem

def ParSeqEq {α : Type} (parProgram seqProgram : ParM α) (σ : State) : Prop := ∀ r σ', Reaches (.run parProgram σ) (.run (.pure r) σ') ↔ Reaches (.run seqProgram σ) (.run (.pure r) σ')theorem par_seq_eq {s : ParProg} (h : wfB s = true) (ρ : Regs) (σ : State) : ParSeqEq (execPar s ρ) (execSeq s ρ) σ := by
wfB s = truedecided by decide: list intersections on footprints
→
every schedule of s ends where execSeq s endsstated as an iff on reachable outcomes

ParSeqEq compares two whole programs, not the two branches of one fork: ParSeqEq p q σ holds when, for every result r and heap σ', some schedule of p ends in pure r with heap σ' exactly when some schedule of q does. The theorem par_seq_eq takes p to be the parallel execution of a ParProg s and q its sequential execution, for every s that wfB accepts, every register file ρ and every initial heap σ.

Proof of Equivalence

  • Under WellFormed each execution — parallel or sequential — reaches exactly one outcome, and those two outcomes are equal. Without it the program is refused and the racing schedule is exhibited.

  • So ParSeqEq reduces to WellFormed for all programs.

every schedule of par p qWellFormed ⇒ no wrong
→
one canonical outcomereached by both sides
←
the one run of seq p q

Scroll Down ↓ for more details

Every schedule is a path through the successor lists

def step {α : Type} : ParM α → State → List (Conf α) | .pure _, _ => [] | .read l k, σ => [.run (k (σ l)) σ] | .write l v k, σ => [.run (k ()) (σ.update l v)] | .par cs k, σ => if racingAll (nextAccsChilds cs) then [.wrong] else match allPure cs with | some vs => [.run (k vs) σ] | none => (stepChilds cs σ).map fun r => match r with | some x => .run (.par x.1 k) x.2 | none => .wrong
the list is the nondeterminismempty = terminal · one entry = no choice · a fork = one entry per ready child
stepChilds (p :: ps) σ = (step p σ).map … ++ (stepChilds ps σ).map …the head child moves, appended to every move the tail can make — that ++ is the fan-out
  • There is no scheduler in the development. A schedule is a path through these lists, and Reaches is the transitive closure of membership in them.

  • So quantifying over schedules is quantifying over ∈.

Comparing two schedules directly does not work

compare schedulestwo arms of n statements · (2n)!/(n!)² interleavings · 184 756 at n = 10 · and the two sides do not even take the same number of steps
≠
compare each side to a third objectrunSeq p σ — a function, not a relation: no schedule, no choice, no wrong
runSeq (par cs k) σ = runSeq (k r₁) r₂ where (r₁, r₂) = runChilds cs σ runChilds (p :: ps) σ = run p, then run ps from the heap p lefta fork’s canonical value is its children run left to right
runSeq (par p q) σ = runSeq (seq p q) σ — by rfl, and freethe measuring stick reads the same on both. It says nothing yet about either one under a scheduler.

The invariant holds of any branch, not only the first

theorem step_combined {τ : Type} (P : ParM τ) : ∀ σ, WellFormed P σ → ∀ P3 σ3, Conf.run P3 σ3 ∈ stepC (Conf.run P σ) → runSeq P3 σ3 = runSeq P σ ∧ (∀ F, FDisjoint F (accesses P σ) → FDisjoint F (accesses P3 σ3)) := step_combined_all.1 P
P3 is any member of the successor list, under ∀ — one statement per scheduler choicethe proof rcases on List.mem_append: head moved, 10 lines · tail moved, 45 lines and the hard case
par [a:=x, y:=7] · x5 y0 a0 [done, y:=7] · x5 y0 a5 [a:=x, done] · x5 y7 a0 x5 y7 a5 head moved tail moved first the same runSeq at all four
  • Why the tail case commutes. Its step changed one location in W₂. By C3 that location is outside R₁, so the head reads the same values — accesses_runSeq_locality. By C1 it is outside W₁, so the head does not overwrite it — runSeq_frame.

  • Why it can be done again. The second conjunct: a step only shrinks a footprint, so the sibling is still independent of the residue, and the invariant chains.

We chain the invariant along a reachability derivation

theorem wellFormed_canonical {τ : Type} (p : ParM τ) : ∀ σ r σ', WellFormed p σ → (Reaches (.run p σ) (.run (.pure r) σ') ↔ (r, σ') = runSeq p σ) := wellFormed_canonical_all.1 p
{ outcomes of p from σ }every terminating schedule, however the scheduler moved
=under WellFormed
{ runSeq p σ }one element
  • The invariant chained along a Reaches derivation, with WellFormed_preserved re-establishing the hypothesis at each link. runSeq is constant along the chain, and at the end it is (r, σ′).

  • Uniqueness is →; existence is ←, from no_race_of_indep. Without existence a racy program reaches nothing and would satisfy the theorem vacuously on both sides.

The proof takes four steps

1. WellFormed (par p q) σ → WellFormed (seq p q) σwellFormed_of_seqPair · seq has no fork, so the Indep conjunct is dropped
2. ⟨par p q, σ⟩ →* ⟨pure r, σ′⟩ ⇔ (r, σ′) = runSeq (par p q) σthe bridge, on the left — every left-hand schedule is eliminated here
3. ⟨seq p q, σ⟩ →* ⟨pure r, σ′⟩ ⇔ (r, σ′) = runSeq (seq p q) σthe bridge, on the right — available only because of 1
4. runSeq (par p q) σ = runSeq (seq p q) σrfl — free, and carrying none of the content
chain 2, 4, 3 — that is ParSeqEq
  • The theorem is not two left-to-right runs compared. That is step 4, and it is rfl. The work is in 2 and 3: each is wellFormed_canonical, a whole set of schedules collapsing to a point.

  • Nothing after step 1 mentioned p or q. So ParSeqEq reduces to WellFormed — proved once, for every program there will ever be.

Parallel Programs are Faster

theorem time_le {s : ParProg} (h : wfB s = true) (ρ : Regs) (σ : State) : time (execPar s ρ) σ ≤ time (execSeq s ρ) σ := time_execPar_le_all.1 s ρ σ h
  • time counts rounds: one round is every running thread taking one step. The same Bool that proves the two readings agree also orders their spans.

  • Parallelism reorders memory operations and removes none, so work is unchanged and only time falls. The inequality is not conditional on a machine, a scheduler or a core count.

Time model is not real, but based on Paralever modelling

  • We exclude synchronization costs, thread initialization costs, and other overheads. The model is a simplification to focus on the theoretical aspects of parallel execution.

The Proof idea is very simple:

  • Consider arbitrary 2 nodes(use induction for proof). 2 parallel nodes take 1 round to execute while the sequential nodes take 2 rounds. It is possible no parallelism can exist due to data dependency. Hence the parallel execution is not slower than the sequential execution.

Examples

  • Matrix multiply — disjoint writes over overlapping reads.

  • Summing an array — quantified over the chunk plan.

  • Merge sort — the fork is the recursion.

  • A neural net — layers in sequence, threads inside each.

Scroll Down ↓ for more details

Matrix multiply

const n = 2 buf A[2, 2] = 1, 2, 3, 4 buf B[2, 2] = 5, 6, 7, 8 buf C[2, 2] parfor i in n { for j in n { %acc := 0 for k in n { %x := A[i, k] %y := B[k, j] %acc := %acc + %x * %y } C[i, j] := %acc } } :: "matmul 2x2"
example : ParProg.wfB (matmul 2) = true := by decide example : ParProg.wfB (matmul 3) = true := by decide /-- …and the equivalence follows with nothing further to supply. -/ theorem par_seq_eq_2 (ρ : Regs) (σ : State) : ParSeqEq (ParProg.execPar (matmul 2) ρ) (ParProg.execSeq (matmul 2) ρ) σ := ParProg.par_seq_eq (by decide) ρ σ

Thread i writes row i of C and reads row i of A and all of B. (C1) holds because i ↦ i*n + j is injective across threads; (C2) and (C3) hold because A and B are never written. The file above carries the same theorem as matmul_parseqeq, read through prl_file.

Sum of an array

A[0, 4) A[4, 9) A[9, 13) A[13, 16) s₀ s₁ s₂ s₃ no thread writes the array, so C1–C3 hold vacuously — for any split
theorem seq_eq_par (chunks : ChunkPlan) (σ : State) : sumRangePar chunks ∥≡ sumRangeSeq chunks from σ := parAll_par_seq_eq (chunks.map sumRange) σ (wellFormed_sumRangePar chunks σ)

Universally quantified over the chunk plan and the heap. This is the shape ∗-disjointness cannot state at all: four threads reading one array.

Merge sort

sort [0, 8) sort [0, 4) sort [4, 8) [0, 2) [2, 4) [4, 6) [6, 8) at each level: lo ≤ mid ≤ hi, so [lo, mid) and [mid, hi) do not meet
theorem seq_eq_par (N lo hi : Nat) (hhiN : hi ≤ N) (σ : State) : mergeSortPar N lo hi ∥≡ mergeSortSeq N lo hi from σ := by

Quantified over the array size and over every range, with no decide: each level's obligation is one interval fact, and the recursion carries it down.

Neural net

x[8] conv1d × 6 relu × 6 dense × 2 y[2] c[6] r[6] w2[2,6], b2[2] every thread of a layer reads the whole weight array; only the output locations are written
/-- **The theorem.** Under every schedule, the net with all its layers' threads running at once reaches exactly the outcomes it reaches run thread-by-thread. -/ theorem forward_par_seq_eq (ρ : Regs) (σ : State) : ParSeqEq (ParProg.execPar forward ρ) (ParProg.execSeq forward ρ) σ := ParProg.par_seq_eq (by decide) ρ σ

The weights are shared reads, so (C2) and (C3) are free inside a layer. Between layers the composition is sequential, and wfB recurses into each arm.

Autoparallelization

  • A sequential program is a chain of statements.

  • We compute its dependences: here, five units and four dependences.

  • The chain is rebuilt as three parallel groups, and the rebuilding is proved sound with no hypothesis.

A[0] := 1 A[1] := 2 A[2] := 3 par A[0] := 1 A[1] := 2 A[2] := 3 autoPar one statement per round three distinct locations, so the regrouping needs no hypothesis

Scroll Down ↓ for more details

A sequential program is a chain of statements

buf a[5] a[0] := 3 a[1] := 4 %0 := a[0] a[2] := %0 * %0 %1 := a[1] a[3] := %1 * %1 %2 := a[2] %3 := a[3] a[4] := %2 + %3chain.prl — ten lines, nine of them statements
seq seq seq seq seq seq seq seq a[0] := 3 a[1] := 4 %0 := a[0] a[2] := %0 * %0 %1 := a[1] a[3] := %1 * %1 %2 := a[2] %3 := a[3] a[4] := %2 + %3 the spine is the order the text was written in; depth 9, one statement per round

ParProg.block [s₀, …, s₈] is seq s₀ (seq s₁ (…)), so the order the statements were written in is the tree's right spine. The pass first cuts that spine into five register-closed units — %0 := a[0] and a[2] := %0 * %0 may not be separated, because a register is thread-private — and one scoreboard pass reads every dependence off them: [[], [], [0], [1], [2, 3]].

We compute the dependences between the statements

1 round 2 rounds 3 rounds a[0] := 3 a[1] := 4 %0 := a[0] a[2] := %0 * %0 %1 := a[1] a[3] := %1 * %1 %2 := a[2] %3 := a[3] a[4] := %2 + %3 u0 u1 u2 u3 u4 u0w a[0] u1w a[1] u2r a[0] · w a[2] u3r a[1] · w a[3] u4r a[2],a[3] · w a[4] the ten lines, cut into units what each unit touches, and what it must wait for a unit is a run of statements no live register crosses depth 6 against a sequential 9, and 6 is the lower bound

A register is thread-private, so %0 := a[0] and a[2] := %0 * %0 may not be separated: the unit, not the statement, is what a thread is built from. One left-to-right scoreboard pass then reads every dependence off the units' footprints — [[], [], [0], [1], [2, 3]]. Four of the ten pairs carry one; u0, u1 share no location, as do u2, u3. That is the whole of the parallelism this program has.

The chain is rebuilt as a sequence of parallel groups

seq seq seq seq seq seq seq seq a[0] := 3 a[1] := 4 %0 := a[0] a[2] := %0 * %0 %1 := a[1] a[3] := %1 * %1 %2 := a[2] %3 := a[3] a[4] := %2 + %3 the spine is the order the text was written in; depth 9, one statement per round
seq par seq a[0] := 3 a[1] := 4 par par %0 := a[0]a[2] := %0 * %0 %1 := a[1]a[3] := %1 * %1 %2 := a[2]%3 := a[3]a[4] := %2 + %3 depth 6: 1 + 2 + 3 rounds, and 6 is the bound
example (ρ : Regs) (σ : State) : (runSeq (ParProg.execPar (rewrite p) ρ) σ).2 = (runSeq (ParProg.execSeq p ρ) σ).2 := rewrite_sound p ρ σ -- and because the rewrite stays well-formed, `par_seq_eq` lifts that from one schedule to all example (ρ : Regs) (σ : State) : ParSeqEq (ParProg.execPar (rewrite p) ρ) (ParProg.execSeq (rewrite p) ρ) σ := rewrite_parSeqEq p (by decide) ρ σ

rewrite checks its own gate and hands back the program unchanged when it fails, which is why rewrite_sound carries no hypothesis. Nothing is ever reordered, so no commutation lemma is needed.

Races

par A[1] := 7 %v := A[1] writes A[1] reads A[1] W ∩ R = {A[1]} — the pair the search has to find
  • We search for a data race whenever the independence test refuses, and we return the interleaving that disagrees.

Scroll Down ↓ for more details

We search for a data race when the test refuses

buf x[1] buf log[2] parallel { log[0] := 1 x[0] := 7 } | { log[1] := 1 x[0] := 9 }Examples/Language/race.prl
par log[0] := 1 log[1] := 1 x[0] := 7 x[0] := 9 W₁ ∩ W₂ = {x[0]} — clause C1 fails log[0], log[1] disjoint — not the problem
stepthreadactionheap after
0T0write log[0] ← 1log ↦ [1, 0], x ↦ [0]
1T1write log[1] ← 1log ↦ [1, 1], x ↦ [0]
2T1write x[0] ← 9pending
2T0write x[0] ← 7pending
aborts at step 2 — T0, T1 write to {x[0]}
parallel x[0] ↦ 7sequential x[0] ↦ 9
paralever race Examples/Language/race.prl

The schedule is found by searching the step relation

findWrongFrom fuel p σ:                      -- phase 1
  sites ← raceSites p σ                      every par node failing Bernstein,
  if sites = [] then none                     measured along the canonical run
  else findWrongGuided (targetsOf sites) fuel (run p σ)

findWrongGuided targets fuel c:               -- phase 2
  if 0 then none
  if some successor of c is wrong then [its index]
  else let (keep, hold) = partition (successors of c)
         by "does this step move a thread PAST a flagged location?"
       first i in keep ++ hold with
         findWrongGuided targets (fuel-1) c′ = some s  ⇒  i :: s
tried first: keeps its position tried last: consumes a target wrong
  • Phase 1 names the locations; phase 2 is depth-first over step's successor list, trying last any step that moves a thread past one of them — a reordering, so nothing becomes unfindable.

  • Whatever comes back is re-run by follow and turned into a Reaches proof by follow_sound, so the counterexample is checked rather than trusted.

We read MLIR modules and translate them

memref.global "private" @A : memref<3xi64> = dense<[2, 3, 4]> func.func @main() { %A = memref.get_global @A : memref<3xi64> affine.parallel (%i) = (0) to (3) { %0 = affine.load %A[%i] : memref<3xi64> %1 = arith.muli %0, %0 : i64 affine.store %1, %A[%i] : memref<3xi64> } return }square-no.mlir — the whole module
buf A[3] = 2, 3, 4 parallel { %0 := A[0] A[0] := (%0 * %0) } | { %0 := A[1] A[1] := (%0 * %0) } | { %0 := A[2] A[2] := (%0 * %0) }paralever to-prl — the whole program
-- and the theorem the verdict stands for, on the program the deck prints theorem square_par_seq_eq (ρ : Regs) (σ : State) : ParSeqEq (Lang.ParProg.execPar square.prog ρ) (Lang.ParProg.execSeq square.prog ρ) σ := Lang.ParProg.par_seq_eq (by decide) ρ σ

A memref.global becomes a buffer, an affine.map is already a footprint function, and the three iterations become three threads of one fork. Nine labelled pilots are #guarded as a confusion matrix — tp = 3, fp = 0, fn = 0, tn = 4 over the seven generic-form ones — and emitting the program back as MLIR compares memory, footprint, verdict and heap.

Performance of Paralever

  • We measure how long the check takes on matrix multiplication, and what speedup it certifies.

  • On a program with no structure the pass runs to completion and finds almost nothing.

  • We place a race at the very bottom of a run and measure what reaching it costs.

× against sequential — log scale 20×10× 1×0.1× matmul, n = 2 … 20 chaos, d = 32 … 2048 crosses 1× at d = 512 problem size →
race found — search time (ms) 3 0002 000 1 0000 k = 2832 128512 3 420 ms at step 1 024 found in every case — the depth is the cost

Scroll Down ↓ for more details

Matrix multiplication is checked, and its speedup is certified

nlocationsthreadsrounds parrounds seqspeedupcheck
212210202×0 ms
4484361444×0 ms
819281361 0888×20 ms
12432123003 60012×120 ms
16768165288 44816×520 ms
201 2002082016 40020×1 600 ms
  • The verdict is every heap · Proved: Yes at every size: wfB accepts, so the threads' footprints are pairwise independent whatever the heap holds.

  • The speedup is exactly n, the thread count, and it is certified rather than measured — these are the two time values the cost theorem orders, not a wall clock.

  • The check grows with the iteration space it writes out, not with the speedup: n = 20 is 1 200 locations and 1.6 s.

An unstructured program yields little parallelism

  • equiv.prl — 36 lines, 224 sequential rounds, a serial chain of dependences rather than a loop nest.

  • autopar takes 1.63 s and returns 2 threads at 196 parallel rounds: a speedup of 1.14×, against 4× on phases.prl, which is the same length and does split.

  • The pass is sound either way. Where the dependences forbid a split it finds nothing, and says so, rather than reporting a parallelism that is not there.

phases.prl16 seq rounds → 4 threads, 4 par · 4×
equiv.prl224 seq rounds → 2 threads, 196 par · 1.14×
tally.prl, strength.prlloop-carried → no split at all · 1×
dinstructionsas writtenautopar, markers offregions only
3263 2343 ms19 ms — 0.15×5 ms — 0.60×
128173 8269 ms22 ms — 0.40×8 ms — 1.12×
512616 19436 ms36 ms — 1.00×21 ms — 1.71×
20482 385 666142 ms91 ms — 1.56×73 ms — 1.94×

chaos.prl — a thousand lines, nothing in it parallel. Forking every statement puts 2 072 children in and does not break even until d = 512; forking only inside the marked regions puts 280 in and is ahead from d = 128. n = 8, m = 32, sixteen cores, median of five runs; planning is a compile-time cost and is in neither column.

A race at the very end of a run is still found

  • Two arms each do k independent writes, then both write one shared location: the only race is the very last statement either arm runs, at step 2k.

  • The search reaches it in every case — it replays the schedule against the semantics rather than guessing, so the depth is the cost.

krace at stepfoundsearch
24yes0 ms
816yes0 ms
3264yes0 ms
128256yes80 ms
5121 024yes3 420 ms

On race.prl the two readings are separated as well as refused: parallel leaves x[0] ↦ 7, sequential leaves x[0] ↦ 9, and the refusal names the locations and the threads — T0, T1 write to: {x[0]}.

Limitations and Future Work

  • Can't model GTA VI.
  • Non-terminating programs are unrepresentable.

    • ParM is an inductive type, so every program it can express terminates.

  • A computed address widens the footprint.

    • A[B[i]] has a subscript the text does not determine, so the syntactic footprint might claim all of A.

  • TODO: Expand the "program" representation for practical usage.

    • Introduce locks, atomics annd other synchronization primitives.

    • Expand memory model to generalized conditions so that any backend memory model(like stack-heap, any memory hierarchy, counting sheeps, etc.) can be supported.

    • Expand the thread model to support a generalized conditions, so that any model satisfying them like grids and blocks, similar to CUDA and TPU frameworks, will work.

Thank you

Any questions?

  • Bernstein, Analysis of Programs for Parallel Processing, IEEE TEC 1966.

  • Reynolds, Toward a Grainless Semantics for Shared-Variable Concurrency, FSTTCS 2004.

  • Brookes, A Semantics for Concurrent Separation Logic, CONCUR 2004.

  • de Moura and Ullrich, The Lean 4 Theorem Prover and Programming Language, CADE 28, 2021.