- 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.
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,
댓글 수: 0
답변 (1개)
Ashok
2024년 11월 26일 8:44
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.
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 Coder’ reveals differences based on whether the outport is virtual or non-virtual.
Non-virtual Outport:
Virtual Outport:
The generated code shows that the additional ‘ex_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
Ashok
2024년 12월 4일 9:40
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 Center 및 File Exchange에서 Component-Based Modeling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!