4.

Examples


The Virtual Cluster Toolbox includes many interesting scientific, graphics, and programming applications for parallel computing. This section illustrates a few simple programming and application examples. You'll need a virtual cluster with at least one node booted up to try out the examples. Refer to Chapter 3. Operation for instructions on booting the cluster.

The examples can be found in the examples directory of each user. The "rocketcalc" user is used in the examples below.

Some of the examples compute an approximation of pi by numerically integrating the function f(x)=4/(1+x2) on the interval [0,1] using the midpoint rule. The examples solve this problem in parallel in different ways.

Figure 4.1 presents a schematic illustration of the integration for the simple case of four uniform subintervals in [0,1] and two parallel computing processes. The function is represented by the solid red curve. The left-most red-filled rectangles show the part of the numerical integration computed by one process (for example), and the green-filled rectangles show the part of the integration computed by another process. The two processes can compute their respective integration sub-problems in parallel. Thus, in this trivial example, four subintervals are computed in the time that two could be computed using a single process (less the time for collecting the partial sums from each child process).
Figure 4.1: Numerical integration.
Figure 4.1: Simple example of numerical integration with four subintervals and two processes.

One can easily imagine alternate ways to divide up the numerical integration problem among a set of computing processes. You can easily modify the examples that compute this result in parallel below to implement your ideas.

Text typeset using a monospaced font indicates terminal session information (commands to type, output, etc.). Italicized text generally indicates a variable argument that should be replaced with something appropriate to its context.

Parallel programming with MP_Lite

MP_Lite is a message-passing system similar to MPI from the Scalable Computing Laboratory at the Ames Laboratory (http://www.scl.ameslab.gov/Projects/MP_Lite). It provides an efficient, high-performance, portable, and user-friendly message-passing library for C and Fortran. This section outlines compiling and running a simple C program with MP_Lite functions to compute the value of ?.
  1. Log in as a non-root user (e.g., "rocketcalc") from a terminal window and issue the command
    cd examples
  2. Compile the "pi.c" example with:
    cc -o pi-mplite pi.c -I /opt/mplite/2.7/gnu -L /opt/mplite/2.7/gnu -lmplite -lm
    The compiled program is named "pi-mplite."
  3. Select one or more nodes on which to run the pi-mplite program. For illustration purposes in this example the example "n1" and "n2" are selected (we assume than an n2 exists).
  4. Run the pi-mplite program with:
    mprun -np 2 -h n1 n2 /home/rocketcalc/examples/pi-mplite
    The -np 2 arguments indicate that two processes are to be run, followed by the -h host names.... The number of hosts must match the number of processes in this example. For more information on starting up programs with mprun, type:
    mprun -usage
This example illustrates the use of MP_Lite with simple MPI programs. View the pi.c source code to see the MPI commands used. There are many very interesting features of MP_Lite--see the documentation for more info. MP_Lite rocks!

Parallel programming with LAM/MPI

MPI--the Message Passing Interface is a rich, standardized API for communication between processes. According to http://www.lam-mpi.org, "LAM/MPI is a high-quality open-source implementation of the Message Passing Interface specification, including all of MPI-1.2 and much of MPI-2. Intended for production as well as research use, LAM/MPI includes a rich set of features for system administrators, parallel programmers, application users, and parallel computing researchers."

We couldn't agree more--LAM is our favorite MPI implementation. In this example, we'll compile and run the pi.c program used in the MP_Lite example--only this time with LAM.

  1. Compile the pi.c example with:
    mpicc -o pi-lam pi.c -lm
    The output program will be called "pi-lam."
  2. LAM/MPI uses a set of daemons to manage running MPI programs. Before running the example, we need to start them up with the command:
    lamboot
  3. Run the example with:
    mpirun -np n /home/rocketcalc/examples/pi-lam
    where n indicates the number of processes to launch. Processes will be assigned in a round-robin fashion on the cluster nodes. For a complete list of the nodes participating, issue the command:
    lamnodes

Parallel Raytracing with POVRay

The Persistence of Vision Raytracer (POV-Ray) is a high-quality tool for creating stunning raytraced three-dimensional graphics. The VCT includes a ready-to-run parallel version of POV-Ray. Follow the steps outlined below on the front-end system to render a sample image:
  1. Log in to a graphical session with VNC or through the Unicluster Web Portal from a web browser as outlined in Chapter 3.
  2. Open a terminal window (right-click on the desktop)
  3. Start the LAM/MPI system running with the command lamboot
  4. Run the mpipov script on a sample scene:
    mpipov n /opt/povray/povray31/scenes/advanced/sombrero.pov
    (where n indicates the number of processes to spawn).
  5. Watch the scene render!

Type mpipov with no arguments for a brief help screen listing the possible arguments. Note that mpipov is only a simple script that initializes LAM/MPI and calls the actual MPI version of POV-Ray to do the rendering.

Additional scenes can be found in the /usr/share/povray31/scenes/advanced directory. Try rendering them in the same way as the sombrero above.

More information on POV-Ray can be found at http://www.povray.org. The particular MPI implementation used here was written by Leon Verrall (http://www.verrall.demon.co.uk/mpipov).

Interactive message passing with GNU/Octave and PVM

GNU/Octave is a powerful numerical linear algebra interpreter. You can interactively compute with and otherwise manipulate things like matrices and vectors with Octave. Octave can also run programs written in an an easy-to-use interpreted language.

The Virtual Cluster Toolbox includes a set of message-passing extensions to Octave based on PVM. The extensions allow multiple Octave processes to communicate with each other. The interactive nature, sophisticated matrix manipulation and simple programming make Octave an excellent platform for experimenting with parallel methods and prototyping algorithms.

Starting PVM
Open a terminal window and start the PVM console with the command:

pvm

Add additional computing nodes to the PVM with the console command "add." For example, to add nodes n2 and n3, type:

add n2 n3

at the PVM console prompt. When you're finished adding nodes, type

quit

Example 1: Interactive message passing
Open two terminal windows. Start an interactive Octave session in each of the windows with the command:

octave

(two distinct GNU/Octave sessions should be running). In each Octave session, enter the command:

pvm_mytid

and make a note of the numbers that are returned. Those numbers are the unique PVM task IDs for each Octave session's underlying process. They are used by the processes to identify each other in the virtual machine.

The two running Octave sessions can now communicate with each other using PVM. Select one of the Octave sessions to receive data--we assume for the purpose of illustration below that its PVM task ID number is 262147:

pvm_recv

The pvm_recv command will block (i.e., wait) until it receives some data from any other Octave process.

Send the waiting Octave process some data from the other Octave session with the commands:

A=rand (5)
pvm_send (A, 262147)

Note that the Octave process with task ID 262147 has received the entries of the random 5x5 matrix "A" defined on the sending node.

This example illustrates the most basic feature of message-passing systems: exchanging data between processes. The two Octave sessions used in this example can be running on different nodes in the cluster. For a complete listing of the available PVM commands in Octave, type pvm_ followed by the TAB key at an Octave prompt. For detailed help on a specific command, type help followed by the name of the command at an Octave prompt.


Example 2: Computing pi yet again in parallel
This example provides the Octave analog of the above MPI and PVM C examples for computing an approximation to pi by parallel numerical integration. Two Octave program files are used: pi.p (the parent program) and pi.c (the child program). The parent process interacts with the user and dynamically spawns the requested number of child processes. Each child process is responsible for integrating a function over a portion of the integration interval. The parent process collects their sums and reports the result when all the children have finished.

To run this example, open a terminal window and issue the following commands:

cd examples
octave

After the Octave session starts, type the command:

pip

to start the parent program. Follow the on-screen instructions...

This example illustrates how easy it is to implement dynamic parallel computation using Octave and PVM. Each child program is run by a dynamically spawned Octave process. Examine the pip.m and pic.m files with your favorite editor (e.g., vi) for more information.