| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
Options are available for signals with Auto storage class:
Signal storage reuse
Enable local block outputs
Reuse block outputs
Eliminate superfluous local variables (Expression folding)
Minimize data copies between local and global variables
Use these options to control signal memory reuse and choose local or global (model_B) storage for signals. The Signal storage reuse option is on the Optimization pane of the Configuration Parameters dialog box, as shown in the next figure.

When you select Signal storage reuse, the Enable local block outputs, Reuse block outputs, Eliminate superfluous local variables (Expression folding), and Minimize data copies between local and global variables options in the Code Generation section of the dialog box are enabled.
These options interact. When the Signal storage reuse option is selected,
The Reuse block outputs option is enabled and selected, and signal memory is reused whenever possible.
The Enable Local block outputs option is enabled and selected. This lets you choose whether reusable signal variables are declared as local variables in functions or as members of model_B.
The Eliminate superfluous local variables (Expression folding) is enabled and selected, and block computations collapse into single expressions.
The Minimize data copies between local and global variables is enabled and cleared, and global memory is not reused.
The following code examples illustrate the effects of the Signal storage reuse, Enable Local block outputs, Reuse block outputs, Eliminate superfluous local variables (Expression folding) and Minimize data copies between local and global variables options. The examples were generated from the signal_examp model (see figure Signal_examp Model).
The first example illustrates signal storage optimization, with Signal storage reuse, Enable Local block outputs, Reuse block outputs, and Minimize data copies between local and global variables selected. (For clarity in showing the individual Gain and Sum block computation, expression folding is off in this example.) The output signal from the Sum block reuses signal_examp_Y.Out1, a variable local to the model output function.
/* Model output function */
static void signal_examp_output(int_T tid)
{
/* Sum: '<Root>Sum' incorporates:
* Constant: '<Root>/Constant'
* Inport: '<Root>>/In1'
*/
signal_examp_Y.Out1 = signal_examp_U.In1 + signal_examp_P.Constant_Value;
/* Gain: '<Root>/Gain' */
signal_examp_Y.Out1 = signal_examp_P.Gain_Gain * signal_examp_Y.Out1;
/* tid is required for a uniform function interface.
* Argument tid is not used in the function. */
UNUSED_PARAMETER(tid);
}
If you are constrained by limited stack space, you can turn Enable local block outputs off and still benefit from memory reuse. The following example was generated with Enable local block outputs cleared and Signal storage reuse, Reuse block outputs, and Minimize data copies between local and global variables selected. The output signals from the Sum and Gain blocks use global structure signal_examp_B rather than declaring local variables and in both cases the signal name is gainSig.
/* Model output function */
static void signal_examp_output(int_T tid)
{
/* Sum: '<Root>/Add' incorporates:
* Constant: '<Root>/Constant'
* Inport: '<Root>/In1'
*/
signal_examp_B.gainSig = signal_examp_U.In1 +
signal_examp_P.Constant_Value;
/* Gain: '<Root>/Gain' */
signal_examp_B.gainSig = signal_examp_P.Gain_Gain *
signal_examp_B.gainSig;
/* Outport: '<Root>/Out1' */
signal_examp_Y.Out1 = signal_examp_B.gainSig;
/* tid is required for a uniform function interface.
* Argument tid is not used in the function. */
UNUSED_PARAMETER(tid);
}When the Signal storage reuse option is cleared, Reuse block outputs, Enable local block outputs, and Minimize data copies between local and global variables are disabled. This makes the block output signals global and unique, signal_examp_B.sumSig and signal_examp_B.gainSig, as shown in the following code.
/* Model output function */
static void signal_examp_output(int_T tid)
{
/* Sum: '<Root>/Add' incorporates:
* Constant: '<Root>/Constant'
* Inport: '<Root>/In1'
*/
signal_examp_B.sumSig = signal_examp_U.In1 +
signal_examp_P.Constant_Value;
/* Gain: '<Root>/Gain' */
signal_examp_B.gainSig = signal_examp_P.Gain_Gain *
signal_examp_B.sumSig;
/* Outport: '<Root>/Out1' */
signal_examp_Y.Out1 = signal_examp_B.gainSig;
/* tid is required for a uniform function interface.
* Argument tid is not used in the function. */
UNUSED_PARAMETER(tid);
}In large models, disabling Signal storage reuse can significantly increase RAM and ROM usage. Therefore, this approach is not recommended for code deployment; however it can be useful in rapid prototyping environments.
The following table summarizes the possible combinations of the Signal storage reuse / Reuse block outputs and Enable local block outputs options.
| Signal storage reuse and Reuse block outputs ON | Signal storage reuse OFF |
|---|---|---|
Enable local block outputs ON | Reuse signals in local memory (fully optimized) | N/A |
Enable local block outputs OFF | Reuse signals in model_B structure | Individual signal storage in model_B structure |
When the Enable local block outputs option is on, the following TLC variables constrain the use of stack space by local block output variables:
MaxStackSize: The maximum number of bytes the Real-Time Workshop product allocates for local variables declared by all block outputs in a model. MaxStackSize can be any positive integer. If the total size of local block output variables exceeds this maximum, the product allocates the remaining block output variables in global, rather than local, memory. The default value for MaxStackSize is Inf, that is, unlimited stack size.
Note Local variables in the generated code from sources other than local block outputs and stack usage from sources such as function calls and context switching are not included in the MaxStackSize calculation. For overall executable stack usage metrics, you should do a target-specific measurement, such as using run-time (empirical) analysis or static (code path) analysis with object code. |
MaxStackVariableSize: The maximum number of bytes n, where n is greater than zero, the Real-Time Workshop product allocates for any local block output variable declared in the code. The product allocates any variable with a size that exceeds MaxStackVariableSize in global, rather than local, memory. The default is 4096 bytes.
You may need to adjust the settings of these variables when working with models that contain large signals. When a variable exceeds MaxStackVariableSize, the Real-Time Workshop product places the variable in global memory space. Similarly, if the accumulated size of variables in local memory exceeds MaxStackSize, the product places subsequent local variables in global memory space. The Real-Time Workshop product analyzes the accumulated size of local variables based on a worst-case scenario without taking into account that local variables are released after functions return.
Consider the following options for your specific model:
Is it important that you maximize potential for signal storage optimization? If so, set MaxStackSize appropriately to accommodate the size and number of signals in your model. This minimizes overflow into global memory space and maximizes use of local memory. Local variables offer more optimization potential through mechanisms such as expression folding and buffer reuse.
Is the accumulated size of local variables exceeding the MaxStackSize setting? If so, consider setting MaxStackVariableSize to a value that forces large local variables into the global memory space and helps retain smaller local variables in local storage.
See Setting Target Language Compiler Options for more information.
![]() | Signal Storage Concepts | Signals with Test Points | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |