Main Content

Array Layout and Code Replacement

To improve execution speed of algorithms or simplify integration with external code or data, you can define code replacement mappings that apply a specific layout for storing array elements in memory. By default, the code generator stores array elements in a column-major layout. To change the layout to row-major, change the value of the entry parameter ArrayLayout to ROW_MAJOR.

The following table definition file for a sum operation uses the row-major array layout.

  1. Create a table definition file that contains a function definition. For example:

    function hTable = crl_table_rowmajor_matrix
  2. Within the function body, create the table by calling the function RTW.TflTable.

    hTable = RTW.TflTable;
  3. Create an entry for the operator mapping with a call to the RTW.TflCOperationEntry function.

    % Create operation entry
    hEnt = RTW.TflCOperationEntry;
    
  4. Set operator entry parameters with a call to the setTflCOperationEntryParameters function. In the function call, set the parameter ArrayLayout to ROW_MAJOR

    setTflCOperationEntryParameters(hEnt, ...
                        'Key',                      'RTW_OP_ADD', ...
                        'Priority',                 100, ...
                        'ArrayLayout',              'ROW_MAJOR', ...   
                        'ImplementationName',        'MyMul_ROW', ...
                        'ImplementationHeaderFile', 'MyMul_ROW.h', ...
                        'ImplementationSourceFile', 'MyMul_ROW.c');
  5. Create conceptual arguments y1, u1, and u2. There are multiple ways to set up the conceptual arguments. This example uses calls to the getTflArgFromString and addConceptualArg functions to create and add the arguments.

    arg = RTW.TflArgMatrix('y1', 'RTW_IO_OUTPUT',  'double'); 
    arg.DimRange = [2 2; 50 50]; 
    hEnt.addConceptualArg(arg); 
    
    arg = RTW.TflArgMatrix('u1', 'RTW_IO_INPUT',  'double'); 
    arg.DimRange = [2 3; 2 3]; 
    hEnt.addConceptualArg(arg); 
    
    arg = RTW.TflArgMatrix('u2', 'RTW_IO_INPUT',  'double'); 
    arg.DimRange = [3 4; 3 4]; 
    hEnt.addConceptualArg(arg); 
    
  6. Create the implementation arguments. There are multiple ways to set up the implementation arguments. This example uses calls to the getTflArgFromString function to create the arguments. When defining the implementation function return argument, create a void output argument. When defining the implementation function argument for the conceptual output argument (y1), set the operator output argument as an additional input argument. Mark its IOType as output. Make its type a pointer type. The convenience methods setReturn and addArgument specify whether an argument is a return value or argument and adds the argument to the entry’s array of implementation arguments.

    % Implementation Args  
    arg = hEnt.getTflArgFromString('unused','void'); 
    arg.IOType = 'RTW_IO_OUTPUT'; 
    hEnt.Implementation.setReturn(arg);  
    
    arg = hEnt.getTflArgFromString('u1','double*'); 
    hEnt.Implementation.addArgument(arg); 
    
    arg = hEnt.getTflArgFromString('u2','double*'); 
    hEnt.Implementation.addArgument(arg); 
    
    arg = hEnt.getTflArgFromString('y1','double*'); 
    arg.IOType = 'RTW_IO_OUTPUT'; 
    hEnt.Implementation.addArgument(arg); 
    
    arg = hEnt.getTflArgFromString('u3','uint32',2); 
    arg.Type.ReadOnly = true; 
    hEnt.Implementation.addArgument(arg); 
    
    arg = hEnt.getTflArgFromString('u4','uint32',3); 
    hEnt.Implementation.addArgument(arg); 
    
    arg = hEnt.getTflArgFromString('u5','uint32',3); 
    hEnt.Implementation.addArgument(arg); 
    
    arg = hEnt.getTflArgFromString('u6','uint32',4); 
    hEnt.Implementation.addArgument(arg); 
    
  7. Add the entry to a code replacement table with a call to the addEntry function.

    addEntry(hTable, hEnt);
  8. Save the table definition file. To name the file, use the name of the table definition function. Then, generate code and a code generation report and review the code replacements.

See Also

| |

Related Topics