parfor
Parallel for
-loop
Syntax
Description
parfor
creates a loop in a generated MEX function or in C/C++
code that runs in parallel on shared-memory multicore platforms.LoopVar
= InitVal
:EndVal
; Statements
;
end
The parfor
-loop executes the Statements
for
values of LoopVar
between InitVal
and Endval
. LoopVar
specifies
a vector of integer values increasing by 1.
parfor (
uses a maximum of LoopVar
= InitVal
:EndVal
, NumThreads
); Statements
;
endNumThreads
threads
when creating a parallel for
-loop.
Examples
Input Arguments
Limitations
You must use a compiler that supports the Open Multiprocessing (OpenMP) application interface. See Supported Compilers. If you use a compiler that does not support OpenMP, MATLAB Coder treats the
parfor
-loops asfor
-loops. In the generated MEX function or C/C++ code, the loop iterations run on a single thread.The OpenMP application interface is not compatible with JIT MEX compilation. See JIT Compilation Does Not Support OpenMP.
Do not use the following constructs inside
parfor
-loops:You cannot call extrinsic functions using
coder.extrinsic
in the body of aparfor
-loop.You cannot write to a global variable inside a
parfor
-loop.MATLAB Coder does not support the use of
coder.ceval
in reductions. For example, you cannot generate code for the followingparfor
-loop:Instead, write a local function that calls the C code usingparfor i = 1:4 y = coder.ceval('myCFcn',y,i); end
coder.ceval
and call this function in theparfor
-loop. For example:parfor i = 1:4 y = callMyCFcn(y,i); end function y = callMyCFcn(y,i) y = coder.ceval('mCyFcn', y , i); end
The type of the loop index must be representable by an integer type on the target hardware. Use a type that does not require a multiword type in the generated code.
parfor
for standalone code generation requires the toolchain approach for building executables or libraries. Do not change settings that cause the code generator to use the template makefile approach. See Project or Configuration Is Using the Template Makefile.To use
parfor
in your MATLAB code, you require a Parallel Computing Toolbox™ license.
For a comprehensive list of restrictions, see parfor Restrictions.
Tips
Use a
parfor
-loop when:You need 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 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.
The input argument
NumThreads
sets the OpenMPnum_threads()
clause in the generated code. OpenMP also supports globally limiting the number of threads in C/C++ by setting the environment variableOMP_NUM_THREADS
or by usingomp_set_num_threads()
. For more information, see the openMP specifications. https://www.openmp.org/specifications/
Version History
Introduced in R2012b