Code Generation of Matrices and Arrays - MATLAB & Simulink (original) (raw)

MATLAB® stores matrix data and arrays (1–D, 2–D, ...) in column-major format as a vector. Simulink® and the code generator can store array data in column-major or row-major format. For an array stored in column-major layout, the elements of the columns are contiguous in memory. In row-major layout, the elements of the rows are contiguous. Array layout is also referred to as order, format, and representation. The order in which elements are stored can be important for integration, usability, and performance. Certain algorithms perform better on data stored in a particular order.

Programming languages and environments assume a single array layout for all data. MATLAB and Fortran use column-major layout by default, whereas C and C++ use row-major layout. With Simulink Coder™, you can generate C/C++ code that uses row-major layout or column-major layout.

Array Storage in Computer Memory

Computer memory stores data in terms of one-dimensional arrays. For example, when you declare a 3-by-3 matrix, the software stores this matrix as a one-dimensional array with nine elements. By default, MATLAB stores these elements with a column-major array layout. The elements of each column are contiguous in memory.

Consider the matrixA:

A translates to an array of length 9 in this order:

A(1) = A(1,1) = 1; A(2) = A(2,1) = 4; A(3) = A(3,1) = 7; A(4) = A(1,2) = 2; A(5) = A(2,2) = 5;

and so on.

In column-major format, the next element of an array in memory is accessed by incrementing the first index of the array. For example, these element pairs are stored sequentially in memory:

The matrix A is represented in memory by default with this arrangement:

In row-major array layout, the programming language stores row elements contiguously in memory. In row-major layout, the elements of the array are stored as:

You can store the N-dimensional arrays in column-major or row-major layout. In column-major layout, the elements from the first (leftmost) dimension or index are contiguous in memory. In row-major layout, the elements from the last (rightmost) dimension or index are contiguous.

For more information on the internal representation of MATLAB data, see MATLAB Data.

Code generation software uses column-major format by default for several reasons:

C uses row-major format. MATLAB and Simulink use column-major format by default. You can configure the code generation software to generate code with a row-major array layout. If you are integrating external C code with the generated code, see the considerations listed in this table.

Action Consider
Configure array layout of the model for code generation. In the Configuration Parameters dialog box, set model configuration parameterArray layout toColumn-major orRow-major.
Enable efficient row-major algorithms for simulation and code generation. Select model configuration parameter Use algorithms optimized for row-major array layout.
Integrate external C code functions in row-major array layout with the generated code. Create S-functions that integrate external code functions with the generated code by using:The SimStruct function ssSetArrayLayoutForCodeGen.The Set Array Layout setting in the S-Function Builder block.The Legacy Code Tool option convert2DMatrixToRowMajor in legacy_code. Usingconvert2DMatrixToRowMajor impacts code efficiency.Use the C Caller block to call external C functions into Simulink. Specify array layout of custom C functions by using model configuration parameter Default function array layout.You can also use coder.ceval in a MATLAB Function block. See Interface with Row-Major Data in MATLAB Function Blocks.

Code Generator Matrix Parameters

The compiled model file, _`model`_.rtw, represents matrices as character vectors in MATLAB syntax, without an implied storage format. This format enables you to copy the character vector out of an .rtw file, paste it into a MATLAB file, and have MATLAB recognize it.

Column-Major Layout

For example, the 3-by-3 matrix in the Constant block

is stored in _`model`_.rtw as

Parameter { Identifier "Constant_Value" LogicalSrc P0 Protected no Value [1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0] CGTypeIdx 18 ContainerCGTypeIdx 19 ReferencedBy Matrix(1,4) [[0, -1, 0, 0];] GraphicalRef Matrix(1,2) [[0, 1];] BHMPrmIdx 0 GraphicalSource [0, 1] OwnerSysIdx [0, -1] VarGroupIdx [1, 0] WasAccessedAsVariable 1 }

The _`model`__data.c file declares the actual storage for the matrix parameter. You can see that the format is in column-major layout.

Parameters _model__P = { /* Expression: [ [1,2,3] ; [4,5,6] ;[7,8,9]]

Row-Major Layout

For example, the 3-by-3 matrix in the Constant block

is stored in _`model`_.rtw as

Parameter { Identifier "Constant_Value" LogicalSrc P0 Protected no Value [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] CGTypeIdx 18 ContainerCGTypeIdx 19 ReferencedBy Matrix(1,4) [[0, -1, 0, 0];] GraphicalRef Matrix(1,2) [[0, 1];] BHMPrmIdx 0 GraphicalSource [0, 1] OwnerSysIdx [0, -1] VarGroupIdx [1, 0] WasAccessedAsVariable 1 }

The _`model`__data.h file declares the actual storage for the matrix parameter. You can see that the format is in row-major layout.

Parameters _model__P = { /* Expression: [ [1,2,3] ; [4,5,6] ;[7,8,9]]

Internal Data Storage for Complex Number Arrays

Simulink and code generator internal data storage formatting differs from MATLAB internal data storage formatting only in the storage of complex number arrays. In MATLAB, the real and imaginary parts are stored in separate arrays. In Simulink and the code generator, the parts are stored in an interleaved format. The numbers in memory alternate real, imaginary, real, imaginary, and so forth. This convention allows efficient implementations of small signals on Simulink lines, for Mux blocks, and other virtual signal manipulation blocks. For example, the signals do not actively copy their inputs, just the references.

Unsupported Blocks for Row-Major Code Generation

The code generator does not support these blocks for code generation in row-major array layout.

Continuous

User-Defined Functions

Sources

Signal Attributes

Discrete

The code generator does not support these blocks for code generation in row-major array layout if the Input processing block parameter is set toElements as channels (sample based) and the block is connected to a State Reader or State Writer block.

Additional Limitations for Row-Major Code Generation

Model Advisor Checks for Row-Major Code Generation

Array layout can be important for integration, usability, and performance. Programming languages and environments assume a single array layout for all data; you can generate C/C++ code that uses row-major layout. The code that you generate by using row-major algorithms performs with better speed and efficient memory usage when operating on data with row-major array layout.

To check that your model is configured for efficient row-major code generation and check the compatibility of existing elements with row-major code generation, use the Model Advisor checks in folder > . Checks include:

See Also