向量算子优化Vector Operation Optimization
查看MATLAB命令View MATLAB Command
示例显示Simulink®编码器™ ,将生成向量的块输出,设置为标量,优化生成的代码,例如Mux、Sum、Gain和Bus。这种优化通过用局部变量替换临时局部数组来减少堆栈内存。
示例模型Example Model
模型采用矢量优化rtwdemo_VectorOptimization,增益块G1和G2的输出为矢量信号tmp1和tmp2。向量的宽度为10。
model = 'rtwdemo_VectorOptimization';
open_system(model);
set_param(model, 'SimulationCommand', 'update')
Generate Code
为生成和检查过程,创建临时文件夹(在系统临时文件夹中)。
currentDir = pwd;
[~,cgDir] = rtwdemodir();
Build the model.
rtwbuild(model)
### Starting build procedure for: rtwdemo_VectorOptimization
### Successful completion of build procedure for: rtwdemo_VectorOptimization
Build Summary
Top model targets built:
Model Action Rebuild Reason
===========================================================================================================
rtwdemo_VectorOptimization Code generated and compiled Code generation information file does not exist.
1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 25.92s
The optimized code is in rtwdemo_VectorOptimization.c. The signals tmp1 and tmp2 are the local variables rtb_tmp1 and rtb_tmp2.
cfile = fullfile(cgDir,'rtwdemo_VectorOptimization_grt_rtw',...
'rtwdemo_VectorOptimization.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void rtwdemo_VectorOptimization_step(void)
{
real_T rtb_Sum3;
real_T rtb_tmp1;
real_T rtb_tmp2;
int32_T i;
for (i = 0; i < 10; i++) {
/* Gain: '<Root>/G2' incorporates:
* UnitDelay: '<Root>/X2'
*/
rtb_tmp2 = 0.3 * rtwdemo_VectorOptimization_DW.X2_DSTATE[i];
/* Gain: '<Root>/G1' incorporates:
* UnitDelay: '<Root>/X1'
*/
rtb_tmp1 = 0.2 * rtwdemo_VectorOptimization_DW.X1_DSTATE[i];
/* Sum: '<Root>/Sum3' incorporates:
* Gain: '<Root>/G3'
* Inport: '<Root>/In2'
* Sum: '<Root>/Sum1'
* Sum: '<Root>/Sum2'
* UnitDelay: '<Root>/X3'
*/
rtb_Sum3 = ((rtwdemo_VectorOptimization_U.In2[i] - 0.4 *
rtwdemo_VectorOptimization_DW.X3_DSTATE[i]) - rtb_tmp2) -
rtb_tmp1;
/* Outport: '<Root>/Out2' */
rtwdemo_VectorOptimization_Y.Out2[i] = rtb_Sum3;
/* Update for UnitDelay: '<Root>/X3' */
rtwdemo_VectorOptimization_DW.X3_DSTATE[i] = rtb_tmp2;
/* Update for UnitDelay: '<Root>/X2' */
rtwdemo_VectorOptimization_DW.X2_DSTATE[i] = rtb_tmp1;
/* Update for UnitDelay: '<Root>/X1' */
rtwdemo_VectorOptimization_DW.X1_DSTATE[i] = rtb_Sum3;
}
}
关闭模型和代码生成报告
bdclose(model)
rtwdemoclean;
cd(currentDir)