Main Content

parfor

Parallel for-loop

Description

parfor loopVar = initVal:endVal; statements; end executes a series of MATLAB® statements for values of loopvar between initval and endval, inclusive, which specify a vector of increasing integer values.

The loop runs in parallel when you have Parallel Computing Toolbox™ or when you create a MEX function or standalone code with MATLAB Coder™. Unlike a traditional for-loop, iterations are not executed in a guaranteed order. You cannot call scripts directly in a parfor-loop. However, you can call functions that call scripts.

example

parfor loopVar = initVal:step:endVal; statements;end increments loopVar 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.

example

parfor(loopVar = initVal:endVal); statements; end and parfor(loopVar = initVal:step:endVal); statements; end also execute statements in a loop. Use parenthesis around the loop expression to allow for additional options.

parfor(___,M); statements; end uses M to specify the maximum number of workers or threads to use in evaluating statements in the loop body. M must be a nonnegative integer.

Examples

collapse all

Perform three large eigenvalue computations using three workers or cores with Parallel Computing Toolbox:

parpool(3)
parfor ii = 1:3 
    c(:,ii) = eig(rand(1000)); 
end

Since R2026a

Use a parfor-loop with a descending range to evaluate a mathematical expression on the workers.

Specify an initial value of 12, step size of -1, and an end value of 1. The parfor-loop iterates from 12 down to 1.

parfor k = 12:-1:1
    out(k) = (5.*(k+8))./2;
end

Input Arguments

collapse all

Loop index variable with initial value initVal and final value endVal. The variable can be any numeric type and the value must be an integer.

Make sure that your parfor-loop variables are consecutive integers. For more details, see Troubleshoot Variables in parfor-Loops (Parallel Computing Toolbox).

Before R2026a: Loop iterations must be consecutive, increasing integer values.

The range of the parfor-loop variable must not exceed the supported range. For more help, see Avoid Overflows in parfor-Loops (Parallel Computing Toolbox).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Initial value loop index variable, loopVar. The variable can be any numeric type and the value must be an integer. With endVal, specifies the parfor range vector, when in the form M:N.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Final value loop index variable, loopVar. The variable can be any numeric type and the value must be an integer. With initVal, specifies the parfor range vector, when in the form M:N.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Step size of loop index, loopVar. The variable can be any numeric type and must be an integer of value 1 or -1 to ensure the loop iterations are consecutive integer values.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Loop body, specified as text. The series of MATLAB commands to execute in the parfor-loop.

You might need to modify your code to use parfor-loops. For more help, see Convert for-Loops into parfor-Loops (Parallel Computing Toolbox).

Do not nest parfor-loops, see Nested parfor and for-Loops and Other parfor Requirements (Parallel Computing Toolbox).

Maximum number of workers running in parallel, specified as a nonnegative integer. If you specify an upper limit, MATLAB uses no more than this number, even if additional workers are available. If you request more workers than the number of available workers, then MATLAB uses the maximum number of workers available at the time of the call. If the loop iterations are fewer than the number of workers, some workers perform no work.

If parfor cannot run on multiple workers (for example, if only one core is available or M is 0), MATLAB executes the loop in a serial manner. In this case, MATLAB still executes the loop body in a nondeterministic order. Use this syntax to switch between parallel and serial when testing your code.

Tips

  • If you have Parallel Computing Toolbox, see for parfor (Parallel Computing Toolbox) and parpool (Parallel Computing Toolbox) for additional information.

  • If you have MATLAB Coder, see parfor (MATLAB Coder) for additional information.

Extended Capabilities

expand all

Version History

Introduced in R2008a

expand all

See Also