0% found this document useful (0 votes)
26 views

Assignment PDF

Given two matrices, MatrixA of size N*M and MatrixB of size M*L, matrix multiplication follows the rule that the result is a matrix of size N*L. The elements of the result matrix are calculated by summing the products of corresponding elements of MatrixA and MatrixB over the common dimension M. This matrix multiplication can be parallelized by dividing the rows of the result matrix among processes, with each process calculating its assigned rows and sending the results back to process 0 for collection into the final result matrix.

Uploaded by

Alaa
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)
26 views

Assignment PDF

Given two matrices, MatrixA of size N*M and MatrixB of size M*L, matrix multiplication follows the rule that the result is a matrix of size N*L. The elements of the result matrix are calculated by summing the products of corresponding elements of MatrixA and MatrixB over the common dimension M. This matrix multiplication can be parallelized by dividing the rows of the result matrix among processes, with each process calculating its assigned rows and sending the results back to process 0 for collection into the final result matrix.

Uploaded by

Alaa
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
You are on page 1/ 2

Matrix Multiplication Assignment

Given
MatrixA of size N * M
MatrixB of size M * L
The result of multiplying them is a matrix of size N *L

Rule of Multiplication:

Example:
A
1
2
3

1
2
3

B
1
2
3

1
2
3

3X4

5
5
5
5

Result
1
1
1
1

4X2

20 4
40 8
60 12

3X2

Parallelization
There are several ways to implement matrix multiplication as parallel program. One way is:
Divide rows of result matrix on the other processes.
o Number of rows to be calculated by each process = N/ (P-1).
For simplicity, if N%(p-1)>0 then the last process will take remaining rows in addition to
its part of rows.
o

To calculate rows i to j in matrixResult, a slave process will need i to j rows of matrixA and all
columns of matrix (i.e. all matrixB).

Steps:
-

Make P0 responsible for distributing data and collecting results.


P0 will set matrices dimensions and read values.
P0 will calculate number of rows to be calculated by each process.
Page 1 of 2

P0 will send dimensions and needed data to each process.


If matrices were statically allocated and size is fixed in code then the only dimension P0 will need to
send is number-of-rows-per-process. Also each matrix will be sent/received using 1 MPI_Send /
MPI_Receive call
Each slave process will:
o Receive data.
o Calculate sub-result.
o Send it back to P0.
P0 will collect all results in matrixResult and prints it.

Note: if dimensions were variable (user input) and used dynamic allocation then the matrices will be
sent/received row by row as the memory wont be contiguous. So it is better in lab to apply static
allocation and who finishes early starts in make code dynamic.

Page 2 of 2

You might also like