Main Content

Best Practices for Accelerating Fixed-Point Code

Recommended Compilation Options for fiaccel

  • -args – Specify input parameters by example

    Use the -args option to specify the properties of primary function inputs as a cell array of example values at the same time as you generate code for the MATLAB® file with fiaccel. The cell array can be a variable or literal array of constant values. The cell array should provide the same number and order of inputs as the primary function.

    When you use the -args option you are specifying the data types and array dimensions of these parameters, not the values of the variables. For more information, see Define Input Properties by Example at the Command Line (MATLAB Coder).

    Note

    Alternatively, you can use the assert function to define properties of primary function inputs directly in your MATLAB file. For more information, see Define Input Properties Programmatically in MATLAB File.

  • -report – Generate code generation report

    Use the -report option to generate a report in HTML format at code generation time to help you debug your MATLAB code and verify that it is suitable for code generation. If you do not specify the -report option, fiaccel generates a report only if build errors or warnings occur.

    The code generation report contains the following information:

    • Summary of code generation results, including type of target and number of warnings or errors

    • Build log that records build and linking activities

    • Links to generated files

    • Error and warning messages (if any)

For more information, see fiaccel.

Build Scripts

Use build scripts to call fiaccel to generate MEX functions from your MATLAB function.

A build script automates a series of MATLAB commands that you want to perform repeatedly from the command line, saving you time and eliminating input errors. For instance, you can use a build script to clear your workspace before each build and to specify code generation options.

This example shows a build script to run fiaccel to process lms_02.m:

close all;
clear all;
clc;

N = 73113;

fiaccel  -report lms_02.m ...
  -args { zeros(N,1) zeros(N,1) }  
In this example, the following actions occur:

  • close all deletes all figures whose handles are not hidden. See close in the MATLAB Graphics function reference for more information.

  • clear all removes all variables, functions, and MEX-files from memory, leaving the workspace empty. This command also clears all breakpoints.

    Note

    Remove the clear all command from the build scripts if you want to preserve breakpoints for debugging.

  • clc clears all input and output from the Command Window display, giving you a “clean screen.”

  • N = 73113 sets the value of the variable N, which represents the number of samples in each of the two input parameters for the function lms_02

  • fiaccel -report lms_02.m -args { zeros(N,1) zeros(N,1) } calls fiaccel to accelerate simulation of the file lms_02.m using the following options:

    • -report generates a code generation report

    • -args { zeros(N,1) zeros(N,1) } specifies the properties of the function inputs as a cell array of example values. In this case, the input parameters are N-by-1 vectors of real doubles.

Check Code Interactively Using MATLAB Code Analyzer

The code analyzer checks your code for problems and recommends modifications to maximize performance and maintainability. You can use the code analyzer to check your code continuously in the MATLAB Editor while you work.

To ensure that continuous code checking is enabled:

  1. On the MATLAB Home tab, click Preferences. Select Code Analyzer to view the list of code analyzer preferences.

  2. Select the Enable integrated warning and error messages check box.

Separating Your Test Bench from Your Function Code

Separate your core algorithm from your test bench. Create a separate test script to do all the pre- and post-processing such as loading inputs, setting up input values, calling the function under test, and outputting test results. See the example on the fiaccel reference page.

Preserving Your Code

Preserve your code before making further modifications. This practice provides a fallback in case of error and a baseline for testing and validation. Use a consistent file naming convention, as described in File Naming Conventions. For example, add a 2-digit suffix to the file name for each file in a sequence. Alternatively, use a version control system.

File Naming Conventions

Use a consistent file naming convention to identify different types and versions of your MATLAB files. This approach keeps your files organized and minimizes the risk of overwriting existing files or creating two files with the same name in different folders.

For example, the file naming convention in the Generating MEX Functions getting started tutorial is:

  • The suffix _build identifies a build script.

  • The suffix _test identifies a test script.

  • A numerical suffix, for example, _01 identifies the version of a file. These numbers are typically two-digit sequential integers, beginning with 01, 02, 03, and so on.

For example:

  • The file build_01.m is the first version of the build script for this tutorial.

  • The file test_03.m is the third version of the test script for this tutorial.