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
- calcpi.c
- calcpi.f
- dboard.c
- dboard.f
- make.calcpi.rs6k.c
- make.calcpi.rs6k.f
- make.calcpi.sp2.c
- make.calcpi.sp2.f
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).
- pvm_mytid
- pvm_parent
- pvm_spawn
- pvm_initsend
- pvm_pkdouble (pvmfpack for Fortran)
- pvm_upkdouble (pvmfunpack for Fortran)
- pvm_send
- pvm_recv
- pvm_exit
Instructions for compiling and running
- 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 .
- 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
- 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:
You can name it "myhostfile" and place it in the default location,
~/pvm3/bin/RS6K . You can also use the sample hostfile from the
"Introduction to PVM" module.
b) For PVM SP2MPI architecture:
You can name it "mynodefile". The POE environment variable
MP_HOSTFILE should be set to the path of "mynodefile". You can use
the sample node file from the "Introduction to PVM (for SP2MPI
architecture)" module.
- Start PVM.
a) For PVM RS6K architecture:
If you do not plan to use the switch for the start up node, enter:
% pvmd myhostfile &
To use the switch for your start up node, enter:
% 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)
- Start an interactive console by typing "pvm", then issue "conf".
a) For PVM RS6K architecture:
Wait until you see a list of nodes, which determine the number
of daemons started. When you have all machines requested, type
"quit" to exit the PVM console without disturbing the daemons
that are running.
b) For PVM SP2MPI architecture:
Only the node which you started the daemon is displayed.
Type "quit" to exit the PVM console without disturbing the running
daemon.
- 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".
- Quit PVM.
Restart the pvm console by typing "pvm". Then type "halt"
to end the PVM process.
- 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