§ 2.6Module 2

Matrix-Vector Multiplication by MapReduce

On this page

2.6 Matrix–Vector Multiplication by MapReduce

Recall first

For y = A v, what is y_i? If a matrix entry is A[i,j], which vector value must travel with it?

First principles

For an m × n matrix A and an n-element vector v:

yi=∑j=1nAijvj,i=1,…,m.y_i = \sum_{j=1}^{n} A_{ij}v_j, \quad i=1,\ldots,m.

The result can be distributed by row. Store each nonzero matrix entry as (i,j,aij). A mapper looks up v_j, computes the partial product aij × v_j, and emits key i. The reducer receives all partial products for row i and sums them.

This is a MapReduce expression of the matrix-vector method discussed in Mining of Massive Datasets, Chapter 2. It is especially useful for a sparse matrix: do not materialize zero entries.

Algorithm

Input: entries (i,j,aij) and vector v.

map((i,j,aij)):
    emit(i, aij * v[j])

reduce(i, partials):
    emit(i, sum(partials))

The vector must be available to every mapper, for example as a small read-only distributed file/cache, or by a preceding distribution step. If the vector is too large for convenient broadcast, use a block/partitioned algorithm that joins matrix blocks with the needed vector pieces; the simple algorithm’s communication assumption then changes.

Worked trace

Let

A=[120 034],v=[5 6 7].A = \begin{bmatrix} 1&2&0\ 0&3&4 \end{bmatrix}, \quad v = \begin{bmatrix} 5\ 6\ 7 \end{bmatrix}.

Nonzero entries produce:

map (1,1,1) -> (1, 1*5) = (1,5)
map (1,2,2) -> (1, 2*6) = (1,12)
map (2,2,3) -> (2, 3*6) = (2,18)
map (2,3,4) -> (2, 4*7) = (2,28)

Grouping gives 1 → [5,12] and 2 → [18,28]. Reducers emit y_1=17 and y_2=46, so y=[17,46]^T.

Assumptions and trade-offs

Exercise — revealed answer

Exercise: For entries (2,1,3) and (2,3,5) with v_1=4 and v_3=10, what does reducer 2 emit?

Answer: The mapper emits (2,12) and (2,50). Reducer 2 sums them and emits (2,62).

Exam lens

Write the formula first, then the two lines mapper emits (row, partial); reducer sums by row. State how the vector reaches mappers and why sparse input avoids zero work. A small numerical trace prevents a vague answer.

Rapid revision checklist

Key takeaways

  1. MapReduce turns each matrix entry into a row-keyed partial product.
  2. The reducer reconstructs one output coordinate by summing that row’s contributions.
  3. The simple algorithm is efficient when the vector is distributable and the matrix is sparse enough.

Sources