Setting Up and Running MPI

We are now ready to write our first MPI program. Select your choice of language below for this example.

A Hello World example.


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

using namespace std;

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

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

    if ( rank == 0 ) {
        cout<<"Running on "<<npes<<"  Processes\n";
    cout<<"Greetings from rank "<<rank<<"\n";




program hello
use mpi

    integer :: myrank, nprocs
    integer :: err

    call MPI_INIT(err)
    call MPI_COMM_RANK(MPI_COMM_WORLD, myrank, err)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, nprocs, err)

    if ( myrank == 0 ) then
        print *, 'Running on ',nprocs,' Processes'

    print *, 'Greetings from process ', myrank
    call MPI_FINALIZE(err)

end program


from mpi4py import MPI
import sys

myrank = MPI.COMM_WORLD.Get_rank()
nprocs = MPI.COMM_WORLD.Get_size()

if myrank == 0:
    sys.stdout.write("Running on %d processes\n" %(nprocs))

sys.stdout.write("Greetings from process %d\n"%(myrank))

Preparing and Running MPI Codes

On a Remote Cluster

Refer to the instructions from your site, for example UVA Research Computing for our local environment. Nearly always, you will be required to prepare your code and run it through a resource manager such as Slurm.

For Python, you will need to install mpi4py. You may wish to create a conda environment for it. On the UVA system you must use pip rather than conda.

module load gcc openmpi
module load anaconda
pip install --user mpi4py

On a Local Computer

If you have access to a multicore computer, you can run MPI programs on it.

If using a compiled language, before you can build MPI codes you must install a compiler, and possibly some kind of IDE. See our guides for C++ or Fortran.

For Python, on all operating systems install mpi4py. To install mpi4py you must have a working mpicc compiler. For Anaconda this generally requires installing the gcc_linux-64 package using conda. Then you can conda install mpi4py. You may wish to create a new conda environment for MPI programming.


Parallel programs are most frequently used in a high-performance computing environment such as the clusters we have discussed, but many multicore workstations are available that can run Linux and many parallel programmers are familiar with this environment. The simplest way to install MPI is to use a precompiled version for your distribution and compiler.

GCC The recommended MPI distribution is OpenMPI. Most distributions provide a package.

Intel oneAPI Installing the HPC Toolkit will also install IntelMPI.

NVIDIA HPC SDK The NVIDIA software ships with a precompiled version of OpenMPI.

The headers and libraries for MPI must match. Using a header from one MPI and libraries from another, or using headers from a version from one compiler and libraries from a different compiler, usually results in some difficult-to-interpret bugs. Moreover, the process manager must be compatible with the MPI used to compile the code. Because of this, if more than one compiler and especially more than one MPI version is installed, the use of modules ( environment modules or lmod becomes particularly beneficial. Both Intel and NVIDIA provide scripts for the environment modules package (lmod can also read these), with possibly some setup required. If you plan to use mpi4py as well as compiled-language versions, creating a module for Anaconda would also be advisable.

Mac OS

GCC Installing homebrew is the simplest way to set up MPI on a Mac. Install the gcc package followed by the open-mpi package.

Intel oneAPI Install the HPC Toolkit for IntelMPI.

NVIDIA HPC SDK The NVIDIA suite is not available for Mac OS.


GCC The simplest way to use OpenMPI on Windows is through Cygwin. In this case, the gcc compiler suite would first be installed, with g++ and/or gfortran added. Then the openmpi package could also be installed through the cygwin package manager.

Intel oneAPI Install the HPC Toolkit.

NVIDIA HPC_SDK Download the package when it is available.

MPI codes must generally be compiled and run through a command line on Windows. Cygwin users can find a variety of tutorials online, for example here.

The Intel oneAPI Basic Toolkit includes a customized command prompt in its folder in the Apps menu.

Build It

In a terminal window on the frontend run

module load gcc openmpi

For Python add

module load anaconda

This will also load the correct MPI libraries. You must have already installed mpi4py. Activate the conda environment if appropriate.

Use mpiexec and –np only on the frontends! Use for short tests only!

Compiling C

mpicc –o mpihello mpi1.c

Compiling C++

mpicxx –o mpihello mpi1.cxx

Compiling Fortran

mpif90 –o mpihello mpi1.f90

Execute it


mpiexec –np 4 ./mpihello


mpiexec –np 4 python

Submit It

Write a SLURM script to run your program. Request 1 node and 10 cores on the standard partition. The process manager will know how many cores were requested from SLURM.

srun ./mpihello


srun python mpihello\.py

Using the Intel Compilers and MPI

Intel compilers, MPI, and math libraries (the MKL) are widely used for high-performance applications such as MPI codes, especially on Intel-architecture systems. The appropriate MPI wrapper compilers are

mpiicc -o mpihello mpi1.c
# C++
mpiicpc -o mpihello mpi1.cxx
# Fortran
mpiifort -o mpihello mpi1.f90

Do not use mpicc, mpicxx, or mpif90 with Intel compilers. Those are provided by Intel but use the gcc suite and can result in conflicts.