Skip to Main Content Skip to Search
Product Documentation

Optimizing Your Generated Code with the Model Advisor

Introduction

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. From the Tools menu of the model you want to analyze, select Model Advisor. The Model Advisor appears.

  2. In the left pane, expand the By Product node and select Embedded Coder.

  3. From the Model Advisor Edit menu, select Select All to enable all Model Advisor checks associated with the selected node. For fixed-point code generation, the most important check boxes to select are Identify questionable fixed-point operations, Identify blocks that generate expensive saturation and rounding code, and Check the hardware implementation.

  4. Click Run Selected Checks. Any tips for improving the efficiency of your fixed-point model appear in the Model Advisor window.

The sections that follow discuss possible messages that might be returned when you use the Model Advisor check titled Identify questionable fixed-point operations. The sections explain the messages, discuss their importance in fixed-point code generation, and offer suggestions for tweaking your model to optimize the code.

Optimize Lookup Table Data

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. Messages like the following are shown in the browser to alert you to these cases:

Reduce Cumbersome Multiplications

Targeting an Embedded Processor discusses the capabilities and limitations of embedded processors. Design Rules recommends that inputs to a multiply operation should not have word lengths larger than the base integer type of your processor. Multiplication with larger word lengths can always be handled in software, but that approach requires much more code and is much slower. The Model Advisor identifies blocks where undesirable software multiplications are required. Visual inspection of the generated code, including the generated multiplication utility function, will make the cost of these operations clear. It is strongly recommended that you adjust the model to avoid these operations. Messages like the following are shown in the browser to alert you to this situation:

Optimize the Number of Multiply and Divide Operations

The number of multiplications and divisions that a block performs can have a big 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 browser message will identify Product blocks that are doing multiple divisions. Note that multiple divisions spread over a series of blocks are not detected by Model Advisor:

Another situation is when a single Product block is configured to do more than one multiplication or division operation. A browser message will identify Product blocks doing multiple operations:

Reduce Multiplies and Divides with Nonzero Bias

Rules for Arithmetic Operations discusses the implementation details of fixed-point multiplication and division. That section shows the significant increase in complexity that occurs when signals with nonzero biases are involved in multiplication and division. The Model Advisor puts a message in the browser that identifies blocks that require these complicated operations. It is strongly recommended that you make changes to eliminate the need for these complicated operations:

Eliminate Mismatched Scaling

Scaling adjustment is an extremely common operation in fixed-point designs. In the vast majority of cases, shifts left or shifts right are sufficient to handle the scaling adjustment. This occurs when the slope adjustment is an exact power of two, and the bias adjustment term is zero. Situations where shifts are not sufficient to handle scaling adjustments are called mismatched scaling. Cases of mismatched scaling can involve either mismatched slopes or mismatched biases.

For mismatched slopes, it is necessary to multiply by an integer correction term in addition to shifting. The need for this extra multiplication often represents a design oversight. The extra multiplication requires extra code, slows down the speed of execution, and usually introduces additional precision loss. By adjusting the scaling of the inputs or outputs, you can eliminate mismatched slopes. The most efficient designs minimize the number of places where mismatched slopes occur. The need to handle mismatched slopes can occur in many Simulink blocks, including Product, Sum, Relational Operator, and MinMax. A browser message will identify these blocks. The Data Type Conversion block can also face mismatched slopes, but it is assumed that this explicit conversion is intentional, so no Model Advisor messages are issued:

For mismatched bias, it is usually necessary to add or subtract an integer correction term as a separate step in addition to the normal shifting. Like slope mismatch, the need to do this extra addition often represents a design oversight. Except for the Data Type Conversion block, Model Advisor assumes mismatched bias is an oversight. A message such as the following appears in the browser, identifying blocks that could be made more efficient by eliminating mismatched biases:

Minimize Internal Conversion Issues

Many fixed-point operations need to do internal data type and scaling conversions. Fixed-point operations are based upon lower-level operations, such as integer addition and integer comparisons, that require the arguments to have the same data type and scaling. This is why blocks built on these operations, such as Sum, Relational Operator, and MinMax, must do internal conversions. There can be issues related to these internal conversions, such as range errors, that lead to overflows and loss of efficiency. Model Advisor warns separately about these two issues with messages like the following:

For some operations, the need to do an internal conversion can represent a design oversight. The impact of this oversight is a loss of efficiency, and possibly a loss of accuracy. As an example, consider the comparison of a signal against a constant using a Relational Operator block. To compare a fixed-point signal against a constant, the underlying implementation should directly compare the stored integer of the input signal against an invariant stored integer. If the scaling or data type of the signal and constant are different, then it is also necessary to do a conversion operation. This extra conversion work is usually inefficient and is often unexpected. The Model Advisor warns about these situations with messages like the following:

Use the Most Efficient Rounding

How to Specify Rounding for Fixed-Point Operations

Specify rounding options for fixed-point operations by setting a combination of these parameters:

How to Choose the Most Efficient Rounding

Traditional handwritten 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 handwritten 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 nonnegative, so rounding to floor and rounding to zero are identical. To improve rounding efficiency, set Configuration Parameters > Hardware Implementation > Embedded Hardware > Signed integer division rounds to using the mode that your production target uses.

For more information on properties to consider when choosing a rounding mode, see Choosing a Rounding Method in the Fixed-Point Toolbox documentation.

Model Advisor Rounding Mode Checks

Use the Model Advisor to alert you when rounding optimizations are available.

  1. From the Tools menu of the model you want to analyze, select Model Advisor. The Model Advisor appears.

  2. In the left pane, expand the By Product node and select Embedded Coder.

  3. Select Identify blocks that generate expensive saturation and rounding code.

  4. Click Run Selected Checks. Any tips for improving the rounding efficiency of your fixed-point model appear in the Model Advisor window.

The Model Advisor alerts you when rounding optimizations are available.

Optimize Net Slope Correction

When a change of fixed-point slope is not a power of two, net slope correction is necessary. Normally, net slope correction 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.

How the Model Advisor Helps You Optimize Net Slope Correction

The Model Advisor alerts you when:

Example: Using the Model Advisor to Verify that Your Model Configuration is Suitable for Using Integer Division for Net Slope Correction

This example uses the following model.

In this model, the net slope for the Data Type Conversion and Product blocks is 1/1000.

To set up the model:

  1. For the two Inport blocks, U and V, set the Data type to int16.

  2. For the Data Type Conversion block, set the Integer rounding mode to Floor. Set the Output data type to fixdt(1, 33, 1000, 0).

      Note   Setting the Output data type word length greater than the length of the long data type results in multiword operations.

  3. For the Product block, set the Integer rounding mode to Convergent. Set the Output data type to fixdt(1, 16, 1000, 0).

  4. Set the Hardware Implementation > Embedded Hardware > Signed integer division rounds to configuration parameter to Zero.

  5. Select the Optimization > Use integer division to handle net slopes that are reciprocals of integers configuration parameter.

  6. Save the model.

To run the Model Advisor check:

  1. From the model menu, select Tools > Model Advisor.

    The Model Advisor appears.

  2. In the left pane, expand the By Product node and then expand the Embedded Coder node.

  3. Select Identify questionable fixed-point operations.

  4. Click Run This Check.

    The Model Advisor provides warnings that your model configuration is not compatible with the use of division for net slope correction. It also provides recommendations on how to change your model configuration to make it compatible:

    • The Product block is not using the correct rounding mode. Change the Integer rounding mode parameter to Simplest or to match the configuration parameter setting, Hardware Implementation > Embedded Hardware > Signed integer division rounds to.

    • The Data Type Conversion block is not using the correct rounding mode. Change the Integer rounding mode parameter to Simplest or to match the configuration parameter setting, Hardware Implementation > Embedded Hardware > Signed integer division rounds to.

    • The Data Type Conversion block is using multiword operations. Change the word length of the block to avoid multiword operations.

  5. Make the suggested changes:

    1. For the Product and Data Type Conversion blocks, change the rounding mode to Simplest.

    2. For the Data Type Conversion block, change the Output data type from fixdt(1, 33, 1000, 0) to fixdt(1, 16, 1000, 0) to avoid multiword operations.

    3. Save the model.

    This is your model configuration.

  6. Rerun the Model Advisor Identify questionable fixed-point operations check.

    The Model Advisor no longer reports the warnings about rounding mode and multiword operations. Your model configuration is compatible with using integer division to handle net slope correction.

Example: Using the Model Advisor to Detect When to Use Integer Division for Net Slope Correction

This example uses the following model.

In this model, the net slope for the Data Type Conversion and Product blocks is 1/1000.

To set up the model:

  1. For the two Inport blocks, U and V, set the Data type to int16.

  2. For the Data Type Conversion block , set the Integer rounding mode to Simplest. Set the Output data type to fixdt(1, 16, 1000, 0).

  3. For the Product block, set the Integer rounding mode to Simplest. Set the Output data type to fixdt(1, 16, 1000, 0).

  4. Set the Hardware Implementation > Embedded Hardware > Signed integer division rounds to configuration parameter to Zero.

  5. Clear the Optimization > Use integer division to handle net slopes that are reciprocals of integers configuration parameter.

  6. Save the model.

To run the Model Advisor check:

  1. From the model menu, select Tools > Model Advisor.

    The Model Advisor appears.

  2. In the left pane, expand the By Product node and then expand the Embedded Coder node.

  3. Select Identify questionable fixed-point operations.

  4. Click Run This Check.

    The Model Advisor warns that your model configuration is not optimal and provides the following recommendation:

    The Product and Data Type Conversion blocks are not using integer division for net slope correction. Selecting Optimization > Use integer division to handle net slopes that are reciprocals of integers might generate more efficient code.

      Note   The generated code is more efficient only if division is more efficient than multiplication followed by shifts on your target hardware.

  5. Select the Use integer division to handle net slopes that are reciprocals of integers optimization parameter.

  6. Rerun the Model Advisor Identify questionable fixed-point operations check.

    The Model Advisor no longer reports the warning. Your model now uses integer division to handle net slope correction. This configuration results in more efficient code if division is more efficient than multiplication followed by shifts on your target hardware. For more information, see Handle Net Slope Correction.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS