15-150 Fall 2020
Stephen Brookes
Lecture 17 Sequences and cost graphs
Halloween, a full moon and a time change all happening simultaneously
15-150 Fall 2020 Stephen Brookes Lecture 17 Sequences and cost - - PowerPoint PPT Presentation
15-150 Fall 2020 Stephen Brookes Lecture 17 Sequences and cost graphs Halloween, a full moon and a time change all happening simultaneously announcements Next Tuesday (3 Nov) is ELECTION DAY Class will be held as usual (on zoom)
Stephen Brookes
Halloween, a full moon and a time change all happening simultaneously
(constant time)
a single node
sequential composition
parallel composition
independent code … add the work dependent code … add the work
dependent code … add the span independent code … max the span
must be done before
and
each node represents a single unit of work
basic ops take constant time
work: single processor span: unlimited processors
(5 rounds,
assign units of work to processors respecting data dependency
uses 5 processors
Optimal schedule using p processors: Do (up to) p units of work each round
Total work to do is w Needs at least s steps
Richard Brent is an illustrious Australian mathematician and computer scientist. He is known for Brent’s Theorem, which shows that a parallel algorithm can always be adapted to run on fewer processors with only the obvious time penalty —a beautiful example of an “obvious” but non-trivial theorem.
David Brent is the manager of the Slough branch
computers to buy to improve office efficiency.
Using more than this many processors won’t yield any speed-up
signature SEQ = sig type ’a seq exception Range val tabulate : (int -> ’a) -> int -> ’a seq val length : ’a seq -> int val nth : int -> ’a seq -> ’a val split : ’a seq -> ’a seq * ’a seq val map : (’a -> ’b) -> ’a seq -> ’b seq val reduce : (’a * ’a -> ’a) -> ’a -> ’a seq -> ’a val mapreduce : (’a -> ’b) -> ’b -> (’b * ’b -> ’b) -> ’a seq -> ’b end
Obviously, a list-based implementation of sequences isn’t going to be faster than lists! But arrays, trees, can be.
G0 Gn-1
W(tabulate f n) = W(f 0) + … + W(f (n-1)) + c S(tabulate f n) = max {S(f 0), …, S(f (n-1))} + c REQUIRES f total & n ≥0 ENSURES
G0 Gn-1
producing a sequence value ⟨x0, ..., xn-1⟩
G0 Gn-1
G0 Gn-1
. . .
showing sequential evaluation
f v0 … f vn-1
map g ⟨f 0, ..., f(n-1)⟩ = ⟨g(f 0), ..., g(f(n-1))⟩ = ⟨(g o f) 0, ..., (g o f)(n-1)⟩
reduce (op +) 0 ⟨v0, ..., vn-1⟩ = v0 + … + vn-1 reduce (op * ) 1 ⟨v0, ..., vn-1⟩ = v0 * … * vn-1 reduce (op @ ) [ ] ⟨v0, ..., vn-1⟩ = v0 @ … @ vn-1
W(2n) = 2*W(n) + c S(2n) = S(n) + c
Let W(m) = work for reduce g z S when length S = m
fun reduce g z S = case (length S) of 0 => z | 1 => nth 0 S | n => let val (L, R) = split S in g(reduce g z L, reduce g z R) end
fun reduce g z Empty = z | reduce g z (Node(A, x, B)) = let val (a, b) = (reduce g z A, reduce g z B) in g(a, g(x, b)) end
Proof: by induction on n
fun reduce g z S = case (length S) of 0 => z | 1 => nth 0 S | n => let val (L, R) = split S in g(reduce g z L, reduce g z R) end
fun sum (s : int seq) : int = reduce (op +) 0 s
fun count (s : int seq seq) : int = sum (map sum s)
sum s1
sum sn
sum si
log2 n
cost graph of sum si cost graph of map sum s
sum s1
sum sn
sum ⟨t1, ..., tn⟩
log2 n log2 n
cost graph of sum (map sum s)
Every sensible implementation of SEQ should validate these equations
fun reverse (s : ’a seq) : ’a seq = let val n = length s in tabulate (fn i => nth (n - i - 1) s) n end
reverse ⟨v1, ..., vn⟩ ?
Use the given information about work/span for length, nth, tabulate
zip : ’a seq * ’b seq -> (’a * ’b) seq REQUIRES length xs = length ys ENSURES zip (⟨x1,. . .,xn⟩, ⟨y1,. . .,yn⟩) = ⟨(x1,y1),. . .,(xn,yn)⟩ fun zip (xs, ys) = let val n = Int.min(length xs, length ys) in tabulate (fn i => (nth i xs, nth i ys)) n end
Use the given information about work/span for length, nth, tabulate
(15150-style) red-black trees vicious bishops
The Fun and Easy Way to Create Complex Incomprehensible Programs that Work Your First Aid Kit For Cleaning Up Messy Direct-style Programs Explained in Plain English How To Make Your Program Backtrack
C O N T I N U A T I O N P A S S I N G S T Y L E F O R D U M M I E S
most generals
NIGHTMARE ON ML STREET
Check out Vote411.org Use it to help register to vote, check registrations,
Use it to find polling locations and check ID requirements for voting