pyMPI tutorial
There are a lot of things left to explore in the base python programming language, but this tutorial
was written with the intention of teaching people how to use MPI (Message Passing Interface) to program
a cluster.
For this reason, we will need a version of python that understands MPI. You can get that
here. If getting pyMPI
to work on your favorite machine is proving to be tricky, you can
download MEMPI from here.
To execute a parallel program you need to use the mpirun command, and specify the number of processes
you want the program to run on. In essence, we are launching two nearly identical programs, the only thing
that is different is the value of mpi.rank that each has. In the example below we will run
in two processes. One process will have mpi.rank == 0, and the other will have mpi.rank == 1. Both will
see mpi.size == 2 (the number of processes).
| file: hello_mpi.py |
| 1 | import mpi |
| 2 | |
| 3 | print "rank=",mpi.rank,"/size=",mpi.size |
> mpirun -np 2 pyMPI hello_mpi.py
rank= 1 rank= 0 /size= 2 /size= 2
|
Notice how confusing the output looks. This is because there is no way to coordinate output generated
by the two processors. For this reason, we will normally use an if statement before each print:
| file: hello_mpi2.py |
| 1 | import mpi |
| 2 | |
| 3 | if mpi.rank == 0: |
| 4 | print "size=",mpi.size |
> mpirun -np 2 pyMPI hello_mpi2.py
size= 2
|
|