1. How to Program, Part I
  2. How to Program, Part II
  3. How to Program, Part III
  4. How to Program, Part IV
  5. How to Program, Part V
  6. exercises
  7. pyMPI tutorial
  8. Calculating PI, Part I
  9. Calculating PI, Part II
  10. Calculating PI, Part III
  11. Poogle - Web Search
  12. Mandelbrot Sets
  13. Mandelbrot, The Code
  14. Mandelbrot, The Images
  15. Conway's Life, Part I
  16. Life Code Listing
  17. Conway's Life, Part II
  18. MPI Life Code Listing

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=rank= 0 /size= 2
 1 /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