Norman Matloff's MPICH2 MPI Tutorial

Norm Matloff's MPICH2 MPI Tutorial

Professor Norm Matloff
Dept. of Computer Science
University of California at Davis
Davis, CA 95616

(Please mail any questions to Norm Matloff.)

(You may wish to also read my general MPI tutorial, which is a full chapter in my open-source book on parallel programming.)


Installing MPICH2:

If you wish to install MPICH/MPICH2 yourself, say on your Linux box, download the source code from the MPICH2 home page. Just unpack the software and type

make install

If you don't like the default installation directory, add --prefix to your configure command line.

Make sure your shell search path includes the directory containing mpicc, as well as the directory in which your MPICH2 application executable will reside.

Please note that MPI implementations typically work by using ssh or equivalent to invoke programs on other nodes. Thus the proper path must be set up by your shell startup file, not just via a temporary resetting of path.

The same is true for library path. If you later get "missing library" messages when you try to run your MPI app, set the environment variable LD_LIBRARY_PATH to the lib/ directory of your MPICH2 installation.

Finally, make sure you can do passwordless login from one MPI machine to another. To arrange the passwordless login on UCD CSIF machines (or others, actually), see these instructions.

Compiling MPICH2 application programs:


mpicc -g -o binary_file_name source_file.c 

For example, for the program PrimePipe.c, make an executable prp this way:

mpicc -g -o prp PrimePipe.c
(You may need to specify the full path to prp.)

(If you wish to use C++, use mpicxx instead of mpicc.)

Running MPICH2 application programs:

Note: As of Version 1.2, MPICH2 no longer uses the mpd daemon. If you are using Version 1.1, see the old version of this tutorial.

Set up a hosts file, listing which machines you wish your MPI app to run on, e.g. hosts3:

Run, say for the above executable prp on the above hosts file, by typing

mpiexec -f hosts3 -n 3 prp 100 0

where 100 and 0 are the command-line arguments to prp.


Note carefully that all the output from printf() and the like will be collected and printed at whichever machine you started it. These may be interspersed together from different nodes, making your output difficult or impossible to read. (Note: This interspersing of printf() outputs is quite common in parallel systems.)


Don't use printf() calls for most of your debugging. Your debugging will be much easier and faster if you use a debugging tool, such as gdb. To use gdb with MPICH2, follow the directions given in my parallel debugging guide, at