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

33 views (last 30 days)
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,

Answers (1)

Ashok
Ashok on 26 Nov 2024 at 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'))

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!