Hello,
I understand that you are looking for ways to optimize the number of computations in the generated code from a Simulink model.
Virtual or non-atomic subsystems are used to modularize and encapsulate sections of the model. These are simply a visual convenience. Simulink treats these subsystems as if all the blocks existed at the same level. Atomic subsystems let you control the execution of the model. It forces Simulink to run all the blocks in that subsystem as if it were its own separate "function". Using atomic subsystems for code generation encapsulates the corresponding code into a function and can help in modularity but this might not have any effect on the amount of computation as the underlying algorithm and the complexity of the code remains same.
For a MATLAB code vectorizing is computationally efficient over doing calculations individually for each element. This is because by vectorization the usage of loops and doing same operations multiple times can be avoided and this can reduce run time significantly. if variable “a” is an array the required calculation can be done as follows in MATLAB:
a=1:1:30;
%Normal implementation
b1=zeros(size(a));
for i=1:length(a)
b1(i)=3*a(i);
end
b1
%Vectorized implementation
b2=a*3;
b
This prevents the use of for loops and is efficient over indexing each element of “a” and doing multiplication.
But for the generated C code from code generation, there is always a use of “for” loop because there is no separate data type for arrays in C. Hence there might be no difference between the two methods in terms of computation. In the above example if code is generated for both the cases, the code is like what is shown below:
int i;
for (i = 0; i < 30; i++) {
b[i] = a[i] * 3.0;
}
Both the implementations will give a code that has “for” loop and runs in O(n) with same number of computations.
There are several ways to optimize the number of computations in the generated code. Some of them include simplifying the model, using subsystems, adjusting configuration parameters, adjusting solver settings or code generation settings to optimize trade-off between accuracy and speed, using appropriate data types, target specific optimization, etc.
You can refer to the below documentation to find out more methods to optimize the generated C code from Simulink:
- https://www.mathworks.com/help/ecoder/execution-speed.html#:~:text=Optimizations%20that%20Improve%20Execution%20Efficiency
- https://www.mathworks.com/support/search.html/answers/689084-how-to-improve-the-runtime-performance-of-my-simulink-model-s-externally-deployed-generated-code.html
- https://www.mathworks.com/help/ecoder/performance.html
- https://www.mathworks.com/help/simulink/slref/subsystem.html