multi-tasking map (mapreduce, tasks in rust)

46
cs4414 Fall 2013 University of Virginia David Evans Class 9: Mapping in Parallel odhpur, India (Dec 2011)

Upload: david-evans

Post on 13-Nov-2014

1.762 views

Category:

Education


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Multi-Tasking Map (MapReduce, Tasks in Rust)

cs4414 Fall 2013University of Virginia

David Evans

Class 9: Mapping in Parallel

Jodhpur, India (Dec 2011)

Page 2: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 2

Plan for Today

• Recap list map • Google’s MapReduce• Tasks in Rust• Multi-threaded map

PS2 is due Monday (30 Sept) at 8:59pm.Submission form will be posted later today, and include signup for scheduling your demo/review. All team members are expected to participate in the review, except in extreme circumstances.

Page 3: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 3

struct Node { head : int, tail : Option<~Node>}

type List = Option<~Node> ;

trait Map { fn mapr(&self, &fn(int) -> int) -> List;}

impl Map for List { fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } } }

You should understand everything in this code.Ask questions now if there is anything unclear.

Page 4: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 4

Cost of Map

Core 1What is the running time of p.map(f) using one core where p is a list of N elements and each evaluation of f(x) takes 1ms?

Page 5: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 5

Cost of Multi-Core Map

Core 1

Core 3

Core 2

Core 4

What is the running time of p.map(f) using k cores where p is a list of N elements and each evaluation of f(x) takes 1ms?

Page 6: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 6

How should we parallelize map?

fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } }

Page 7: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 7

“MapReduce is a programming model and an associated implementation for processing and generating large data sets. Users specify a map function that processes a key/value pair to generate a set of intermediate key/value pairs, and a reduce function that merges all intermediate values associated with the same intermediate key. Many real world tasks are expressible in this model, as shown in the paper.

Programs written in this functional style are automatically parallelized and executed on a large cluster of commodity machines.”

OSDI 2004

Page 8: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 8

Did Google invent map?

Page 9: Multi-Tasking Map (MapReduce, Tasks in Rust)

9

John McCarthy1927-2011

Page 10: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 10

Page 11: Multi-Tasking Map (MapReduce, Tasks in Rust)

11

1955-1960: First “mass-produced” computer (sold 123 of them)1 accumulator register (38 bits), 3 decrement registers (15 bit)Instructions had 3 bit opcode, 15 bit decrement, 15 bit address

Magentic Core Memory32,000 36-bit words40,000 instructions/second

Page 12: Multi-Tasking Map (MapReduce, Tasks in Rust)

12

John McCarthyplaying chess with IBM 7090(1967)

Page 13: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 13

fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } }

Page 14: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 14

Page 15: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 15

@ pointers (in 1960)

Page 16: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 16

Page 17: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 17

MapReduceGoogle’s map:

fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } }

Page 18: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 18

Page 19: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 19

fn mapg<K1, V1, K2, V2>(List<Pair<K1, V1>>, f: &fn(K1, V1) -> (K2, V2)) -> List<Pair<K2, V2>>fn reduceg<K, V, R>(K, List<V>) -> List<R>

Page 20: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 20

fn mapg<K1, V1, K2, V2>(List<Pair<K1, V1>>, f: &fn(K1, V1) -> (K2, V2)) -> List<Pair<K2, V2>>fn reduceg<K, V, R>(K, List<V>) -> List<R>

fn map_reduce<K1, V1, K2, V2, R>( List<Pair<K1, V2>>, mapf: &fn(K1, V1) -> (K2, V2)), reducef: &fn(K2, List<V2>) -> R)) -> List<R>

Page 21: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 21

fn map_reduce<K1, V1, K2, V2, R>( data: List<Pair<K1, V2>>, mapf: &fn(K1, V1) -> (K2, V2)), reducef: &fn(K2, List<V2>) -> R)) -> List<R> {

}

Page 22: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 22

fn map_reduce<K1, V1, K2, V2, R>( data: List<Pair<K1, V2>>, mapf: &fn(K1, V1) -> (K2, V2)), reducef: &fn(K2, List<V2>) -> R)) -> List<R> { let ivalues = data.map(mapf)

let mvalues = // merge ivalues by k2 mvalues.map(reducef)}

Completing the code (with parallel map will finish today) is left as sticker-worthy exercise!

Page 23: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 23

Mapping in Parallel

Page 24: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 24

Processes, Threads, Tasks

ProcessOriginally: abstraction for owning the whole

machineWhat do you need:

Thread(Illusion of) independent sequence of instructionsWhat do you need:

Page 25: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 25

Processes, Threads, Tasks

ProcessOriginally: abstraction for owning the whole

machineWhat do you need:

Own program counterOwn stack, registersOwn memory space

Own program counterOwn stack, registersShares memory space

Thread(Illusion of) independent sequence of instructionsWhat do you need:

Page 26: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 26

Tasks in Rust

Page 27: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 27

Tasks

Own PCOwn stack, registersSafely shared

immutable memorySafely independent

own memory

fn spawn(f: ~fn())

spawn( | | { println(“Get back to work!”); });

do spawn { println(“Get back to work!”); }

syntactic sugar:

Task = Thread – unsafe memory sharingor

Task = Process + safe memory sharing – cost of OS process

Page 28: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 28

impl Map for List { fn mapr(&self, f: &fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { Some(~Node{ head: f(node.head), tail: node.tail.mapr(f) }) }, } } }

Original single-threaded mapr

fn spawn(f: ~fn())

Page 29: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 29

impl Map for List { fn mapr(&self, f: extern fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => {

do spawn { f(node.head) } Some(~Node{ head: ?, tail: node.tail.mapr(f) }) }, } } }

First attempt

Cannot use node here!

Page 30: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 30

impl Map for List { fn mapr(&self, f: extern fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { let val = node.head;

do spawn { f(val) } Some(~Node{ head: ?, tail: node.tail.mapr(f) }) }, } } }

How can we get results back from a spawned task without shared memory?

Page 31: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 31

Channels

let (port, chan) : (Port<int>, Chan<int>) = stream();let val = node.head;do spawn { chan.send(f(val));}let newval = port.recv();

Page 32: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 32

Using streams to spawn is dangerous for salmon, but Rust saves you from (data) races with the bears!

Page 33: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 33

First attempt

fn mapr(&self, f: extern fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { let (port, chan) : (Port<int>, Chan<int>) = stream(); let newtail = node.tail.mapr(f); let val = node.head; do spawn { chan.send(f(val)); } Some(~Node{ head: port.recv(), tail: newtail }) } } }} Compiles are runs fine and produces correct output…

but has a major bug!

Page 34: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 34

Now we’re spawning!

fn mapr(&self, f: extern fn(int) -> int) -> List { match(*self) { None => None, Some(ref node) => { let (port, chan) : (Port<int>, Chan<int>) = stream(); let val = node.head; do spawn { chan.send(f(val)); } let newtail = node.tail.mapr(f); Some(~Node{ head: port.recv(), tail: newtail }) } } }}

Page 35: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 35

fn collatz_steps(n: int) -> int { if n == 1 { 0 } else { 1 + collatz_steps(if n % 2 == 0 { n / 2 } else { 3*n + 1 }) }}

fn find_collatz(k: int) -> int { // Returns the minimum value, n, with Collatz stopping time >= k. let mut n = 1; while collatz_steps(n) < k { n += 1; } n}

fn main() { let lst0 : List = Some(~Node{head: 400, tail: . Some(~Node{head : 410, tail: // … 16 total similar elements } ); println(lst0.to_str()); let lst1 = lst0.mapr(find_collatz); println(lst1.to_str()); let lst2 = lst1.mapr(find_collatz); println(lst2.to_str());}

Page 36: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 36

When 350+% of your CPU isn’t fast enough, its time to buy a new computer!

Page 37: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 37

Page 38: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 38

Intel i7 Quad-Core Processor

Page 39: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 39

Intel i7 Quad-Core Processor

Core Core Core Core

Shared Memory Cache (L3 = 6MB)

~256

KB

L2

Cach

e (?

)

Page 40: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 40

Why so few?

Page 41: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 41

Hannah Bowers, a 4th year student reading Spanish and Portuguese, was beavering away in the library when ‘smoke suddenly started to come out’ of her computer. Fortunately, she removed the fire hazard from the library, averting disaster at the last moment. The student gave The Tab her version of the story:“I was in the library working at my computer when smoke suddenly started to come out of it. I freaked out for a second, trying to save my work onto my hard disk, but then I realised it was probably more important to take it out of the library.

The Tab (Oxford), “Laptop Fire Almost Destroys College Library”

Page 42: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 42

Where the Cores Are

nVIDIA GeForce GTX 650M

384 cores(but even harder for typical programs to use well than Intel’s cores)

Page 43: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 43

How much faster will my Rust mapping program be on my new machine?

2013 MacBook ProIntel i7-3740QM 2.7 GHz, 4 cores (8 threads)6MB shared L3 cache

2011 MacBook AirIntel i5-2557M1.7 GHz, 2 cores (4 threads)3 MB shared L3 cache

both support “hyperthreading” (two threads per core)

60 seconds(normalized time, running on 16-element list)

?

Page 44: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 44

Page 45: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 45

Submit your “guesses” and reasoning in course forum….hopefully I will know the actual answer by Tuesday!

PS2 is due Monday (30 Sept) at 8:59pm.Submission form will be posted later today, and include signup for scheduling your demo/review. All team members are expected to participate in the review, except in extreme circumstances.

Page 46: Multi-Tasking Map (MapReduce, Tasks in Rust)

April 8, 2023 University of Virginia cs4414 46