How to avoid duplicate functions in generated code with embedded coder?

9 views (last 30 days)
The simulink model control_model has a certain library lib_i_boolean which is set to by a reusable function (subsystem properties -> treat as atomic unit, code generation function packaging -> reusable function). This library is used dozens of times in control_model.
Now, the embedded coder generates the following files:
  • control_model_lib_i_boolean.c
  • control_model_lib_i_boolean.h
  • control_model_lib_i_boolean_d.c
  • control_model_lib_i_boolean_d.h
  • control_model_lib_i_boolean_f.c
  • control_model_lib_i_boolean_f.h
  • control_model_lib_i_boolean_g.c
  • control_model_lib_i_boolean_g.h
  • control_model_lib_i_boolean_i.c
  • control_model_lib_i_boolean_i.h
  • control_model_lib_i_boolean_m.c
  • control_model_lib_i_boolean_m.h
  • control_model_lib_i_boolean_n.c
  • control_model_lib_i_boolean_n.h
We want to avoid these duplicate files, so only have
  • control_model_lib_i_boolean.c
  • control_model_lib_i_boolean.h
The internet provided a half answer, see: https://nl.mathworks.com/help/coder/ug/avoid-duplicate-functions-in-the-generated-code.html , which seems to be applicable for matlab code only?
We already did the following:
  • Checked number of input or output variables --> always 6 inputs and 5 outputs, although some outputs are connected to terminators
  • Checked type of input or output variables --> always the same datatype (5 boolean inputs, 1 single)
  • Checked size of input or output variables --> always the same size (scalar, dimension 1)
  • Checked values of input variables --> different constants are connected to the inputs
  • Searched the model configuration for an "coder.ignoreConst" parameter
We're suspecting it has to do with using different constants as inputs and/or output terminators, so the compiler wants to optimize the function reuse. We want to tell the compiler to stop doing that, or change the simulink model such that the compiler stops doing this.
We're using embedded coder in R2016b, R2019b and R2020b.
Thanks for the help, much appreciated.

Answers (3)

Jesús Zambrano
Jesús Zambrano on 4 Aug 2021
Apart from setting the function packaging to 'Reusable function', could you set the Function name option to 'User specified' and then set Function name as you prefer. Set this to all the exact copies of your subsystem in the model.
Hope it helps.
  1 Comment
Gregor Gombac
Gregor Gombac on 31 Jan 2024
For me even this is absolutely useless.
I still get multiple functions, maybe inside a single file ok, but I still get multiple functions and this is absolutely against my requirements of reducing program memory.
The only two solutions I found are:
  • to set the default parameter behaviour as Tunable instead of Inlined:
  • to define the constant as a Simulink Parameter and its storage class to a not inlined class like const, and I have to do this every single time I generate the code, cause the Coder fails if I do not delete all its cache and I don't restart Simulink every single time.
All of this is such a pain guys, is it possible?
Thanks

Sign in to comment.


Jesús Zambrano
Jesús Zambrano on 31 Jan 2024
The get the most out of the reusability, the blocks you want to use the reusability feature should be identical copies.
  1 Comment
Gregor Gombac
Gregor Gombac on 31 Jan 2024
They are identical functions, copied just before starting Code Generation.
Teh oinly thing that changes is that one of the two has a constant at one of the inputs, and Default parameter behaviour is set to Inline in Embedded Coder Configuration Parameters.
I hate that you are assuming me doing something wrong immidiately.
Gregor

Sign in to comment.


Rene Vugts
Rene Vugts on 1 Feb 2024
Apologies for late reply, only a few years later haha.
We ended up setting the function packaging to "Auto" and let the compiler do it's thing. In that case, these "control_model_lib_i_boolean_X.Y" are not generated, obviously. I did not try to find the code of this library in the generated code, but this would be interesting.
"Apart from setting the function packaging to 'Reusable function', could you set the Function name option to 'User specified' and then set Function name as you prefer. Set this to all the exact copies of your subsystem in the model."
This is what we did already, the result is all these "control_model_lib_i_boolean_X.Y" that you see above. In Simulink it's a library block, so exactly the same subsystem, with the function name set to "control_model_lib_i_boolean" in the library.
  1 Comment
Gregor Gombac
Gregor Gombac on 1 Feb 2024
Ah ok. It's just disappointing that we have to find workarounds to such a big oversight by the Embedded Coder.
"We ended up setting the function packaging to "Auto" and let the compiler do it's thing. In that case, these "control_model_lib_i_boolean_X.Y" are not generated, obviously. I did not try to find the code of this library in the generated code, but this would be interesting."
Since I'm using the exported functions one by one in my final deployment I can not do that. Additionally I plan to the modify and optimize the code by hand, cause the Generated Code is just plain bad for my use case requirements.

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!