Execute for
-loop iterations in parallel
on workers in parallel pool
parfor loopVar = initVal:endVal; statements;
end
parfor (loopVar = initVal:endVal,M); statements;
end
parfor
executes loopVar
= initVal
:endVal
; statements
;
end 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-Loops and for-Loops.
parfor (
uses loopVar
= initVal
:endVal
,M
); statements
;
end 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,
select 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.
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.
Use a parfor
-loop when:
You have many loop iterations of a simple calculation. parfor
divides
the loop iterations into groups so that each thread can execute one
group of iterations.
You have some loop iterations that take a long time to execute.
Do not use a parfor
-loop when an
iteration in your loop depends on the results of other iterations.
Reductions are one exception to this rule. A reduction variable accumulates a value that depends on all the iterations together, but is independent of the iteration order. For more information, see Reduction Variables.
When you use parfor
, you have
to wait for the loop to complete to obtain your results. Your client MATLAB is
blocked and you cannot break out of the loop early. If you want to
obtain intermediate results, or break out of a for
-loop
early, try parfeval
instead.
A parfor
-loop runs on the existing
parallel pool. If no pool exists, parfor
starts
a new parallel pool, unless the automatic starting of pools is disabled
in your parallel preferences. If there is no parallel pool and parfor
cannot
start one, the loop runs serially in the client session.
If the AutoAttachFiles
property
in the cluster profile for the parallel pool is set to true
,
MATLAB performs an analysis on a parfor
-loop
to determine what code files are necessary for its execution, see listAutoAttachedFiles
. Then MATLAB automatically
attaches those files to the parallel pool so that the code is available
to the workers.
You cannot call scripts directly in a parfor
-loop.
However, you can call functions that call scripts.
Do not use clear
inside a parfor
loop because
it violates workspace transparency. See Ensure Transparency in parfor-Loops.
You can run Simulink® models in parallel with
the parsim
command instead of using parfor
-loops.
For more information and examples of using Simulink in parallel, see Run Multiple Simulations (Simulink).
afterEach
| for
| gcp
| listAutoAttachedFiles
| parfeval
| parpool
| send
| ticBytes
| tocBytes