MPI Programming Model
Definition: MPI (Message Passing Interface) is a parallel programming model used for
communication between processes in distributed memory systems.
Diagram:
+-------------+ +-------------+
| Process 0 | | Process 1 |
| Local Mem | | Local Mem |
+------+------+ +------+------+
| |
| Message Passing |
| <-----------------> |
| |
+------+------+ +------+------+
| Process 2 | | Process 3 |
| Local Mem | | Local Mem |
+-------------+ +-------------+
Features: Distributed memory, explicit communication, scalability, portability, and parallel
execution.
Program Structure:
MPI_Init();
MPI_Comm_rank();
MPI_Comm_size();
MPI_Send();
MPI_Recv();
MPI_Finalize();
Example Code:
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int rank, data;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
data = 100;
MPI_Send(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (rank == 1) {
MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Finalize();
return 0;
}
Advantages: Efficient, scalable, and suitable for high-performance computing.
Disadvantages: Complex programming and debugging.