In-place code replacement is an optimization technique that
uses a single buffer, that is, the same memory, to store function
input and output data, as in x=foo(x)
.
You can develop a code replacement library to optimize in-place function argument replacement . To develop a code replacement library use either the interactive or programmatic approach. For more information, see Develop a Code Replacement Library.
Open the Code Replacement Tool (crtool), from the MATLAB command line with the following command:
>>crtool
Create a table.
From the crtool context menu, select File > New Table.
In the right pane, name the table crl_table_inplace
.
Click Apply.
Create an entry. From the crtool context menu, select File > New entry > Function.
Create entry parameters. In the Function drop-down list,
select custom
. Function information appears in the crtool. For
this example, specify the parameter as customFunction
.
Create the conceptual representation. The conceptual representation describes the signature of the function that you want to replace. In the Conceptual function subsection of the crtool, specify the return argument, y1, and the input argument, u1, with the Data Type of double and the Argument Type of Pointer.
Create the implementation representation. The implementation representation
describes the signature of the optimization function. Add the implementation
parameter y1
and u1
. For each input argument
that you want to specify as in-place with a corresponding output argument, in the
Argument properties box, select the
Pointer check box. For the implementation parameter
y1
in the Argument properties section of
the dialog box, set the In-place argument to
u1
.
Specify build information. Click the Build Information tab to open the build requirements pane. Specify the files (source, header, object) that the code generator requires for code replacement. For this example, you do not need to specify build information.
Validate and save the table. In the Mapping Information tab, click Validate entry. In the crtool context menu, select File > Save table > Save.
Register a code replacement library. Registration creates a library composed of the tables that you specify. Select File > Generate registration file. In the Generate registration file dialog box, fill out these fields:
To use your code replacement library, refresh your current MATLAB session with the command:
>>sl_refresh_customizations
Verify the code replacement library. From the MATLAB command line, open the library by using the Code Replacement Viewer and verify that the table and entry are correctly specified. For more information, see Verify Code Replacement Library. Configure your model to use the code replacement library, generate code, and verify that replacement occurs as expected. If unexpected behavior occurs, examine the hit and miss logs to troubleshoot the issues.
Open the programmatic interface from the MATLAB menu by selecting New > Function.
Create a table.
Create a function with the name of your code replacement library table that does not have arguments and returns a table object. You can use this function to call your code replacement library table.
Create a table object by calling RTW.TflTable
.
function hTable = crl_table_inplace() % Create a function to call the code replacement library table %% Create a table object hTable = RTW.TflTable;
Create an entry. Because this example replaces a function, create a code
replacement entry in your table by calling the entry function RTW.TflCFunctionEntry
.
function hTable = crl_table_inplace % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = rtw.TflCFunctionEntry;
Create entry parameters. Because this examples replaces a function, create entry
parameters by calling the function setTflCFunctionEntryParameters
.
function hTable = crl_table_inplace() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCFunctionEntry; %% Create entry parameters hEntry.setTflCFunctionEntryParameters(... 'Key', 'customFunction', ... 'Priority', 100, ... 'ImplementationName', 'custom_function_inplace_impl', ... 'SideEffects', true);
Create the conceptual representation. The conceptual representation describes the
signature of the function that you want to replace. To explicitly specify argument
properties, call the function getTflArgFromString
and addConceptualArg
.
function hTable = crl_table_inplace() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCFunctionEntry; %% Create entry parameters hEntry.setTflCFunctionEntryParameters(... 'Key', 'customFunction', ... 'Priority', 100, ... 'ImplementationName', 'custom_function_inplace_impl', ... 'SideEffects', true); %% Create the conceptual representation arg = getTflArgFromString(hEntry, 'y1','double'); arg.IOType = 'RTW_IO_OUTPUT'; addConceptualArg(hEntry, arg); arg = getTflArgFromString(hEntry, 'u1','double'); addConceptualArg(hEntry, arg);
Create the implementation representation. The implementation representation
describes the signature of the optimization function. This example uses calls to the
getTflArgFromString
function to
create implementation arguments that map to arguments in the replacement function
prototype: output argument y1
and input argument
u1
. For each argument, the example uses the
convenience method setReturn
or
addArgument
to specify whether an argument is a return
value or argument. For each argument, this example adds the argument to the entry
array of implementation arguments.
function hTable = crl_table_inplace() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCFunctionEntry; %% Create entry parameters hEntry.setTflCFunctionEntryParameters(... 'Key', 'customFunction', ... 'Priority', 100, ... 'ImplementationName', 'custom_function_inplace_impl', ... 'SideEffects', true); %% Create the conceptual representation arg = getTflArgFromString(hEntry, 'y1','double'); arg.IOType = 'RTW_IO_OUTPUT'; addConceptualArg(hEntry, arg); arg = getTflArgFromString(hEntry, 'u1','double'); addConceptualArg(hEntry, arg); %% Create the Implementation Representation arg = getTflArgFromString(hEntry, 'y2','void'); arg.IOType = 'RTW_IO_OUTPUT'; hEntry.Implementation.setReturn(arg); arg = getTflArgFromString(hEntry, 'u1','double*'); arg.ArgumentForInPlaceUse = 'y1'; hEntry.Implementation.addArgument(arg); %% Add the entry to the table hTable.addEntry(hEntry);
Specify build information. In the entry parameters, specify files (header, source, object) that the code generator needs for code replacement. For this example, build information is not required.
Validate and save the customization file. From the MATLAB menu, save this customization file by selecting File > Save. From the command line, validate the code replacement library table by calling it:
>> hTable = crl_table_inplace
Register the code replacement library. Registration creates a code replacement library by defining the library name, code replacement tables, and other information. Create a registration file (a new function file) with these specifications:
function rtwTargetInfo(cm) cm.registerTargetInfo(@loc_register_crl); end function this = loc_register_crl this(1) = RTW.TflRegistry; this(1).Name = 'CRL for inplace function replacement’; this(1).TableList = {'crl_table_inplace.m'}; % table created in this example this(1).TargetHWDeviceType = {'*'}; this(1).Description = 'Example code replacement library'; end
To use your code replacement library, refresh your current MATLAB session with the command:
>>sl_refresh_customizations
Verify the code replacement library. From the MATLAB command line, open the library by using the Code Replacement Viewer and verify that the table and entry are correctly specified. For more information, see Verify Code Replacement Library. Configure your model to use the code replacement library, generate code, and verify that replacement occurs as expected. If unexpected behavior occurs, examine the hit and miss logs to troubleshoot the issues.