Difference between revisions of "MatrixMultiply"
| Line 62: | Line 62: | ||
--> | --> | ||
| − | == | + | ==Loop2dAutoCoarsenMatrixMultiplier== |
{{CodeToImplement|Loop2dAutoCoarsenMatrixMultiplier|multiply|matrixmultiply.exercise}} | {{CodeToImplement|Loop2dAutoCoarsenMatrixMultiplier|multiply|matrixmultiply.exercise}} | ||
Revision as of 22:06, 1 February 2022
Contents
Motivation
We gain experience using the parallel for loop constructs.
Background
Matrix multiplication is a simple mathematical operation which we will replicate in this studio. For our purposes, we will only deal with square matrices (same number of rows and columns). However, we will approach this problem with several different parallel constructs and approaches.
For those unfamiliar on how to multiply two matrices, take a look at these overviews:
If Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathbf{A}} is an Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle n \times m} matrix and Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathbf{B}} is an Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle m \times p} matrix
for each i=[0..n) and for each j=[0..p)
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle (\mathbf{A}\mathbf{B})_{ij} = \sum_{k=1}^m A_{ik}B_{kj}}
- Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathbf{C}=\begin{pmatrix} a_{11}b_{11} +\cdots + a_{1n}b_{n1} & a_{11}b_{12} +\cdots + a_{1n}b_{n2} & \cdots & a_{11}b_{1p} +\cdots + a_{1n}b_{np} \\ a_{21}b_{11} +\cdots + a_{2n}b_{n1} & a_{21}b_{12} +\cdots + a_{2n}b_{n2} & \cdots & a_{21}b_{1p} +\cdots + a_{2n}b_{np} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}b_{11} +\cdots + a_{mn}b_{n1} & a_{m1}b_{12} +\cdots + a_{mn}b_{n2} & \cdots & a_{m1}b_{1p} +\cdots + a_{mn}b_{np} \\ \end{pmatrix} }
source: Matrix Multiplication on Wikipedia
Code To Investigate
Demo Video
SequentialMatrixMultiplier
| class: | SequentialMatrixMultiplier.java | DEMO: |
| methods: | multiply | |
| package: | matrixmultiply.demo | |
| source folder: | src//java |
SequentialMatrixMultiplierClient
| class: | SequentialMatrixMultiplierClient.java | DEMO: |
| methods: | main | |
| package: | matrixmultiply.client | |
| source folder: | src//java |
The Core Questions
- What are the tasks?
- What is the data?
- Is the data mutable?
- If so, how is it shared?
Code To Implement
There are three methods you will need to implement, all of which are different ways to use parallel for loops to solve the problem. To assist you, the sequential implementation has already been completed for you. We recommend starting from the top and working your way down. There is also an optional recursive implementation and a manual grouping implementation which has been done for you (this is just to demonstrate how chunking works behind the scenes).
LoopLoopMatrixMultiplier
| class: | LoopLoopMatrixMultiplier.java | |
| methods: | multiply | |
| package: | matrixmultiply.exercise | |
| source folder: | student/src/main/java |
method: public double[][] multiply(double[][] a, double[][] b)
(parallel implementation required)
In this implementation, you will simply convert the sequential solution into a parallel one using parallel fork loops.
Loop2dMatrixMultiplier
| class: | Loop2dMatrixMultiplier.java | |
| methods: | multiply | |
| package: | matrixmultiply.exercise | |
| source folder: | student/src/main/java |
method: public double[][] multiply(double[][] a, double[][] b)
(parallel implementation required)
Loop2dAutoCoarsenMatrixMultiplier
| class: | Loop2dAutoCoarsenMatrixMultiplier.java | |
| methods: | multiply | |
| package: | matrixmultiply.exercise | |
| source folder: | student/src/main/java |
method: public double[][] multiply(double[][] a, double[][] b)
(parallel implementation required)
Testing Your Solution
Correctness
| class: | __MatrixMultiplyTestSuite.java | |
| package: | matrixmultiply.studio | |
| source folder: | testing/src/test/java |
Performance
| class: | MatrixMultiplicationTiming.java | |
| package: | matrixmultiply.studio | |
| source folder: | src/main/java |