User`s guide
5 Math with Codistributed Arrays
5-20
Looping Over a Distributed Range (for-drange)
In this section...
“Parallelizing a for-Loop” on page 5-20
“Codistributed Arrays in a for-drange Loop” on page 5-21
Note Using a for-loop over a distributed range (drange) is intended for explicit indexing
of the distributed dimension of codistributed arrays (such as inside an spmd statement or
a communicating job). For most applications involving parallel for-loops you should first
try using parfor loops. See “Parallel for-Loops (parfor)”.
Parallelizing a for-Loop
If you already have a coarse-grained application to perform, but you do not want to
bother with the overhead of defining jobs and tasks, you can take advantage of the ease-
of-use that pmode provides. Where an existing program might take hours or days to
process all its independent data sets, you can shorten that time by distributing these
independent computations over your cluster.
For example, suppose you have the following serial code:
results = zeros(1, numDataSets);
for i = 1:numDataSets
load(['\\central\myData\dataSet' int2str(i) '.mat'])
results(i) = processDataSet(i);
end
plot(1:numDataSets, results);
save \\central\myResults\today.mat results
The following changes make this code operate in parallel, either interactively in spmd or
pmode, or in a communicating job:
results = zeros(1, numDataSets, codistributor());
for i = drange(1:numDataSets)
load(['\\central\myData\dataSet' int2str(i) '.mat'])
results(i) = processDataSet(i);
end
res = gather(results, 1);
if labindex == 1