#include <mpi.h>
#include <iostream>
using namespace std;
#include <vector>
#include <cstdlib>
using namespace std;
// Function to multiply matrices
void matrixMultiply(const vector<vector<int>>& A, const vector<vector<int>>& B, vector<vector<int>>& C, int startRow, int endRow, int N, int P) {
for (int i = startRow; i < endRow; i++) {
for (int j = 0; j < P; j++) {
C[i][j] = 0;
for (int k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
int main(int argc, char* argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int M = 4, N = 4, P = 4; // Dimensions of matrices
vector<vector<int>> A(M, vector<int>(N, 0));
vector<vector<int>> B(N, vector<int>(P, 0));
vector<vector<int>> C(M, vector<int>(P, 0));
// Master initializes matrices
if (rank == 0) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A
[i
][j
] = rand() % 10; // Random values between 0 and 9 }
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < P; j++) {
}
}
cout << "Matrix A:\n";
for (const auto& row : A) {
for (int val : row) cout << val << " ";
cout << "\n";
}
cout << "\nMatrix B:\n";
for (const auto& row : B) {
for (int val : row) cout << val << " ";
cout << "\n";
}
cout << "\n";
}
// Broadcast matrix B to all processes
for (int i = 0; i < N; i++) {
MPI_Bcast(B[i].data(), P, MPI_INT, 0, MPI_COMM_WORLD);
}
int rows_per_proc = M / size;
vector<vector<int>> local_A(rows_per_proc, vector<int>(N));
vector<vector<int>> local_C(rows_per_proc, vector<int>(P, 0));
for (int i = 0; i < rows_per_proc; i++) {
MPI_Scatter(A[i].data(), N, MPI_INT, local_A[i].data(), N, MPI_INT, 0, MPI_COMM_WORLD);
}
int startRow = rank * rows_per_proc;
int endRow = startRow + rows_per_proc;
matrixMultiply(A, B, C, startRow, endRow, N, P);
for (int i = 0; i < rows_per_proc; i++) {
MPI_Gather(local_C[i].data(), P, MPI_INT, C[startRow].data(), P, MPI_INT, 0, MPI_COMM_WORLD);
}
if (rank == 0) {
cout << "Result Matrix C:\n";
for (const auto& row : C) {
for (int val : row) cout << val << " ";
cout << "\n";
}
}
MPI_Finalize();
return 0;
}
I2luY2x1ZGUgPG1waS5oPgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y3N0ZGxpYj4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byBtdWx0aXBseSBtYXRyaWNlcwp2b2lkIG1hdHJpeE11bHRpcGx5KGNvbnN0IHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIEEsIGNvbnN0IHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIEIsIHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIEMsIGludCBzdGFydFJvdywgaW50IGVuZFJvdywgaW50IE4sIGludCBQKSB7CiAgICBmb3IgKGludCBpID0gc3RhcnRSb3c7IGkgPCBlbmRSb3c7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgUDsgaisrKSB7CiAgICAgICAgICAgIENbaV1bal0gPSAwOwogICAgICAgICAgICBmb3IgKGludCBrID0gMDsgayA8IE47IGsrKykgewogICAgICAgICAgICAgICAgQ1tpXVtqXSArPSBBW2ldW2tdICogQltrXVtqXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqIGFyZ3ZbXSkgewogICAgTVBJX0luaXQoJmFyZ2MsICZhcmd2KTsKCiAgICBpbnQgcmFuaywgc2l6ZTsKICAgIE1QSV9Db21tX3JhbmsoTVBJX0NPTU1fV09STEQsICZyYW5rKTsKICAgIE1QSV9Db21tX3NpemUoTVBJX0NPTU1fV09STEQsICZzaXplKTsKCiAgICBpbnQgTSA9IDQsIE4gPSA0LCBQID0gNDsgLy8gRGltZW5zaW9ucyBvZiBtYXRyaWNlcwoKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gQShNLCB2ZWN0b3I8aW50PihOLCAwKSk7CiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IEIoTiwgdmVjdG9yPGludD4oUCwgMCkpOwogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBDKE0sIHZlY3RvcjxpbnQ+KFAsIDApKTsKCiAgICAvLyBNYXN0ZXIgaW5pdGlhbGl6ZXMgbWF0cmljZXMKICAgIGlmIChyYW5rID09IDApIHsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IE07IGkrKykgewogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IE47IGorKykgewogICAgICAgICAgICAgICAgQVtpXVtqXSA9IHJhbmQoKSAlIDEwOyAvLyBSYW5kb20gdmFsdWVzIGJldHdlZW4gMCBhbmQgOQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgUDsgaisrKSB7CiAgICAgICAgICAgICAgICBCW2ldW2pdID0gcmFuZCgpICUgMTA7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIGNvdXQgPDwgIk1hdHJpeCBBOlxuIjsKICAgICAgICBmb3IgKGNvbnN0IGF1dG8mIHJvdyA6IEEpIHsKICAgICAgICAgICAgZm9yIChpbnQgdmFsIDogcm93KSBjb3V0IDw8IHZhbCA8PCAiICI7CiAgICAgICAgICAgIGNvdXQgPDwgIlxuIjsKICAgICAgICB9CgogICAgICAgIGNvdXQgPDwgIlxuTWF0cml4IEI6XG4iOwogICAgICAgIGZvciAoY29uc3QgYXV0byYgcm93IDogQikgewogICAgICAgICAgICBmb3IgKGludCB2YWwgOiByb3cpIGNvdXQgPDwgdmFsIDw8ICIgIjsKICAgICAgICAgICAgY291dCA8PCAiXG4iOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICJcbi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