Main Content

How Is dspunfold Different from parfor?

The dspunfold and parfor (MATLAB Coder) functions accelerate MATLAB® algorithms through parallelization. Each function has its own advantages and disadvantages.

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.

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 persistent variables.

dspunfold Introduces Latency

If your application does not tolerate latency, use parfor instead. parfor does not introduce latency. Latency is the number of input frames processed before generating the first output frame.

parfor Requires 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 of parfor, replacing a for-loop with a parfor-loop often requires significant code refactoring. dspunfold does not require you to restructure your code.

parfor Used with dspunfold

When you call dspunfold on an entry-point MATLAB function that contains parfor, parfor multi-threading is disabled. dspunfold calls codegen with the –O option set to disable:openmp. With this option set, parfor loops are treated as for- loops. The multi-threading behavior of the generated MEX file is due entirely to dspunfold.

See Also

Functions

Related Topics