Main Content

Shared Constant Parameters for Code Reuse

You can share the generated code for constant parameters across models if either of the following conditions applies:

  • Constant parameters are shared in a model reference hierarchy.

  • The model configuration parameter Shared code placement is set to Shared location.

If you do not want to generate shared constants, and model configuration parameter Shared code placement is set to Shared location, set parameter GenerateSharedConstants to off. For example, to turn off shared constants for the current model, in the Command Window, type the following.

set_param(gcs,'GenerateSharedConstants','off');
When the model configuration parameter GenerateSharedConstants is set to off, the code generator generates the constant parameters shared across the model reference hierarchy in a shared location, in the slprj/target/_sharedutils folder but outside the const_params.c file. The code generator defines the individual constant parameters in the reusable library subsystem files individually.

When the model configuration parameter GenerateSharedConstants is set to on, the code generator produces shared constant parameters individually and places them in the file const_params.c. The code generator places that file in the shared utilities folder slprj/target/_sharedutils.

For example, if a constant has multiple uses within a model reference hierarchy where the top model is named topmod, the code for the shared constant is as follows:

  • In the shared utility folder, slprj/grt/_sharedutils, the constant parameters are defined in const_params.c and named rtCP_pooled_ appended to a unique checksum:

    extern const real_T rtCP_pooled_lfcjjmohiecj[7];
    const real_T rtCP_pooled_lfcjjmohiecj[7] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
    
    extern const real_T rtCP_pooled_ppphohdbfcba[7];
    const real_T rtCP_pooled_ppphohdbfcba[7] = { 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 };

  • In top_model_private.h or in a referenced model, ref_model_private.h, for better readability, the constants are renamed as follows:

    extern const real_T rtCP_pooled_lfcjjmohiecj[7];
    extern const real_T rtCP_pooled_ppphohdbfcba[7];
    
    #define rtCP_Constant_Value   rtCP_pooled_lfcjjmohiecj  /* Expression: [1 2 3 4 5 6 7]
                                                             * Referenced by: '<Root>/Constant'*/
    #define rtCP_Gain_Gain        rtCP_pooled_ppphohdbfcba  /* Expression: [7 6 5 4 3 2 1]
                                                             * Referenced by: '<Root>/Gain' */

  • In topmod.c or refmod.c, the call site might be:

    for (i = 0; i < 7; i++) {
       topmod_Y.Out1[i] = (topmod_U.In1 + rtCP_Constant_Value[i]) * rtCP_Gain_Gain[i];
    }

The code generator attempts to generate constants as individual constants to the const_params.c file in the shared utilities folder. If a model contains a parameter that the code generator cannot inline directly (for example, an array parameter), the code defines a structure type that represents the data. This structure uses the const storage type qualifier, so some build toolchains can optimize the assembly code further.

The declaration of the structure, ConstParam_model, is in model.h.

Suppress Shared Constants in the Generated Code

You can choose whether the code generator produces shared constants and shared functions. You might want to be able to keep the code and data separate between subsystems, or you might find that sharing constants results in a memory shortage during code generation.

You can change this setting programmatically by using the parameter GenerateSharedConstants with set_param and get_param.

In the following example, when GenerateSharedConstants is set to on, the code generator defines the constant values in the _sharedutils folder in the const_params.c file. When GenerateSharedConstants is set to off, the code generator defines the constant values in a nonshared area, in the model_ert_rtw file in the model_data.c file.

Open the model ThrottleControl.

open_system('ThrottleControl')

Verify that model configuration parameter Shared code placement is set to Shared location. If the parameter is set to Auto, the code generator ignores setting GenerateSharedConstants. If you try to set the parameter value, an error message appears. By default, value GenerateSharedConstants is selected.

In the Command Window, set GenerateSharedConstants to on.

  set_param('ThrottleControl','GenerateSharedConstants','on')

You see that the shared constant definitions in the folder slprg/grt/_sharedutils, in the file const_param.c:

  extern const real_T rtCP_pooled_H4eTKtECwveN[9];
  const real_T rtCP_pooled_H4eTKtECwveN[9] = { 1.0, 0.75, 0.6, 0.0, 0.0, 0.0, 0.6,
    0.75, 1.0 } ;
  extern const real_T rtCP_pooled_SghuHxKVKGHD[9];
  const real_T rtCP_pooled_SghuHxKVKGHD[9] = { -1.0, -0.5, -0.25, -0.05, 0.0, 0.05,
    0.25, 0.5, 1.0 } ;
  extern const real_T rtCP_pooled_WqWb2t17NA2R[7];
  const real_T rtCP_pooled_WqWb2t17NA2R[7] = { -1.0, -0.25, -0.01, 0.0, 0.01, 0.25,
    1.0 } ;
  extern const real_T rtCP_pooled_Ygnal0wM3c14[7];
  const real_T rtCP_pooled_Ygnal0wM3c14[7] = { 1.0, 0.25, 0.0, 0.0, 0.0, 0.25, 1.0
  } ;

In the Command Window, set GenerateSharedConstants to off.

  set_param('ThrottleControl','GenerateSharedConstants','off')

You can see the unshared constants in the folder ThrottleControl_grt_rtw, in the file ThrottleControl_data.c:

  /* Constant parameters (auto storage) */
  const ConstP_rtwdemo_throttlecntrl_T rtwdemo_throttlecntrl_ConstP = {
    /* Pooled Parameter (Expression: P_OutMap)
     * Referenced by:
     *   '<S2>/Proportional  Gain Shape'
     *   '<S3>/Proportional  Gain Shape'
     */
    { 1.0, 0.25, 0.0, 0.0, 0.0, 0.25, 1.0 },
    /* Pooled Parameter (Expression: P_InErrMap)
     * Referenced by:
     *   '<S2>/Proportional  Gain Shape'
     *   '<S3>/Proportional  Gain Shape'
     */
    { -1.0, -0.25, -0.01, 0.0, 0.01, 0.25, 1.0 },
    /* Pooled Parameter (Expression: I_OutMap)
     * Referenced by:
     *   '<S2>/Integral  Gain Shape'
     *   '<S3>/Integral  Gain Shape'
     */
    { 1.0, 0.75, 0.6, 0.0, 0.0, 0.0, 0.6, 0.75, 1.0 },
    /* Pooled Parameter (Expression: I_InErrMap)
     * Referenced by:
     *   '<S2>/Integral  Gain Shape'
     *   '<S3>/Integral  Gain Shape'
     */
    { -1.0, -0.5, -0.25, -0.05, 0.0, 0.05, 0.25, 0.5, 1.0 }
  };

Shared constant Parameters Limitations

The code generator does not produce shared constants for a model when:

  • The model has a code replacement library (CRL) that is specified for data alignment.

  • The parameter GenerateSharedConstants is set to off.

  • Constants have a Memory Section setting other than the default None, such as MemVolatile, MemConstVolatile, or a custom memory section. In this case, the code generator overrides the GenerateSharedConstants parameter and places the constants in the file model_data.c.

Individual constants are not shared, if:

  • A constant is referenced by a non-inlined S-function.

  • A constant has a user-defined type where Data Scope is not set to Exported.

Related Topics