parfor
Execute for-loop iterations in parallel on workers
Syntax
Description
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 and for-Loops and Other parfor Requirements.
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 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 loopVar = initVal:endVal,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 loopVar = initVal:endVal,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 loopVar = initVal:endVal,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
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 a- for-loop early, try- parfevalinstead.
- Unless you specify a cluster 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 and- parforcannot start one, the loop runs serially in the client session.
- If the - AutoAttachFilesproperty 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- parforloop because it violates workspace transparency. See Ensure Transparency in parfor-Loops or spmd Statements.
- You can run Simulink® models in parallel with the - parsimcommand instead of using- parfor-loops. For more information and examples of using Simulink in parallel, see Running Multiple Simulations (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 a- parfor-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 a- parfor-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)