QUAD_SPMD is a MATLAB program which uses the SPMD (single program, multiple data) command to estimate an integral using quadrature.

The SPMD command allows a programmer to set up parallel computations that require more user control than the simple parfor command. In particular, users familiar with MPI will see many features that are similar to that parallel programming model, including the ability to send and receive messages. (Messages will NOT be exhibited in this simple example, however!)

The algorithm carried out estimates the integral of a function f(x) from 0 to 1 by the trapezoidal rule:

```        Integral ( a <= x <= b ) f(x) dx = ( b - a ) / ( 2 * ( n - 1 ) )
* ( f(x1) + 2*f(x2) + 2*f(x3) + ... + 2*f(xn-2) + 2*f(xn-1) + f(xn) )
```
where xi is the i-th point equally spaced between a and b, with the endpoints included.

It's clear that this calculation can be done in parallel, and in fact, simply by defining appropriate values of ai and bi, we can have parallel process i carry out the trapezoidal rule over [ai,bi] and sum the results to get the answer.

This example demonstrates how MATLAB's SPMD facility can be used to implement this parallel calculation. No attempt is made to compare the timings of the parallel code to a sequential calculation. The point here is simply the mechanics of setting up an SPMD calculation, and showing what you can expect.

Several points are worth mentioning:

• Parallel sections of the code begin with the spmd statement, and end with an end statement. The computations in these blocks occur on the MATLAB workers. The client sits idly and "watches".
• The matlabpool parameter of the batch command defines a number of workers. Each spmd block brings these workers into activity. Each worker has access to the variable numlabs, which contains the number of workers. Each worker has a unique value of the variable labindex, between 1 and numlabs.
• Any variable defined by the client is "visible" to the workers and can be used on the right hand side of equations within the spmd blocks.
• Any variable defined by the workers is a "composite" variable. If a variable called X is defined by the workers, then each worker has its own value, and the set of values is accessible by the client, using the worker's index. Thus X{1} is the value of X computed by worker 1.
• A program can have several spmd blocks. If the program completes an spmd block, carries out some commands in the client program, and then enters another spmd block, then all the variables defined during the previous spmd block still exist. The data that was on worker 1 is still there, for instance. It is simply as though each worker was "paused" while the client did some work.
• Workers cannot directly see each other's variables. Communication from one worker to another can be done through the client. However, a limited number of special operators are available, that can be used within spmd blocks, which combine variables. In particular, the command gplus sums the values of a variable that exists on all the workers, and returns to each worker the value of that sum.

The function has the form:

function value = quad_fun ( n )
where
• n, is the number of points to use;
• value, is the estimated integral.

Depending on the situation, the function could be executed in parallel:

• interactively, and locally, using the matlabpool command;
• indirectly, and locally, using the batch command;
• indirectly, and on the Ithaca cluster, using the batch command;
• indirectly, and on the FSU HPC cluster, using the fsuClusterMatlab command;

### Languages:

QUAD_SPMD is available in a MATLAB version.

### Related Data and Programs:

CELL_DETECTION_TASKS, a MATLAB program which creates modified versions of a sequence of gray-scale TIF files containing images of cells; the process of each file is carried out independently, using the "task" feature of MATLAB's parallel computing toolbox.

CG_DISTRIBUTED, a MATLAB program which implements a version of the NAS CG conjugate gradient benchmark, using distributed memory.

COLLATZ_PARFOR, a MATLAB program which seeks the maximum Collatz sequence between 1 and N, running in parallel using MATLAB's "PARFOR" feature.

CONTRAST_SPMD, a MATLAB program which demonstrates the SPMD parallel programming feature for image operations; the client reads an image, the workers increase contrast over separate portions, and the client assembles and displays the results.

CONTRAST2_SPMD, a MATLAB program which demonstrates the SPMD parallel programming feature for image operations; this improves the contrast_spmd program by allowing the workers to share some data; this makes it possible to eliminate artificial "seams" in the processed image.

DIJKSTRA_SPMD, a MATLAB program which uses the SPMD feature to parallelize a simple example of Dijkstra's minimum distance algorithm for graphs.

FACE_SPMD, a MATLAB program which demonstrates the SPMD parallel programming feature; the client has a 3D box that has been dissected into tetrahedrons. Multiple workers cooperate to construct a list of the triangular faces that lie on the boundaries of the box.

FD2D_HEAT_EXPLICIT_SPMD, a MATLAB program which uses the finite difference method and explicit time stepping to solve the time dependent heat equation in 2D. A black and white image is used as the "initial condition". MATLAB's SPMD facility is used to carry out the computation in parallel.

FMINCON_PARALLEL, a MATLAB program which demonstrates the use of MATLAB's FMINCON constrained minimization function, taking advantage of MATLAB's Parallel Computing Toolbox for faster execution.

IMAGE_DENOISE_SPMD, a MATLAB program which demonstrates the SPMD parallel programming feature for image operations; the client reads an image, the workers process portions of it, and the client assembles and displays the results.

LINEAR_SOLVE_DISTRIBUTED, a MATLAB program which solves a linear system A*x=b using MATLAB's spmd facility, so that the matrix A is "distributed" across multiple MATLAB workers.

MATLAB_PARALLEL, programs which illustrate "local" parallel programming on a single computer with MATLAB's Parallel Computing Toolbox.

MATRIX_ASSEMBLE_SPMD, a MATLAB program which demonstrates the SPMD parallel programming feature by having each worker assemble part of the Hilbert matrix, which is then combined into one array by the client program.

MD_PARFOR, a MATLAB program which carries out a molecular dynamics simulation, running in parallel using MATLAB's "PARFOR" feature.

ODE_SWEEP_PARFOR, a MATLAB program which demonstrates how the PARFOR command can be used to parallelize the computation of a grid of solutions to a parameterized system of ODE's.

PLOT_SPMD, a MATLAB library which demonstrates the SPMD parallel programming feature, by having a number of labs compute parts of a sine plot, which is then displayed by the client process.

PRIME_PARFOR, a MATLAB program which counts the number of primes between 1 and N; running in parallel using MATLAB's "PARFOR" feature.

PRIME_SPMD, a MATLAB program which counts the number of primes between 1 and N; running in parallel using MATLAB's "SPMD" feature.

QUAD_PARFOR, a MATLAB program which estimates an integral using quadrature; running in parallel using MATLAB's "PARFOR" feature.

QUAD_SERIAL, a MATLAB program which approximates an integral using a quadrature rule, and is intended as a starting point for parallelization exercises.

RANDOM_WALK_2D_AVOID_TASKS, a MATLAB program which computes many self avoiding random walks in 2D by creating a job which defines each walk as a task, and then computes these independently using MATLAB's Parallel Computing Toolbox task computing capability.

SATISFY_PARFOR, a MATLAB program which demonstrates, for a particular circuit, an exhaustive search for solutions of the circuit satisfiability problem, running in parallel using MATLAB's "PARFOR" feature.

SUBSET_SUM_TASKS, a MATLAB program which solves a subset sum problem by exhaustive search, subdividing the search range among separate tasks.

### Reference:

The User's Guide for the Parallel Computing Toolbox is available at http://www.mathworks.com/access/helpdesk/help/pdf_doc/distcomp/distcomp.pdf

• Gaurav Sharma, Jos Martin,
MATLAB: A Language for Parallel Computing,
International Journal of Parallel Programming,
Volume 37, Number 1, pages 3-36, February 2009.

### Source Code:

• quad_fun.m, a MATLAB function which returns an estimate for an integral, using a given number of points.
• quad_pool.m a script which uses the MATLABPOOL command to run the function locally and interactively.
• quad_script.m, a MATLAB script file which simply invokes the function with N = 10000 points.
• quad_batch_local.m, a batch command to run the job indirectly on the local system, plus a few more commands to monitor its progress, print the diary, and destroy the job at the end.
• quad_batch_ithaca.m, a batch command to run the job indirectly on the Ithaca cluster, plus a few more commands to monitor its progress, print the diary, and destroy the job at the end.
• quad_fsu.m a script which uses the fsuClusterMatlab command to run the function indirectly on the FSU HPC cluster.

You can go up one level to the MATLAB source codes.

Last revised on 04 February 2010.