Mpi 1
Mpi 1
with MPI
• Introduction to MPI
• Basic MPI functions
• Most of the MPI materials are obtained
from William Gropp and Rusty Lusk’s MPI
tutorial at
http://www.mcs.anl.gov/mpi/tutorial/
Message Passing Interface (MPI)
• MPI is an industrial standard that specifies library
routines needed for writing message passing
programs.
– Mainly communication routines
– Also include other features such as topology.
• MPI allows the development of scalable portable
message passing programs.
– It is a standard supported pretty much by everybody in
the field.
• MPI uses a library approach to support
parallel programming.
– MPI specifies the API for message passing
(communication related routines)
– MPI program = C/Fortran program + MPI
communication calls.
– MPI programs are compiled with a regular
compiler(e.g gcc) and linked with an mpi
library.
MPI execution model
• Separate (collaborative) processes are running all
the time.
– ‘mpirun –machinefile machines –np 16 a.out’ The
same a.out is executed on 16 machines.
– Different from the OpenMP model.
• What about the sequential portion of an application?
MPI data model
• No shared memory. Using explicit
communications whenever necessary.
• How to solve large problems
– Logically partition the large array and logically
distribute the large array into processes.
• MPI specification is both simple and
complex.
– Almost all MPI programs can be realized with
six MPI routines.
– MPI has a total of more than 100 functions and
a lot of concepts.
– We will mainly discuss the simple MPI, but we
will also give a glimpse of the complex MPI.
• MPI is about just the right size.
– One has the flexibility when it is required.
– One can start using it after learning the six
routines.
The hello world MPI program
#include "mpi.h" • Mpi.h contains MPI
#include <stdio.h> definitioins and types.
int main( int argc, char *argv[] ) • MPI program must start with
{ MPI_init
MPI_Init( &argc, &argv ); • MPI program must exit with
printf( "Hello world\n" ); MPI_Finalize
MPI_Finalize(); • MPI functions are just
library routines that can be
return 0;
used on top of the regular C,
} C++, Fortran language
constructs.
Compiling, linking and running
MPI programs
• MPICH is installed on linprog
• To run a MPI program, do the following:
– Create a file called .mpd.conf in your home directory with content
‘secretword=cluster’
– Create a file ‘hosts’ specifying the machines to be used to run MPI
programs.
– Boot the system ‘mpdboot –n 3 –f hosts’
– Check if the system is corrected setup: ‘mpdtrace’
– Compile the program: ‘mpicc hello.c’
– Run the program: mpiexec –machinefile hostmap –n 4 a.out
• Hostmap specifies the mapping
• -n 4 says running the program with 4 processes.
– Exit MPI: mpdallexit
Login without typing password
• Key based authentication
• Password based authentication is inconvenient at times
– Remote system management
– Starting a remote program (starting many MPI processes!)
– ……
• Key based authentication allows login without typing the
password.
– Key based authentication with ssh in UNIX
• Remote ssh from machine A to machine B
Step 1: at machine A: ssh-keygen –t rsa
(do not enter any pass phrase, just keep typing “enter”)
Step 2: append A:.ssh/id_rsa.pub to B:.ssh/authorized_keys
• MPI uses the SPMD model (one copy of
a.out).
– How to make different process do different
things (MIMD functionality)?
• Need to know the execution environment: Can
usually decide what to do based on the number of
processes on this job and the process id.
– How many processes are working on this problem?
» MPI_Comm_size
– What is myid?
» MPI_Comm_rank
» Rank is with respect to a communicator (context of
the communication). MPI_COM_WORLD is a
predefined communicator that includes all processes
(already mapped to processors).
Sending and receiving messages
in MPI
• Questions to be answered:
– To who are the data sent?
– What is sent?
– How does the receiver identify the message
• Send and receive routines in MPI
– MPI_Send and MPI_Recv (blocking send/recv)
– Identify peer: Peer rank (peer id)
– Specify data: Starting address, datatype, and count.
• An MPI datatype is recursively defined as:
– predefined, corresponding to a data type from the
language (e.g., MPI_INT, MPI_DOUBLE)
– a contiguous array of MPI datatypes
– a strided block of datatypes
– an indexed array of blocks of datatypes
– an arbitrary structure of datatypes
– There are MPI functions to construct custom datatypes, in
particular ones for subarrays
– Identifying message: sender id + tag
MPI blocking send
MPI_Send(start, count, datatype, dest, tag, comm)