Parallel Computing Toolbox
for-loops let you distribute a set of independent tasks over a set of workers. The
parfor construct uses the familiar
for-loop syntax and is ideal for parameter sweeps and similar tasks.
parfor construct has mechanisms for detecting and exchanging the necessary data and code between the client MATLAB session and workers. It also detects the presence of workers automatically. As a result you do not have to construct and submit complex batch jobs to the cluster.
parfor in the command window to distribute work among four workers. Note that
parfor detects the presence of workers, letting you mix serial and parallel code in an interactive session without creating and submitting jobs.
In this example, the
parfor loop tries to divide the work among multiple processors by allocating iterations to the four different workers. The only requirement for distributing execution using
parfor is that iterations must be independent of each other, and no communication can occur between workers during the execution of the loop.
Work distribution is dynamic. Instead of being allocated a fixed iteration range, the workers are allocated a new iteration only after they finish processing their current iteration, which results in an even work load distribution.
You interact with workers using the
matlabpool command directly from MATLAB command window. The command sets up the interactive execution environment for parallel constructs such as
parfor loops can be issued from command line, as well as within functions and scripts. Using the batch command (for MATLAB scripts) and the
createMatlabPoolJob command (for MATLAB functions), you can send code containing
parfor for execution offline.