Home High Performance Computing Hello World in Message Passing Interface (MPI)

Hello World in Message Passing Interface (MPI)

In this lesson, we will learn how to write a simple hello world program using Message Passing Interface, and to run it across multiple processes.

If you don’t know what MPI is, please refer to Introduction to Message Passing Interface – MPI.

MPI program to write Hello World in C

Let’s break the program into steps and understand it

The first step is to import the mpi.h header file. It is a standard MPI header file in C and contains the definition of functions we will be using.

The  MPI_Init  initialized the MPI execution environment by constructing all the global and internal variables.

The next function MPI_Comm_size returns the total number of processes in a communicator. It takes two inputs: first is a communicator( MPI_COMM_WORLD  is a default communicator ) and second is an integer passed by reference which will store the return value.

 MPI_Comm_rank  function returns the rank of the current process. Each process is assigned a unique rank starting from zero and it is used to uniquely identify a process. The result will be stored in an integer variable which is passed by reference.

Finally, we will write, MPI_Finalize  which will clean the MPI environment.

Note: MPI_COMM_WORLD is not the only communicator in MPI we can create custom communicators.

MPI_COMM_WORLD groups all the processes when the program is started such that each process is connected and can communicate inside this communicator.

5 process grouped using MPI_COMM_WORLD communicator

Running the above program

Before running the above program we need to make sure that the MPI environment is set up in our operating system.
Run the command in Ubuntu:

$ ompi_info

It will give you a description of the MPI library if installed. If not installed try running following commands.

$ sudo apt update
$ sudo apt install mpich
$ sudo apt install lam4-dev libmpich-dev libopenmpi-dev

Now that MPI library (mpich) is installed in your system we can proceed with running the above program.

$ mpicc -o hello hello_world_mpi.c
$ mpirun hello                       // it will run the program in 1 processors
$ mpirun -np 3 hello                // it will run the program in 3 processors

Note, the  -np tells to run this many copies of the program on the given nodes. This option indicates that the specified file is an executable program and not an application context. If no value is provided for
the number of copies to execute, Open MPI will automatically execute a copy of the program on each process slot.

$ mpirun -np -machinefile machine.file hello

In this variation  -machinefile tells MPI to run the program in the hosts specified in the machine.file in the specified number of times. Note, we must have ssh access to all the hosts.

Subscribe to our weekly newsletter

Join our community of 1000+ developers and stay updated with the fast moving world of computer science

We promise, we won't spam
Even we hate spam as much as you hate them


Please enter your comment!
Please enter your name here