Parallel for
-loop
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.
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 as for
-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 a
-loop.parfor
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 following parfor
-loop:
parfor i = 1:4 y = coder.ceval('myCFcn',y,i); end
coder.ceval
and call this function
in the parfor
-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.
For a comprehensive list of restrictions, see parfor Restrictions.