1. Task
2. Description
5. Testing
6. Measurements
The program represents a calculator for matrix multiplication with used 2 algorithms: Strassen's Algorithm and Coppersmith Winograd Algorithm.
Matrix multiplication is one of the most interesting topics in linear algebra. In this program, we can set the number of rows and columns for each matrix, specify the numbers and get the answer with a description of the operations performed on the result. The matrix implementation was inspired by the presentations of the course B6B01LAG Doc. RNDr. Jiří Velebil, Ph.D. (Algebra of matrices) and also by Abdul Bari's video (Strassen's Matrix Multiplication)
- The program asks the user for the dimensions of matrices A and B (number of rows and columns).
- After entering the dimensions, the program asks for the elements of matrices A and B one by one.
- The matrices A and B are multiplied, and the result is displayed on the screen.
The code does not use multiple threads explicitly. It performs matrix multiplication using a loop structure, but there is no use of multithreading or parallelism. The multiplication is done using single-threaded calculations.
The code includes implementations of several matrix multiplication algorithms, including the Coppersmith–Winograd algorithm and Strassen's Algorithm. The program allows the user to choose between these algorithms and input two matrices for multiplication.
The code does not use any explicit language extensions like OpenMP or variable length arrays (VLA). It uses standard C++ functions and libraries.
The code does not use non-portable libraries such as POSIX or Win32. It relies on standard C++ libraries for input/output and vector operations.
If you are using CLion, you can run the program by opening the project matrix_mult.cpp
and clicking the Run
button.
Or you can run the program in the terminal by following these steps:
- To get the executable file, you need to write this command in the terminal:
g++ -o matrix_mult matrix_mult.cpp
To get the executable file with optimization, you need to write this command in the terminal:
g++ -O3 -o matrix_mult matrix_mult.cpp
- To run the program, you need to write this command in the terminal:
./matrix_mult
If you want to get help, you need to write this command:
./matrix_mult --help
And you will see this description:
Matrix Multiplication Program
This program performs matrix multiplication of two matrices using different algorithms.
Usage: ./matrix_mult [options]
Options:
--help Display this help message
--copsmth Use Coppersmith Winograd Algorithm for multiplication (default)
--strassen Use Strassen's Algorithm for multiplication
Description:
After running the matrix_mult program, you will see the following interactive process in the terminal:
- The program will prompt you to enter the dimensions of square matrices A and B.
- After entering the dimensions of matrices A and B, the program will prompt you to enter the elements of both matrices in turn.
- The program will display the result of the multiplication.
- The program will then exit.
If you try calling another option that does not exist, you will get an invalid message:
Unknown command: [your written switch]
After running the program, you will see the following interactive process in the terminal:
- The program will prompt you to enter the dimensions of square matrices A and B.
- After entering the dimensions of matrices A and B, the program will prompt you to enter the elements of both matrices in turn.
- The program will display the result of the multiplication.
- The program will then exit.
Enter number of rows for matrix A: 2
Enter number of columns for matrix A: 2
Enter number of rows for matrix B: 2
Enter number of columns for matrix B: 2
Enter matrix A (2x2):
1 2
3 4
Enter matrix B (2x2):
5 6
7 8
Result:
19 22
43 50
The program also contains error handling. If you enter the wrong data, you will see the following error messages:
Enter number of rows for matrix A: cpp
Invalid input for number of rows for matrix A
Enter number of rows for matrix A: 2
Enter number of columns for matrix A: pcc
Invalid input for number of columns for matrix A
Enter number of rows for matrix A: -1
Invalid number of rows for matrix A
Enter number of rows for matrix A: 1
Enter number of columns for matrix A: 2
Enter number of rows for matrix B: 1
Enter number of columns for matrix B: 1
Invalid matrix dimensions: Number of columns in matrix A must match number of rows in matrix B
Enter number of rows for matrix A: 2
Enter number of columns for matrix A: 2
Enter number of rows for matrix B: 2
Enter number of columns for matrix B: 2
Enter matrix A (2x2):
1 2 3
Invalid format: Entered row has more than the required number of elements
Enter number of rows for matrix A: 2
Enter number of columns for matrix A: 2
Enter number of rows for matrix B: 2
Enter number of columns for matrix B: 2
Enter matrix A (2x2):
1
Invalid input for matrix element
The project contains testing folder where you can find different matrices and their multiplication results.
Measurements were performed on an 8-core Apple M1 3.2GHz CPU.
To measure the code rate time, the following was used:
chrono
library:
template <typename TimePoint>
std::chrono::milliseconds to_ms(TimePoint tp) {
return std::chrono::duration_cast<std::chrono::milliseconds>(tp);
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
// do work
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Needed " << to_ms(end - start).count() << " ms to finish.\n";
}
- Testing folder with different matrices.
To demonstrate the result, I attach a table with files, matrix sizes, and algorithms (data are specified in milliseconds).
File | Type | Coppersmith Winograd Algorithm | Strassen's Algorithm |
---|---|---|---|
testing_1.txt | 2x2 | 0.0 ms | 0.0 ms |
testing_2.txt | 3x3 | 0.0 ms | 0.0 ms |
testing_3.txt | 4x4 | 0.0 ms | 0.0 ms |
testing_4.txt | 5x5 | 0.0 ms | ~1 ms |
testing_5.txt | different | 0.0 ms | designed for multiplying square matrices |
testing_6.txt | 10x10 | ~4 ms | ~10 ms |
testing_7.txt | 12x12 | ~6 ms | ~12 ms |
testing_8.txt | 15x15 | ~11 ms | ~10 ms |
testing_9.txt | 20x20 | ~20 ms | ~72 ms |