Remove Zero Initialization Code

The Remove root level I/O zero initialization and Remove internal data zero initialization parameters control whether the generated code contains initialization code for internal data (block states and block outputs) and external data (root inports and outports) whose value is zero. Eliminating zero initialization code accelerates model initialization, reduces ROM consumption, and increases the execution speed of the generated code.

During startup, standards-compliant C and C++ compilers initialize global data to zero eliminating the need to include zero initialization code for this data in the generated code. Standards-compliant compilers do not necessarily initialize dynamically allocated data and local variables to zero. Before leaving the Remove root level I/O zero initialization and Remove internal data zero initialization parameters selected, confirm the following:

  • If your compiler is not standards compliant, confirm that it initializes global data to zero.

  • If you set the Code interface packaging to Reusable function or C++ class, confirm that data is either statically allocated or that dynamically allocated data is initialized to zero.

If you set the Code interface packaging parameter to Reusable function and select the Use dynamic memory allocation for model initialization parameter, the Remove root level I/O zero initialization and Remove internal data zero initialization check boxes are cleared and at the command line, ZeroExternalMemoryAtStartup and ZeroInternalMemoryAtStartup parameters are set to 'on'.

For a model in which the Code interface packaging parameter is set to C++ class and the Use dynamic memory allocation for model block instantiation parameter is selected, the Remove internal data zero initialization check box is cleared and ZeroInternalMemoryAtStartup is set to 'on' and is read-only .

Remove Zero Initialization Code for Internal Data

This example shows how to eliminate code that initializes internal data to zero.

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.

model = 'rtwdemo_internal_init';
open_system(model);

Generate Code Without Optimization

Build the model by using Embedded Coder.

currentDir = pwd;
[~,cgDir] = rtwdemodir();
evalc('rtwbuild(model)');

This code is in the rtwdemo_internal_init.c file.

cfile = fullfile(cgDir,'rtwdemo_internal_init_ert_rtw','rtwdemo_internal_init.c');
rtwdemodbtype(cfile,'/* Model initialize', '* File trailer', 1, 0);
/* 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. Set the model parameter ZeroInternalMemoryAtStartup to 'off'.

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

Generate Code with Optimization

Build the model by using Embedded Coder.

evalc('rtwbuild(model)');

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

rtwdemodbtype(cfile,'/* Model initialize', '* File trailer', 1, 0);
/* Model initialize function */
void rtwdemo_internal_init_initialize(void)
{
  /* (no initialization code required) */
}

/*
bdclose(model)
rtwdemoclean;
cd(currentDir)

Remove Initialization Code from Root-Level Inports and Outports Set to Zero

This example shows how to remove initialization code from root-level inports and outports set to zero.

Example Model

In the model rtwdemo_rootlevel_zero_initialization, all of the input and output signals have a numeric value of zero. Because signals sig1 and sig2 have data types int16 and Boolean, respectively, and all of the output signals have data type double, these signals also have initial values of bitwise zero. The signals have an integer bit pattern of 0, meaning that all bits are off. Signals sig1_b and sig2_b have a fixed-point data type with bias, so their initial value is not bitwise zero.

model = 'rtwdemo_rootlevel_zero_initialization';
open_system(model);

Generate Code

In your system temporary folder, create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

Build the model by using Embedded Coder.

set_param(model, 'ZeroExternalMemoryAtStartup','on');
evalc('rtwbuild(model)');

These lines of rtwdemo_rootlevel_zero_initialization.c code show the initialization of root-level inports and outports without the optimization. The four input signals are individually initialized as global variables. The four output signals are members of a global structure that the memset function initializes to bitwise zero.

cfile = fullfile(cgDir,'rtwdemo_rootlevel_zero_initialization_ert_rtw',...
    'rtwdemo_rootlevel_zero_initialization.c');
rtwdemodbtype(cfile, 'rtwdemo_rootlevel_zero_initialization_initialize',...
    'trailer for generated code', 1, 0);
void rtwdemo_rootlevel_zero_initialization_initialize(void)
{
  /* Registration code */

  /* external inputs */
  sig1 = 0;
  sig1_b = -3;
  sig2 = false;
  sig2_b = -3;

  /* external outputs */
  (void) memset((void *)&rtY, 0,
                sizeof(ExternalOutputs));
}

/*

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. On the Optimization pane, select Remove root level I/O zero initialization.

Alternatively, use the command-line API to enable the optimization:

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

Generate Code with Optimization

The optimized code does not contain initialization code for the input signals sig1, sig2, and the four output signals because their initial values are bitwise zero.

Build the model.

evalc('rtwbuild(model)');

Here is the rtwdemo_rootlevel_zero_initialization.c optimized code in the initialization function.

cfile = fullfile(cgDir,'rtwdemo_rootlevel_zero_initialization_ert_rtw',...
    'rtwdemo_rootlevel_zero_initialization.c');
rtwdemodbtype(cfile, 'rtwdemo_rootlevel_zero_initialization_initialize',...
    'trailer for generated code', 1, 0);
void rtwdemo_rootlevel_zero_initialization_initialize(void)
{
  /* Registration code */

  /* external inputs */
  sig1_b = -3;
  sig2_b = -3;
}

/*

Close the model and the code generation report.

bdclose(model)
rtwdemoclean;
cd(currentDir)

Additional Information

  • You can use the Use memset to initialize floats and doubles parameter to control the representation of zero during initialization. See Use memset to initialize floats and doubles to 0.0.

  • The code still initializes data structures whose value is not zero when you select the Remove internal data zero initialization and Remove root level I/O zero initialization parameters.

  • The data of ImportedExtern or ImportedExternPointer storage classes are not initialized, regardless of the settings of the Remove internal data zero initialization and Remove root level I/O zero initialization parameters.

Related Topics