User`s guide
3 Single Program Multiple Data (spmd)
3-4
R = rand(4,4);
end
Note All subsequent examples in this chapter assume that a parallel pool is open and
remains open between sequences of spmd statements.
Unlike a parfor-loop, the workers used for an spmd statement each have a unique
value for labindex. This lets you specify code to be run on only certain workers, 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
end
Load unique data on each worker according to labindex, and use the same function on
each worker to compute a result from the data:
spmd (3)
labdata = load(['datafile_' num2str(labindex) '.ascii'])
result = MyFunction(labdata)
end
The workers executing an spmd statement operate simultaneously and are aware of each
other. As with a communicating job, you are allowed to directly control communications
between the workers, transfer data between them, and use codistributed arrays among
them.
For example, use a codistributed array in an spmd statement:
spmd (3)
RR = rand(30, codistributor());
end
Each worker has a 30-by-10 segment of the codistributed array RR. For more information
about codistributed arrays, see “Working with Codistributed Arrays” on page 5-5.