Accelerating the pace of engineering and science

MATLAB Distributed Computing Server 

Accelerating Calculations Through Parallel for-loops

Parallel 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.

The 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.

using parfor
Using 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 or spmd. The 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.

Refer to the Parallel for-Loops section in the documentation for more information, or watch a example of Using parfor to Run Loops in Parallel (2:48)

using parfor
Using parfor in a function. You can embed parfor-loops in MATLAB scripts and functions for execution on cluster without any additional setup. Click on image to see enlarged view.