MPI stands for Messaging Passing Interface. It is a standard that allows communication between processes. Thanks to that we can write high-performance parallel programmes.
Basic commands
To use MPI commands you have to include the library.
#include <mpi.h>
Before using MPI functions, you have to initialize it.
int main(int argc, char** argv){
...
MPI_Init(&argc, &argv);
}
You have to also remember to finish MPI at the end of your programm.
MPI_Finalize();
In MPI application there are many processes. They might be grouped in a group called communicator. Each process in a group has its own identifier called a rank.
You can get a rank of the currently running process.
int currentProcessRank;
MPI_Comm_rank(MPI_COMM_WORLD, ¤tProcessRank);
And also the count of processes inside the communicator.
int processCount;
MPI_Comm_size(MPI_COMM_WORLD, &processCount);
MPI_COMM_WORLD is a default communicator. It groups all the processes when the program started.
Each process makes some calculations but in the end, we almost always want to calculate the final value. We can do it by MPI_Reduce(...) function. An example of the function call is shown below.
MPI_Reduce(&process_calc_val, &final_value, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
In that example, we sum the calculated values from each process. The result of this operation will be stored in the final_value variable.
P2P communication
MPI allows us to exchange messages between processes. P2P (Peer to Peer) communication is possible.
The command to send a message is as follows:
int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
So for example we use the code below to send some value to process with rank 1.
int some_value = 5;
MPI_Send(&some_value, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
To receive the message, below command is used.
int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status);
Process 1 can receive a message from process 0.
int received_value;
MPI_Recv(&received_value, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
Note that MPI_Send(...) and MPI_Recv(...) are blocking functions.
Compiling
To compile the program put the line below in the terminal.
mpicc name.c -o name
And run it by:
mpirun -np <process count> ./name
Top comments (2)
Please I need help I have to resolve some problems with Mpi or did you have released some projects with Mpi
Unfortunately, I did not create any project yet. I just started learning about MPI in one of my classes. These are just my notes. I am not sure if I can help you. What problems do you have to resolve?