parfor
Execute for-loop iterations in parallel on workers
Syntax
Description
Simple Form
parfor
executes loopVar = initVal:endVal; statements;endfor-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 with a step size of 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:
MATLAB loop iterations 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.If you use this syntax, 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 anotherparfor-loop. For more help, see Nested parfor and for-Loops and Other parfor Requirements.
parfor
increments loopVar = initVal:step:endVal; statements;endloopVar by the value of step on each
iteration, or decrements loopVar when the value of
step is negative. step must be
1 or -1. Use this syntax to run
parfor-loops with a descending loop index variable.
If you use this syntax, loop iterations must be consecutive integer values.
Before R2026a: step must be
1 and loop iterations must be consecutive, increasing integer
values.
Advanced Form with Additional Options
parfor(loopVar = initVal:endVal); statements; end and
parfor(loopVar = initVal:step:endVal); statements; end also execute
loop iterations in parallel on workers in a parallel pool. Use parenthesis around the loop
expression to allow for additional options.
parfor(___,
uses M); statements;
endM 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 default
number of workers in your parallel pool using the
PreferredPoolNumWorkers property of the default profile. For all
factors that can affect your default pool size, see Factors That Affect Pool Size.
You can override the default number of workers in a parallel pool by using the parpool function. 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 parallel environment defined by your default
profile. The default parallel environment is Processes. You can
change your profile in Parallel Settings. For more details, see
Specify Your Parallel Settings.
parfor(___, uses opts); statements;
endopts 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.
parfor(___, executes cluster); statements;
endstatements on workers in
cluster without creating a parallel pool. This is equivalent to
executing parfor (loopVar = initVal:endVal,parforOptions(cluster)); statements;
end.
parfor(___, executes pool); statements;
endstatements on the parallel pool specified by
the parallel.Pool object
pool. Use this syntax when you want to evaluate
parfor statements on a pool other than the pool the gcp function returns. (since R2025a)
Examples
Input Arguments
Limitations
Noncommutative reductions are not supported in
parfor-loops when the step size is negative. (since R2026a)
Tips
Use a
parfor-loop when:You have many loop iterations of a simple calculation.
parfordivides 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 afor-loop early, tryparfevalinstead.Unless you specify a cluster object or a pool object, a
parfor-loop runs on the existing parallel pool. If no pool exists,parforstarts a new parallel pool, unless the automatic starting of pools is disabled in your parallel settings. If there is no parallel pool andparforcannot start one, the loop runs serially in the client session.If the
AutoAttachFilesproperty in the cluster profile for the parallel pool is set totrue, MATLAB performs an analysis on aparfor-loop to determine what code files are necessary for its execution, seelistAutoAttachedFiles. 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
clearinside aparfor-loop because it violates workspace transparency. For more details, see Ensure Transparency in parfor-Loops or spmd Statements.You can run Simulink® models in parallel with the
parsimcommand instead of usingparfor-loops. For more information and examples of using Simulink in parallel, see Running Multiple Simulations in Simulink (Simulink).For GPU computations:
Do not use a
parfor-loop if you have a single GPU and your loop iterations all use the same GPU. GPUs contain many microprocessors that can perform computations in parallel and trying to further parallelize GPU computations using aparfor-loop is unlikely to speed up your code.Use a
parfor-loop if you have multiple GPUs and your computations use GPU-enabled functions. For more information about using multiple GPUs in aparfor-loop, see Run MATLAB Functions on Multiple GPUs.
Extended Capabilities
Version History
Introduced in R2008aSee Also
for | gcp | listAutoAttachedFiles | parpool | parfeval | ticBytes | tocBytes | send | afterEach | parforOptions
Topics
- Decide When to Use parfor
- Convert for-Loops into parfor-Loops
- Ensure That parfor-Loop Iterations Are Independent
- Nested parfor and for-Loops and Other parfor Requirements
- Troubleshoot Variables in parfor-Loops
- Scale Up parfor-Loops to Cluster and Cloud
- Specify Your Parallel Settings
- Run Parallel Simulations (Simulink)