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 model BlockReductionOptimization, a Gain block of value 1.0 is in between Inport and Outport blocks.

model = 'BlockReductionOptimization';
open_system(model);

Generate Code

Build the model.

set_param(model,'BlockReduction','off');
slbuild(model)
### Starting build procedure for: BlockReductionOptimization
### Successful completion of build procedure for: BlockReductionOptimization

Build Summary

Top model targets built:

Model                       Action                        Rebuild Reason                                    
============================================================================================================
BlockReductionOptimization  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 8.8197s

This code is the code from BlockReductionOptimization.c.

cfile = fullfile('BlockReductionOptimization_ert_rtw','BlockReductionOptimization.c');
coder.example.extractLines(cfile, '/* Model step function */',...
    '/* Model initialize function */', 1, 0);
/* Model step function */
void BlockReductionOptimization_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/In1'
   */
  BlockReductionOptimization_Y.Out1 = BlockReductionOptimization_U.In1;
}

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. If the Block reduction check box is not selected, select it.

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: BlockReductionOptimization
### Successful completion of build procedure for: BlockReductionOptimization

Build Summary

Top model targets built:

Model                       Action                        Rebuild Reason                   
===========================================================================================
BlockReductionOptimization  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.4239s

Here is the optimized code from BlockReductionOptimization.c.

cfile = fullfile('BlockReductionOptimization_ert_rtw','BlockReductionOptimization.c');
coder.example.extractLines(cfile, '/* Model step function */',...
    '/* Model initialize function */', 1, 0);
/* Model step function */
void BlockReductionOptimization_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/In1'
   */
  BlockReductionOptimization_Y.Out1 = BlockReductionOptimization_U.In1;
}

Because multiplying the input signal by a value of 1.0 does not impact computational results, the code generator excludes the Gain block from the generated code. Close the model.

bdclose(model)

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 parameter Ensure outport is virtual is 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 use ssSetBlockReduction to 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