User`s guide

spmd
11-287
By default, MATLAB uses as many workers as it finds available in the pool. When there
are no MATLAB workers available, MATLAB executes the block body locally and creates
Composite objects as necessary.
spmd(n), statements, end uses n to specify the exact number of MATLAB workers
to evaluate statements, provided that n workers are available from the parallel pool.
If there are not enough workers available, an error is thrown. If n is zero, MATLAB
executes the block body locally and creates Composite objects, the same as if there is no
pool available.
spmd(m,n), statements, end uses a minimum of m and a maximum of n workers to
evaluate statements. If there are not enough workers available, an error is thrown. m
can be zero, which allows the block to run locally if no workers are available.
For more information about spmd and Composite objects, see “Distributed Arrays and
SPMD”.
Examples
Perform a simple calculation in parallel, and plot the results:
parpool(3)
spmd
% build magic squares in parallel
q = magic(labindex + 2);
end
for ii=1:length(q)
% plot each magic square
figure, imagesc(q{ii});
end
delete(gcp)
More About
Tips
An spmd block runs on the workers of the existing parallel pool. If no pool exists, spmd
will start a new parallel pool, unless the automatic starting of pools is disabled in
your parallel preferences. If there is no parallel pool and spmd cannot start one, the
code runs serially in the client session.