Clear Filters
Clear Filters

Simulink 연산량 최적화

2 views (last 30 days)
hyoungjong wi
hyoungjong wi on 4 Jul 2022
Edited: hyoungjong wi on 16 Jan 2024
안녕하세요
Simulink 모델을 C로 auto code generation 하여 사용하고 있는데
simulink 모델 개발 시 연산량을 최적화 하는 방법이 있는지 문의드리고자 문의 남깁니다
  1. subsystem 설정 방식 : automic 설정 또는 Library or reference model 화 하면 c코드로 내렸을 때 연산량 향상을 기대할 수 있는지
  2. 연산 방식 : vector로된 신호를 연산하는게 나은지 신호를 쪼개 각 신호별로 따로 연산하는것이 나은지 ex) a* 3 = b 이때 a=[1,2,3,...,30] 이라고 할때 어떤 방식으로 연산하는것이 나은지
  3. 연산시 데이터 타입 맞추고, dead logic 삭제하고, constant folding, subsystem은 flattening, 삼각함수 사용하기 보다는 테이블 사용하고 있긴한데 추가적으로 어떤 방법으로 연산량을 최적화 가능할지 문의드립니다.

Accepted Answer

sai charan sampara
sai charan sampara on 16 Jan 2024
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
b1 = 1×30
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90
%Vectorized implementation
b2=a*3;
b
b2 = 1×30
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90
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:

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!