0% found this document useful (0 votes)
11 views7 pages

PDC Lab 8

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

PDC Lab 8

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

ICS 311 PARALLEL AND DISTRIBUTED

COMPUTING

Lab 8

Name: Kartik Shettiwar


Roll: 2022BCS0226

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>

int main(int argc, char** argv) {

int rank, size;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &size);

if (size != 4) {

if (rank == 0) {

printf("Please run the program with 4 processes.\n");

MPI_Finalize();

return 0;
}

printf("Process %d: Your Name - Roll Number\n", rank);

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;

for (int rank = 0; rank < np; rank++) {

if (rank == 0) {

c = a & b;
printf("Process %d: c = a & b = %d\n", rank, c);

else if (rank == 1) {

c = a | b;

printf("Process %d: c = a | b = %d\n", rank, c);

else {

c = a ^ b;

printf("Process %d: c = a ^ b = %d\n", rank, c);

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>

int main(int argc, char* argv[]) {

int rank, size;

char msg1[] = "Chandrayaan3";

char msg2[] = "23-August-2023";

char recv_msg1[50], recv_msg2[50];

// Initialize the MPI environment

MPI_Init(&argc, &argv);

// Get the rank and size

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &size);

if (size != 2) {

if (rank == 0) {

printf("This program requires exactly 2 processes.\n");

MPI_Finalize();

return 1;

if (rank == 0) {

// Process 0 (ISRO): send messages to Process 1


MPI_Send(msg1, strlen(msg1) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);

MPI_Send(msg2, strlen(msg2) + 1, MPI_CHAR, 1, 1, MPI_COMM_WORLD);

printf("Process 0 (ISRO) sent: %s and %s to Process 1 (Moon).\n", msg1, msg2);

else if (rank == 1) {

// Process 1 (Moon): receive messages from Process 0

MPI_Recv(recv_msg1, 50, MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

MPI_Recv(recv_msg2, 50, MPI_CHAR, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

printf("Process 1 (Moon) received: %s and %s.\n", recv_msg1, recv_msg2);

// Finalize the MPI environment

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>

int main(int argc, char* argv[]) {

int rank, size;

const int rows = 4;

const int cols = 4;

char B[rows][cols] = {

{'A', 'B', 'C', 'D'},

{'E', 'F', 'G', 'H'},

{'I', 'J', 'K', 'L'},

{'M', 'N', 'O', 'P'}

};

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) {

printf("This program requires %d processes to run.\n", rows);

MPI_Abort(MPI_COMM_WORLD, 1);

MPI_Scatter(B, cols, MPI_CHAR, sub_row, cols, MPI_CHAR, 0, MPI_COMM_WORLD);

printf("Process %d received row: %c %c %c %c\n", rank, sub_row[0], sub_row[1], sub_row[2],

sub_row[3]);

MPI_Finalize();
return 0;

Output:

You might also like