An excellent tutorial and resources can be found here (look for English version) : http://www.idris.fr/formations/mpi/
Only few tips and examples are provided here because the IDRIS tutorial is more than enough to learn MPI.
Never use the same variable at in and out of MPI subroutines ( call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 0 , tag , val , … ). You will get segfault or wrong results with some MPI implementations.
Beware, using random_number in MPI will results in extremely slow code execution if misuse, only one random_number call (C/Fortran) can be called at a time on a single socket (not a core !). A good way to bypass this issue is to use Marsaglia’s Ziggurat algorithm : http://people.sc.fsu.edu/~jburkardt/f_src/ziggurat/ziggurat.html
(Many thanks to Adrien Cassagne who translated to C the Heat equation and the Conjugate gradient)
First, you need to install a working mpi tool chain. If you are on a workstation, you may need to build it yourself. Copy/past the following lines into your shell, it will build a ready to use mpi environment (note that you can change the 1.6.4 (and v1.6 for download URL) number by the last version, check on openmpi web site, http://www.open-mpi.org/ , top right, stable release) :
wget http://www.open-mpi.org/software/ompi/v1.6/downloads/openmpi-1.6.4.tar.gz tar xvzf openmpi-1.6.4.tar.gz cd openmpi-1.6.4 ./configure --prefix=$HOME/openmpi/1.6.4 CC=gcc CXX=g++ F77=gfortran FC=gfortran make make check make install export PATH=$HOME/openmpi/1.6.4/bin:$PATH export LD_LIBRARY_PATH=$HOME/openmpi/1.6.4/lib:$LD_LIBRARY_PATH
Now, you should have a working mpi system. Try it using :
And you should get :
mpirun (Open MPI) 1.6.4 Report bugs to http://www.open-mpi.org/community/help/
Do not forget that if you want to make it persistent, you have to add :
export PATH=$HOME/openmpi/1.6.4/bin:$PATH export LD_LIBRARY_PATH=$HOME/openmpi/1.6.4/lib:$LD_LIBRARY_PATH
In your .bashrc.
The example here is the hello world seen before.
Compile it using the mpif90 wraper (if you type mpif90 –version, you will see it's just a wraper of gfortran) :
mpif90 hello-mpi.f90 -o hello-mpi
Then execute it, using 4 processes (you can load as many processes you want on a single computer. You will get max performances if you have one process per core. DO NOT use more than 64 processes on a single machine, it may freeze and you may be force to reboot the OS manually).
mpirun -np 4 ./hello-mpi Hello world ! I am process 0 on 4 processes.I am running on tallgeese Hello world ! I am process 3 on 4 processes.I am running on tallgeese Hello world ! I am process 1 on 4 processes.I am running on tallgeese Hello world ! I am process 2 on 4 processes.I am running on tallgeese
For C code, use mpicc (or mpic++ for C++) instead of mpif90.