There are multiple ways to skin matrix multiplication. If you begin to think about it, there are probably 4 or 5 ways in which you could approach matrix multiplication. In this post, we look at another, easier, way of multiplying two matrices, and attempt to build a MapReduce version of the algorithm. Before we dive into the code itself, we’ll quickly review the actual algebraic process we’re trying to parallelise.
We are going to view the quotient matrix as a series of column vectors, each of which is the linear combination of the weighted columns of the LHS (Left Hand Side) matrix. The best way to explain this is through a diagram explaining a sample calculation.
The diagram shows the multiplication of two matrices A and B, yielding C as the result. The algebraic process for the calculation of the first column of C is shown. As you can see, the this is derived from weighting each column of A by some scalar, and summing then up. The scalars, or ‘weights’, are simply the individual elements of the first column of B. Hence the phrase “linear combination of weighted columns.”
The algebraic process, in fact, leads very intuitively to the MapReduce version of the algorithm. Given two matrices to be multiplied, these would be the steps.
- Mapper: This will emit the pairings consisting of (scalar, vector) for each of the columns of the LHS matrix.
- Mapper: This will be run for each (scalar, vector) pairing, and will simply multiply them to produce a new vector.
- Reducer: This will add up all the subvectors for a single column of the RHS matrix to produce the corresponding column of the quotient matrix.
- Reducer: This will join all the separate columns of the quotient matrix to form the complete result.
The code for this runs using Snail-MapReduce.
Here is the code for the MapReduce version.
Could anything else be parallelised in this version? Well, the first Reduce step could be broken up into multiple summations, if the number of elements to be added up was very large; so that’s one thought.
Next time, I’ll try digging into a few eigenvector computation algorithms to see what it takes to get them to run as MapReduce jobs.