Matrix Multiplication Algorithm
1. Introduction
Matrix multiplication is a powerful mathematical tool that is widely used across different
fields and domains. It enables complex calculations and modeling of systems in a compact
and efficient way.
2. Domains of Application
Computer Graphics
Used to perform transformations such as translation, rotation, and scaling of 2D and 3D
models in games, simulations, and animations.
Machine Learning & Artificial Intelligence
Neural networks use matrix multiplication to compute weights and activations during
training and testing.
Data Science & Statistics
Used for data representation, covariance matrices, and dimensionality reduction techniques
like PCA.
Physics & Engineering
Used in mechanics, quantum physics, circuit analysis, and control systems to model and
solve equations.
Computer Vision
Used for image transformations, filtering, and object recognition by treating images as
matrices.
Robotics
Used for motion planning, kinematics, and sensor data processing.
Cryptography
Matrix multiplication is used in classical encryption algorithms such as the Hill Cipher.
Economics and Business
Used to model input-output systems, financial networks, and predictive analysis.
Signal Processing
Used in operations like Fourier transforms, image/audio filtering, and compression
Matrix Multiplication for Sequential programming:
Code :
#include <iostream>
using namespace std;
int main() {
int rows1, cols1, rows2, cols2;
// Input matrix dimensions
cout << "Enter rows and columns of Matrix A: ";
cin >> rows1 >> cols1;
cout << "Enter rows and columns of Matrix B: ";
cin >> rows2 >> cols2;
// Check if multiplication is possible
if (cols1 != rows2) {
cout << "Matrix multiplication not possible! (cols of A != rows of B)";
return 0;
// Declare matrices
int A[100][100], B[100][100], C[100][100] = {0};
// Input Matrix A
cout << "Enter elements of Matrix A:\n";
for (int i = 0; i < rows1; ++i)
for (int j = 0; j < cols1; ++j)
cin >> A[i][j];
// Input Matrix B
cout << "Enter elements of Matrix B:\n";
for (int i = 0; i < rows2; ++i)
for (int j = 0; j < cols2; ++j)
cin >> B[i][j];
// Matrix Multiplication
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
C[i][j] += A[i][k] * B[k][j];
// Output Resultant Matrix
cout << "Resultant Matrix C (A x B):\n";
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
cout << C[i][j] << " ";
cout << endl;
return 0;
}
Output:
Matrix Multiplication fot Distributed programming in C++ Using
MPI:
Code:
Output:
output of single process
Here is the output for 2 processes:
Output for 3 processes:
Output for 4 processes:
PARALLEL MATRIX MULTIPLICATION WITH C++ Using OPENMP:
CODE:
// matrix_multiplication_detailed.cpp
#include <iostream>
#include <omp.h>
#include <chrono>
#define N 4 // Small for readable output. You can increase this (e.g., 500) later.
int main() {
int A[N][N], B[N][N], C[N][N] = {0};
int C_seq[N][N] = {0}; // For sequential version
int num_threads;
// Initialize A and B
std::cout << "Matrix A:\n";
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
A[i][j] = i + j;
std::cout << A[i][j] << " ";
std::cout << "\n";
std::cout << "\nMatrix B:\n";
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
B[i][j] = i - j;
std::cout << B[i][j] << " ";
std::cout << "\n";
}
// Start parallel timing
double start_time = omp_get_wtime();
// Matrix multiplication in parallel
#pragma omp parallel shared(A, B, C) private(num_threads)
num_threads = omp_get_num_threads();
#pragma omp for collapse(2)
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) {
C[i][j] += A[i][k] * B[k][j];
int tid = omp_get_thread_num();
std::cout << "Thread " << tid << " calculated C[" << i << "][" << j << "] = " << C[i][j]
<< "\n";
double end_time = omp_get_wtime();
double parallel_duration = end_time - start_time;
// Sequential version (for comparison)
auto start_seq = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
for (int k = 0; k < N; ++k)
C_seq[i][j] += A[i][k] * B[k][j];
auto end_seq = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seq_duration = end_seq - start_seq;
// Output Matrix C
std::cout << "\nMatrix C (Result):\n";
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j)
std::cout << C[i][j] << " ";
std::cout << "\n";
std::cout << "\nParallel time: " << parallel_duration << " seconds\n";
std::cout << "Sequential time: " << seq_duration.count() << " seconds\n";
double speedup = seq_duration.count() / parallel_duration;
std::cout << "Speedup: " << speedup << "x\n";
std::cout << "Number of threads used: " << num_threads << "\n";
return 0;
Output: