DEV Community

loading...

Introduction to MPI programming

mateusz800 profile image Mateusz Budnik Updated on ・2 min read

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>
Enter fullscreen mode Exit fullscreen mode

Before using MPI functions, you have to initialize it.

int main(int argc, char** argv){
  ...
  MPI_Init(&argc, &argv);
}
Enter fullscreen mode Exit fullscreen mode

You have to also remember to finish MPI at the end of your programm.

MPI_Finalize();
Enter fullscreen mode Exit fullscreen mode

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, &currentProcessRank);
Enter fullscreen mode Exit fullscreen mode

And also the count of processes inside the communicator.

int processCount;
MPI_Comm_size(MPI_COMM_WORLD, &processCount);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And run it by:

mpirun -np <process count> ./name
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

pic
Editor guide
Collapse
fatimasenouci profile image
Fatima Senouci

Please I need help I have to resolve some problems with Mpi or did you have released some projects with Mpi

Collapse
mateusz800 profile image
Mateusz Budnik Author

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?