Distributed Computing
Lab 4
2025
Basics
Initialization
• All MPI programs must begin with a call to
MPI_Init(argc,argv ) //Initialize the MPI execution environment
• And close with a call to
MPI_Finalize( ) // Terminates MPI execution environment
Basics
Size and rank
• Get how many processes are running in a given communicator,
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
• and the rank of the calling process within that communicator.
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
Hello World!
Point to Point communication
• Is the most basic form of communication in MPI, allowing a
program to send a message from one process to another over a
given communicator.
• There are two kinds of communication for sending and
receiving message via MPI which are Blocking and Non-
Blocking.
Point-to-Point Communication
• MPI_Send • MPI_Recv
(const void *buf, (const void *buf,
int count, int count,
MPI_Datatype datatype, MPI_Datatype datatype,
int dest, int source,
int tag, int tag,
MPI_Comm comm) MPI_Comm comm,
MPI_STATUS stat)
MPI_Send(&data, 1, MPI_Status stat;
MPI_INT,Receiver, 0, MPI_Recv(&data, 1, MPI_INT,
MPI_COMM_WORLD); sender, 0, MPI_COMM_WORLD,
&stat);
Send Process
1. The data is copied to the user buffer by the user.
2. The user calls one of the MPI send subroutines.
3. The system copies the data from the user buffer to the system
buffer.
4. The system sends the data from the system buffer to the
destination process.
Receive Process
1. The user calls one of the MPI receive subroutines.
2. The system receives the data from the source process and copies
it to the system buffer.
3. The system copies the data from the system buffer to the user
buffer.
4. The user uses the data in the user buffer.
Example 1 : Ping - Pong
MPI_Init(NULL, NULL);
int size; else if (rank == 1)
MPI_Comm_size(MPI_COMM_WORLD, &size); {
int rank; Receiver = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank); sender = 0;
int data = 1 , sender,Receiver; MPI_Status stat;
if (rank == 0) MPI_Recv(&data, 1, MPI_INT, sender,
{ 0, MPI_COMM_WORLD, &stat);
Receiver = 1; printf("Ping");
sender = 1; MPI_Send(&data, 1, MPI_INT,
MPI_Send(&data, 1, MPI_INT,Receiver, Receiver, 0, MPI_COMM_WORLD);
0, MPI_COMM_WORLD); }
MPI_Status stat; MPI_Finalize();
MPI_Recv(&data, 1, MPI_INT,sender,
0, MPI_COMM_WORLD, &stat);
printf("Pong");
}
Thank You ☺