# Introduction to MPI

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

## Handout

## Lectures

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)

`hello.c`(`hello.cc`)`hello.f90`and`hello.f08``version.c`(or`version.f90`)`int1.c`(trivial integration) (or`int1.cc`or`int1.f90`)

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)

`gsl.c`GSL quadrature, serial`gsl_mpi.c`GSL quadrature, MPI`square.c`pi/4, serial (or`square.f90`)`square_mpi.c`pi/4, MPI (or`square_mpi.f90`)`int1_gather.c`(or`int1_gather.cc`) trivial integration plus MPI_Gather

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/Preview.app/

)

`mandel.c`Mandelbrot, serial (or`mandel.cc`)`mandel_mpi.c`Mandelbrot, MPI (or`mandel_mpi.cc`)`mandel_mpi_io.c`Mandelbrot, MPI, I/O on rank zero`mandel_mpi_io_pp.c`Mandelbrot, MPI, I/O on rank zero, point to point`mandel_mpi_ms.c`Mandelbrot, MPI, master-slave (or`mandel_mpi_ms.cc`)

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)

`laplace.c`Laplace, serial`laplace.f90`ditto, Fortran`laplace_mpi.c`Laplace, MPI (or`laplace_mpi.cc`)`laplace_mpi_bsend.c`Laplace, MPI, Bsend`laplace_mpi_isend.c`Laplace, MPI, Isend`laplace_mpi_analytic.f90`Laplace, MPI, Isend, periodic boundary conditions and comparison with analytic result`laplace_mpi_analytic_old.f90`as above, but using the older`use mpi`

interface, showing the difference with requests and statuses

### Miscellaneous (lecture 6)

### Communicators (lecture 7)

`comms.f90`and`comms.cc`: synthetic examples of reductions on matrix (and these examples are not translations of each other, so one may wish to read both)

### MPI-IO (lecture 7)

`hello_ordered.c``mandel_mpi-io.c`MPI_File_write_at`mandel_mpi-io.f90`ditto, Fortran`mandel_mpi-io_datatype.f90`(user defined datatype and file view to interleave rows)`mandel_mpi-io_datatype_v2.f90`(user defined datatype and file view to interleave pixels)

### Hardware (lecture 8)

`stream_mpi.f`Stream benchmark, Fortran, MPI, directly copied from Dr McAlpin's Stream website. You will also need a timer function such as`mysecond.f90``timer_test.f90`a possible solution to testing timer resolution