CS 251 Fall 2019 Principles of Programming Languages
Ben Wood
λ
CS 251 Fall 2019
Principles of Programming Languages
Ben Wood
λ
https://cs.wellesley.edu/~cs251/f19/
Parallelism
(and Concurrency)
Parallelism 1
CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming - - PowerPoint PPT Presentation
CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood Parallelism (and Concurrency) https://cs.wellesley.edu/~cs251/f19/ 1 Parallelism Parallelism and Concurrency
CS 251 Fall 2019 Principles of Programming Languages
Ben Wood
CS 251 Fall 2019
Principles of Programming Languages
Ben Wood
https://cs.wellesley.edu/~cs251/f19/
Parallelism 1
– essence, key concerns – non-sequential thinking – some high-level models – some mid-to-high-level mechanisms
– performance engineering / measurement – deep programming proficiency – exhaustive survey of models and mechanisms
Parallelism 2
Parallelism 3
data / work data = resources workers = computations workers = resources di divide ded d among sh share Use more resources to complete work faster. Coordinate access to shared resources. Both can be expressed using a variety of primities.
Parallelism 4
parallelism/concurrency.
– data parallelism:
– task parallelism:
– futures / tasks
– work-stealing
Manticore
Parallelism 5
[| 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
Parallelism 6
with no
raint
relative timing or
Manticore
Parallelism 7
many argument data of same type pa parallelize ze ap appli licat cation o
same ame o
ation to to all data many result data of same type no ordering/ interdependence
Parallelism 8
: ('a -> 'b) -> 'a parray -> 'b parray
: ('a -> bool) -> 'a parray -> 'a parray
Manticore
Parallelism 9
: (('a * 'a) -> 'a) -> 'a -> 'a parray -> 'a
f ( , ) f ( , ) f ( , ) f ( , ) f ( , ) f ( , ) f ( , )
sibling of fold f must be associative … … … … Manticore
Parallelism 10
pa parallelize ze appl pplication
wi within larg rger r computation some ordering/interdependence controlled explicitly
Parallelism 11
fun qsortP (a: int parray) : int parray = if lengthP a <= 1 then a else let val pivot = a ! 0 (* parray indexing *) pval sorted_lt = qsortP (filterP (fn x => x < pivot) a) pval sorted_eq = filterP (fn x => x = pivot) a pval sorted_gt = qsortP (filterP (fn x => x > pivot) a) in concatP (sorted_lt, concatP (sorted_eq, sorted_gt)) end St Start ev evaluating in p in parallel el bu but do don’t wait unt until il need needed ed. Wa Wait until results are ready before using them. sorted_eq sorted_gt pivot concatP sorted_lt Manticore
Parallelism 12
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 t else pcase find_any left e & find_any right e of SOME tree & ? => SOME tree | ? & SOME tree => SOME tree | NONE & NONE => NONE
Ev Eval aluat uate these in n par aral allel.
If If o
ne f finis inishes w wit ith S SOME, r , retur urn it n it wi without waiting for r the other. If If b both f finis inish w wit ith N NONE, r , retur urn N n NONE.
Parallelism 13
signature FUTURE = sig type '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
Parallelism 14
Parallelism 15
let val f = future (fn () => e) in work … (touch f) … end
time
Parallelism 16
let val f = future (fn () => e) in work … (touch f) … end
time
let pval x = e in … x … end let val x = future (fn () => e) in … (touch x) … end
*a bit more: implicitly cancel an untouched future
Parallelism 17
Parallelism 18
Suppose we represent parrays as lists* of
*not the actual implementation
Parallelism 19
Suppose we represent parrays as lists* of
*not the actual implementation
Ke Key semantic difference 1 vs 2?
– Choice is a distinct primitive* not offered by futures alone.
– function calls vs. val bindings.
Parallelism 20
*at least when implemented well.