Benefit of non-virtual output port vs. virtual output port in codegen

21 次查看(过去 30 天)
Hi,
Reading through this document, https://www.mathworks.com/help/simulink/ug/ensure-outport-is-virtual.html, my understanding is that for the case of conditional subsystems with a merge block, setting output port to be virtual is a must. Then my questions are
  • If non-virtual signal results in a hidden Signal Copy block, which will double the memory, what is the benefit of non-virtual signal when using with codegen?
  • Is my understanding correct that if we have an implementation below with Vector2 is virtual, there won't be additional allocated memory (temporary variable) nor assignment for every element of the vector performed and vice versa for the case of non-virtual?
Thanks,

回答(1 个)

Ashok
Ashok 2024-11-26,8:44
Hey @John,
The discrepancy in output when using a non-virtual 'Outport' with a 'Merge' block, as described in the below page, highlights an extreme case of incorrect block type usage: 
Choosing between virtual and non-virtual blocks generally involves balancing performance and readability.
  • Non-virtual blocks actively participate in the simulation and altering them changes model behaviour.
  • Virtual blocks, on the other hand, help organize the model graphically and don't participate in the simulation, but they can affect the execution order of other blocks.
As mentioned in the query, if an outport and inport are directly connected with the outport set as virtual, the following error may occur during code generation.
However, the understanding of virtual and non-virtual outports mentioned in the query is mostly correct. Here's a simple example demonstrating their effect on generated code. Using the 'ex_conditional_write.slx' model from the linked documentation, an outport is added instead of the scope.
Generating C-code using the ‘Embedded Coderreveals differences based on whether the outport is virtual or non-virtual.
Non-virtual Outport:
Virtual Outport:
The generated code shows that the additionalex_conditional_write_copy_B.Merge variable is eliminated when the 'Ensure outport is virtual' option is enabled. This avoids extra memory allocation but requires careful attention to identify the 'Merge' block output, as no variable with the keyword 'Merge' is present.
For more details on memory usage in generated code, run the following command in the MATLAB command window: 
web(fullfile(docroot, 'ecoder/memory-usage.html'))
  2 个评论
John
John 2024-12-1,1:55
Hi @Ashok,
Thanks for the reply. From your experience, was there any case you deliberately wanted a non-virtual output for your model algorithm?
Ashok
Ashok 2024-12-4,9:40
Hi @John,
There was a situation where replacing virtual blocks with non-virtual ones was necessary for better readability, making it easier for those working downstream to understand the code. As model complexity grows and some team members work directly with the code without viewing the Simulink model, opting for non-virtual blocks over virtual ones can become important.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Component-Based Modeling 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by