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

조회 수: 21 (최근 30일)
John
John 2024년 11월 19일
댓글: Ashok 2024년 12월 4일 9:40
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