parfor - Parallel for-loop

Syntax

parfor loopvar = initval:endval; statements; end
parfor (loopvar = initval:endval, M); statements; end

Description

parfor loopvar = initval:endval; statements; end executes a series of MATLAB commands denoted here as statements for values of loopvar between initval and endval, inclusive, which specify a vector of increasing integer values. Unlike a traditional for-loop, there is no guarantee of the order in which the loop iterations are executed.

The general format of a parfor statement is:

parfor loopvar = initval:endval
    <statements>
end

Certain restrictions apply to the statements to ensure that the iterations are independent, so that they can execute in parallel. If you have Parallel Computing Toolbox™, the iterations of statements can execute in parallel on separate MATLAB workers on your multi-core computer or computer cluster.

To execute the loop body in parallel, you must open a pool of MATLAB workers using the matlabpool function, which is available in Parallel Computing Toolbox software.

parfor (loopvar = initval:endval, M); statements; end executes statements in a loop using a maximum of M MATLAB workers to evaluate statements in the body of the parfor-loop. Input variable M must be a nonnegative integer. By default, MATLAB uses up to as many workers as it finds available.

When any of the following are true, MATLAB does not execute the loop in parallel:

If you have Parallel Computing Toolbox software, you can read more about parfor and matlabpool by typing

doc distcomp/parfor
doc distcomp/matlabpool

Examples

Perform three large eigenvalue computations using three computers or cores:

matlabpool(3)
parfor i=1:3, c(:,i) = eig(rand(1000)); end

See Also

for

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS