#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <omp.h>
#define N 5 // Set a small value (e.g., N=5) for readable output
using namespace std;
// Function to generate a random matrix
void generateMatrix(vector<vector<int>>& matrix) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = rand() % 10; // Random values from 0 to 9
}
}
}
// Function to print a matrix
void printMatrix(const vector<vector<int>>& matrix, const string& name) {
cout << name << " Matrix:\n";
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
// Function to multiply matrices using OpenMP
void multiplyMatrices(const vector<vector<int>>& A, const vector<vector<int>>& B, vector<vector<int>>& C) {
#pragma omp parallel for collapse(2)
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
C[i][j] = 0;
for (int k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
int main() {
srand(time(0));
vector<vector<int>> A(N, vector<int>(N));
vector<vector<int>> B(N, vector<int>(N));
vector<vector<int>> C(N, vector<int>(N, 0));
generateMatrix(A);
generateMatrix(B);
if (N <= 10) { // Print only if N is small to avoid large output
printMatrix(A, "A");
printMatrix(B, "B");
}
clock_t start = clock(); // Use clock() instead of omp_get_wtime()
multiplyMatrices(A, B, C);
clock_t end = clock(); // End time
if (N <= 10) { // Print result matrix only if N is small
printMatrix(C, "C");
}
double time_taken = double(end - start) / CLOCKS_PER_SEC;
cout << "Matrix multiplication completed in " << time_taken << " seconds." << endl;
return 0;
}