Main Content

Optimize Generated Code by Using Multiple Code Replacement Libraries

You can generate code that has code replacements enabled by multiple code replacement libraries. You can select multiple Code Replacement libraries in a unified workflow so that the generated code contains optimizations from varied code replacement libraries such as AUTOSAR 4.0 and GNU C99 extensions. You can also select your own custom code replacement libraries along with libraries included with Embedded Coder® to further optimize the generated code. You can select and use a combination of libraries and achieve the requisite functionalities, and need not author separate libraries. In instances where there are multiple custom code replacement libraries and you need functionalities from all of them, individually select the libraries.

Example Model

This example shows how to replace the generated code for a model containing an Add block and an Abs block by using multiple code replacement libraries. You replace the Abs block using a code replacement customization that replaces the regular fabs function with the custom implementation CRL_abs_d from the ScalarFcn library. You replace the Add block with the custom implementation CRL_d_add_d_d_cao from the ScalarOp library.

Open the model mMultiLibrary_abs that has an Add block and an Abs block.

model = 'mMultiLibrary_abs';
open_system(model);

copyfile multipleFnRtwTargetInfo.txt rtwTargetInfo.m

The MATLAB® customization files hcrl_make_abs and hcrl_sum_float have already been executed. Run the sl_refresh_customizations function to register the libraries ScalarFcn and ScalarOp.

sl_refresh_customizations;

Select Code Replacement Libraries

  1. Open the Configuration Parameters dialog box.

  2. In the Configuration Parameters dialog box, set the correct Device Vendor and Device Type depending on the hardware target. In this example Intel is the Device Vendor and x86-64(Windows64) is the Device Type.

  3. On the Interface pane, set Code Replacement Library parameter by clicking Select and adding the code replacement libraries to the Selected code replacement libraries - prioritized list pane. For this example, choose ScalarFcn and ScalarOp. The libraries work on the Windows® platforms. Linux® users must supply a different library.

  4. Order the code replacement libraries according to the priority in which you want to see them replaced in the generated code. Code replacement libraries at the top of order have a higher priority during code replacement.

Alternatively, use the command-line API to enable the code replacement:

set_param('mMultiLibrary_abs','CodeReplacementLibrary','ScalarFcn,ScalarOp')

Generate code for the model.

evalc('slbuild(model)');

NOTE: When registering a code replacement library, you cannot use a comma in the code replacement library name.

View the generated code with the replacements. Here is a portion of mMultiLibrary_abs.c.

cfile = fullfile('mMultiLibrary_abs_ert_rtw','mMultiLibrary_abs.c');
coder.example.extractLines(cfile,'/* Model step function ','/* Model initialize function',1, 1);
/* Model step function */
void mMultiLibrary_abs_step(void)
{
  int32_T i;

  /* Outport: '<Root>/Out1' incorporates:
   *  Abs: '<Root>/Abs1'
   *  Inport: '<Root>/In1'
   */
  mMultiLibrary_abs_Y.Out1 = CRL_abs_d(mMultiLibrary_abs_U.In1);

  /* Outport: '<Root>/Output' incorporates:
   *  Inport: '<Root>/In2'
   *  Inport: '<Root>/In3'
   *  Sum: '<Root>/Add'
   */
  for (i = 0; i < 2500; i++) {
    mMultiLibrary_abs_Y.Output[i] = CRL_d_add_d_d_cao(mMultiLibrary_abs_U.In2[i],
      mMultiLibrary_abs_U.In3[i]);
  }

  /* End of Outport: '<Root>/Output' */
}

The generated code contains the CRL_abs_d function enabled by the custom code replacement library ScalarFcn. Note that this library only works for Windows. The code also contains the function CRL_d_add_d_d_cao enabled by the ScalarOp code replacement library. Selecting multiple code replacement libraries enables multiple optimizations in the generated code.

Close the model and code generation report.

delete ./rtwTargetInfo.m
bdclose(model)

Limitations:

  • When you use different argument types such as matrix and scalar, the priority ordering is ignored. A matrix code replacement library has a higher priority than a scalar code replacement library, regardless of the order specified by the code replacement library.

  • When the code replacement library parameter specifies a language standard, for instance through its BaseTfl, and it does not match the Language standard parameter, the library specified by the Language standard parameter is used.

Related Topics