Optimize Generated Code with the Model Advisor - MATLAB & Simulink (original) (raw)

You can use the Simulink® Model Advisor to help you configure your fixed-point models to achieve a more efficient design and optimize your generated code. To use the Model Advisor to check your fixed-point models:

  1. In the Modeling tab of the model you want to analyze, click .
  2. In the System Selector, select the system to analyze.
  3. In the Model Advisor left pane, expand the By Product node and then the Embedded Coder node.
  4. For fixed-point code generation, the most important check boxes to select areIdentify blocks that generate expensive fixed-point and saturation code, Identify questionable fixed-point operations,Identify blocks that generate expensive rounding code, andCheck the hardware implementation.
    To enable all Model Advisor checks associated with the selected node, select the check box of the folder containing the checks. In our case, select theEmbedded Coder check box.
  5. Click Run checks. Any tips for improving the efficiency of your fixed-point model appear in the Model Advisor window.

The sections that follow discuss fixed-point related checks and sub-checks found in the Model Advisor. These sections explain the checks, discuss their importance in fixed-point code generation, and offer suggestions for tweaking your model to optimize your generated code.

Identify Blocks That Generate Expensive Fixed-Point and Saturation Code

Identify Sum blocks for questionable fixed-point operations

Identify Min Max blocks for questionable fixed-point operations

Identify Discrete Integrator blocks for questionable fixed-point operations

Identify Compare to Constant blocks for questionable fixed-point operations

Identify Lookup Table blocks for questionable fixed-point operations

Efficiency trade-offs related to lookup table data are described in Effects of Spacing on Speed, Error, and Memory Usage. Based on these trade-offs, the Model Advisor identifies blocks where there is potential for efficiency improvements, such as:

For more information on lookup table optimization, see Lookup Table Optimization.

Check optimization and hardware implementation settings

Identify blocks that will invoke net slope computation

When a change of fixed-point slope is not a power of two, net slope computation is necessary. Normally, net slope computation is implemented using an integer multiplication followed by shifts. Under some conditions, an alternate implementation requires just an integer division by a constant. One of the conditions is that the net slope can be very accurately represented as the reciprocal of an integer. When this condition is met, the division implementation produces more accurate numerical behavior. Depending on your compiler and embedded hardware, the division implementation might be more desirable than the multiplication and shifts implementation. The generated code might be more efficient in either ROM size or model execution size.

The Model Advisor alerts you when:

For more information, see Net Slope Computation.

Identify product blocks that are less efficient

The number of multiplications and divisions that a block performs can have a significant impact on accuracy and efficiency. The Model Advisor detects some, but not all, situations where rearranging the operations can improve accuracy, efficiency, or both.

One such situation is when a calculation using more than one division operation is computed. A general guideline from the field of numerical analysis is to multiply all the denominator terms together first, then do one and only one division. This improves accuracy and often speed in floating-point and especially fixed-point. This can be accomplished in Simulink by cascading Product blocks. Note that multiple divisions spread over a series of blocks are not detected by the Model Advisor.

Another situation is when a single Product block is configured to do more than one multiplication or division operation. This is supported, but if the output data type is integer or fixed-point, then better results are likely if this operation is split across several blocks each doing one multiplication or one division. Using several blocks allows the user to control the data type and scaling used for intermediate calculations. The choice of data types for intermediate calculations affects precision, range errors, and efficiency.

Check for expensive saturation code

Setting the Saturate on integer overflow parameter can produce condition checking code that your application might not require.

Check whether your application requires setting Block Parameters > Signal Attributes > Saturate on integer overflow. Otherwise, clear this parameter for the most efficient implementation of the block in the generated code.

Identify Questionable Fixed-Point Operations

This check identifies blocks that generate multiword operations, cumbersome multiplication and division operations, expensive conversion code, inefficiencies in lookup table blocks, and expensive comparison code.

Check for multiword operations

When an operation results in a data type larger than the largest word size of your processor, the generated code contains multiword operations. Multiword operations can be inefficient on hardware. To prevent multiword operations, adjust the word lengths of inputs to operations so that they do not exceed the largest word size of your processor. For more information on controlling multiword operations in generated code, see Fixed-Point Multiword Operations in Generated Code.

Check for expensive multiplication code

Check for expensive division code

The rounding behavior of signed integer division is not fully specified by C language standards. Therefore, the generated code for division is too large to provide bit-true agreement between simulation and code generation. To avoid integer division generated code that is too large, in the Configuration Parameters dialog box, on the Hardware Implementation pane, set the Signed integer division rounds to parameter to the recommended value.

Identify lookup blocks with uneven breakpoint spacing

Efficiency trade-offs related to lookup table data are described in Effects of Spacing on Speed, Error, and Memory Usage. Based on these trade-offs, the Model Advisor identifies blocks where there is potential for efficiency improvements, and issues a warning when:

Check for expensive pre-lookup division

For a Prelookup or n-D Lookup Table block,Index search method is Evenly spaced points. Breakpoint data does not have power of 2 spacing.

If breakpoint data is nontunable, it is recommended that you adjust the data to have even, power of 2 spacing. Otherwise, in the block parameter dialog box, specify a different Index search method to avoid the computation-intensive division operation.

Check for expensive data type conversions

When a block is configured such that it would generate inefficient code for a data type conversion, the Model Advisor generates a warning and makes suggestions on how to make your model more efficient.

Check for fixed-point comparisons with predetermined results

When you select isInf, isNaN, orisFinite as the operation for the Relational Operator block, the block switches to one-input mode. In this mode, if the input data type is fixed point, boolean, or a built-in integer, the output is FALSE for isInf and isNaN, TRUE for isFinite. This might result in dead code which will be eliminated by Simulink Coder™.

Check for expensive binary comparison operations

Check for expensive comparison code

When your model is configured such that the generated code contains expensive comparison code, the Model Advisor generates a warning.

Check for expensive fixed-point data types in generated code

When a design contains integer or fixed-point word lengths that do not exist on your target hardware, the generated code can contain extra saturation code, shifts, and multiword operations. By changing the data type to one that is supported by your target hardware, you can improve the efficiency of the generated code. The Model Advisor flags these expensive data types in your model. For example, the Model Advisor would flag a fixed-point data type with a word length of 17 if the target hardware was 32 bits.

Identify Blocks That Generate Expensive Rounding Code

This check alerts you when rounding optimizations are available. To check for blocks that generate expensive rounding code, the Model Advisor performs the following sub-checks:

Traditional user-written code, especially for control applications, almost always uses “no effort” rounding. For example, for unsigned integers and two's complement signed integers, shifting right and dropping the bits is equivalent to rounding to floor. To get results comparable to, or better than, what you expect from traditional user-written code, use the simplest rounding mode. In general the simplest mode provides the minimum cost solution with no overflows. If the simplest mode is not available, round to floor.

The primary exception to this rule is the rounding behavior of signed integer division. The C standard leaves this rounding behavior unspecified, but for most production targets the “no effort” mode is to round to zero. For unsigned division, everything is non-negative, so rounding to floor and rounding to zero are identical. To improve rounding efficiency, set Model Configuration Parameters > Hardware Implementation > Device details > Signed integer division rounds to using the mode that your production target uses.

Use the Integer rounding mode parameter on your model's blocks to simulate the rounding behavior of the C compiler that you use to compile code generated from the model. This setting appears on the Signal Attributes pane of the parameter dialog boxes of blocks that can perform signed integer arithmetic, such as theProduct block. To obtain the most efficient generated code, change theInteger rounding mode parameter of the block to the recommended setting.

For more information on properties to consider when choosing a rounding mode, see Choosing a Rounding Method.