Simulink generated code optimization - CPU Load

21 次查看(过去 30 天)
Hi all,
I'm successfully generating code from the Simulink model. This code substitute manual one with the same function. Code is the integrated with the rest of the manual code, flashed to the HW and running. This substituting of the manual code part with the generated one increses the CPU Load. When I'm checking the Code generation setting, basic targets are RAM minimazing or Execution speed mazimazing. Non of these will directly reduce the CPU Load I suppose.
So I'd like to ask you, do you know what Advanced or Detailed Code generation Optimization setting can reduce the CPU Load? I'm attaching screenshots of the current setting.
And possibly follow up question - is there any metric which can estimate affect on the CPU Load without need of flashing the integrated code to the HW?
Thank you for your help in advance, have a nice day!
Oldrich

采纳的回答

Raghava S N
Raghava S N 2024-8-2
Hi Oldrich,
The increase in CPU load when substituting manual code with generated code from the Simulink model can arise due to the inherent complexity and computational requirements of the generated code. The following steps can be taken to reduce the CPU load:
Simplifying the complex subsystems and removing unnecessary blocks will reduce the overall computational load. This can significantly decrease the CPU usage.
Some code generation settings can be helpful, like “Default parameter behavior”, which when set to “Inlined” can help reduce overhead by embedding parameter values directly in the code. This option can be found in the path: Configuration Parameters > Code Generation > Optimization > Default parameter Behavior. For more details on the “Inlined” parameter, please refer to this link - https://www.mathworks.com/help/rtw/ref/defaultparameterbehavior.html
Another option is to lower the “Loop unrolling threshold”, which can help improve execution speed by reducing the overhead of loop control. This option can be found in the path: Configuration Parameters > Optimization > Loop unrolling threshold. For more details on the “Loop unrolling threshold” parameter, please refer to this link - https://www.mathworks.com/help/rtw/ref/loopunrollingthreshold.html
Also ensure that the “Block reduction” option is checked under Configuration Parameters > Simulation Target > Advanced parameters > Block reduction. This removes redundant blocks during code generation. For more information on the “Block reduction” parameter, please refer to this link - https://www.mathworks.com/help/simulink/gui/blockreduction.html
Please refer to this MATLAB Answer by a staff member regarding handling CPU load for generated code - https://www.mathworks.com/matlabcentral/answers/1984294-how-to-use-more-cpu-in-simulink-for-a-power-system-simulation
Regarding your follow-up question, estimating the CPU load can be done using a software-in-the-loop (SIL), processor-in-the-loop (PIL), or XCP-based external mode simulation that produces execution-time metrics for your generated code. Please refer to this documentation link for a detailed description of the workflow involved in using these methods - https://www.mathworks.com/help/ecoder/ug/view-and-compare-code-execution-times.html
I hope this helps!
Raghava
  1 个评论
Oldrich
Oldrich 2024-8-14
Thank you very much for valuable comments. I'll check your proposals.
Thank you, have a nice day!
Oldrich

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by