Real-Time Workshop    

Tutorial 4: A First Look at Generated Code

In this tutorial, you examine code generated from a simple model, to observe the effects of some of the many code optimization features available in Real-Time Workshop.

The source model, example.mdl, is shown below.

Setting up the Model

First, create the model and set up basic Simulink and Real-Time Workshop parameters as follows:

  1. Create a directory example_codegen and make it your working directory:
  2. Create a new model and save it as example.mdl.
  3. Add Sine Wave, Gain, and Out1 blocks to your model and connect them as shown in the above diagram. Label the signals as shown.
  4. From the Simulation menu, choose Simulation Parameters. The Simulation Parameters dialog box opens.
  5. Click the Solver tab and enter the following parameter values on the Solver pane:
  1. Solver options: set Type to Fixed-step. Select the ode5 (Dormand-Prince) solver algorithm.

    Leave the other Solver pane parameters set to their default values.

  1. Click Apply.
  2. Click the Workspace I/O tab and make sure all check boxes are cleared.
  3. Click Apply.
  4. Click the Real-Time Workshop tab. Select Target configuration from the Category menu. Next, select the Generate code only option. This option causes Real-Time Workshop to generate code without invoking make to compile and link the code. This option is convenient for this exercise, as we are only interested in looking at the generated code. Note that the Build button caption changes to Generate code.
  1. Also, make sure that the generic real-time (GRT) target is selected. The pane should appear as below.

  1. Click Apply.
  2. Save the model.

Generating Code Without Buffer Optimization

When the block I/O optimization feature is enabled, Real-Time Workshop uses local storage for block outputs wherever possible. We now disable this option to see what the nonoptimized generated code looks like:

  1. From the Simulation menu, choose Simulation Parameters. The Simulation Parameters dialog box opens.
  2. Click the Advanced tab. Select the Signal storage reuse option and select the Off radio button, as shown below.

  3. Click Apply.
  4. Click the Real-Time Workshop tab and select Target configuration from the Category menu. Then click the Generate code button.
  5. Because the Generate code only option was selected, Real-Time Workshop does not invoke your make utility. The code generation process ends with the message
  1. ### Successful completion of Real-Time Workshop build procedure for model: example

  1. The generated code is in the build directory, example_grt_rtw. The file example_grt_rtw\example.c contains the output computation for the model. Open this file into the MATLAB editor:
  1. edit example_grt_rtw\example.c

  1. In example.c, find the function MdlOutputs.

The generated C code consists of procedures that implement the algorithms defined by your Simulink block diagram. Your target's execution engine executes the procedures as time moves forward. The modules that implement the execution engine and other capabilities are referred to collectively as the run-time interface modules. See Program Architecture" in Chapter 7 of the Real-Time Workshop documentation for a complete discussion of how Real-Time Workshop interfaces and executes application, system-dependent, and system-independent modules, in each of the two styles of generated code.

In our example, the generated MdlOutputs function implements the actual algorithm for multiplying a sine wave by a gain. The MdlOutputs function computes the model's block outputs. The run-time interface must call MdlOutputs at every time step.

With buffer optimizations turned off, MdlOutputs assigns unique buffers to each block output. These buffers (rtB.sin_out, rtB.gain_out) are members of a global data structure, rtB. The code is shown below:

We now turn buffer optimizations on and observe how these optimizations improve the code.

Generating Code with Buffer Optimization

Enable buffer optimizations and regenerate the code as follows:

  1. From the Simulation menu, choose Simulation Parameters. The Simulation Parameters dialog box opens.
  2. Click the Advanced tab. Select the Signal storage reuse option and select the On radio button.
  3. Click Apply.
  4. Click the Real-Time Workshop tab. Select General code generation options from the Category menu.

  5. Make sure that the Local block outputs option is selected, as shown above.
  6. Click Apply.
  7. Select General code generation options (cont.) from the Category menu.

  8. Make sure that the Buffer reuse option is selected, as shown above. Make sure that the Expression folding option is off, which will disable the two options below it, as shown. We will observe the effects of Expression folding later in this tutorial.
  9. Click Apply.
  10. Click the Generate code button.
  11. As before, Real-Time Workshop generates code in the example_grt_rtw directory. Note that the previously generated code is overwritten.
  12. Edit example_grt_rtw/example.c, and examine the function MdlOutputs.

With buffer optimizations enabled, the code in MdlOutputs reuses rtb_temp0, a temporary buffer with local scope, rather than assigning global buffers to each input and output:

This code is more efficient in terms of memory usage. The efficiency improvement gained by enabling Buffer reuse and Local block outputs would be more significant in a large model with many signals.

A Further Optimization: Expression Folding

As a final optimization, we will turn on expression folding, a code optimization technique that minimizes the computation of intermediate results and the use of temporary buffers or variables.

Enable expression folding and regenerate the code as follows:

  1. Select General code generation options (cont.) from the Category menu, if you have not already done so.

  2. Select the Expression folding option. Make sure that the options Fold unrolled vectors and Enforce integer downcast (below Expression folding) are selected, as shown.
  3. Make sure that Buffer reuse is still selected, as shown.
  4. Click Apply.
  5. Click the Generate code button.
  6. As before, Real-Time Workshop generates code in the example_grt_rtw directory.
  7. Edit example_grt_rtw/example.c, and examine the function MdlOutputs.

In the previous examples, the Gain block computation was computed in a separate code statement and the result was stored in a temporary location before the final output computation.

With Expression folding selected, there is a subtle but significant difference in the generated code: the gain computation is incorporated (or "folded") directly into the Outport computation, eliminating the temporary location and separate code statement. This computation is on the last line of the MdlOutputs function:

In many cases, Expression folding can incorporate entire model computations into a single, highly optimized line of code. Expression folding is turned on by default. We strongly recommend that you use this option.

HTML Code Generation Reports

When the Generate HTML report option under General code generation options is selected (see figure below), a navigable summary of source files is produced when the model is built.

Code generation causes Real-Time Workshop to produce an HTML file for each source file, plus a summary and an index file, in a directory named /html within the build directory. Unless you are running MATLAB in -nodesktop mode (a UNIX option only), the HTML summary and index are automatically loaded into the MATLAB Help browser and displayed, as the following figure illustrates. You can click on links in the report to inspect source and include files, and view relevant documentation. Block header comments in source files displayed in the browser have hyperlinks back to the model that cause the block that generated that section of code to be highlighted. To review any HTML report at a later time, use any Web browser to open the file html/model_codgen_rpt.html within your build directory.

For further information, consult these sections of the Real-Time Workshop documentation:

HTML Report for Code Generated for a GRT Target


  Tutorial 3: Code Validation Tutorial 5: Getting Started with External Mode Using GRT