Algebraic Multiparty Protocol Programming David Castro-Perez Nobuko - - PowerPoint PPT Presentation

algebraic multiparty protocol programming
SMART_READER_LITE
LIVE PREVIEW

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, . . .


slide-1
SLIDE 1

Algebraic Multiparty Protocol Programming

David Castro-Perez Nobuko Yoshida

Imperial College London

December 17, 2018

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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.

slide-4
SLIDE 4

Overview

Algebraic Functional Language (Alg) Parallel Algebraic Language (ParAlg) Parallel Code MPST

role annotations code generation protocol inference typability

slide-5
SLIDE 5

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)

slide-6
SLIDE 6

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)

slide-7
SLIDE 7

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

))

slide-8
SLIDE 8

Evaluating dftn

((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y)

slide-9
SLIDE 9

Evaluating dftn

((add △ sub) ◦ ((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2)))(x, y) = (add △ sub) (((dftn/2 ◦ π1) △ (exp ◦ dftn/2 ◦ π2))(x, y))

slide-10
SLIDE 10

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))

slide-11
SLIDE 11

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))

slide-12
SLIDE 12

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

)

slide-13
SLIDE 13

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

)

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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}
slide-16
SLIDE 16

ParAlg: Size-2 FFT protocol

(add △ sub ) ◦ ((dftn/2

  • π1) △ ( exp ◦ dftn/2
  • π2))
slide-17
SLIDE 17

ParAlg: Size-2 FFT protocol

(add@r0 △ sub@r1) ◦ ((dftn/2@r2 ◦ π1) △ ({exp ◦ dftn/2}@r3 ◦ π2))

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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)  

slide-21
SLIDE 21

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).

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

Conclusions

◮ We developed an algebraic approach to protocol inference and

code generation.

◮ By adding role annotations, we interpret data-flow as

communication.

◮ Different mappings of computations to roles yield different

parallelisations: i.e. programmers can control how to parallelise their code by assigning parts of it to different roles.

◮ Global types provide valuable documentation about how a

program was parallelised.

slide-29
SLIDE 29

Future Work

◮ More examples, run on a machine with more cores. ◮ Explore code generation for GPUs/FPGAs. ◮ Support wider range of parallel patterns by using extensions to

MPST: e.g. dynamic roles.

◮ Cost-models based on the inferred global type. ◮ Perform low-level code optimisations to the generated code,

ensuring that the protocol is not modified.

◮ Implement semi-automatic strategies for rewriting programs

and assigning roles.

slide-30
SLIDE 30

Thank you!