execute for-pg电子麻将胡了
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 .
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 .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 .you cannot use a
parfor-loop inside anotherparfor-loop. for more help, see .
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 numworkers or preferredpoolnumworkers
property of the default profile. for all factors that can affect your default pool size, see
. 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 preferences. for more details, see specify your parallel preferences.
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 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.
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 .
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, try instead.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 preferences. 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, see . 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 inside a
parforloop because it violates workspace transparency. see .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 (simulink).
version history
introduced in r2008a
see also
| | | parpool | | | | | |