Why is redundant code generated for user defined S-function blocks?

I am using 2 identical instances of a subsystem both defined as Atomic and Reusable that contain a dynamic lookup table as a user defined S-Function block. Why does Simulink Coder still generate 2 sets of code for the subsystem and does not reuse the C++ function generated for the two subsystems?

 Accepted Answer

Simulink Coder uses a checksum to determine whether subsystems are identical and reusable: https://www.mathworks.com/help/rtw/ug/generate-reentrant-code-from-subsystems-0c8c534ae90b.html.
Follow the workflow in the following documentation to check which checksum is not equal:
If the checksums for the "BlockFullPath" identifier are unequal and preventing code reuse, the following is an explanation and workaround:
 
WORKAROUND (1)
 
When the sample times of the input signals to the S-Function block of the dynamic Lookup Table (LUT) are constant (including inf), Simulink Coder performs an optimized lookup using the block's full path. Hence, the "BlockFullPath" checksums are compared and since the two subsystems would have different/unique names, the checksums will not be unequal. Thereby, preventing code reuse.
To prevent Simulink Coder from comparing the block full path for code-reuse, the sample times for the input signals to the S-function block need to be set to non-constant (-1, inherited). This change in sample time can be made at the source blocks driving the input signals of the S-Function blocks (here, dynamic LUTs). This prevents Simulink Coder from optimizing from considering the "BlockFullPath" for code generation optimization and code is reused.
WORKAROUND (2)
The root cause of the issue preventing code reusability is the look-up optimization performed by codegen for dynamic LUTs. Specifically, it uses an input value based algorithm that uses tunable parameters for optimization. To avoid using dynamic LUTs on which Simulink Coder performs such optimization, one can use the following alternatives to achieve the same workflow:
  1. Prelookup_InterpND blocks combination: have always supported ‘Table’ and ‘breakpoints’ fed into ports.
  2. N-D Lookup Table: started supporting ‘Table’ and ‘breakpoints’ via ports in and after R2020b.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!