Main Content

Remove Code for Blocks That Have No Effect on Computational Results

This example shows how the code generator optimizes generated code by removing code that has no effect on computational results. This optimization:

  • Increases execution speed.

  • Reduces ROM consumption.

Example

In the modelrtwdemo_blockreduction, a Gain block of value1.0is in between Inport and Outport blocks.

model ='rtwdemo_blockreduction'; open_system(model);

Generate Code

Create a temporary folder for the build and inspection process.

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

Build the model.

set_param(model,'BlockReduction','off'); slbuild(model)
### Starting build procedure for: rtwdemo_blockreduction ### Successful completion of build procedure for: rtwdemo_blockreduction Build Summary Top model targets built: Model Action Rebuild Reason ======================================================================================================== rtwdemo_blockreduction 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 10.165s

This code is the code fromrtwdemo_blockreduction.c.

cfile = fullfile(cgDir,'rtwdemo_blockreduction_ert_rtw','rtwdemo_blockreduction.c'); rtwdemodbtype(cfile,'/* Model step function */',...'/* Model initialize function */', 1, 0);
/* Model step function */ void rtwdemo_blockreduction_step(void) { /* Outport: '/Out1' incorporates: * Gain: '/Gain' * Inport: '/In1' */ rtwdemo_blockreduction_Y.Out1 = 1.0 * rtwdemo_blockreduction_U.In1; }

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. If theBlock reduction复选框没有被选中,选中它。

Alternatively, use the command-line API to enable the optimization.

set_param(model,'BlockReduction','on');

Generate Code with Optimization

slbuild(model)
### Starting build procedure for: rtwdemo_blockreduction ### Successful completion of build procedure for: rtwdemo_blockreduction Build Summary Top model targets built: Model Action Rebuild Reason ======================================================================================= rtwdemo_blockreduction Code generated and compiled. Generated code was out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 8.8193s

Here is the optimized code fromrtwdemo_blockreduction.c.

cfile = fullfile(cgDir,'rtwdemo_blockreduction_ert_rtw','rtwdemo_blockreduction.c'); rtwdemodbtype(cfile,'/* Model step function */',...'/* Model initialize function */', 1, 0);
/* Model step function */ void rtwdemo_blockreduction_step(void) { /* Outport: '/Out1' incorporates: * Inport: '/In1' */ rtwdemo_blockreduction_Y.Out1 = rtwdemo_blockreduction_U.In1; }

Because multiplying the input signal by a value of1.0does not impact computational results, the code generator excludes the Gain block from the generated code. Close the model and clean up.

bdclose rtwdemoclean(模型);cd (currentDir)

Limitations

The code generator cannot remove blocks under these circumstances:

  • The block output is configured as a test point or has a storage class and the block input and output have different data types, complexities, or widths.

  • The block has a nonconstant sample time, has an output connected to a root outport, and has a source that has a constant sample time.

  • The block has multiple sources, some of which have constant sample times and some of which have nonconstant sample times.

  • The block output has a storage class and has a source that is muxed, is a virtual bus, is connected to a root outport, is configured as a test point, or has a storage class.

  • The block has an output that is connected to the block input.

  • The block is inside an Initialize Function subsystem, Reset Function subsystem, or Terminate Function subsystem whose output is connected to an outport of the subsystem.

  • The block has an output that is connected to a Subsystem Outport block that has an initial condition.

  • The block has an output that is connected to an outport of an Initialize Function subsystem, Reset Function subsystem, or Terminate Function subsystem.

  • The block has an output that is connected to an outport for which the parameterEnsure outport is virtualis selected.

  • The block has an output that is connected to an outport that has an initial condition parameter.

  • The block has an output that is connected to a Merge, a Vector Concatenate, or a Matrix Concatenate block.

  • The block has tunable run-time parameters.

  • The block is inside a reusable subsystem whose output is connected to a block outside the reusable subsystem.

  • The block has a continuous state.

  • The block has a discrete state and the states are logged or have a nonauto storage class.

  • The block is an S-Function, unless you usessSetBlockReductionto enable block reduction.

  • The block output is configured as a test point or has a storage class and its source is a block inside a different conditional subsystem.

See Also

Related Topics