执行速度
提高生成的 C/C++ 代码的执行速度
利用代码生成选项和优化来提高所生成代码的执行速度。您可以修改或禁用可能会影响执行速度的动态内存分配。可以使用 parfor
循环生成并行代码。如果可用,可以利用预先存在的经过优化的 C 代码和专用库来加快执行速度。
有关如何针对特定条件优化代码的详细信息,请参阅优化生成的 C/C++ 和 MEX 代码。
函数
类
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 | 用于指定生成代码中 FFTW 调用的 FFTW 库的抽象类 |
coder.loop.Control | Customize loop optimizations in generated code (自 R2023a 起) |
主题
生成代码优化
- 优化生成的 C/C++ 和 MEX 代码
优化生成的 C/C++ 和 MEX 代码的执行速度或内存使用量。 - MATLAB Coder 对生成代码进行优化
代码生成器使用优化来提高生成代码的性能。 - 优化生成代码中的隐式扩展
默认情况下,生成代码中的隐式扩展是启用的。代码生成器在生成的代码中引入修改以执行隐式扩展。生成的代码中的更改可能导致额外的代码来扩展操作数。操作数的扩展可能会影响生成代码的性能。请参阅生成启用隐式扩展的代码。
可变大小数组
- 动态内存分配与性能
动态内存分配可能会降低执行速度。 - Provide Maximum Size for Variable-Size Arrays
Use techniques to help the code generator determine the upper bound for a variable-size array. - 在代码生成期间禁用动态内存分配
在 App 或命令行中禁用动态内存分配。 - 设置动态内存分配阈值
对小于特定大小的数组禁用动态内存分配。 - 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. - Specify Maximum Number of Threads in parfor-Loops
Generate a MEX function that executes loop iterations in parallel on specific number of available cores. - 指定在生成的代码中运行并行 for 循环的最大线程数
在生成的代码中,在特定数量的可用核上运行并行for
循环。 - 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. - Automatic Parallelization of for-Loops in the Generated Code
MATLAB® Coder™ automatically parallelizesfor
-loops in generated C/C++ code by default, using the Open Multiprocessing (OpenMP) library. - 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.
函数调用
- 避免在生成的代码中存在函数输入的数据副本
生成对输入参量使用按引用传递的代码。 - 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.
数值边缘情形
- 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 调用
BLAS 函数调用可提高为某些低级向量和矩阵运算生成的代码的执行速度。 - 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 of Standalone Generated Code Using Preconfigured BLAS and LAPACK Callbacks
Use BLAS and LAPACK calls to improve the execution speed of generated 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.