Text Only Login to PAWS Baton Rouge, Louisiana |
LSU Homepage
HPC RESOURCES EMPLOYMENT LOG IN SITE MAP SEARCH
homeaboutprogramprojectscyberinfrastructurenewseventscontact
  1. Bootcamp Home
  2. How to Program, Part I
  3. How to Program, Part II
  4. How to Program, Part III
  5. How to Program, Part IV
  6. How to Program, Part V
  7. exercises
  8. pyMPI tutorial
  9. Calculating PI, Part I
  10. Calculating PI, Part II
  11. Calculating PI, Part III
  12. Poogle - Web Search
  13. Mandelbrot Sets
  14. Mandelbrot, The Code
  15. Mandelbrot, The Images
  16. Conway's Life, Part I
  17. Life Code Listing
  18. Conway's Life, Part II
  19. 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= 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
LSU Homepage