0% found this document useful (0 votes)
22 views7 pages

Matrix Multiplication with MapReduce

The document outlines an experiment on implementing algorithms using Hadoop Map-Reduce/PySpark, focusing on matrix multiplication. It explains the MapReduce technique, detailing the roles of Mapper and Reducer, and provides a step-by-step example of matrix multiplication using these concepts. The final output demonstrates the results of the matrix multiplication process.

Uploaded by

dory34547
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Matrix Multiplication with MapReduce

The document outlines an experiment on implementing algorithms using Hadoop Map-Reduce/PySpark, focusing on matrix multiplication. It explains the MapReduce technique, detailing the roles of Mapper and Reducer, and provides a step-by-step example of matrix multiplication using these concepts. The final output demonstrates the results of the matrix multiplication process.

Uploaded by

dory34547
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment No: 4

Date of Performance:

Date of Submission:
Aim: Experiment on hadoop Map-Reduce/PySpark: -
Implementing simple algorithms in Map Reduce: Matrix multiplication, Aggregate, Joins,
Sorting, Searching etc.

Theory: MapReduce is a technique in which a huge program is subdivided into small tasks and
run parallelly to make computation faster, save time, and mostly used in distributed systems. It
has 2 important parts:

● Mapper: It takes raw data input and organizes into key, value pairs. For example, in a
dictionary, you search for the word “Data” and its associated meaning is “facts and
statistics collected together for reference or analysis”. Here the Key is Data and the
Value associated with is facts and statistics collected together for reference or
analysis.
● Reducer: It is responsible for processing data in parallel and produce final output.

● Let us consider the matrix multiplication example to visualize MapReduce. Consider the
following matrix:

● Here matrix A is a 2×2 matrix which means the number of rows(i)=2 and the number of
columns(j)=2. Matrix B is also a 2×2 matrix where number of rows(j)=2 and number of
columns(k)=2. Each cell of the matrix is labelled as Aij and Bij. Ex. element 3 in matrix A
is called A21 i.e. 2nd-row 1st column. Now one step matrix multiplication has 1 mapper and
1 reducer.
● The formula for Mapper is:

Mapper for Matrix A (k, v) = ((i, k), (A, j, Aij)) for all k Mapper
for Matrix B (k, v) = ((i, k), (B, j, Bjk)) for all i
● Therefore, computing the mapper for Matrix A:

# k, i, j computes the number of times it occurs.

# Here all are 2, therefore when k=1, i can have 2 values 1 & 2 # Each
case can have 2 further values of j=1 and j=2. # Substituting all values
in formula:
1. k=1, i=1, j=1 ((1, 1), (A, 1, 1))

j=2 ((1, 1), (A, 2, 2))

i=2, j=1 ((2, 1), (A, 1, 3))

j=2 ((2, 1), (A, 2, 4))

2. k=2, i=1, j=1 ((1, 2), (A, 1, 1))

j=2 ((1, 2), (A, 2, 2))

i=2, j=1 ((2, 2), (A, 1, 3))

j=2 ((2, 2), (A, 2, 4))

● Computing the mapper for Matrix B:

1. i=1, j=1, k=1 ((1, 1), (B, 1, 5))


k=2 ((1, 2), (B, 1, 6))

j=2, k=1 ((1, 1), (B, 2, 7))

k=2 ((1, 2), (B, 2, 8))

2. i=2, j=1, k=1 ((2, 1), (B, 1, 5))

k=2 ((2, 2), (B, 1, 6))

j=2, k=1 ((2, 1), (B, 2, 7))


k=2 ((2, 2), (B, 2, 8))
The formula for Reducer is:

Reducer (k, v) = (i, k) =>Make sorted Alist and Blist (i, k)


=> Summation (Aij * Bjk)) for j
Output => ((i, k), sum)

Therefore, computing the reducer:

# We can observe from Mapper computation # that


4 pairs are common (1, 1), (1, 2),
# (2, 1) and (2, 2)

# Make a list separate for Matrix A &


# B with adjoining values taken from

# Mapper step above:


(1, 1) =>Alist ={(A, 1, 1), (A, 2, 2)}

Blist ={(B, 1, 5), (B, 2, 7)}

Now Aij x Bjk: [(1*5) + (2*7)] =19 -------(i)


(1, 2) =>Alist ={(A, 1, 1), (A, 2, 2)}

Blist ={(B, 1, 6), (B, 2, 8)}

Now Aij x Bjk: [(1*6) + (2*8)] =22 -------(ii)


(2, 1) =>Alist ={(A, 1, 3), (A, 2, 4)}

Blist ={(B, 1, 5), (B, 2, 7)}

Now Aij x Bjk: [(3*5) + (4*7)] =43 -------(iii) (2, 2)


=>Alist ={(A, 1, 3), (A, 2, 4)}
Blist ={(B, 1, 6), (B, 2, 8)}

Now Aij x Bjk: [(3*6) + (4*8)] =50 -------(iv)


From (i), (ii), (iii) and (iv) we conclude that

((1, 1), 19)

((1, 2), 22)

((2, 1), 43)

((2, 2), 50)

Therefore, the Final Matrix is:

Final output of Matrix multiplication.


Output:
Conclusion: Thus, we have studied Matrix Multiplication using MapReduce.
R1 R2 R3 R4 Total Signature

(4 Marks) (4 Marks) (4 Marks) (3 Mark) (15 Marks)

Common questions

Powered by AI

The example of Matrix Multiplication demonstrates the advantages of using MapReduce by illustrating efficient management of complex computations through parallelization. The process shows how tasks, when divided into independent key-value pairs, can be distributed across multiple nodes, yielding speedier and resource-efficient computations. As seen, each necessary computation step is handled separately, reducing bottlenecks and capitalizing on distributed computing's scalability. This highlights MapReduce's capability to streamline and accelerate data-intensive computations effectively on platforms like Hadoop .

Generating key-value pairs in the MapReduce Mapper phase is crucial because it structures input data for parallel processing, aligning elements to the correct computational tasks. For matrix multiplication, it ensures that each entry of two matrices that need to be multiplied align properly under a common key. This alignment allows the Reducer to easily aggregate contributions from both matrices by summing the products of correctly indexed elements, thereby forming a new resultant matrix .

MapReduce facilitates faster computation in matrix multiplication by splitting the computation into two phases, mapper and reducer, which parallelize the process. The mapper organizes raw matrix data into key-value pairs, allowing simultaneous processing. For matrix multiplication, each element in matrix A and B is processed to pair in numerous such key-value sets. The reducer then aggregates these sets by keys which represent particular positions in the resulting matrix, summing products from corresponding elements. This division of labor into parallel tasks significantly decreases computation time compared to a sequential one-step multiplication process .

Key-value pairs enable parallel processing in distributed systems by standardizing data into discrete units of work that can be processed independently across different nodes. In Hadoop's MapReduce, these pairs allow for the partitioning of data into concurrent tasks handled by various mappers, ultimately aggregating results in reducers. This setup leverages the parallel processing capabilities of distributed systems, optimizing resource use and decreasing execution time by allowing simultaneous data handling and task execution .

The Mapper and Reducer in the MapReduce model have distinct roles. The Mapper organizes input data into key-value pairs, readying it for parallel processing. Specifically, for matrix operations, it tags matrix elements with necessary indices to align them for computation. On the other hand, the Reducer processes these pre-organized key-value pairs. It collects intermediate values, performs operations like summation based on these organized keys, and produces the final aggregated output .

In MapReduce for matrices, the Mapper transforms each matrix element into a key-value pair where the key is a tuple representing the resultant matrix's index. For example, with matrix A and index values (i,j) for Aij, it forms ((i, k), (A, j, Aij)) where k represents column indices for the resulting multiplication. This transformation ensures that elements from both matrices align correctly in parallel processes, enabling efficient computation of matrix products with distributed data for the reducer's step .

The Reducer phase in matrix multiplication using MapReduce takes the organized output from the Mapper phase, which consists of key-value pairs grouped by resultant matrix positions. It builds separate lists A-list and B-list for matrix A and B values corresponding to each key. The Reducer computes the dot product of matching elements from these lists. Specifically, it performs element-wise multiplication of Aij and Bjk followed by summation for values sharing the same key, effectively computing the component of the resultant matrix .

MapReduce balances workload and scalability in matrix-based computations by distributing tasks evenly across multiple computing nodes. Each node handles a specific set of key-value pairs, ensuring that no single node becomes a bottleneck. This distribution increases scalability since the processing capability can be enhanced simply by adding more nodes to the system. Moreover, the workload is effectively managed by parallelizing the task of multiplying matrix elements, consequently resulting in an optimized utilization of resources and quicker computations .

Organizations might opt for implementing matrix multiplication using MapReduce over traditional sequential methods due to its significant ability to handle large-scale data efficiently. The framework's parallel processing capabilities mean computations are completed faster, critical for time-sensitive or high-volume data scenarios. MapReduce excels in utilizing cluster resources effectively, offering a cost-effective solution for comprehensive data processing by reducing computation time and potentially lowering operational costs compared to traditional methods .

For efficient matrix multiplication in MapReduce, the Mapper phase employs key-value pair generation where each matrix element is tagged with indices aligning with how matrix multiplication is computed. This tagging ensures each element contributes to the proper multiplication between matrix A and B. The Reducer phase then takes these organized pairs, forms intermediate lists for matrix parts sharing the same key, and computes dot products followed by summation. This coordinated strategy leverages parallel processing to ensure each function computes relevant product sums accurately and efficiently .

You might also like