parfor - Parallel for-loop - MATLAB (original) (raw)

Main Content

Syntax

Description

parfor _`loopvar`_ = _`initval`_:_`endval`_; _`statements`_; end executes a series of MATLAB® statements for values of loopvar betweeninitval 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 aparfor-loop. However, you can call functions that call scripts.

example

parfor (_`loopvar`_ = _`initval`_:_`endval`_, M); _`statements`_; end executes statements in a loop using a maximum of M workers or threads, where M is a nonnegative integer.

Examples

collapse all

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

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

Tips

Extended Capabilities

expand all

If you have Parallel Computing Toolbox installed, then when you use parfor, MATLAB automatically opens a parallel pool of workers on your local machine. MATLAB runs the loop across the available workers. Control parallel behavior with the parallel preferences, including scaling up to a cluster.

For details, see Parallel for-Loops (parfor) (Parallel Computing Toolbox).

Version History

Introduced in R2008a