MATLAB Examples

Remove Zero Initialization Code for Internal Data

This example shows how to eliminate code that initializes internal data to zero. If your embedded application does not require generating initialization code for internal data whose value is zero, you can enable this optimization.

Contents

Example Model

Open the model rtwdemo_internal_init. The model contains an enabled subsystem whose initial output is zero. The subsystem contains a Unit Delay block whose initial condition is 0.

Generate Code Without Optimization

Build the model using Embedded Coder.

### Starting build procedure for model: rtwdemo_internal_init
### Successful completion of build procedure for model: rtwdemo_internal_init

This code is in the rtwdemo_internal_init.c file.

/* Model initialize function */
void rtwdemo_internal_init_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(rtM, (NULL));

  /* states (dwork) */
  (void) memset((void *)&rtDWork, 0,
                sizeof(D_Work));

  /* SystemInitialize for Enabled SubSystem: '<Root>/Enabled Subsystem' */
  /* InitializeConditions for UnitDelay: '<S1>/Unit Delay' */
  rtDWork.UnitDelay_DSTATE = 0.0;

  /* End of SystemInitialize for SubSystem: '<Root>/Enabled Subsystem' */
}

/*

Enable Optimization

Open the Configuration Parameters dialog box. On the Optimization pane, select Remove internal data zero initialization.

Alternatively, you can use the command prompt to enable the optimization. To enable the optimization, set the model parameter ZeroInternalMemoryAtStartup to 'off'.

set_param(model, 'ZeroInternalMemoryAtStartup', 'off');

Generate Code with Optimization

Build the model using Embedded Coder.

### Starting build procedure for model: rtwdemo_internal_init
### Successful completion of build procedure for model: rtwdemo_internal_init

This code is in the rtwdemo_internal_init.c file. The generated code does not initialize internal data by assignment to zero.

/* Model initialize function */
void rtwdemo_internal_init_initialize(void)
{
  /* (no initialization code required) */
}

/*

If you select the Remove internal data zero initialization parameter, be aware that memory might not be in a known state each time the generated code executes. This means that running a model (or a generated S-function) multiple times can result in different answers for each run. This behavior is sometimes desirable. For example, you can select the Remove internal data zero initialization parameter if you want to test the behavior of your design during a warm boot (that is, a restart without full system reinitialization). For models in which you select the Remove internal data zero initalization parameter but still want to get the same answer on every run from an S-function, you can use either of the following commands before each run:

clear SFcnName

where SFcnName is the name of the S-function, or

clear mex