PP

Program 6: Write a MPI program to demonstration of deadlock using point to point

communication and avoidance of deadlock by altering the call sequence.

#include <mpi.h>

#include <stdio.h>

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

int rank, size;

int msg_send = 100, msg_recv;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &size);

if (size < 2) {

if (rank == 0)

printf("Run with at least 2 processes.\n");

MPI_Finalize();

return 0;

}

if (rank == 0) {

printf("Process 0 sending to Process 1...\n");

MPI_Send(&msg_send, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // blocking send

MPI_Recv(&msg_recv,1,MPI_INT,1,0,MPI_COMM_WORLD, MPI_STATUS_IGNORE);

printf("Process 0 received from Process 1: %d\n", msg_recv);

} else if (rank == 1) {

printf("Process 1 sending to Process 0...\n");

MPI_Send(&msg_send, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); // blocking send

MPI_Recv(&msg_recv,1,MPI_INT,0,0,MPI_COMM_WORLD, MPI_STATUS_IGNORE);

printf("Process 1 received from Process 0: %d\n", msg_recv);

}

MPI_Finalize();

return 0;

}


7. Write a MPI Program to demonstration of Broadcast operation.

#include <mpi.h>

#include <stdio.h>

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

int rank, size;

int number;

// Initialize the MPI environment

MPI_Init(&argc, &argv);

// Get the rank and number of processes

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &size);

// Process 0 gets the input

if (rank == 0) {

printf("Enter a number to broadcast: ");

fflush(stdout); // Ensure prompt is printed before input

scanf("%d", &number);

}

// Broadcast the number from process 0 to all other processes

MPI_Bcast(&number, 1, MPI_INT, 0, MPI_COMM_WORLD);

// Each process prints the received number

printf("Process %d received number: %d\n", rank, number);

// Finalize the MPI environment

MPI_Finalize();

return 0;

}



8. Write a MPI Program demonstration of MPI_Scatter and MPI_Gather.

#include <stdio.h>

#include <mpi.h>

int main(int argc, char** argv)

{

int rank, size, send_data[4] = {10, 20, 30, 40},

recv_data; MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &size);

MPI_Scatter(send_data, 1, MPI_INT, &recv_data, 1, MPI_INT, 0, MPI_COMM_WORLD);

printf("Process %d received: %d\n", rank, recv_data);

recv_data += 1;

MPI_Gather(&recv_data, 1, MPI_INT, send_data, 1, MPI_INT, 0, MPI_COMM_WORLD);

if (rank == 0)

{

printf("Gathered data: ");

for (int i = 0; i< size; i++)

printf("%d ", send_data[i]);

printf("\n");

}

MPI_Finalize();

return 0;

}


Program 9: Write a MPI Program to demonstration of MPI_Reduce and MPI_Allreduce

(MPI_MAX, MPI_MIN, MPI_SUM, MPI_PROD)

#include <mpi.h>

#include <stdio.h>

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

int rank, size;

int value;

int sum, prod, max, min;

int sum_all, prod_all, max_all, min_all;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &size);

// Each process sets its own value (e.g., rank + 1)

value = rank + 1;

printf("Process %d has value %d\n", rank, value);

// ---------- MPI_Reduce ----------

MPI_Reduce(&value, &sum,  1, MPI_INT, MPI_SUM,  0, MPI_COMM_WORLD);

MPI_Reduce(&value, &prod, 1, MPI_INT, MPI_PROD, 0, MPI_COMM_WORLD);

MPI_Reduce(&value, &max,  1, MPI_INT, MPI_MAX,  0, MPI_COMM_WORLD);

MPI_Reduce(&value, &min,  1, MPI_INT, MPI_MIN,  0, MPI_COMM_WORLD);

if (rank == 0) {

printf("\n[Using MPI_Reduce at Root Process]\n");

printf("Sum   = %d\n", sum);

printf("Prod  = %d\n", prod);

printf("Max   = %d\n", max);

printf("Min   = %d\n", min);

}

// ---------- MPI_Allreduce ----------

MPI_Allreduce(&value, &sum_all,  1, MPI_INT, MPI_SUM,  MPI_COMM_WORLD);

MPI_Allreduce(&value, &prod_all, 1, MPI_INT, MPI_PROD, MPI_COMM_WORLD);

MPI_Allreduce(&value, &max_all,  1, MPI_INT, MPI_MAX,  MPI_COMM_WORLD);

MPI_Allreduce(&value, &min_all,  1, MPI_INT, MPI_MIN,  MPI_COMM_WORLD);

printf("\n[Process %d] MPI_Allreduce Results:\n", rank);

1

printf("  Sum  = %d\n", sum_all);

printf("  Prod = %d\n", prod_all);

printf("  Max  = %d\n", max_all);

printf("  Min  = %d\n", min_all);

MPI_Finalize();

return 0;

}


Comments