Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parallel and Distributed Computing: MapReduce Alona Fyshe.

Similar presentations


Presentation on theme: "Parallel and Distributed Computing: MapReduce Alona Fyshe."— Presentation transcript:

1 Parallel and Distributed Computing: MapReduce Alona Fyshe

2 About Me I worked @Google for 3 years And used MapReduce almost every day Now I’m a PhD student in Machine Learning I work on brains

3 Overview Motivating example considerations for distributed computation MapReduce the idea some examples

4 Inspiration not Plagiarism This is not the first lecture ever on Mapreduce I borrowed from: Jimmy Lin http://www.umiacs.umd.edu/~jimmylin/cloud-computing/SIGIR-2009/Lin-MapReduce- SIGIR2009.pdfhttp://www.umiacs.umd.edu/~jimmylin/cloud-computing/SIGIR-2009/Lin-MapReduce- SIGIR2009.pdf Google http://code.google.com/edu/submissions/mapreduce-minilecture/listing.html http://code.google.com/edu/submissions/mapreduce/listing.html Cloudera http://vimeo.com/3584536

5 INTERNET Motivating Example Wikipedia is a very small part of the internet* *may not be to scale Wikipedia

6 Scaling up Let’s do assignment 2 for the whole internet

7 Distributing NB How can you handle new data? What about deleted data? What happens at classification time? Can we distribute classification too? Can we avoid merging the dictionaries then?

8 Distributing NB Questions: How will you know when each machine is done? Communication overhead How will you know if a machine is dead?

9 Failure How big of a deal is it really? A huge deal. In a distributed environment disks fail ALL THE TIME. Large scale systems must assume that any process can fail at any time. Ken Arnold (Sun, CORBA designer): Failure is the defining difference between distributed and local programming, so you have to design distributed systems with the expectation of failure. Imagine asking people, "If the probability of something happening is one in 10 13, how often would it happen?" Common sense would be to answer, "Never." That is an infinitely large number in human terms. But if you ask a physicist, she would say, "All the time. In a cubic foot of air, those things happen all the time." When you design distributed systems, you have to say, "Failure happens all the time." So when you design, you design for failure. It is your number one concern. What does designing for failure mean? One classic problem is partial failure. If I send a message to you and then a network failure occurs, there are two possible outcomes. One is that the message got to you, and then the network broke, and I just didn't get the response. The other is the message never got to you because the network broke before it arrived. So if I never receive a response, how do I know which of those two results happened? I cannot determine that without eventually finding you. The network has to be repaired or you have to come up, because maybe what happened was not a network failure but you died. How does this change how I design things? For one thing, it puts a multiplier on the value of simplicity. The more things I can do with you, the more things I have to think about recovering from. [2]

10 Well, that’s a pain What will you do when a task fails?

11 Well, that’s a pain What’s the difference between slow and dead? Who cares? Start a backup process. If the process is slow because of machine issues, the backup may finish first If it’s slow because you poorly partitioned your data... waiting is your punishment

12 Surprise, you mapreduced! Mapreduce has three main phases Map (send each input record to a key) Sort (put all of one key in the same place) handled behind the scenes Reduce (operate on each key and its set of values)

13 Mapreduce overview Map Shuffle/Sort Reduce

14 Mapreduce: slow motion The canonical mapreduce example is word count Example corpus: Joe likes toast Jane likes toast with jam Joe burnt the toast

15 Map 3 Map 2 Map 1 MR: slow motion: Map Joe likes toast Jane likes toast with jam Joe burnt the toast Joe likes toast Jane likes toast with jam Joe burnt the toast 1 InputOutput

16 Joe Jane likes toast with jam burnt the 1 MR: slow motion: Sort Joe likes toast Jane likes toast with jam Joe burnt the toast 1 Input Output

17 MR: slow mo: Reduce Joe Jane likes toast with jam burnt the 1 Input Joe Jane likes toast with jam burnt the 2 1 2 3 1 Output Reduce 1 Reduce 2 Reduce 3 Reduce 4 Reduce 5 Reduce 6 Reduce 7 Reduce 8

18 Word count = Naive Bayes Building your NB dictionary was word count The interesting bit will be classifying using a map reduce where interesting = homework

19 MR ~ functional programming If you’re familiar with Functional Programming: Map = map Reduce ~ fold (acts on a subset of the map output, not all) If you’re not familiar with FP: Map = divide Reduce = conquer (kind of)

20 MR Code: Word Count Map public static class Map extends Mapper { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); }

21 Map 3 Map 2 Map 1 Reminder Joe likes toast Jane likes toast with jam Joe burnt the toast 1 InputOutput Joe likes toast Jane likes toast with jam Joe burnt the toast

22 MR code: Word count Reduce public static class Reduce extends Reducer { public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); }

23 Reminder Joe Jane likes toast with jam burnt the 1 Input Joe Jane likes toast with jam burnt the 2 1 2 3 1 Output Reduce 1 Reduce 2 Reduce 3 Reduce 4 Reduce 5 Reduce 6 Reduce 7 Reduce 8

24 MR code: Word count Main public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf, "wordcount"); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); }

25 MR Overview

26 Is any part of this wasteful? Remember - moving data around and writing to/reading from disk are very expensive operations No reducer can start until: all mappers are done data in its partition has been sorted

27 Combiners Sits between the map and the shuffle Do some of the reducing while you’re waiting for other stuff to happen Avoid moving all of that data over the network Only applicable when order of reduce values doesn’t matter effect is cumulative

28 MR code: Word count Combiner public static class Combiner extends Reducer { public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); }

29 Deja vu: Combiner = Reducer Often the combiner is the reducer. like for word count but not always

30 Some common pitfalls You have no control over the order in which reduces are performed You have “no” control over the order in which you encounter reduce values More on this later The only ordering you should assume is that Reducers always start after Mappers

31 Some common pitfalls You should assume your Maps and Reduces will be taking place on different machines with different memory spaces Don’t make a static variable and assume that other processes can read it They can’t. It appear that they can when run locally, but they can’t No really, don’t do this.

32 Some common pitfalls Do not communicate between mappers or between reducers overhead is high you don’t know which mappers/reducers are actually running at any given point there’s no easy way to find out what machine they’re running on because you shouldn’t be looking for them anyway

33 When mapreduce doesn’t fit The beauty of mapreduce is its separability and independence If you find yourself trying to communicate between processes you’re doing it wrong or what you’re doing is not a mapreduce

34 When mapreduce doesn’t fit Not everything is a mapreduce Sometimes you need more communication We’ll talk about other programming paradigms later

35 Thinking in Mapreduce A new task: Word co-occurrence statistics (simplified) Input: Sentences Output: P(Word B is in sentence| Word A started the sentence)

36 Thinking in mapreduce We need to calculate P(B in sentence | A started sentence) = P(B in sentence & A started sentence)/P(A started sentence)= count /count

37 Word Co-occurrence: Solution 1 The Pairs paradigm: For each sentence, output a pair E.g Map(“Machine learning for big data”) creates: :1

38 Word Co-occurrence: Solution 1 Reduce would create, for example: :10 :1000 :50 :200... :12000

39 Word Co-occurrence: Solution 1 P(B in sentence | A started sentence) = P(B in sentence & A started sentence)/P(A started sentence)= / Do we have what we need? Yes!

40 Word Co-occurrence: Solution 1 But wait! There’s a problem.... can you see it?

41 Word Co-occurrence: Solution 1 Each reducer will process all counts for a pair We need to know at the same time as The information is in different reducers!

42 Word Co-occurrence: Solution 1 Solution 1 a) Make the first word the reduce key Each reducer has: key: word_i values:.............

43 Word Co-occurrence: Solution 1 Now we have all the information in the same reducer But, now we have a new problem, can you see it? Hint: remember - we have no control over the order of values

44 Word Co-occurrence: Solution 1 There could be too many values to hold in memory We need to be the first value we encounter Solution 1 b): Keep as the reduce key Change the way Hadoop does its partitioning.

45 Word Co-occurrence: Solution 1 /** * Partition based on the first part of the pair. */ public static class FirstCommaPartitioner extends Partitioner { @Override public int getPartition(Text key, Text value, int numPartitions) { String[] s = key.toString().split(","); return (s[0].hashCode() & Integer.MAX_VALUE) % numPartitions; } Removes the sign bit, if set

46 Word Co-occurrence: Solution 1 Ok cool, but we still have the same problem. The information is all in the same reducer, but we don’t know the order But now, we have all the information we need in the reduce key!

47 Word Co-occurrence: Solution 1 We can use a comparator to sort the keys we encounter in the reducer See KeyFieldBasedComparator public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {}

48 Word Co-occurrence: Solution 1 Now the order of key, value pairs will be as we need: :12000 :50 :200 :1000 :10... P(“big” in sentence | “Machine” started sentence) = 50/12000

49 Word Co-occurrence: Solution 2 The Stripes paradigm For each sentence, output a key, record pair E.g Map(“Machine learning for big data”) creates: : E.g Map(“Machine parts are for machines”) creates: :

50 Word Co-occurrence: Solution 2 Reduce combines the records: E.g Reduce for key receives values: And merges them to create

51 Word Co-occurrence: Solution 2 This is nice because we have the * count already created we just have to ensure it always occurs first in the record

52 Word Co-occurrence: Solution 2 There is a really big (ha ha) problem with this solution Can you see it? The value may become too large to fit in memory

53 Performance IMPORTANT You may not have room for all reduce values in memory In fact you should PLAN not to have memory for all values Remember, small machines are much cheaper you have a limited budget

54 Performance Which is faster, stripes vs pairs? Stripes has a bigger value per key Pairs has more partition/sort overhead

55 Performance

56 Conclusions Mapreduce Can handle big data Requires minimal code-writing

57 Tuesday On Tuesday Mapreduce Tips Shared memory programming

58 Done! Questions? I’ll be having special office hours Friday Feb 16, 10-11, outside GHC 8009 Tuesday Feb 22, 12-1 via Skype/Google hangout Monday Feb 27, 10-11 outside GHC 8009


Download ppt "Parallel and Distributed Computing: MapReduce Alona Fyshe."

Similar presentations


Ads by Google