MPI Introduction

MPI (Message Passing Interface) is a library of definitions and functions designed by a group of researchers from academia that can be used to parallise C, C++ or Fortran programs.

MPI is a standardised message-passing system, and is merely a standardized interface, not a specific implementation. The standard defines the syntax and semantics of a core of library routines useful to a wide range of users writing portable message-passing programs in C, C++, and Fortran.  There are several implementations of MPI in existance, a freely available one is MPICH from Argonne National Laboratory and Mississipi State University, LAM from Ohio Supercomputer Centre, and OpenMPI that is developed and maintained by a consortium of academic and research people. The NWU-HPC supports openMPI and MPICH at the moment.

MPI example

The complete MPI interface consist of 129 functions. The functions that are used most often, are those that enable processes to exchange data, that is to be able to send and receive messages.

The following are basic functions that are used to build most MPI programs:

  • All MPI/C programs must include a mpi.h header
  • All MPI programs must call MPI_INT as the first MPI call, to initialize
  • MPI_COMM_SIZE must be called to get the number of processes that are running
  • MPI_COMM_RANK to determine the process rank, which is a number between 0 and (number of processes) -1
  • General message passing will use MPI_SEND and MPI_RECV
  • All MPI programs must call MPI_Finalize as the last call to an MPI library routine

Tutorial

 

Compile MPI code

The most generic way to compile an MPI / C program is using the mpicc script provided by the MPI implementation. These commands appear similar to the basic cc comand. eg: {path to mpi implementation}/mpicc – o executable filename.c

At the NWU-HPC an example of the command above will be:

/opt/mpich/gnu/bin/mpicc -o MPI_Example.out MPI_Example.c

 

Run Binary

To execute an MPI/C program, the most generic way is to use the provided mpirun script. Roughly this script determines machine architecture, which include other machines, and launch the desired processes on the other machines. The following command launches the executable program on 2 nodes:

mpirun -np 2 executable

–np 2 specifies the number of nodes (not processors) to use for your binary’s execution. Therefore, if there are 8 processors per node, -np 2 means that 16 processors will be used.

Choice of nodes can be specified by setting a machinefile, e.g.:

mpirun -machinefile machine-file-name -np number-of-nodes executable