When you use
parfor inside the entry-point MATLAB function, and call
codegen on this function, the
generated MEX file is multithreaded. For more information, see Algorithm Acceleration Using Parallel for-Loops (parfor) (MATLAB Coder). However,
parfor is not ideal for DSP algorithms. The reason being that
DSP algorithms involve states.
Most algorithms in DSP System Toolbox™ contain states and stream data. States in MATLAB are modeled using persistent variables. Because
parfor does not support persistent variables, you cannot
model states using
parfor loops. See Global or Persistent Declarations in parfor-Loop (MATLAB Coder). In addition, you cannot have any data dependency across
parfor loops. Hence, you cannot maintain state information
across these loops. See When Not to Use parfor-Loops (MATLAB Coder).
dspunfold overcomes these limitations by supporting
If your application does not tolerate latency, use
parfor does not introduce latency. Latency is the
number of input frames processed before generating the first output frame.
parforRequires Significant Restructuring in Code
parfor requires you to restructure your algorithm to have a
loop-like structure that is iteration independent. Due to the semantic limitations
parfor, replacing a
for-loop with a
parfor-loop often requires significant code refactoring.
dspunfold does not require you to restructure your code.
When you call
dspunfold on an entry-point MATLAB function that contains
parfor multi-threading is disabled.
codegen with the
–O option set to
disable:openmp. With this
parfor loops are treated as
loops. The multi-threading behavior of the generated MEX file is due entirely to