执行速度
提高生成的 C/C++ 代码的执行速度
利用代码生成选项和优化来提高所生成代码的执行速度。您可以修改或禁用可能会影响执行速度的动态内存分配。可以使用 parfor
循环生成并行代码。如果可用,可以利用预先存在的经过优化的 C 代码和专用库来加快执行速度。
有关如何针对特定条件优化代码的详细信息,请参阅优化策略。
函数
parfor | 并行 for 循环 |
coder.varsize | 声明可变大小数据 |
coder.const | 在生成的代码中将表达式折叠为常量 |
coder.inline | 控制生成的代码中特定函数的内联 |
coder.loop.interchange | Interchange loop indices in generated code (自 R2023a 起) |
coder.loop.parallelize | Parallelize specific for loops in generated code; disable
automatic parallelization (自 R2021a 起) |
coder.loop.reverse | Reverse loop iteration order in generated code (自 R2023a 起) |
coder.loop.tile | Tile for -loops in the generated code (自 R2023a 起) |
coder.loop.unrollAndJam | Unroll and jam for -loops in the generated code (自 R2023a 起) |
coder.unroll | Unroll for -loop by making a copy of
the loop body for each loop iteration |
coder.loop.vectorize | Vectorize for loops in generated code (自 R2023a 起) |
coder.ceval | 调用外部 C/C++ 函数 |
类
coder.BLASCallback | Abstract class for specifying the BLAS library and CBLAS header and data type information for BLAS calls in generated code |
coder.LAPACKCallback | Abstract class for specifying the LAPACK library and LAPACKE header file for LAPACK calls in generated code |
coder.fftw.StandaloneFFTW3Interface | Abstract class for specifying an FFTW library for FFTW calls in generated code |
coder.loop.Control | Loop optimization control object (自 R2023a 起) |
主题
生成代码优化
- 优化策略
优化所生成的代码的执行速度或内存使用量。 - MATLAB Coder 对生成代码进行优化
代码生成器使用优化来提高生成代码的性能。 - Optimize Implicit Expansion in Generated Code
Implicit expansion in the generated code is enabled by default.
memcpy 和 memset 优化
- memcpy 优化
代码生成器通过使用memcpy
来优化生成的代码。 - memset Optimization
The code generator optimizes generated code by usingmemset
.
可变大小数组
- Dynamic Memory Allocation and Performance
Dynamic memory allocation can slow down execution speeds. - 最小化动态内存分配
通过最小化动态内存分配来缩短执行时间。 - Provide Maximum Size for Variable-Size Arrays
Use techniques to help the code generator determine the upper bound for a variable-size array. - Disable Dynamic Memory Allocation During Code Generation
Disable dynamic memory allocation in the app or at the command line. - Set Dynamic Memory Allocation Threshold
Disable dynamic memory allocation for arrays less than a certain size. - Optimize Dynamic Array Access
Improve execution time of dynamic arrays in generated C code.
数组布局
- Generate Code That Uses Row-Major Array Layout
Generate C/C++ code with row elements stored contiguously in memory.
循环
- 使用并行 for 循环 (parfor) 的算法加速
为parfor
循环生成 MEX 函数。 - Classification of Variables in parfor-Loops
Variables insideparfor
-loops are classified as loop, sliced, broadcast, reduction, or temporary. - 使用并行 for 循环 (parfor) 生成代码
生成在共享内存多核平台上并行运行的循环。 - Specify Maximum Number of Threads in parfor-Loops
Generate a MEX function that executes loop iterations in parallel on specific number of available cores. - Specify Maximum Number of Threads to Run Parallel for-Loops in the Generated Code
Run parallelfor
-loops on specific number of available cores in the generated code. - Reduction Assignments in parfor-Loops
A reduction variable accumulates a value that depends on all the loop iterations together. - Control Compilation of parfor-Loops
Treatparfor
-loops asparfor
-loops that run on a single thread. - 在 macOS 平台上安装 OpenMP 库
安装 OpenMP 库以在 macOS 平台上生成并行for
循环。 - Minimize Redundant Operations in Loops
Move operations outside of loop when possible. - Unroll for-Loops and parfor-Loops
Control loop unrolling. - Automatically Parallelize for Loops in Generated Code
Iterations of parallelfor
-loops can run simultaneously on multiple cores on the target hardware. - Reduction Operations Supported for Automatic Parallelization of for-loops
Supported operations for automatic parallelization offor
-loops. - Generate SIMD Code from MATLAB Functions for Intel Platforms
Improve the execution speed of the generated code using Intel® SSE and Intel AVX technology. - Optimize Loops in Generated Code
Generate code with loop transformations according to your performance requirements.
函数调用
- Avoid Data Copies of Function Inputs in Generated Code
Generate code that passes input arguments by reference. - Control Inlining to Fine-Tune Performance and Readability of Generated Code
Inlining eliminates the overhead of function calls but can produce larger C/C++ code and reduce code readability. - Fold Function Calls into Constants
Reduce execution time by replacing expression with constant in the generated code.
数值边缘情形
- Disable Support for Integer Overflow or Nonfinites
Improve performance by suppressing generation of supporting code to handle integer overflow or nonfinites.
外部代码集成
- LAPACK Calls in Generated Code
LAPACK function calls improve the execution speed of code generated for certain linear algebra functions. - BLAS Calls in Generated Code
BLAS function calls improve the execution speed of code generated for certain low-level vector and matrix operations. - Optimize Generated Code for Fast Fourier Transform Functions
Choose the correct fast Fourier transform implementation for your workflow and target hardware. - Integrate External/Custom Code
Improve performance by integrating your own optimized code. - Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls
Generate LAPACK calls for certain linear algebra functions. Specify LAPACK library to use. - Speed Up Matrix Operations in Generated Standalone Code by Using BLAS Calls
Generate BLAS calls for certain low-level matrix operations. Specify BLAS library to use. - Speed Up Fast Fourier Transforms in Generated Standalone Code by Using FFTW Library Calls
Generate FFTW library calls for fast Fourier transforms. Specify the FFTW library. - Synchronize Multithreaded Access to FFTW Planning in Generated Standalone Code
Implement FFT library callback class methods and provide supporting C code to prevent concurrent access to FFTW planning.
疑难解答
诊断 parfor
循环的代码生成错误。
Resolve Issue: coder.inline("never") and coder.nonInlineCall Do Not Prevent Function Inlining
Troubleshoot instances of coder.inline('never')
not
preventing inlining.
MEX Generated on macOS Platform Stays Loaded in Memory
Troubleshoot issues that occur when the source MATLAB® code contains global or persistent variables that are reachable
from the body of a parfor
-loop.