Define Code Replacement Library Optimizations - MATLAB & Simulink (original) (raw)

A code replacement library contains one or more code replacement tables. Each table contains one or more code replacement entries. Each entry contains a mapping and entry parameters. The entry maps the conceptual representation of an automatically generated MATLAB or Simulink function to an implementation representation of an optimized C or C++ function. The entry parameters describe additional properties for the code generator to honor during code replacement.

Diagram showing a library that contains tables and each table contains entries. Each entry contains a conceptual representation, implementation representation, and priority.

To define a code replacement library, first define code replacement tables in a customization file. Then, during the registration step, create a code replacement library by selecting tables to include in your library. You can define your code replacement tables both interactively or programmatically using the same work flow.

The interactive and programmatic approaches both follow these 8 steps:

  1. Open the environment.
  2. Create a table.
  3. Create an entry.
  4. Create entry parameters.
  5. Create the conceptual representation.
  6. Create the implementation representation.
  7. Specify build information.
  8. Validate and save.

Interactively Develop a Code Replacement Library

  1. Open the environment.
    To interactively create a code replacement library, open the Code Replacement Tool (crtool) from the MATLAB command line with the command:
  2. Create a table.
    A code replacement table organizes code replacement entries into groupings that are easier to manage.
    To create a code replacement table:
    1. From the toolstrip of the Code Replacement tool, click > .
    2. In the right pane, name the table and clickApply.
  3. Create an entry.
    A code replacement table entry contains entry parameters and a mapping of a conceptual representation that describes a generated function to an implementation representation that describes an optimized implementation of that function.
    To create a code replacement entry:
    • Click > and click the type of entry that you want to create.
      The new entry appears without a name in the middle pane.
  4. Create entry parameters.
    Entry parameters specify optimization and build requirements for the code generator to honor during code replacement. These parameters vary and automatically appear in various sections of the crtool interface depending on the type of function replacement.
    To create entry parameters:
    • On the right pane, from the Function orOperation drop-down list, select the name of the function you want to replace.
      This selection determines the entry parameters that appear in the crtool.
  5. Create the conceptual representation.
    The conceptual representation describes the signature of the function you want to replace. The conceptual representation consists of the function name, return arguments, and parameters. The return arguments are referred to as output arguments, and the parameters are referred to as input arguments. Specify each conceptual argument with a name, I/O type, and, depending on the type of replacement, other properties that appear in the crtool.
    To define your conceptual representation:
    • In the Conceptual Function orConceptual Operation section, specify the input and output arguments of the function you want the code generator to replace.
  6. Create the implementation representation.
    The implementation representation describes the signature of the replacement function defined in the code replacement library. The implementation representation consists of a function name, return arguments, and parameters. Specify each implementation argument with a type, type qualifier, and complexity.
    To define your implementation representation:
    • In the Implementation Function orOperation Function section of the, specify the input and output arguments for the optimized function defined in the code replacement library.
  7. Specify build information.
    Build information provides the code generator with the files to perform function replacement.
    To specify build information:
    • Click the Build Information tab. Specify build information in these fields:
      Parameter Description
      Implementation header file Name of header file required for code replacement.
      Implementation source file Name of source file required for code replacement.
      Additional header files/include paths Name and path of additional header files required for code replacement.
      Additional source files/paths Name and path of additional source files required for code replacement.
      Additional object files/paths Name and path of additional object files required for code replacement.
      Additional link flags Flags the linker needs to generate an executable file for the replacement code.
      Additional compile flags Flags the compiler needs to generate object code for the replacement code.
      Copy files to build folder Flags whether to copy header, source, or object files, which are required to generate replacement code, to the build folder before code generation. If you specify files with Additional header files/include paths or Additional source files/ paths and you copy files, the compiler and utilities such as packNGo might find duplicate instances of files.
  8. Validate and save.
    Save your code replacement library with the same name as your code replacement table on your MATLAB path. Saving your library automatically validates unvalidated content.
    To validate and save your library:
    • From the Code Replacement tool toolstrip, clickSave. Specify a name for the file and save it.

Programmatically Develop a Code Replacement Library

  1. Open the environment.
    To programmatically create a code replacement library, open a MATLAB function file. From the MATLAB menu select New >Function.
  2. Create a table.
    A code replacement table organizes code replacement entries into groupings that are easier to manage.
    To create a code replacement table:
    1. Create a function you can use to call your code replacement library table. The function should not have arguments and return a table object.
    2. Create a table object with a call to RTW.TflTable.
      function hTable = code_replacement_library_table()
      % Create a function to call code replacement library table
      %% Create a code replacement library table object
      hTable = RTW.TflTable;

end 3. Create an entry.
A code replacement table entry contains entry parameters and a mapping of a conceptual representation of a generated function to the implementation representation of an optimized implementation of that function.
To create a code replacement table:

  1. Identify the type of function you want to replace.

  2. Call the corresponding Entry Function to create an entry in your table.
    function hTable = code_replacement_library_table()
    % Create a function to call code replacement library table
    %% Create a code replacement library table object
    hTable = RTW.TflTable;
    %% Create a code replacement library entry
    % Pick one from list below
    hEntry = RTW.TFLCOperationEntry;
    hEntry = RTW.TflCFunctionEntry;
    hEntry = RTW.TflBlasEntryGenerator;
    hEntry = RTW.TflCBlasEntryGenerator;
    hEntry = RTW.TflCOperationEntryGenerator;
    hEntry = RTW.TflCOperationEntryGenerator_NetSlope;
    hEntry = RTW.TflCSemaphoreEntry;
    hEntry = RTW.TflCFunctionEntryML;
    hEntry = RTW.TflCOperationEntryML;
    end

  3. Create entry parameters.
    Entry parameters specify optimization and build requirements for the code generator to honor during code replacement. These parameters vary depending on the type of function replacement.
    To create your entry parameters:

    • Call the Set Parameters function that corresponds with the type of function you want to replace identified in the previous step (step 3).
      Each Set Parameter function is shown with the available properties set to their default values. The exact properties and values you need to specify depends on the function you replace. See examples for specific replacement details.
      function hTable = code_replacement_library_table()
      % Create a function to call code replacement library table
      %% Create a code replacement library table object
      hTable = RTW.TflTable;
      %% Create a code replacement library entry
      % Pick one from list below
      hEntry = RTW.TFLCOperationEntry;
      hEntry = RTW.TflCFunctionEntry;
      hEntry = RTW.TflBlasEntryGenerator;
      hEntry = RTW.TflCBlasEntryGenerator;
      hEntry = RTW.TflCOperationEntryGenerator;
      hEntry = RTW.TflCOperationEntryGenerator_NetSlope;
      hEntry = RTW.TflCSemaphoreEntry;
      hEntry = RTW.TflCFunctionEntryML;
      hEntry = RTW.TflCOperationEntryML;
      %% Create entry parameters
      % Pick one from list below
      hEntry.setTflCOperationEntryParameters(...
      'Key', ' ', ...
      'Priority', 100, ...
      'AcceptExprInput', true, ...
      'AdditionalHeaderFiles', {}, ...
      'AdditionalIncludePaths', {}, ...
      'AdditionalLinkObjs', {}, ...
      'AdditionalLinkObjsPaths', {}, ...
      'AdditionalSourceFiles', {}, ...
      'AdditionalSourcePaths', {}, ...
      'AdditionalCompileFlags', {}, ...
      'AdditionalLinkFlags', {}, ...
      'AllowShapeAgnostic', false, ...
      'ArrayLayout', 'COLUMN_MAJOR', ...
      'EntryInfoAlgorithm', 'RTW_CAST_BEFORE_OP', ...
      'GenCallback', ' ', ...
      'ImplementationHeaderFile', ' ', ...
      'ImplementationHeaderPath', ' ', ...
      'ImplementationName', ' ', ...
      'ImplementationSourceFile', ' ', ...
      'ImplementationSourcePath', ' ', ...
      'ImplType', 'FCN_IMPL_FUNCT', ...
      'MustHaveZeroNetBias', false, ...
      'NetFixedExponet', 0, ...
      'NetAdjustmentFactor', 1, ...
      'RoundingModes', 'RTW_ROUND_UNSPECIFIED', ...
      'SaturationMode', 'RTW_SATURATE_UNSPECIFIED', ...
      'SideEffects', false, ...
      'SlopesMustBeTheSame', false, ...
      'StoreFcnReturnInLocalVar', false);

    hEntry.setTflCFunctionEntryParameters(...
    'Key', ' ', ...
    'Priority', 100, ...
    'AcceptExprInput', true, ...
    'AdditionalHeaderFiles', {}, ...
    'AdditionalIncludePaths', {}, ...
    'AdditionalLinkObjs', {}, ...
    'AdditionalLinkObjsPaths', {}, ...
    'AdditionalSourceFiles', {}, ...
    'AdditionalSourcePaths', {}, ...
    'AdditionalCompileFlags', {}, ...
    'AdditionalLinkFlags', {}, ...
    'ArrayLayout', 'COLUMN_MAJOR', ...
    'EntryInfoAlgorithm', 'RTW_DEFAULT', ...
    'GenCallback', ' ', ...
    'ImplementationHeaderFile', ' ', ...
    'ImplementationHeaderPath', ' ', ...
    'ImplementationName', ' ', ...
    'ImplementationSourceFile', ' ', ...
    'ImplementationSourcePath', ' ', ...
    'ImplType', 'FCN_IMPL_FUNCT', ...
    'RoundingModes', 'RTW_ROUND_UNSPECIFIED', ...
    'SaturationMode', 'RTW_SATURATE_UNSPECIFIED', ...
    'SideEffects', false, ...
    'StoreFcnReturnInLocalVar', false);

    hEntry.setTflCSemaphoreEntryParameters(...
    'Key', ' ', ...
    'Priority', 100, ...
    'AcceptExprInput', true, ...
    'AdditionalHeaderFiles', {}, ...
    'AdditionalIncludePaths', {}, ...
    'AdditionalLinkObjs', {}, ...
    'AdditionalLinkObjsPaths', {}, ...
    'AdditionalSourceFiles', {}, ...
    'AdditionalSourcePaths', {}, ...
    'AdditionalCompileFlags', {}, ...
    'AdditionalLinkFlags', {}, ....
    'GenCallback', ' ', ...
    'ImplementationHeaderFile', ' ', ...
    'ImplementationHeaderPath', ' ', ...
    'ImplementationName', ' ', ...
    'ImplementationSourceFile', ' ', ...
    'ImplementationSourcePath', ' ', ...
    'ImplType', 'FCN_IMPL_FUNCT', ...
    'RoundingModes', 'RTW_ROUND_UNSPECIFIED', ...
    'SaturationMode', 'RTW_SATURATE_UNSPECIFIED', ...
    'SideEffects', false, ...
    'StoreFcnReturnInLocalVar', false);

    end

  4. Create the conceptual representation.
    The conceptual representation describes the signature of the function you want to replace. It consists of the function name, return arguments, and parameters. The return arguments are referred to as output arguments, and the parameters are referred to as input arguments. Specify each conceptual argument with the required properties, and, depending on the type of replacement, other optional properties.
    Required properties:

    • Name- Defined as y1..yn for output arguments and u1..un for input arguments
    • I/O type- Defined asRTW_IO_OUTPUT orRTW_IO_INPUT
      To define your conceptual representation, use one of these approaches:
      Approach Function Goal
      1 createAndAddConceptualArg Customization and control.If you want to explicitly specify arguments as scalar or matrix and other properties.

| 2 | getTflArgFromString | Rapid prototyping.If you want to quickly specify arguments with built-in data types. |
Programming approaches are shown with the available properties set to default values. The exact properties and values you need to specify depends on the function you replace. See examples for specific replacement details.
function hTable = code_replacement_library_table()
% Create a function to call code replacement library table
%% Create a code replacement library table object
hTable = RTW.TflTable;
%% Create a code replacement library entry
% Pick one from list below
hEntry = RTW.TFLCOperationEntry;
hEntry = RTW.TflCFunctionEntry;
hEntry = RTW.TflBlasEntryGenerator;
hEntry = RTW.TflCBlasEntryGenerator;
hEntry = RTW.TflCOperationEntryGenerator;
hEntry = RTW.TflCOperationEntryGenerator_NetSlope;
hEntry = RTW.TflCSemaphoreEntry;
hEntry = RTW.TflCFunctionEntryML;
hEntry = RTW.TflCOperationEntryML;
%% Create entry parameters
% Pick one from list below
% To view properties, see step 4
hEntry.setTflCOperationEntryParameters( );
hEntry.setTflCFunctionEntryParameters( );
hEntry.setTflCSemaphoreEntryParameters( );
%% Create the conceptual representation
% Approach 1: createAndAddConceptualArg
% arg = createAndAddConceptualArg(hEntry, argType, varargin)
% argType - 'RTW.TflArgNumeric' | 'RTW.TflArgMatrix'
% varargin - 'name', 'value' pairs shown below set to default values,
% includes name and I/O type
% Define Output Arguments
hEntry.createAndAddConceptualArg(...
'RTW.TflArgNumeric', ...
'Name', 'y1', ...
'IOType', 'RTW_IO_OUTPUT', ...
'IsSigned', true, ...
'WordLength', 32, ...
'CheckSlope', true, ...
'CheckBias', true, ...
'DataTypeMode', 'Fixed-point:binary point scaling', ...
'DataType', 'Fixed', ...
'Scaling', 'BinaryPoint', ...
'Slope', 1, ...
'SlopeAdjustmentFactor', 1.0, ...
'FixedExponent', -15, ...
'Bias', 0.0, ...
'FractionLength', 15, ...
'BaseType', 'double', ...
'DimRange', [2,2]);
% Define Input Arguments
hEntry.createAndAddConceptualArg(...
'RTW.TflArgNumeric', ...
'Name', 'u1', ...
'IOType', 'RTW_IO_INPUT', ...
'IsSigned', true, ...
'WordLength', 32, ...
'CheckSlope', true, ...
'CheckBias', true, ...
'DataTypeMode', 'Fixed-point:binary point scaling', ...
'DataType', 'Fixed', ...
'Scaling', 'BinaryPoint', ...
'Slope', 1, ...
'SlopeAdjustmentFactor', 1.0, ...
'FixedExponent', -15, ...
'Bias', 0.0, ...
'FractionLength', 15, ...
'BaseType', 'double', ...
'DimRange', [2,2]);
% Approach 2: getTflArgFromString
% arg = getTflArgFromString(hEntry, name, I/O type)
% name- y1..yn | u1..un
% I/O type- RTW_IO_OUTPUT | RTW_IO_INPUT
% Define Output Arguments
arg = getTflArgFromString('y1', 'double');
arg.IOType = 'RTW_IO_OUTPUT';
hEntry.addConceptualArg(arg);
% Define Input Arguments
arg = getTflArgFromString('u1', 'double');
arg.IOType = 'RTW_IO_INPUT';
hEntry.addConceptualArg(arg);
end 6. Create the implementation representation.
The implementation representation describes the signature of the replacement function defined by the code replacement library. It consists of the function name, return arguments, and parameters. Specify the implementation arguments with a type, type qualifier, and complexity. After you define your implementation representation, add your completed entry to the code replacement table with a call to the function addEntry.
To define your implementation representation, use one of these approaches:
Programming approaches are shown with the available properties set to their default values. The exact properties and values you need to specify depends on the function you replace. See examples for specific replacement details.
function hTable = code_replacement_library_table()
% Create a function to call code replacement library table
%% Create a code replacement library table object
hTable = RTW.TflTable;
%% Create a code replacement library entry
% Pick one from list below
hEntry = RTW.TFLCOperationEntry;
hEntry = RTW.TflCFunctionEntry;
hEntry = RTW.TflBlasEntryGenerator;
hEntry = RTW.TflCBlasEntryGenerator;
hEntry = RTW.TflCOperationEntryGenerator;
hEntry = RTW.TflCOperationEntryGenerator_NetSlope;
hEntry = RTW.TflCSemaphoreEntry;
hEntry = RTW.TflCFunctionEntryML;
hEntry = RTW.TflCOperationEntryML;
%% Create entry parameters
% Pick one from list below
% To view properties, see step 4
hEntry.setTflCOperationEntryParameters( );
hEntry.setTflCFunctionEntryParameters( );
hEntry.setTflCSemaphoreEntryParameters( );
%% Create the conceptual representation
% Approach 1: createAndAddConceptualArg
% arg = createAndAddConceptualArg(hEntry, argType, varargin)
% argType - 'RTW.TflArgNumeric' | 'RTW.TflArgMatrix'
% varargin - 'name', 'value' pairs shown below set to default values,
% includes name and I/O type, to view properties, see step 5
% Define Output Arguments
hEntry.createAndAddConceptualArg( );
% Define Input Arguments
hEntry.createAndAddConceptualArg( );
% Approach 2: getTflArgFromString
% arg = getTflArgFromString(hEntry, name, I/O type)
% name- y1..yn | u1..un
% I/O type- RTW_IO_OUTPUT | RTW_IO_INPUT
% Define Output Arguments
arg = getTflArgFromString('y1', 'double');
arg.IOType = 'RTW_IO_OUTPUT';
hEntry.addConceptualArg(arg);
% Define Input Arguments
arg = getTflArgFromString('u1', 'double');
arg.IOType = 'RTW_IO_INPUT';
hEntry.addConceptualArg(arg);
%% Create the implementation representation
% Approach 1: copyConceptualArgsToImplementation
copyConceptualArgsToImplementation(hEntry);
% Approach 2: createAndSetCImplementation, createAndSetCImplementationArg
% arg = createAndAddImplementation(hEntry, argType,varargin),
% arg = createAndSetCImplementationReturn(hEntry, argType,varargin)
% argType - 'RTW.TflArgNumeric' | 'RTW.TflArgMatrix'
% varargin - 'name', 'value' pairs shown below set to default values,
% includes name and I/O type
% Define Output Arguments
hEntry.createAndSetCImplementationReturn(...
'RTW.TflArgNumeric', ...
'Name', 'y1', ...
'IOType', 'RTW_IO_OUTPUT', ...
'IsSigned', true, ...
'WordLength', 16, ...
'DataTypeMode', 'Fixed-point:binary point scaling', ...
'DataType', 'Fixed', ...
'Scaling', 'BinaryPoint', ...
'Slope', 1.0, ...
'SlopeAdjustmentFactor', 1.0, ...
'FixedExponent', -15, ...
'Bias', 0.0, ...
'FractionLength', 15, ...
'Value', 0);

% Define Input Arguments
hEntry.createAndAddImplementationArg(...
'RTW.TflArgNumeric', ...
'Name', 'u1', ...
'IOType', 'RTW_IO_INPUT', ...
'IsSigned', true, ...
'WordLength', 16, ...
'DataTypeMode', 'Fixed-point:binary point scaling', ...
'DataType', 'Fixed', ...
'Scaling', 'BinaryPoint', ...
'Slope', 1.0, ...
'SlopeAdjustmentFactor', 1.0, ...
'FixedExponent', -15, ...
'Bias', 0.0, ...
'FractionLength', 15, ...
'Value', 0);

% Approach 3: getTflArgFromString
% arg = getTflArgFromString(hEntry, name, datatype)
% name- y1..yn | u1..un, datatype - built-in data type
% Define Output arguments
arg = getTflArgFromString(hEntry, 'y1', 'double');
arg.IOType = 'RTW_IO_OUTPUT';
hEntry.Implementation.setReturn(arg);
% Define Input arguments
arg = getTflArgFromString(hEntry, 'u1', 'double', 0);
hEntry.Implementation.addArgument(arg);
% Approach 4: getTflDWorkFromString
% arg = getTflDWorkFromString(hEntry, name, datatype)
% Define arguments
arg = getTflDWorkFromString('d1', 'void*');
hEntry.addDWorkArg(arg);
% Approach 5: createCRLEntry
% tableEntry = createCRLEntry(crTable,conceptualSpecification,implementationSpecification)
% Define code replacement conceptual and implementation arguments
hEntry = createCRLEntry(...
hTable, ...
'conceptualSpecification', ...
'implementationSpecification');
%% Add code replacement library entry to table
hTable.addEntry(hEntry);
end 7. Specify build information.
Specify build information for your code replacement library. Build information provides the code generator with the files required to perform function replacement.
To specify build information:

  1. Validate and save.
    Save your code replacement library customization file with the same name as your code replacement library table on your MATLAB path.
    To save and validate your library:
    1. Save your file. From the MATLAB menu, select File > Save.
    2. Validate your library by calling it from the MATLAB command line. For example:

      hTable = code_replacement_library_table()

See Also

Topics