Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Parallel Computing Toolbox 4.0

Parallel Programming in MATLAB


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 demo of Using parfor to Run Loops in Parallel

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.

Contact sales
Free technical kit
Trial software
E-mail this page

Get Pricing and
Licensing Options

Tell Us What You Think

How helpful are these features to you: distributed arrays, parallel for-loops, and interactive parallel mode.