PDC Lab 8
PDC Lab 8
COMPUTING
Lab 8
1. Write an MPI program with C to show the text “Your Name” and “Roll Number” with its
process ID (rank). Assume, number of processes (np) = 4.
Solution=>
Code:
#include <mpi.h>
#include <stdio.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 4) {
if (rank == 0) {
MPI_Finalize();
return 0;
}
MPI_Finalize();
return 0;
Output:
2. Write an MPI program with C without MPI built-in functions to compute the following:
Process ID = 0, do c=a&b Process ID = 1, do c=a|b Process ID > 1, do c=a^b Show the output
with its respective process ID. Assume np=5.
Solution=>
Code:
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
int c;
int np = 5;
if (rank == 0) {
c = a & b;
printf("Process %d: c = a & b = %d\n", rank, c);
else if (rank == 1) {
c = a | b;
else {
c = a ^ b;
return 0;
Output:
3. Consider the scenario. ISRO sent Chandrayan-3 satellite successfully soft-landed on the
moon on 23-August-2023. Write an MPI program for the given scenario. It is expected to
create two-process, process 0 (ISRO)send the string tokens Chandrayaan3 and 23-August-
2023 to the process 1 (Moon). Use appropriate MPI built-in functions with blocking
communications scenario to solve this problem.
Solution=>
Code:
#include <mpi.h>
#include <stdio.h>
#include <string.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 2) {
if (rank == 0) {
MPI_Finalize();
return 1;
if (rank == 0) {
else if (rank == 1) {
MPI_Finalize();
return 0;
Output:
4. Write a program using MPI for the following: a) Consider a parallel matrix-vector
multiplication using MPI, where a large matrix is distributed among different processes. The
distribution is achieved using MPI_Scatter, and after computation, partial results are
gathered using MPI_Allgather. Matrix size should not be less than 3*3. Requested to receive
input matrix and vector from user. b) Consider the following two-dimensional array values
and distribute the values of each row to one process. B[4][4] = {{‘A’, ‘B’, ‘C’, ‘D’}, {‘E’, ‘F’, ‘G’,
‘H’}, {‘I’, ‘J’, ‘K’, ‘L’}, {‘M’, ‘N’, ‘O’, ‘P’}};
Solution=>
Code:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
char B[rows][cols] = {
};
char sub_row[cols];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != rows) {
if (rank == 0) {
MPI_Abort(MPI_COMM_WORLD, 1);
sub_row[3]);
MPI_Finalize();
return 0;
Output: