map reduce - school of computingmapreduce βword counting input set of documents map: reads a...
TRANSCRIPT
Map Reduce
Last Time β¦
Parallel Algorithms
Work/Depth Model
Spark
Map Reduce
Assignment 1
Questions ?
Today β¦
Map Reduce
Matrix multiplication
Similarity Join
Complexity theory
MapReduce β word counting
Input set of documents
Map:
reads a document and breaks it into a sequence of wordsπ€1, π€2, β¦ ,π€π
Generates (π, π£) pairs,π€1, 1 , π€2, 1 , β¦ , (π€π, 1)
System:
group all π, π£ by key
Given π reduce tasks, assign keys to reduce tasks using a hash function
Reduce:
Combine the values associated with a given key
Add up all the values associated with the word total count for that word
Matrix-vector multiplication
π Γ π matrix π with entries πππ
Vector π of length π with values π£π
We wish to compute
π₯π =
π=1
π
ππππ£π
If π can fit in memory
Map: generate (π,ππππ£π)
Reduce: sum all values of π to produce (π, π₯π)
If π is too large to fit in memory? Stripes? Blocks?
What if we need to do this iteratively?
Matrix-Matrix Multiplication
π = ππ πππ = πππππππ
2 mapreduce operations
Map 1: produce π, π£ , π, π, π, πππ and π, π, π, πππ
Reduce 1: for each π π, π, πππ Γ πππ
Map 2: identity
Reduce 2: sum all values associated with key π, π
Matrix-Matrix multiplication
In one mapreduce step
Map:
generate π, π£ π, π , π, π,πππ & π, π , π, π, πππ
Reduce:
each key (π, π) will have values π, π , π, π,πππ & π, π , π, π, πππ βπ
Sort all values by π
Extract πππ & πππ and multiply, accumulate the sum
Complexity Theory for mapreduce
Communication cost
Communication cost of a task is the size of the input to the task
We do not consider the amount of time it takes each task to
execute when estimating the running time of an algorithm
The algorithm output is rarely large compared with the input or the
intermediate data produced by the algorithm
Reducer size & Replication rate
Reducer size π
Upper bound on the number of values that are allowed to appear in the list associated with a single key
By making the reducer size small, we can force there to be many reducers
High parallelism low wall-clock time
By choosing a small π we can perform the computation associated with a single reducer entirely in the main memory of the compute node
Low synchronization (Comm/IO) low wall clock time
Replication rate (π)
number of (π, π£) pairs produced by all the Map tasks on all the inputs, divided by the number of inputs
π is the average communication from Map tasks to Reduce tasks
Example: one-pass matrix mult
Assume matrices are π Γ π
π β replication rate
Each element πππ produces π keys
Similarly each πππ produces π keys
Each input produces exactly π keys load balance
π β reducer size
Each key has π values from π and π values from π
2π
Example: two-pass matrix mult
Assume matrices are π Γ π
π β replication rate
Each element πππ produces 1 key
Similarly each πππ produces 1 key
Each input produces exactly 1 key (2nd pass)
π β reducer size
Each key has π values from π and π values from π
2π (1st pass), π (2nd pass)
Real world example: Similarity Joins
Given a large set of elements π and a similarity measure π (π₯, π¦)
Output: pairs whose similarity exceeds a given threshold π‘
Example: given a database of 106 images of size 1MB each, find pairs of images that are similar
Input: (π, ππ), where π is an ID for the picture and ππ is the image
Output: ππ, ππ or simply π, π for those pairs where π ππ , ππ > π‘
Approach 1
Map: generate (π, π£)
π, π , ππ , ππ
Reduce:
Apply similarity function to each value (image pair)
Output pair if similarity above threshold π‘
Reducer size β π 2 (2MB)
Replication rate β π 106 β 1
Total communication from mapreduce tasks?
106 Γ 106 Γ 106 bytes 1018 bytes 1 Exabyte (kB MB GB TB PB EB)
Communicate over GigE 1010 sec 300 years
Approach 2: group images
Group images into π groups with 106
πimages each
Map: Take input element π, ππ and generate
π β 1 keys π’, π£ | ππ β π’ π’ , π£ β {1, β¦ , π} β {π’}
Associated value is (π, ππ)
Reduce: consider key (π’, π£)
Associated list will have 2 Γ106
πelements π, ππ
Take each π, ππ and π, ππ where π, π belong to different groups and
compute π ππ , ππ
Compare pictures belonging to the same group
heuristic for who does this, say reducer for key π’, π’ + 1
Approach 2: group images
Replication rate: π = π β 1
Reducer size: π = 2 Γ 106/π
Input size: 2 Γ 1012/π bytes
Say π = 1000,
Input is 2GB
Total communication: 106 Γ 999 Γ 106 = 1015 bytes 1 petabyte
Graph model for mapreduce
problems Set of inputs
Set of outputs
many-many relationship between the inputs and outputs, which describes which inputs are necessary to produce which outputs.
Mapping schema
Given a reducer size π
No reducer is assigned more than πinputs
For every output, there is at least one reducer that is assigned all input related to that output
π1
π4
π2
π3
π1, π2
π1, π3
π1, π4
π2, π3
π2, π4
π3, π4
Grouping for Similarity Joins
Generalize the problem to π images
π equal sized groups of π
πimages
Number of outputs is π2βπ2
2
Each reducer receives 2π
πinputs (π)
Replication rate π = π β 1
π =2π
π
The smaller the reducer size, the larger the replication rate, and therefore higher the communication
communication β reducer size
communication β parallelism
Lower bounds on Replication rate
1. Prove an upper bound on how many outputs a reducer with πinputs can cover. Call this bound π(π)
2. Determine the total number of outputs produced by the problem
3. Suppose that there are π reducers, and the ππ‘β reducer has ππ < π
inputs. Observe that π=1π π(ππ)must be no less than the number of
outputs computed in step 2
4. Manipulate inequality in 3 to get a lower bound on π=1π ππ
5. 4 is the total communication from Map tasks to reduce tasks. Divide
by number of inputs to get the replication rate
Lower bounds on Replication rate
1. Prove an upper bound on how many outputs a reducer with πinputs can cover. Call this bound π(π)
2. Determine the total number of outputs produced by the problem
3. Suppose that there are π reducers, and the ππ‘β reducer has ππ < π
inputs. Observe that π=1π π(ππ)must be no less than the number of
outputs computed in step 2
4. Manipulate inequality in 3 to get a lower bound on π=1π ππ
5. 4 is the total communication from Map tasks to reduce tasks. Divide
by number of inputs to get the replication rate
π2βπ2
2
π2βπ2
2
π=1
πππ2
2β₯π2
2
π
π=1
π
ππ β₯ π2
π=1
π
ππ β₯π2
ππ β₯π
π
Matrix Multiplication
Consider the one-pass algorithm extreme case
Lets group rows/columns into bands π groups π/π columns/rows
=
π π π
π Γ π
Matrix Multiplication
Map:
for each element of π,π generate π (π, π£) pairs
Key is group paired with all groups
Value is π, π, πππ or π, π, πππ
Reduce:
Reducer corresponds to key (π, π)
All the elements in the ππ‘β band of π and ππ‘β band of π
Each reducer gets ππ
πelements from 2 matrices
π =2π2
π, π = π π =
2π2
π
Lower bounds on Replication rate
1. Prove an upper bound on how many outputs a reducer with πinputs can cover. Call this bound π(π)
2. Determine the total number of outputs produced by the problem
3. Suppose that there are π reducers, and the ππ‘β reducer has ππ < πinputs. Observe that π=1
π π(ππ)must be no less than the number of outputs computed in step 2
4. Manipulate inequality in 3 to get a lower bound on π=1
π ππ
5. 4 is the total communication from Map tasks to reduce tasks. Divide by number of inputs to get the replication rate
Each reducer receives π rows from πand π π = 2ππ and produces π2
outputs π π =π2
4π2
π2
π=1π ππ
2
4π2β₯ π2
π=1
π
ππ2 β₯ 4π4
π=1π ππ β₯
4π4
π
π =1
2π2
π=1
π
ππ =2π2
π
Matrix Multiplication LET US REVISIT THE TWO-PASS APPROACH
Matrix-Matrix Multiplication
π = ππ πππ = πππππππ
2 mapreduce operations
Map 1: produce π, π£ , π, π, π, πππ and π, π, π, πππ
Reduce 1: for each π π, π, πππ Γ πππ
Map 2: identity
Reduce 2: sum all values associated with key π, π
Grouped two-pass approach
=
π2 groups of π2
π2elements each
First pass: compute products of square
(πΌ, π½) of π with square (π½, πΎ) of π
Second pass: βπΌ, πΎ sum over all π½
Grouped two-pass approach
Replication rate for map1 is π 2ππ2 total communication
Each reducer gets 2π2
π2 π =
2π2
π2 π = π
2
π
Total communication 22π3
π
Assume map2 runs on same nodes as reduce1 no communication
Communication ππ2 2π3
π
Total communication 32π3
π
Comparison
π4
π
π3
ππ < π2<
If π is closer to the minimum of 2π, two pass is better by a factor of πͺ π