Basic usage of MPI (Message Passing Interface)

If you do not know why use MPI, check out here. If that’s your first time struggling with MPI you will need [download (Linux) | download (Windows)] it first. That’s the MPICH, one of the MPI’s “code-approach”.

Linux users: Extract it in any folder you want and using console step into this folder and:

make install

Before you try code your parallel application, there is some things to do. First you need to create a .mpd.conf file and define its secretword:

cd $HOME
touch .mpd.conf
chmod 600 .mpd.conf
echo “secretword=secret12345” >> .mpd.conf

If you’re trying use your app and receiving:

“cannot connect to local mpd (/tmp/mpd2.console_machinename); possible causes:
1. no mpd is running on this host
2. an mpd is running but was started without a “console” (-n option)”

You should create the .mpd.conf file (description above).

Well, let’s create the first parallel app that uses MPI.

#include "mpi.h"
#include <stdio.h>

int main( int argc, char *argv[] ) {
int numprocs, rank;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

printf ("Number of procces: %d My ID is: %d\n", numprocs, rank);

return 0;

Save it as peer.c and compile and generate the executable:

mpicc -o peer peer.c.

Start one instance of mdp and run four instances of your app:

mpdboot -n 1
mpiexec -l -n 4 ./peer

That’s all for now.