You are on page 1of 2

parfor

Execute for-loop iterations in parallel on workerscollapse all in page

Syntax

parfor loopVar = initVal:endVal; statements; end

parfor (loopVar = initVal:endVal,M); statements; end

parfor (loopVar = initVal:endVal,opts); statements; end

parfor (loopVar = initVal:endVal,cluster); statements; end

Description

example

parfor loopVar = initVal:endVal; statements; end executes for-loop iterations in parallel on


workers in a parallel pool.

MATLAB® executes the loop body commands in statements for values of loopVar between initVal
and endVal. loopVar specifies a vector of integer values increasing by 1. If you have Parallel
Computing Toolbox™, the iterations of statements can execute on a parallel pool of workers on
your multi-core computer or cluster. As with a for-loop, you can include a single line or multiple
lines in statements.

To find out how parfor can help increase your throughput, see Decide When to Use parfor.

parfor differs from a traditional for-loop in the following ways:

Loop iterations are executed in parallel in a nondeterministic order. This means that you might
need to modify your code to use parfor. For more help, see Convert for-Loops Into parfor-Loops.

Loop iterations must be consecutive, increasing integer values.

The body of the parfor-loop must be independent. One loop iteration cannot depend on a
previous iteration, because the iterations are executed in a nondeterministic order. For more help,
see Ensure That parfor-Loop Iterations are Independent.
You cannot use a parfor-loop inside another parfor-loop. For more help, see Nested parfor and
for-Loops and Other parfor Requirements.

example

parfor (loopVar = initVal:endVal,M); statements; end uses M to specify the maximum number of
workers from the parallel pool to use in evaluating statements in the loop body. M must be a
nonnegative integer.

By default, MATLAB uses the available workers in your parallel pool. You can change the number
of workers on the Home tab in the Environment section, by selecting Parallel > Parallel
Preferences. You can override the default number of workers in a parallel pool by using parpool.
When no workers are available in the pool or M is zero, MATLAB still executes the loop body in a
nondeterministic order, but not in parallel. Use this syntax to switch between parallel and serial
execution when testing your code.

With this syntax, to execute the iterations in parallel, you must have a parallel pool of workers. By
default, if you execute parfor, you automatically create a parallel pool of workers on the cluster
defined by your default cluster profile. The default cluster is local. You can change your cluster in
Parallel Preferences. For more details, see Specify Your Parallel Preferences.

parfor (loopVar = initVal:endVal,opts); statements; end uses opts to specify the resources to use in
evaluating statements in the loop body. Create a set of parfor options using the parforOptions
function. With this approach, you can run parfor on a cluster without first creating a parallel pool
and control how parfor partitions the iterations into subranges for the workers.

example

parfor (loopVar = initVal:endVal,cluster); statements; end executes statements on workers in


cluster without creating a parallel pool. This is equivalent to executing parfor (loopVar =
initVal:endVal,parforOptions(cluster)); statements; end.

You might also like