0% found this document useful (0 votes)
3 views3 pages

Hpcexp3 1832

The document is a lab assignment for a matrix multiplication program using MPI in C. It includes code that initializes matrices, distributes the computation among processes, and gathers the results. The program demonstrates parallel processing by splitting the workload and printing the computed portions of the resulting matrix.

Uploaded by

aryangadpayle45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Hpcexp3 1832

The document is a lab assignment for a matrix multiplication program using MPI in C. It includes code that initializes matrices, distributes the computation among processes, and gathers the results. The program demonstrates parallel processing by splitting the workload and printing the computed portions of the resulting matrix.

Uploaded by

aryangadpayle45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

hpc

Lab assignment 4
Name: ARYAN GADPAYLE
branch: csai=1
roll no.: 2022UCA1832

Q. Write a C program for parallel implementation of Matrix


Multiplication
using MPI.

Code:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

#define N 4

void print_matrix(int matrix[N][N])


{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

void print_local_matrix(int rank, int matrix[][N], int rows_per_process)


{
printf("\nProcess %d - Computed Portion of Matrix C:\n", rank);
for (int i = 0; i < rows_per_process; i++)
{
for (int j = 0; j < N; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main(int argc, char **argv)


{
int rank, size;
int A[N][N], B[N][N], C[N][N];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

int rows_per_process = N / size;


int local_A[rows_per_process][N];
int local_C[rows_per_process][N];

if (rank == 0)
{
printf("Matrix A:\n");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
A[i][j] = i + j;
}
}
print_matrix(A);

printf("\nMatrix B:\n");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
B[i][j] = i - j;
}
}
print_matrix(B);
}

MPI_Bcast(B, N * N, MPI_INT, 0, MPI_COMM_WORLD);


MPI_Scatter(A, rows_per_process * N, MPI_INT, local_A, rows_per_process * N, MPI_INT, 0,
MPI_COMM_WORLD);
for (int i = 0; i < rows_per_process; i++)
{
for (int j = 0; j < N; j++)
{
local_C[i][j] = 0;
for (int k = 0; k < N; k++)
{
local_C[i][j] += local_A[i][k] * B[k][j];
}
}
}

print_local_matrix(rank, local_C, rows_per_process);


MPI_Barrier(MPI_COMM_WORLD);
MPI_Gather(local_C, rows_per_process * N, MPI_INT, C, rows_per_process * N, MPI_INT, 0,
MPI_COMM_WORLD);
if (rank == 0)
{
printf("\nFinal Result Matrix C (A x B):\n");
print_matrix(C);
}

MPI_Finalize();
return 0;
}
Output:

You might also like