User`s guide
Looping Over a Distributed Range (for-drange)
5-21
plot(1:numDataSets, res);
print -dtiff -r300 fig.tiff;
save \\central\myResults\today.mat res
end
Note that the length of the for iteration and the length of the codistributed array
results need to match in order to index into results within a for drange loop.
This way, no communication is required between the workers. If results was simply
a replicated array, as it would have been when running the original code in parallel,
each worker would have assigned into its part of results, leaving the remaining parts
of results 0. At the end, results would have been a variant, and without explicitly
calling labSend and labReceive or gcat, there would be no way to get the total results
back to one (or all) workers.
When using the load function, you need to be careful that the data files are accessible to
all workers if necessary. The best practice is to use explicit paths to files on a shared file
system.
Correspondingly, when using the save function, you should be careful to only have one
worker save to a particular file (on a shared file system) at a time. Thus, wrapping the
code in if labindex == 1 is recommended.
Because results is distributed across the workers, this example uses gather to collect
the data onto worker 1.
A worker cannot plot a visible figure, so the print function creates a viewable file of the
plot.
Codistributed Arrays in a for-drange Loop
When a for-loop over a distributed range is executed in a communicating job,
each worker performs its portion of the loop, so that the workers are all working
simultaneously. Because of this, no communication is allowed between the workers while
executing a for-drange loop. In particular, a worker has access only to its partition of
a codistributed array. Any calculations in such a loop that require a worker to access
portions of a codistributed array from another worker will generate an error.
To illustrate this characteristic, you can try the following example, in which one for loop
works, but the other does not.
At the pmode prompt, create two codistributed arrays, one an identity matrix, the other
set to zeros, distributed across four workers.