MPI - hello world!

Hands-on MPI - hello world!

Hello world! MPI

In this lesson, we will show you a basic MPI hello world code and also discuss how to run an MPI program. The lesson will cover the basics of initializing MPI and running an MPI job across several processes.


Let's get introduced with the functions and environment veriables used in the code:

  • MPI_Init, all of MPI's global and internal variables are constructed.
  • MPI_Comm_size returns the size of a communicator and the built-in MPI_COMM_WORLD encloses all of the processes in the job, so this call should return the amount of processes that were requested for the job.
  • MPI_Comm_rank returns the rank of a process in a communicator. Each process inside of a communicator is assigned an incremental rank starting from zero.
  • MPI_Finalize is used to clean up the MPI environment. No more MPI calls can be made after this one.

Run MPI with Batch system


To execute the program under batch, let's first compile it.

$ mpicc mpi_hello.c -o mpi_hello.out
$ ls | grep mpi

Let's now test mpi_hello.out using the MPI versions avalaible on the system with srun.

Use single node using openmpi:

Use two nodes (compute nodes c1 and c2) and Openmpi:


MPI will automatically detect when it is running in a PBS batch environment and assign processes to the assigned resources. The policy for placing processes can be modified using arguments to the mpirun command. See man mpirun for details.

#PBS -l nodes=3:ppn=12
module load openmpi
mpirun ./a.out

Submit the job with qsub command.

Scientific Computing Essentials