Algebraic Multiparty Protocol Programming David Castro-Perez Nobuko - - PowerPoint PPT Presentation
Algebraic Multiparty Protocol Programming David Castro-Perez Nobuko - - PowerPoint PPT Presentation
Algebraic Multiparty Protocol Programming David Castro-Perez Nobuko Yoshida Imperial College London December 17, 2018 Parallel Programming Parallel programming is increasingly important: many-core architectures, GPUs, FPGAs, . . .
Parallel Programming
◮ Parallel programming is increasingly important: many-core
architectures, GPUs, FPGAs, . . .
◮ Low-level techniques are error-prone: deadlocks, data races,
etc.
◮ High-level techniques constraints programmers to using a
particular model, or a fixed set of parallel constructs.
◮ Achieving (predictable) speedups is hard! ◮ Our goal: generate message-passing parallel code from
sequential implementations.
◮ Not constrained by a fixed set of high-level parallel constructs. ◮ Guarantee correctness ◮ Predictability
Proposal : Algebraic Multiparty Protocol Programming
◮ Algebra of programming for specifying sequential algorithms.
◮ Use higher-order combinators. ◮ Use their equational theory for program optimisation and
parallelisation.
◮ Multiparty session types for message-passing concurrency.
◮ We provide an abstraction of the communication protocol of
the generated parallel code as a global type.
◮ We prove that we do not introduce concurrency errors, using
the theory of Multiparty Session Types (MPST).
◮ Key idea: convert the implicit data-flow of the higher-order
combinators to explicit communication.
Overview
Algebraic Functional Language (Alg) Parallel Algebraic Language (ParAlg) Parallel Code MPST
role annotations code generation protocol inference typability
Algebra of Programming
◮ Mathematical framework that codifies the basic laws of
- algorithmics. [Backus 78, Meertens 86, Bird 89].
◮ We define Algebraic Functional Language (Alg), a point-free
functional programming language with a number of categorically-inspired combinators as syntactic constructs: composition, polynomial functors, recursion.
◮ Examples:
◮ Function composition and identity:
e1 ◦ e2 = λx. e1 (e2 x) id = λx. x e1 ◦ (e2 ◦ e3) ≡ (e1 ◦ e2) ◦ e3 id ◦ e ≡ e ◦ id ≡ e
◮ Split and projections:
e1 △ e2 = λx. (e1 x, e2 x) πi = λ(x1, x2). xi πi ◦ (e1 △ e2) ≡ ei (e1 △ e2) ◦ e ≡ (e1 ◦ e) △ (e2 ◦ e)
Algebra of Programming
◮ Mathematical framework that codifies the basic laws of
- algorithmics. [Backus 78], “Squiggol”.
◮ We define Algebraic Functional Language (Alg), a point-free
functional programming language with a number of categorically-inspired combinators as syntactic constructs: composition, polynomial functors, recursion.
◮ Examples:
◮ Function composition and identity:
e1 ◦ e2 = λx. e1 (e2 x) id = λx. x e1 ◦ (e2 ◦ e3) ≡ (e1 ◦ e2) ◦ e3 id ◦ e ≡ e ◦ id ≡ e
◮ Split and projections:
e1 △ e2 = λx. (e1 x, e2 x) πi = λ(x1, x2). xi πi ◦ (e1 △ e2) ≡ ei (e1 △ e2) ◦ e ≡ (e1 ◦ e) △ (e2 ◦ e)
Example: Cooley-Tukey FFT
◮ Discrete Fourier Transform
Xk =
N−1
- n=0
xne− 2πi
N nk = Ek + e− 2πi N kOk
Xk+ N
2
= Ek − e− 2πi
N kOk
Ek = dft of the even-indexed part of xn Ok = dft of the odd-indexed part of xn
◮ Alg expression
dftn = (add
- +
△ sub
- −
) ◦ ((dftn/2 ◦ π1)
- Ek
△( exp
- e− 2πi
N k
- dftn/2 ◦ π2
- Ok
))
Evaluating dftn
((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y)
Evaluating dftn
((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y) = (add △ sub) (((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2))(x, y))
Evaluating dftn
((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y) = (add △ sub) (((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2))(x, y)) = (add △ sub) ((dftn/2 ◦ π1) (x, y), (exp ◦ dftn/2 ◦ π2) (x, y))
Evaluating dftn
((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y) = (add △ sub) (((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2))(x, y)) = (add △ sub) ((dftn/2 ◦ π1) (x, y), (exp ◦ dftn/2 ◦ π2) (x, y)) = (add △ sub) (dftn/2 x, exp (dftn/2 y))
Evaluating dftn
((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y) = (add △ sub) (((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2))(x, y)) = (add △ sub) ((dftn/2 ◦ π1) (x, y), (exp ◦ dftn/2 ◦ π2) (x, y)) = (add △ sub) (dftn/2 x, exp (dftn/2 y)) = ( add (dftn/2 x, exp (dftn/2 y))
- Xk
, sub (dftn/2 x, exp (dftn/2 y))
- Xk+ N
2
)
Evaluating dftn
Xk = Ek + e− 2πi
N kOk
Xk+ N
2 = Ek − e− 2πi N kOk
((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y) = (add △ sub) (((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2))(x, y)) = (add △ sub) ((dftn/2 ◦ π1) (x, y), (exp ◦ dftn/2 ◦ π2) (x, y)) = (add △ sub) (dftn/2 x, exp (dftn/2 y)) = ( add (dftn/2 x, exp (dftn/2 y))
- Xk
, sub (dftn/2 x, exp (dftn/2 y))
- Xk+ N
2
)
ParAlg: Alg + role annotations
◮ We call Parallel Algebraic Language (ParAlg) to Alg extended
with role annotations.
◮ ⊢ e ⇒ p : A → B | C: “Alg expression e synthethises ParAlg
expression p, with type A → B and choices C”.
◮ E.g.
◮ A = a@r0 × b@r1 is the product a × b, where a is at r0 and b
at r1.
◮ p = e2@r2 ◦ e1@r1 is the composition of e2 ◦ e1, where e2 is
applied at r2, and e1 at r1.
r0 r1 r2
ParAlg: Inferring Global Types
◮ A global type, in Multiparty Session Types, is a global
description of a communication protocol between multiple participants.
◮ Inferring a global type from ParAlg implies representing the
implicit dataflow with explicit communication.
◮ C p ⇐ A ∼ G: “Expression p with domain A, in a choice
context C behaves as global type G.”
ParAlg global type e0@r0 ◦ e1@r1 : a@r → c@r0 r → r1 : a. r1 → r0 : b. end e0@r0 △ e1@r1 : a@r → b@r0 × c@r1 r → r0 : a. r → r1 : a. end e0@r0▽e1@r1 : (a + b)@r → c@r0 ∪ c@r1 r → {r0, r1}{inj1. r → r0 : a. end,
- inj2. r → r1 : b. end}
ParAlg: Size-2 FFT protocol
(add △ sub ) ◦ ((dftn/2
- π1) △ ( exp ◦ dftn/2
- π2))
ParAlg: Size-2 FFT protocol
(add@r0 △ sub@r1) ◦ ((dftn/2@r2 ◦ π1) △ ({exp ◦ dftn/2}@r3 ◦ π2))
ParAlg: Size-2 FFT protocol
(add@r0 △ sub@r1) ◦ ((dftn/2@r2 ◦ π1) △ ({exp ◦ dftn/2}@r3 ◦ π2)) Global type assuming that the domain is: V @r4 × V @r5: r4 → r2 : V . r5 → r3 : V . r2 → r0 : V . r2 → r1 : V . r3 → r0 : V . r3 → r1 : V .end r4 r5 r2 r3 r0 r1
ParAlg: Size-2 FFT protocol
(add@r0 △ sub@r1) ◦ ((dftn/2@r2 ◦ π1) △ ({exp ◦ dftn/2}@r3 ◦ π2)) Global type assuming that the domain is: (V × V )@r4: r4 → r2 : V . r4 → r3 : V . r2 → r0 : V . r2 → r1 : V . r3 → r0 : V . r3 → r1 : V .end r4 r2 r3 r0 r1
Message Passing Monad(I)
◮ We translate ParAlg to the Message Passing Monad (Mp):
send r x, recv r a, branch r m1 m2, choice x r f1 f2.
◮ The translation keeps track of:
◮ Location of the data. ◮ Branches in the control flow: which roles perform choices, and
which roles are affected by which choice.
◮ For each role r in p : A → B, we “project” its behaviour as a
monadic action. E.g. e0@r0 ◦ e1@r1 : a@r → c@r0 r → λx. send r1 x r0 → λ . recv r1 b > > = λx. return (e0 x) r1 → λ . recv r a > > = λx. send r0 (e1 x)
Correctness
Theorem (Protocol Deadlock Freedom)
For all e, p, A, B, C, if ⊢ e ⇒ p : A → B | C, then there exists a global type G s.t. C p ⇐ A ∼ G, and G is well-formed.
Theorem (Deadlock Freedom of the Generated Code)
For all p, A, B, C, G, r, if ⊢ e ⇒ p : A → B | C and C p ⇐ A ∼ G then pr
A : A ↾ r → Mp (G ↾ r) (B ↾ r).
Speedups on a 4-Core Machine
FFT
1 2 3 4 5 6 7 8
+RTS -N
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5
Speedup Size = 1048576
K2 K4 K6 K8
Speedups on a 4-Core Machine
FFT
1 2 3 4 5 6 7 8
K
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5
Speedup +RTS -N8
1024 8192 32768 1048576
Speedups on a 4-Core Machine
FFT
103 104 105 106
Size (n. elems)
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5
Speedup +RTS -N8
K1 K2 K6 K8
Speedups on a 4-Core Machine
Mergesort
1 2 3 4 5 6 7 8
+RTS -N
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5
Speedup Size = 640000
K2 K4 K6 K8
Speedups on a 4-Core Machine
Mergesort
1 2 3 4 5 6 7 8
K
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
Speedup +RTS -N8
10000 40000 80000 640000
Speedups on a 4-Core Machine
Mergesort
104 105
Size (n. elems)
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5
Speedup +RTS -N8
K1 K2 K6 K8