Documentation

Schedule

Why psync's schedules are safe #

A small model of what #[psync::graph] does, with the three facts its correctness rests on.

Proven here:

  1. commute: independent operations commute.

  2. interleave_correct: running chains of a level concurrently, under any interleaving that keeps each chain's own order, gives the same final state as running them one after another, provided the chains are pairwise independent. This is what each level's join does.

  3. levels_correct: a schedule of levels gives the state of running its chains in order.

  4. speculation_harmless: operations that write nothing (reads) leave the state unchanged, so running extra ones after an exit sequential code would have taken changes nothing. psync's speculation rule never runs a write early, so only such operations are ever extra.

  5. reorders_correct: a reordering that moves operations earlier only past independent ones has the effect of the program as written.

  6. replay_correct: recording every statement's exit outcome and replaying the first one in program order gives the state and the exit of sequential execution.

  7. recording_slot: recording an outcome in a slot is an ordinary operation.

Not modeled: the analyses that compute footprints (the access and effect analyses, and cargo psync check), and the generated Rust (type- and borrow-checked by rustc). The theorems state what those parts must provide: correct footprints, and edges between every pair of operations that are not independent.

Notation used in the theorem statements #

@[reducible, inline]
abbrev Psync.State (Loc Val : Type) :
Equations
Instances For
    structure Psync.Op (Loc Val : Type) :

    An operation with its footprint. frame: it changes only what it writes. local_dep: what it writes depends only on what it reads.

    • run : State Loc Val → State Loc Val
    • R : Loc → Prop
    • W : Loc → Prop
    • frame (s : State Loc Val) (l : Loc) : ¬self.W l → self.run s l = s l
    • local_dep (s s' : State Loc Val) : (∀ (l : Loc), self.R l → s l = s' l) → ∀ (l : Loc), self.W l → self.run s l = self.run s' l
    Instances For
      def Psync.Independent {Loc Val : Type} (a b : Op Loc Val) :

      No edge between a and b: neither writes what the other reads or writes.

      Equations
      Instances For
        theorem Psync.Independent.symm {Loc Val : Type} {a b : Op Loc Val} (h : Independent a b) :

        $$a \mathrel{I} b \implies b \mathrel{I} a$$

        theorem Psync.commute {Loc Val : Type} (a b : Op Loc Val) (h : Independent a b) (s : State Loc Val) :
        b.run (a.run s) = a.run (b.run s)

        $$a \mathrel{I} b \implies \mathrm{run}_b(\mathrm{run}_a(s)) = \mathrm{run}_a(\mathrm{run}_b(s))$$

        1. Independent operations commute.

        def Psync.runAll {Loc Val : Type} :
        List (Op Loc Val) → State Loc Val → State Loc Val

        Running a list of operations in order.

        Equations
        Instances For
          theorem Psync.runAll_append {Loc Val : Type} (xs ys : List (Op Loc Val)) (s : State Loc Val) :
          runAll (xs ++ ys) s = runAll ys (runAll xs s)

          $$[\![ xs \mathbin{+\!\!+} ys ]\!](s) = [\![ ys ]\!]([\![ xs ]\!](s))$$

          theorem Psync.commute_list {Loc Val : Type} (y : Op Loc Val) (xs : List (Op Loc Val)) (h : ∀ (x : Op Loc Val), x ∈ xs → Independent x y) (s : State Loc Val) :
          runAll xs (y.run s) = y.run (runAll xs s)

          $$(\forall x \in xs,\ x \mathrel{I} y) \implies [\![ xs ]\!](\mathrm{run}_y(s)) = \mathrm{run}_y\bigl([\![ xs ]\!](s)\bigr)$$

          An operation independent of every operation of a list commutes with the whole list.

          inductive Psync.Interleave {Loc Val : Type} :
          List (Op Loc Val) → List (Op Loc Val) → List (Op Loc Val) → Prop

          m interleaves the chains a and b, keeping the order within each.

          Instances For
            theorem Psync.interleave_correct {Loc Val : Type} {a b m : List (Op Loc Val)} (hab : ∀ (x : Op Loc Val), x ∈ a → ∀ (y : Op Loc Val), y ∈ b → Independent x y) (hm : Interleave a b m) (s : State Loc Val) :
            runAll m s = runAll (a ++ b) s

            $$(\forall x \in a,\ \forall y \in b,\ x \mathrel{I} y) \;\wedge\; m \in a \parallel b \implies [\![ m ]\!](s) = [\![ a \mathbin{+\!\!+} b ]\!](s)$$

            2. Concurrent chains: any interleaving of two independent chains has the effect of running the first, then the second. (For more chains, interleave one with the interleaving of the rest.)

            inductive Psync.InterleaveAll {Loc Val : Type} :
            List (List (Op Loc Val)) → List (Op Loc Val) → Prop

            Interleaving several chains: the first with an interleaving of the rest.

            Instances For
              theorem Psync.interleaveAll_mem {Loc Val : Type} {cs : List (List (Op Loc Val))} {m : List (Op Loc Val)} (h : InterleaveAll cs m) (y : Op Loc Val) :
              y ∈ m → ∃ (c : List (Op Loc Val)), c ∈ cs ∧ y ∈ c

              $$m \in \textstyle\Vert_{c \in cs}\, c \implies \forall y \in m,\ \exists c \in cs,\ y \in c$$

              def Psync.PairwiseIndependent {Loc Val : Type} :
              List (List (Op Loc Val)) → Prop

              Chains of one level are pairwise independent: no edge between any two of their operations.

              Equations
              Instances For
                theorem Psync.level_correct {Loc Val : Type} {cs : List (List (Op Loc Val))} {m : List (Op Loc Val)} (hp : PairwiseIndependent cs) (hm : InterleaveAll cs m) (s : State Loc Val) :

                $$\mathrm{PairwiseInd}(cs) \;\wedge\; m \in \textstyle\Vert_{c \in cs}\, c \implies [\![ m ]\!](s) = [\![ c_1 \mathbin{+\!\!+} \cdots \mathbin{+\!\!+} c_k ]\!](s)$$

                A whole level: any interleaving of pairwise independent chains has the effect of running the chains one after another.

                inductive Psync.Runs {Loc Val : Type} :
                List (List (List (Op Loc Val))) → List (List (Op Loc Val)) → Prop

                How a schedule runs: each level's chains, pairwise independent, in some interleaving.

                Instances For
                  theorem Psync.levels_correct {Loc Val : Type} {levels : List (List (List (Op Loc Val)))} {ms : List (List (Op Loc Val))} (h : Runs levels ms) (s : State Loc Val) :

                  $$\mathrm{Runs}(\mathit{levels}, ms) \implies [\![ \mathrm{flatten}(ms) ]\!](s) = [\![ \mathrm{flatten}(\mathrm{map}\ \mathrm{flatten}\ \mathit{levels}) ]\!](s)$$

                  3. A schedule is a list of levels run one after another: whatever interleaving each level takes, the result is that of running every chain in order.

                  theorem Psync.speculation_harmless {Loc Val : Type} (extra : List (Op Loc Val)) (hread : ∀ (o : Op Loc Val), o ∈ extra → ∀ (l : Loc), ¬o.W l) (s : State Loc Val) :
                  runAll extra s = s

                  $$(\forall o \in \mathit{extra},\ W_o = \emptyset) \implies [\![ \mathit{extra} ]\!](s) = s$$

                  4. Operations that write nothing leave the state as it was.

                  inductive Psync.Reorders {Loc Val : Type} :
                  List (Op Loc Val) → List (Op Loc Val) → Prop

                  Reorders P L: L is P with operations moved earlier only past operations independent of them. Concretely, the first operation x of P sits in L after a run A of operations that are all independent of x, and the rest of L reorders the rest of P the same way. psync's schedule (levels of chains, from the dependency graph) has this property: an operation scheduled ahead of an earlier one has no edge to it, and psync draws an edge exactly when two operations are not independent.

                  Instances For
                    theorem Psync.reorders_correct {Loc Val : Type} {P L : List (Op Loc Val)} (h : Reorders P L) (s : State Loc Val) :
                    runAll L s = runAll P s

                    $$\mathrm{Reorders}(P, L) \implies [\![ L ]\!](s) = [\![ P ]\!](s)$$

                    5. Such a reordering has the effect of the program as written. Together with levels_correct, the concurrent execution of psync's schedule ends in the state the sequential program ends in.

                    Early exits #

                    A statement such as let x = f().await? may exit: whether it does, and with which value, depends on what it reads. psync never takes an exit inside a chain: it records the outcome in an exit slot and stops only that chain, and after the join it replays the recorded exits in program order (exits.rs). Writes never start before an earlier statement that may exit, so the only work done after the first exit is reads.

                    The theorems below say that this gives exactly the sequential behaviour: the same final state and the same exit.

                    structure Psync.XOp (Loc Val E : Type) :

                    An operation that may exit, with an outcome that depends only on what it reads.

                    • op : Op Loc Val
                    • exit : State Loc Val → Option E
                    • exit_dep (s s' : State Loc Val) : (∀ (l : Loc), self.op.R l → s l = s' l) → self.exit s = self.exit s'
                    Instances For
                      def Psync.seqX {Loc Val E : Type} :
                      List (XOp Loc Val E) → State Loc Val → State Loc Val × Option E

                      Sequential execution with exits: run each operation; stop at the first that exits.

                      Equations
                      Instances For
                        def Psync.outcomes {Loc Val E : Type} :
                        List (XOp Loc Val E) → State Loc Val → List (Option E)

                        Every operation's outcome, in program order, when all of them run (what the exit slots hold).

                        Equations
                        Instances For
                          def Psync.firstExit {E : Type} :
                          List (Option E) → Option E

                          Replay: the first recorded exit in program order.

                          Equations
                          Instances For
                            def Psync.SpecSafe {Loc Val E : Type} :
                            List (XOp Loc Val E) → State Loc Val → Prop

                            psync's speculation rule, along a run: every operation after the first one that exits writes nothing.

                            Equations
                            Instances For
                              theorem Psync.replay_correct {Loc Val E : Type} (xs : List (XOp Loc Val E)) (s : State Loc Val) (h : SpecSafe xs s) :

                              $$\mathrm{SpecSafe}(xs, s) \implies \bigl([\![ \mathrm{op}(xs) ]\!](s),\ \mathrm{firstExit}(\mathrm{outcomes}(xs, s))\bigr) = \mathrm{seq}_X(xs, s)$$

                              6. Running every operation, recording the outcomes and replaying the first exit in program order gives the state and the exit of sequential execution, provided only reads run past the first exit.

                              def Psync.XOp.recording {Loc Val E : Type} [DecidableEq Loc] (x : XOp Loc Val E) (k : Loc) (enc : Option E → Val) :
                              Op Loc Val

                              What psync's generated code runs for a statement that may exit: the statement, which also writes its outcome into its own exit slot k (encoded as a value by enc).

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                theorem Psync.recording_slot {Loc Val E : Type} [DecidableEq Loc] (x : XOp Loc Val E) (k : Loc) (enc : Option E → Val) (s : State Loc Val) :
                                (x.recording k enc).run s k = enc (x.exit s)

                                $$\mathrm{run}_{\mathrm{rec}_k(x)}(s)(k) = \mathrm{enc}\bigl(\mathrm{exit}_x(s)\bigr)$$

                                7. The slot holds the statement's outcome. Since recording is an ordinary Op, every theorem above applies to programs whose statements record their exits: a schedule of independent chains (independence now includes the slots, which no other statement touches) leaves the same values in every slot as program order, so the replay reads the same outcomes.