Main Content

Use Design Evolution Manager App to Explore Different Coder Settings

This example shows how to use the Design Evolution Manager app to manage alternative design versions while you explore coder settings.

Coder provides many settings and optimizations for improving your generated code, such as reducing memory consumption. In the process of optimizing your design for deployment, you may need to try out several different combinations of settings on the model and understand their impact on generated code in order to arrive at the optimal configuration for your application. In this example, you explore changing different coder settings and analyze their impact on the memory consumed by the generated code.

The Design Evolution Manager app helps you to track these different versions of your model, along with any notes, reports, metrics, or other artifacts you record along with these versions. Each evolution captures a snapshot of your design, including any notes, reports, metrics, or other artifacts you want to record alongside version. The evolution tree provides a visual representation of your design process, helping you to understand the relationships between different versions of the design which are recorded as evolutions. If you want to combine two different design paths, you can compare files from any two evolutions and merge changes from an evolution into files in your project.

Open Model and Set Up Example Project

Open ex_reusable_csc_model in the DEM_Coder_Example project. The model contains the nonreusable subsystem DeltaSubsystem and the MATLAB Function block Downsample. DeltaSubsystem contains the MATLAB Function blocks DeltaX and DeltaY. For more information on this model and how to specify buffer reuse for multiple signals in a path, see Specify Buffer Reuse for Signals in a Path (Embedded Coder).

model ='ex_reusable_csc_model';
open_system(model);

Use the helper script setupDEMCoderExample to set up a project for this example. To use the Design Evolution Manager app, all of your design files must be contained in a single project. The helper script creates a project containing the Simulink® model and code generated from the model.

setupDEMCoderExample

For this example, you will compare the impact of different coder optimizations on the code generated for this design. The code generated by the helper script provides a baseline for exploration of different settings. When you generate code, coder generates a report summarizing the results. The Static Code Metrics Report provides statistics of the generated code. In this example, you will use coder settings to optimize the metrics summarized in the Global Variables and Function Information sections of this report.

Open Design Evolution Manager and Create Evolution Tree

Open the Design Evolution Manager app and create a new evolution tree.

1. On the Project tab, in the Tools section, click the button arrow, then click the Design Evolution Manager app icon.

2. In the Create New Evolution Tree dialog box, under a name for the evolution tree. You can also add optional notes about the evolution tree.

3. Click Create to continue.

The Design Evolution Manager creates a new evolution tree. The Baseline evolution contains a snapshot of files in the project at the time the evolution tree was created. The File Explorer pane shows a list of files that were captured in the Baseline evolution. The Baseline evolution is the reference point for the evolution tree and is locked by default to further changes you make to files in the project from being recorded to this evolution.

4. To facilitate comparison of the impact of different coder settings on this model, enter notes about relevant statistics in the Static Code Metrics Report for the Baseline evolution in the Notes field of the Property Inspector pane. For the Baseline version of the model, these statistics are reported for the global variables and the ex_reusable_csc_model_step function:

Global Variables Total Size (bytes)

46,112,792

Accumulated Stack Size (bytes)

64

Lines

30

Reuse Global Memory for Block Outputs

Configure the code generator to reuse global variables by selecting the model configuration parameter Reuse global block outputs (Embedded Coder). This setting can reduce ROM and RAM consumption and data copies and increase execution speed of generated code. Save the change to the model and generate code again.

set_param(model,'GlobalBufferReuse','on');
save_system(model);
slbuild(model);

Create New Evolution

In the Design Evolution Manager app, create a new evolution to record a snapshot of this version of the design.

1. In the toolstrip, click Update Tree. The app creates a new Untitled evolution as a child of the Baseline evolution.

2. Rename the evolution. Select the evolution in the tree, then in the Property Inspector pane, under Name, enter Reuse global block outputs.

3. Enter notes on the static code metrics for this evolution.

Global Variables Total Size (bytes)

46,112,792

Accumulated Stack Size (bytes)

64

Lines

30

4. Lock the Reuse global block outputs evolution to prevent it from being updated further. Select the evolution in the tree, then click Lock Evolution.

Create Branch in Evolution Tree to Try New Design Direction

The version of the design recorded in the Reuse global block outputs evolution did not show any improvement to the metrics of interest. Return to the original version of the design captured in the Baseline evolution to try a different coder optimization. This will create a new branch in the evolution tree.

1. In the evolution tree, select the Baseline evolution.

2. In the toolstrip, click Get Evolution. The app gets the version of files stored in the Baseline evolution into the project. The shape of the evolution tree changes to reflect a new branch in the design direction. The next evolution created will be a child of the Baseline evolution.

Enable Expression Folding and Create Evolution

1. Enable expression folding on the model by enabling the configuration parameter Eliminate superfluous local variables (Expression folding) (Simulink Coder).

This parameter specifies whether to collapse block computations into single expressions, rather than generating separate code statements and storage declarations for each block in the model. Expression folding optimizes code to minimize the computation of intermediate results at block outputs and the storage of such results in temporary buffers or variables.

Save the change to the model and generate code again.

set_param(model,'ExpressionFolding','on');
save_system(model);
slbuild(model);

2. In the Design Evolution Manager app, click Update Tree to save this version of the project to a new evolution. The new evolution is a child of the Baseline evolution.

3. Rename the evolution. In the evolution tree, double-click the Untitled evolution name. Enter the new name Expression folding.

4. Enter notes on the static code metrics for this evolution. You can click back to your notes on the Baseline evolution to see that enabling expression folding has reduced the accumulated stack size from 64 to 56 bytes, and lines has increased from 30 to 32.

Global Variables Total Size (bytes)

46,112,792

Accumulated Stack Size (bytes)

56

Lines

32

5. Lock the Expression folding evolution to prevent it from being updated further. Select the evolution in the tree, then click Lock Evolution.

Compare Generated Code With and Without Expression Folding

Compare the Expression folding evolution to the Baseline evolution in which the expression folding setting was disabled.

1. In the evolution tree, select the connector between the Expression folding and the Baseline evolution.

2. The File Explorer pane displays a list of files which changed between these two evolutions.

3. In the File Explorer pane, select the file ex_reusable_csc_model.c.

4. In the File Explorer pane, click Compare Files. The Comparison Tool opens and displays differences between the two versions of this file. You can use the Comparison Tool to explore these differences in detail to understand how enabling expression folding changed the generated code.

Compare and Merge Changes from Evolution Into Active Project Files

The evolution tree now has two branches, representing two different directions of exploration. The Expression folding evolution showed improvements in the generated code, so continue working on this successful branch. Use the Design Evolution Manager app to merge changes from the Reuse global block outputs evolution into the active project files.

1. In the evolution tree, select the Reuse global block outputs evolution.

2. In the File Explorer pane:

a. Use the Compare Reuse global block output to dropdown to select Project Files.

b. Select the model file ex_reusable_csc_model.slx.

c. Click Compare Files. The Comparison Tool opens and displays differences in the ex_reusable_csc_model.slx model file between the Reuse global block outputs evolution and the active project files.

3. Merge differences from the Reuse global block outputs evolution into the active project files.

a. In the Comparison Tool, click Merge Mode.

b. Use the merge tool to enable the Reuse global block outputs setting in the active project files.

c. Click Save File to save the merged change to the model.

4. Press Ctrl+B to generate code.

5. Update the evolution tree. In the Design Evolution Manager app, click Update Tree to save this version of the project files in a new evolution.

6. Name the new evolution Merged.

7. Enter notes on the static code metrics for this evolution. You can click back to your notes on the Expression folding evolution to see that merging in the Reuse global block outputs setting had no improvement on these metrics.

Global Variables Total Size (bytes)

46,112,792

Accumulated Stack Size (bytes)

56

Lines

32

8. Lock the Merged evolution. Selected the Merged evolution in the tree, then click Lock Evolution.

Specify Buffer Reuse for Signals and Create Evolution

You might be able to remove additional data copies by using the Resuable storage class on signals in the model. This optimization reduces RAM and ROM consumption and increases code execution speed. For more information, see Specify Buffer Reuse for Signals in a Path (Embedded Coder).

1. Specify the Reusable storage class on signals in the model using the setReusableStorageClass helper function. Save the change to the model and generate code.

setReusableStorageClass;
save_system(model);
slbuild(model);

2. Update the evolution tree. In the Design Evolution Manager app, click Update Tree to save this version of the project files in a new evolution.

3. Name the new evolution Reusable storage class.

4. Enter notes on the static code metrics for this evolution. You can click back to your notes on the Merged evolution to see that enabling the Resuable storage class on signals in the model reduced the total size of global variables from 46,112,792 to 41,926,680.

Global Variables Total Size (bytes)

41,926,680

Accumulated Stack Size (bytes)

56

Lines

33

Generate and Review Report

When you have completed your design exploration, you can generate a report summarizing the evolution tree.

1. In the Design Evolution Manager app, click Generate Report.

2. In the dialog box, enter a title for the report and a name for the pdf file to be generated.

3. Click Generate.

4. Review the report. You can use the report to review the structure of the evolution tree, files changed in each evolution, and any notes you recorded for the evolution tree and individual evolutions. In this example, the notes in the report show that enabling expression folding improved the accumulated stack size metric for the ex_reusable_csc_model_step function and setting the Resuable storage class on signals in the model reduced the total size of global variables.

Close Project

Close the project. On the Project tab, in the Close section, click Close Project.

proj = currentProject;
close(proj);

See Also