Introduction to MPI

This page gathers together resources for the Introduction to MPI course by MJ Rutter for the proposed 2022 MPhil / CDT in the Centre for Scientific Computing. The copyright of the contents is held by its author, unless otherwise stated.



Are available only to those registered with the course organisers. For those, a link to the lectures.

Example Materials

The files below use the convention that .c is C source code, and .cc is C++ source code. The C should be comprehensible to anyone who knows C++, so not all C examples have been translated. If this course also teaches C++ programmers about printf(), then that is a bonus.

The Beginning (lecture 2)

Save for the Hello examples, all .f90 examples use the Fortran 2008 MPI interface of use mpi_f08. Your Fortran compiler might, or might not, recognise f08 as a valid extension for Fortran source. Gfortran does, ifort does not...

More Quadrature (lecture 3)

Calling the C library GSL from Fortran is sufficiently messy that those examples have been tidied away to a separate page as an example of using Fortran's iso_c_binding and GSL.

Mandelbrot Set (lecture 4)

(Note that C (C99) and C++ differ in their treatment of complex numbers. The C examples below will not work with C++ compilers. Note too that Preview on MacOS will read .pam files. One way of achieving this is to type

$ open mandel.pam -a /Applications/


Some prefer to see examples in Fortran, and, for them, mandel_mpi.f90 is provided. This uses mpi_gather to collect the whole array onto the root process before writing it out. It uses stream I/O (introduced in Fortran 2003). It could be modified to use the inquire statement and pos option to the write statement in order to let each MPI process write its own line, in a similar fashion to the C codes above.

As an extra example, combining MPI with GTK (with no proof that they are compatible), mgtk.c. This is a master-slave Mandelbrot set which displays its output via GTK. It is intended as an MPI example, not an example of good MPI+GTK programming. One can readily see idle slaves consuming CPU time in this example.

(One might well argue that modern computers are so fast that there is little reason to use the power of MPI for this problem, and even serial python codes such as this TK python Mandelbrot set are quite fast enough. On the other hand, the MPI code presented here was used to produce a 31,500x10,500 pixel image of part of the Mandelbrot set with maxiter set to 2048. This was then printed at 300dpi to produce a poster for a school. An attempt to produce an image of this size using Mathematica's MandelbrotSetPlot function failed, whereas the MPI code succeeded on a much more modest computer. One can waste much time admiring images of different parts of the Mandelbrot set. Regrettably many images on the web fail to include co-ordinates, so are hard to reproduce. But sometimes one can find a gallery of Mandelbrot images with co-ordinates.)

Laplace's Equation (lecture 5)

Miscellaneous (lecture 6)

Communicators (lecture 7)

MPI-IO (lecture 7)

Hardware (lecture 8)

Useful Links