| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Parallel Computing Toolbox |
| Contents | Index |
| Learn more about Parallel Computing Toolbox |
| On this page… |
|---|
The single program multiple data (spmd) language construct allows seamless interleaving of serial and parallel programming. The spmd statement lets you define a block of code to run simultaneously on multiple labs. Variables assigned inside the spmd statement on the labs allow direct access to their values from the client by reference via Composite objects.
This chapter explains some of the characteristics of spmd statements and Composite objects.
The "single program" aspect of spmd means that the identical code runs on multiple labs. You run one program in the MATLAB client, and those parts of it labeled as spmd blocks run on the labs. When the spmd block is complete, your program continues running in the client.
The "multiple data" aspect means that even though the spmd statement runs identical code on all labs, each lab can have different, unique data for that code. So multiple data sets can be accommodated by multiple labs.
Typical applications appropriate for spmd are those that require running simultaneous execution of a program on multiple data sets, when communication or synchronization is required between the labs. Some common cases are:
Programs that take a long time to execute — spmd lets several labs compute solutions simultaneously.
Programs operating on large data sets — spmd lets the data be distributed to multiple labs.
You use the function matlabpool to reserve a number of MATLAB labs (workers) for executing a subsequent spmd statement or parfor-loop. Depending on your scheduler, the labs might be running remotely on a cluster, or they might run locally on your MATLAB client machine. You identify a scheduler and cluster by selecting a parallel configuration. For a description of how to manage and use configurations, see Programming with User Configurations.
To begin the examples of this section, allocate local MATLAB labs for the evaluation of your spmd statement:
matlabpool
This command starts the number of MATLAB worker sessions defined by the default parallel configuration. If the local configuration is your default and does not specify the number of workers, this starts one worker per core (maximum of eight) on your local MATLAB client machine.
If you do not want to use default settings, you can specify in the matlabpool statement which configuration or how many labs to use. For example, to use only three labs with your default configuration, type:
matlabpool 3
To use a different configuration, type:
matlabpool MyConfigName
To inquire whether you currently have a MATLAB pool open, type:
matlabpool size
This command returns a value indicating the number of labs in the current pool. If the command returns 0, there is currently no pool open.
Note If there is no MATLAB pool open, an spmd statement runs locally in the MATLAB client without any parallel execution, provided you have Parallel Computing Toolbox software installed. In other words, it runs in your client session as though it were a single lab. |
When you are finished using a MATLAB pool, close it with the command:
matlabpool close
The general form of an spmd statement is:
spmd
<statements>
endThe block of code represented by <statements> executes in parallel simultaneously on all labs in the MATLAB pool. If you want to limit the execution to only a portion of these labs, specify exactly how many labs to run on:
spmd (n)
<statements>
endThis statement requires that n labs run the spmd code. n must be less than or equal to the number of labs in the open MATLAB pool. If the pool is large enough, but n labs are not available, the statement waits until enough labs are available. If n is 0, the spmd statement uses no labs, and runs locally on the client, the same as if there were not a pool currently open.
You can specify a range for the number of labs:
spmd (m, n)
<statements>
endIn this case, the spmd statement requires a minimum of m labs, and it uses a maximum of n labs.
If it is important to control the number of labs that execute your spmd statement, set the exact number in the configuration or with the spmd statement, rather than using a range.
For example, create a random matrix on three labs:
matlabpool
spmd (3)
R = rand(4,4);
end
matlabpool closeNote All subsequent examples in this chapter assume that a MATLAB pool is open and remains open between sequences of spmd statements. |
Unlike a parfor-loop, the labs used for an spmd statement each have a unique value for labindex. This lets you specify code to be run on only certain labs, or to customize execution, usually for the purpose of accessing unique data.
For example, create different sized arrays depending on labindex:
spmd (3)
if labindex==1
R = rand(9,9);
else
R = rand(4,4);
end
endLoad unique data on each lab according to labindex, and use the same function on each lab to compute a result from the data:
spmd (3)
labdata = load(['datafile_' num2str(labindex) '.ascii'])
result = MyFunction(labdata)
endThe labs executing an spmd statement operate simultaneously and are aware of each other. As with a parallel job, you are allowed to directly control communications between the labs, transfer data between them, and use codistributed arrays among them. For a list of toolbox functions that facilitate these capabilities, see the Function Reference sections Interlab Communication Within a Parallel Job and Codistributed Arrays.
For example, use a codistributed array in an spmd statement:
spmd (3)
RR = rand(30, codistributor());
endEach lab has a 30-by-10 segment of the codistributed array RR. For more information about codistributed arrays, see Math with Codistributed Arrays.
![]() | Single Program Multiple Data (spmd) | Accessing Data with Composites | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |