Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in...

20
CS 251 Fall 2019 Principles of Programming Languages Ben Wood λ CS 251 Spring 2020 Principles of Programming Languages Ben Wood λ https://cs.wellesley.edu/~ cs251/s20/ Parallelism (and Concurrency) Parallelism 1

Transcript of Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in...

Page 1: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

CS 251 Fall 2019Principles of Programming LanguagesBen Woodλ CS 251 Spring 2020Principles of Programming LanguagesBen Woodλ

https://cs.wellesley.edu/~cs251/s20/

Parallelism(and Concurrency)

Parallelism 1

Page 2: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallelism and Concurrency in 251

• Goal: encounter– essence, key concerns– non-sequential thinking– some high-level models– some mid-to-high-level mechanisms

• Non-goals:– performance engineering / measurement– deep programming proficiency– exhaustive survey of models and mechanisms

Parallelism 2

Page 3: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Eliminate 1 big assumption:Evaluation happens

as a sequence of ordered steps.

Parallelism 3

Page 4: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallelism Concurrency

data / work

data = resources

workers = computations

workers = resources

divided among

share

Use more resourcesto complete work faster.

Coordinate accessto shared resources.

Both can be expressed using a variety of primitives.Parallelism 4

Page 5: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallelism via Manticore• Extends SML with language features for

parallelism/concurrency.• Mix research vehicle / established models.• Parallelism patterns:

– data parallelism:• parallel arrays• parallel tuples

– task parallelism:• parallel bindings• parallel case expressions

• Unifying model:– futures / tasks

• Mechanism:– work-stealing

Parallelism 5

Manticore

Page 6: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Data parallelism

Parallelism 6

many argument data of same type

parallelizeapplication of same operationto all data

many result data of same type

no ordering/interdependence

Page 7: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallel arrays: 'a parray

Parallelism 7

[| e1, e2, …, en |]

[| elo to ehi by estep |]

[| e | x in elems |]

[| e | x in elems where pred |]

parallel mapping comprehensions

parallel filtering comprehensions

integer ranges

literal parray

Manticore

Page 8: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallel array comprehensions

[| e1 | x in e2 |]

Evaluation rule:1. Under the current environment, E, evaluate

e2 to a parray v2.2. For each element vi in v2, with no constraint

on relative timing order:1. Create new environment Ei = x↦vi, E.2. Under environment Ei, evaluate e1 to a value vi'

3. The result is [| v1', v2', …, vn' |]

Parallelism 8

Manticore

Page 9: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallel map / filter

fun mapP f xs =[| f x | x in xs |]

: ('a -> 'b) -> 'a parray -> 'b parray

fun filterP p xs =[| x | x in xs where p x |]

: ('a -> bool) -> 'a parray -> 'a parray

Parallelism 9

Manticore

Page 10: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallel reduce

Parallelism 10

fun reduceP f init xs = …

: (('a * 'a) -> 'a) -> 'a -> 'a parray -> 'a

f ( , ) f ( , ) f ( , ) f ( , )

f ( , ) f ( , )

f ( , )

sibling of foldl/foldrcombiner function, f, must be associative

… … … …

Manticore

Page 11: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Task parallelism

Parallelism 11

parallelize applicationof different operationswithin larger computation

some ordering/interdependencecontrolled explicitly

Page 12: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallel bindings

Parallelism 12

fun qsortP (a: int parray) : int parray =if lengthP a <= 1then aelse

letval pivot = a ! 0 (* parray indexing *)pval sorted_lt =

qsortP (filterP (fn x => x < pivot) a)pval sorted_eq =

filterP (fn x => x = pivot) apval sorted_gt =

qsortP (filterP (fn x => x > pivot) a)in

concatP (sorted_lt, concatP (sorted_eq, sorted_gt))end

Start evaluatingin parallelbutdon’t waituntil needed.

Wait until results are ready before using them.

sorted_eq sorted_gt

pivot

concatP

sorted_lt

concatP

Manticore

Page 13: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parallel cases

Parallelism 13

Manticore

datatype 'a bintree = Empty| Node of 'a * 'a bintree * 'a bintree

fun find_any t e =case t of

Empty => NONE| Node (elem, left, right) =>if e = elem then SOME telse

pcase find_any left e & find_any right e of

SOME tree & ? => SOME tree| ? & SOME tree => SOME tree

| NONE & NONE => NONE

Evaluate these in parallel.

If one finishes with SOME, return itwithout waiting for the other.

If both finish with NONE, return NONE.

Page 14: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Futures: unifying model for Manticore parallel features

Parallelism 14

signature FUTURE =sigtype 'a future

(* Produce a future for a thunk.Like Promise.delay. *)

val future : (unit -> ’a) -> ’a future

(* Wait for the future to complete and return the result.Like Promise.force. *)

val touch : ’a future -> ’a

(* More advanced features. *)datatype 'a result = VAL of 'a | EXN of exn

(* Check if the future is complete and get result if so. *)val poll : ’a future -> ’a result option

(* Stop work on a future that won't be needed. *)val cancel : ’a future -> unit

end

future = promise speculatively forced in parallel

Page 15: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Futures: timeline visualization 1

Parallelism 15

letval f = future (fn () => e)

in

work

…(touch f)…

end

time

Page 16: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Futures: timeline visualization 2

Parallelism 16

letval f = future (fn () => e)

in

work

…(touch f)…

end

time

Page 17: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

pval as future sugar

Parallelism 17

let pval x = ein … x … end

let val x = future (fn () => e)in … (touch x) … end

*a bit more: implicitly cancel an untouched future once it becomes clear it won't be touched.

Page 18: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parray ops as futures: rough idea 1

Parallelism 18

[| f x | x in xs |]

map touch(map (fn x =>

future (fn () => f x))xs)

Suppose we represent parrays as lists* of elements:

*actual implementation uses a more sophisticated data structure

Page 19: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Parray ops as futures: rough idea 2

Parallelism 19

[| f x | x in xs |]

map (fn x => future(fn () => f (touch x)))

xs

Suppose we represent parrays as lists* of element futures:

*actual implementation uses a more sophisticated data structure

Key semantic difference 1 vs 2?

Page 20: Parallelism - cs.wellesley.educs251/s20/slides/parallelism.pdf · Parallelism and Concurrency in 251 •Goal: encounter –essence, key concerns –non-sequential thinking –some

Odds and ends• pcase: not just future sugar

– Choice is a distinct primitive* not offered by futures alone.

• Where do execution resources from futures come from? How are they managed?

• Tasks vs futures:– Analogy: function calls vs. val bindings.

• Forward to concurrency and events…

Parallelism 20

*at least when implemented well.