parfor
Executefor
-loop iterations in parallel on workers
Syntax
Description
parfor
executesloopVar
=initVal
:endVal
;statements
; endfor
-loop iterations in parallel on workers in a parallel pool.
MATLAB®executes the loop body commands instatements
for values ofloopVar
betweeninitVal
andendVal
.loopVar
specifies a vector of integer values increasing by 1. If you have Parallel Computing Toolbox™, the iterations ofstatements
can execute on a parallel pool of workers on your multi-core computer or cluster. As with afor
-loop, you can include a single line or multiple lines instatements
.
找出parfor
can help increase your throughput, seeDecide When to Use parfor.
parfor
differs from a traditionalfor
-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, seeConvert 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, seeEnsure That parfor-Loop Iterations are Independent.You cannot use a
parfor
-loop inside anotherparfor
循环。For more help, seeNested parfor and for-Loops and Other parfor Requirements.
parfor (
usesloopVar
=initVal
:endVal
,M
);statements
; endM
to specify the maximum number of workers from the parallel pool to use in evaluatingstatements
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 theHometab in theEnvironmentsection, by selectingParallel>Parallel Preferences. You can override the default number of workers in a parallel pool by usingparpool
. When no workers are available in the pool orM
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 executeparfor
, you automatically create a parallel pool of workers on the parallel environment defined by your default profile. The default parallel environment isProcesses. You can change your profile inParallel Preferences. For more details, seeSpecify Your Parallel Preferences.
parfor (
usesloopVar
=initVal
:endVal
,opts
);statements
; endopts
to specify the resources to use in evaluatingstatements
in the loop body. Create a set ofparfor
options using theparforOptions
function. With this approach, you can runparfor
on a cluster without first creating a parallel pool and control howparfor
partitions the iterations into subranges for the workers.
Examples
Input Arguments
Tips
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. Areductionvariable accumulates a value that depends on all the iterations together, but is independent of the iteration order. For more information, seeReduction 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, tryparfeval
instead.Unless you specify a cluster object, 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 andparfor
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 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
循环。然而,您可以调用的函数调用cripts.Do not use
clear
inside aparfor
loop because it violates workspace transparency. SeeEnsure Transparency in parfor-Loops or spmd Statements.You can run Simulink®models in parallel with the
parsim
command instead of usingparfor
-loops. For more information and examples of using Simulink in parallel, seeRunning Multiple Simulations(Simulink).
Version History
Introduced in R2008a
See 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 Preferences
- Run Parallel Simulations(Simulink)