Documentation

Inline Numeric Values of Block Parameters

This example shows how to optimize the generated code by inlining the numeric values of block parameters.Block parametersinclude theGainparameter of a Gain block and the table data and breakpoint sets of an n-D Lookup Table block.

This optimization determines whether numeric block parameters occupy global memory in the generated code. The optimization can:

  • Improve execution speed.

  • Reduce RAM and ROM consumption.

Explore Example Model

Open the example modelrtwdemo_paraminlineand configure it to show the generated names of blocks.

load_system('rtwdemo_paraminline') set_param('rtwdemo_paraminline','HideAutomaticNames','off') open_system('rtwdemo_paraminline')

The model contains blocks that have these numeric parameters:

  • TheGainparameters of the Gain blocks

  • TheConstant valueparameters of the Constant blocks

  • The table data and breakpoint sets of the n-D Lookup Table blocks

的输出块G2,鼓风机的输出cks upstream of G2, change only if you tune the values of the block parameters during simulation or during code execution. When you update the model diagram, these blocks and signal lines appear magenta in color.

几个街区useSimulink.Parameterobjects in the base workspace to set the values of their parameters. The parameter objects all use the storage classAuto, which means that you can configure the generated code to inline the parameter values.

Generate Code Without Optimization

Create a temporary folder for the build and inspection process.

currentDir = pwd; [~,cgDir] = rtwdemodir();

通过设置禁用优化Configuration Parameters > Default parameter behaviortoTunable.

set_param('rtwdemo_paraminline','DefaultParameterBehavior','Tunable')

Generate code from the model.

rtwbuild('rtwdemo_paraminline')
### Starting build procedure for model: rtwdemo_paraminline ### Successful completion of build procedure for model: rtwdemo_paraminline

In the code generation report, view the source filertwdemo_paraminline_data.c. The code defines a global structure that contains the block parameter values. Each block parameter in the model, such as a lookup table array, breakpoint set, or gain, appears as a field of the structure.

cfile = fullfile(...cgDir,'rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline_data.c'); rtwdemodbtype(cfile,'/* Block parameters (default storage) */','};', 1, 1);
/* Block parameters (default storage) */ P_rtwdemo_paraminline_T rtwdemo_paraminline_P = { /* Variable: MAX_LIFT * Referenced by: '/Constant' */ 10.0, /* Variable: SLIDER_POS * Referenced by: '/Constant1' */ 0.0, /* Variable: T1Break * Referenced by: '/1D Lookup' */ { -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }, /* Variable: T1Data * Referenced by: '/1D Lookup' */ { -1.0, -0.99, -0.98, -0.96, -0.76, 0.0, 0.76, 0.96, 0.98, 0.99, 1.0 }, /* Variable: T2Break * Referenced by: '/2D Lookup' */ { 1.0, 2.0, 3.0 }, /* Variable: T2Data * Referenced by: '/2D Lookup' */ { 4.0, 16.0, 10.0, 5.0, 19.0, 18.0, 6.0, 20.0, 23.0 }, /* Expression: 2 * Referenced by: '/G1' */ 2.0, /* Expression: -2 * Referenced by: '/G2' */ -2.0, /* Computed Parameter: uDLookup_maxIndex * Referenced by: '/2D Lookup' */ { 2U, 2U } };

You can tune the structure fields during code execution because they occupy global memory. However, at each step of the generated algorithm, the code must calculate the output of each block, including the outputs of the block G2 and the upstream blocks. View the algorithm in the modelstepfunction in the filertwdemo_paraminline.c.

cfile = fullfile(cgDir,'rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c'); rtwdemodbtype(...cfile,'/* Model step function */','/* Model initialize function */',1,0);
/* Model step function */ void rtwdemo_paraminline_step(void) { /* Outport: '/Out1' incorporates: * Constant: '/Constant' * Constant: '/Constant1' * Gain: '/G1' * Gain: '/G2' * Inport: '/In1' * Lookup_n-D: '/1D Lookup' * Lookup_n-D: '/2D Lookup' * Sum: '/Sum' */ rtwdemo_paraminline_Y.Out1 = rtwdemo_paraminline_P.G1_Gain * rtwdemo_paraminline_U.In1 + rtwdemo_paraminline_P.G2_Gain * look2_binlx (rtwdemo_paraminline_P.MAX_LIFT, look1_binlx (rtwdemo_paraminline_P.SLIDER_POS, rtwdemo_paraminline_P.T1Break, rtwdemo_paraminline_P.T1Data, 10U), rtwdemo_paraminline_P.T2Break, rtwdemo_paraminline_P.T2Break, rtwdemo_paraminline_P.T2Data, rtwdemo_paraminline_P.uDLookup_maxIndex, 3U); }

Generate Code with Optimization

SetDefault parameter behaviortoInlined.

set_param('rtwdemo_paraminline','DefaultParameterBehavior','Inlined')

Generate code from the model.

rtwbuild('rtwdemo_paraminline')
### Starting build procedure for model: rtwdemo_paraminline ### Successful completion of build procedure for model: rtwdemo_paraminline

In the code generation report, view the algorithm in the filertwdemo_paraminline.c.

rtwdemodbtype(...cfile,'/* Model step function */','/* Model initialize function */',1,0);
/* Model step function */ void rtwdemo_paraminline_step(void) { /* Outport: '/Out1' incorporates: * Gain: '/G1' * Inport: '/In1' * Sum: '/Sum' */ rtwdemo_paraminline_Y.Out1 = 2.0 * rtwdemo_paraminline_U.In1 + 150.0; }

The code does not allocate memory for block parameters or for parameter objects that use the storage classAuto. Instead, the code generator uses the parameter values from the model, and from the parameter objects, to calculate and inline the constant output of the block G2,150.0. The generator also inlines the value of theGainparameter of the Gain block G1,2.0.

With the optimization, the generated code leaves out computationally expensive algorithmic code for blocks such as the lookup tables. The optimized code calculates the output of a block only if the output can change during execution. For this model, only the outputs of the Inport block In1, the Gain block G1, and the Sum block can change.

Close the model and the code generation report.

bdclose('rtwdemo_paraminline') rtwdemoclean; cd(currentDir)

Preserve Block Parameter Tunability

When you setDefault parameter behaviortoInlined, you can preserve block parameter tunability by creatingSimulink.Parameterobjects for individual parameters. You can configure each object to appear in the code as a tunable field of the global parameter structure or as an individual global variable. You can change parameter values during code execution and interface the generated code with your own handwritten code. For more information, seeCreate Tunable Calibration Parameter in the Generated Code.

Inline Invariant Signals

You can select theInline invariant signalscode generation option (which also places constant values in the generated code) only when you setDefault parameter behaviortoInlined. SeeInline Invariant Signals.

See Also

Related Topics