w04-g04-matrix-matrix-multiplication
Matrix-Matrix multiplication
Objective
You will implement a matrix-matrix multiplication and optimize it using pointer arithmetrics. You will implement three different approaches and compare their speed.
Implementation
-
Use the file named
matrix_matrix_multiplication.cpp
as a starting point, which is provided in thetehpc/basics/
directory. -
Follow instructions in the provided file. And implement the different approach to multiply matrices (see notes below)
-
Make sure that your code is well structured and commented.
-
For all three approaches, measure the execution time for various sizes of matrix, and comment your observations (in your MR).
Notes
Some notes regarding the three approaches:
- First approach: declare and allocate 2D arrays for matrices
$A$
,$B$
, and$C$
. ComputeC = AxB
using the[]
operators (as you would expect). - Second approach: continue using the same arrays, but now compute the multiplication by dereferencing the pointers. e.g. the
C[i][j]
can be accessed by*(*(C+i)+j)
. Explain what this does. - Third approach: declare and allocate 1D arrays for matrices
$A1$
,$B1$
, and$C1$
, but with size$N\timesM$
. Compute matrix multiplication by using pointer arithmetics, i.e. use pointers p_A1 to point to the array and modify them through thefor
loops. This means that you will need to use operations, such as++p_A
.
Compile and run your code by:
- going into the
tehpc/build/
folder - compile with the following command:
g++ -Werror -Wpedantic -g -o matrix_matrix_multiplication ../basics/matrix_matrix_multiplication.cpp
- run code with
./matrix_matrix_multiplication 4
(or any other number)