Cornell Theory Center

PVM Example Program: calcpi (a SPMD program)

Calculating pi using random numbers

NOTICE:

CTC Staff are no longer updating PVM education materials.

If you are in the process of selecting a message-passing library, we strongly recommend MPI. MPI has all of the features of PVM, with better performance.

If you are already using PVM and choose not to move to MPI, please be aware that these materials were written for PVM 3.3.9. Look on the CTC Software list to see the PVM level we are currently running.


List of Files


Quick summary

This is a very simple parallel program in which each task independently approximates the solution. The amount of work done by each task remains the same as the number of tasks increases, but the averaged solution becomes more correct.


Problem solved

Picture a circle with radius 1 centered at the origin. The circle sits inside a square whose corners are at (-1,-1), (-1,1), (1,1) and (1,-1). The area of the circle divided by the area of the square is pi/4.

Think of this as a dartboard. The darts hit at x and y coordinates which are random numbers between -1 and 1. Darts must fall within the square, and may also fall within the circle. The program approximates the value of pi by dividing the number of darts that fall within the circle by the total darts thrown, and multiplying by four.

A full description of this problem is available in Fox et al., 1988, Solving Problems on Concurrent Processors, volume 1, page 207.


Parallel implementation

There are two ways to benefit from parallelism: you may run the same program in less time, or run a larger program in the same amount of time. This example takes the latter approach.

The serial calculation of pi involves throwing 5000 darts for each of ten iterations, with the cumulative average reported at each iteration. For the parallel implementation, each task performs this process independently, reporting its calculated pi value to the master task (task ID 0), which prints the cumulative average. The more tasks that participate, the more accurate the calculated value of pi. The program is SPMD, i.e. each task runs the same executable.


Subroutines used

The pi program demonstrates the use of the following PVM routines. The routines listed are C routines. The Fortran versions of the program use the Fortran routines, which start with pvmf instead of pvm_ (with the exception of the two listed).


Instructions for compiling and running

  1. Copy the needed files from /usr/local/doc/www/Edu/Tutor/PVM/templates/calcpi to the default pvm directory.
       % cd ~/pvm3/bin/ARCH
         (substitute ARCH with RS6K or SP2MPI, depending on the PVM
          architecture used)
        
       % cp /usr/local/doc/www/Edu/Tutor/PVM/templates/calcpi/*.c .
           - or -
         cp /usr/local/doc/www/Edu/Tutor/PVM/templates/calcpi/*.f .
  2. Compile the program.

    a) If you use PVM RS6K architecture issue

      	% make -f make.calcpi.rs6k.c      
      	     - or -
       	% make -f make.calcpi.rs6k.f
    b) If you use PVM SP2MPI architecture issue
      	% make -f make.calcpi.sp2.c      
      	     - or -
       	% make -f make.calcpi.sp2.f
  3. Create a file that lists the hosts on which the program will run. The calcpi program can run as two to sixteen tasks.

    a) For PVM RS6K architecture:


    b) For PVM SP2MPI architecture:
  4. Start PVM.

    a) For PVM RS6K architecture:

     	 % pvmd myhostfile &
    	 % pvmd -nrXXnXX-hps myhostfile &
    	   (substitute the name of your local host for rXXnXX)
    b) For PVM SP2MPI architecture, enter:
    	 % pvmd -nrXXnXX-hps &
    	   (substitute the name of your local host for rXXnXX)

  5. Start an interactive console by typing "pvm", then issue "conf".

    a) For PVM RS6K architecture:


    b) For PVM SP2MPI architecture:
  6. Run the program.
       % ./calcpi
    You will be requested to enter the number of parallel tasks to spawn. Enter a number between one and sixteen.

    NOTE: If you are using PVM SP2MPI and if nodes you specified in the node file are not available, your program will fail, e.g., with an error message "JM_EXCEEDED CAPACITY".

  7. Quit PVM.

    Restart the pvm console by typing "pvm". Then type "halt" to end the PVM process.

  8. Some or all of the following files will be left in your directory, and should be removed to save space:
    calcpi.c	  calcpi.f	  calcpi.o
    dboard.c	  dboard.f	  dboard.o
    calcpi            myhostfile      mynodefile
    make.calcpi.rs6k.c
    make.calcpi.rs6k.f
    make.calcpi.sp2.c
    make.calcpi.sp2.f
URL http://www.tc.cornell.edu/Edu/Tutor/PVM/templates/calcpi/
updated June 7, 1996