PDC-Assignment#03
PDC-Assignment#03
PDC-Assignment#03
ipynb - Colab
keyboard_arrow_down Assignment#03
Name: Muhammad Muneeb Khan
Roll Number: 210276
keyboard_arrow_down Objective
The goal of this assignment is to write and implement MPI programs to explore basic message-passing concepts and implement parallel
algorithms using MPI.
Initial Setup
Assignment Questions
1. Write and Implement a Hello World Program Write and implement an MPI program where each process prints its rank and the total number
of processes. Use MPI_Comm_rank() to retrieve the process rank and MPI_Comm_size() to get the total number of processes. Run the
program with at least 4 processes.
%%writefile mpi_hello.c
#include <stdio.h>
#include <mpi.h>
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 1/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
return 0;
}
Overwriting mpi_hello.c
2. Write and Implement Array Summation Write and implement an MPI program to calculate the sum of an integer array. Divide the array
equally among processes, compute partial sums locally, and send the results to the root process using MPI_Send() and MPI_Recv(). The root
process should compute and display the total sum.
%%writefile mpi_array_sum.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 2/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
// Scatter the array to all processes
MPI_Scatter(array, local_n, MPI_INT, local_array, local_n, MPI_INT, 0, MPI_COMM_WORLD);
// Root process (rank 0) collects the partial sums and computes the total sum
if (rank == 0) {
total_sum = local_sum;
// Receive partial sums from other processes
for (int i = 1; i < size; i++) {
int partial_sum;
MPI_Recv(&partial_sum, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
total_sum += partial_sum;
}
// Display the total sum
printf("Total sum: %d\n", total_sum);
} else {
// Other processes send their partial sums to the root process
MPI_Send(&local_sum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
// Clean up memory
if (rank == 0) {
free(array);
}
free(local_array);
return 0;
}
Writing mpi_array_sum.c
3. Write and Implement Matrix-Vector Multiplication Write and implement an MPI program to perform matrix-vector multiplication. Distribute
the rows of the matrix among processes, compute partial results locally, and send them to the root process using MPI_Send() and
MPI_Recv(). The root process should assemble and display the final resultant vector.
%%writefile mpi_matrix_vector.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Allocate memory for the matrix and vector on root process (rank 0)
if (rank == 0) {
matrix = (int*)malloc(n * n * sizeof(int)); // n x n matrix
vector = (int*)malloc(n * sizeof(int)); // n-dimensional vector
result = (int*)malloc(n * sizeof(int)); // Resultant vector
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 3/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
// Initialize the matrix (for simplicity, let's use sequential values)
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
matrix[i * n + j] = i * n + j + 1; // Example values
}
}
// Root process (rank 0) collects the partial results and assembles the final result
if (rank == 0) {
for (i = 0; i < rows_per_process; i++) {
result[i] = local_result[i];
}
// Receive partial results from other processes
for (i = 1; i < size; i++) {
MPI_Recv(&result[i * rows_per_process], rows_per_process, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// Clean up memory
if (rank == 0) {
free(matrix);
free(vector);
free(result);
}
free(local_matrix);
free(local_result);
return 0;
}
Writing mpi_matrix_vector.c
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 4/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
4. Write and Implement Broadcasting with MPI Write and implement an MPI program where the root process broadcasts an integer array to all
other processes using MPI_Send() and MPI_Recv(). Each process should modify the array by adding its rank to each element. The root
process should display the modified array after gathering the results from all processes.
%%writefile mpi_broadcast.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Broadcast the array from the root process to all other processes
MPI_Bcast(array, n, MPI_INT, 0, MPI_COMM_WORLD);
// Clean up memory
if (rank == 0) {
free(array);
}
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 5/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
return 0;
}
Writing mpi_broadcast.c
Modified Array:
1 2 3 4 5
5. Write and Implement Matrix Addition Write and implement an MPI program to add two matrices. Divide the rows of the matrices among
processes. Each process performs the addition for its assigned rows, and the root process assembles and displays the resulting matrix.
%%writefile mpi_matrix_addition.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 6/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
if (rank == 0) {
printf("Matrix A:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("%d ", A[i * m + j]);
}
printf("\n");
}
printf("\nMatrix B:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("%d ", B[i * m + j]);
}
printf("\n");
}
return 0;
}
Writing mpi_matrix_addition.c
Matrix A:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Matrix B:
2 4 6 8
10 12 14 16
18 20 22 24
26 28 30 32
6. Write and Implement Arrays Addition Write and implement an MPI program to add two arrays element-wise. Distribute the arrays across
processes, and each process computes the sum for its assigned elements. The root process should collect the results and display the final
summed array.
%%writefile mpi_array_addition.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 7/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
int rank, size;
int n = 16; // Length of the arrays
int *A = NULL, *B = NULL, *C = NULL;
int *local_A, *local_B, *local_C;
int i, elements_per_process;
printf("Array B:\n");
for (i = 0; i < n; i++) {
printf("%d ", B[i]);
}
printf("\n");
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 8/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
free(local_C);
return 0;
}
Writing mpi_array_addition.c
Array A:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Array B:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32
Resulting Array (A + B):
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
7. Write and Implement a Parallel Search Write and implement an MPI program to search for a specific value in a large array. Distribute the
array among processes, and each process searches its segment. Use MPI_Send() and MPI_Recv() to send the index of the value (if found) to
the root process, which should display the first occurrence of the value.
%%writefile mpi_parallel_search.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 9/10
12/26/24, 11:07 PM PDC-Assignment#03.ipynb - Colab
}
}
return 0;
}
Writing mpi_parallel_search.c
https://colab.research.google.com/drive/10NW0mhA8l5WCfardpX1Js6vBZxbsaTQ1#scrollTo=oGWdbDouA0Ij&printMode=true 10/10