Developer Reference for Intel® oneAPI Math Kernel Library for C
Matrix Layout for LAPACK Routines
There are two general methods of storing a two dimensional matrix in linear (one dimensional) memory: column-wise (column major order) or row-wise (row major order). Consider an \(M\)-by-\(N\) matrix \(A\) :
\[A = {\scriptstyle \left [ \begin{matrix} a_{1,1} & a_{1,2} & \ldots & a_{1,j_0+1} & a_{1,j_0+2} & \ldots & a_{1,j_0+L-1} & a_{1,j_0+L} & \ldots & a_{1,N} \\ a_{2,1} & a_{2,2} & \ldots & a_{2,j_0+1} & a_{2,j_0+2} & \ldots & a_{2,j_0+L-1} & a_{2,j_0+L} & \ldots & a_{2,N} \\ \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\ a_{i_0+1,1} & a_{i_0+1,2} & \ldots & a_{i_0+1,j_0+1} & a_{i_0+1,j_0+2} & \ldots & a_{i_0+1,j_0+L-1} & a_{i_0+1,j_0+L} & \ldots & a_{i_0+1,N} \\ a_{i_0+2,1} & a_{i_0+2,2} & \ldots & a_{i_0+2,j_0+1} & a_{i_0+2,j_0+2} & \ldots & a_{i_0+2,j_0+L-1} & a_{i_0+2,j_0+L} & \ldots & a_{i_0+2,N} \\ \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\ a_{i_0+K-1,1} & a_{i_0+K-1,2} & \ldots & a_{i_0+K-1,j_0+1} & a_{i_0+K-1,j_0+2} & \ldots & a_{i_0+K-1,j_0+L-1} & a_{i_0+K-1,j_0+L} & \ldots & a_{i_0+K-1,N} \\ a_{i_0+K ,1} & a_{i_0+K ,2} & \ldots & a_{i_0+K ,j_0+1} & a_{i_0+K ,j_0+2} & \ldots & a_{i_0+K ,j_0+L-1} & a_{i_0+K ,j_0+L} & \ldots & a_{i_0+K ,N} \\ \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\ a_{M,1} & a_{M,2} & \ldots & a_{M,j_0+1} & a_{M,j_0+2} & \ldots & a_{M,j_0+L-1} & a_{M,j_0+L} & \ldots & a_{M,N} \end{matrix} \right ]}\]
Column Major Layout
In column major layout the first index, \(i\), of matrix elements \(a_{i,j}\) changes faster than the second index when accessing sequential memory locations. In other words, for \(1 \leq i < M\) , if the element \(a_{i,j}\) is stored in a specific location in memory, the element \(a_{i+1,j}\) is stored in the next location, and, for \(1 \leq j < N\), the element \(a_{M,j}\) is stored in the location previous to element \(a_{1,j+1}\) . So the matrix elements are located in memory according to this sequence:
\[\{ a_{1,1}~a_{2,1}~\dots~a_{M,1}~a_{1,2}~a_{2,2}~\dots~a_{M,2}~\dots~a_{1,N}~a_{2,N}~\dots~a_{M,N} \}\]
Row Major Layout
In row major layout the second index, \(j\) , of matrix elements \(a_{i,j}\) changes faster than the first index when accessing sequential memory locations. In other words, for \(1 \leq j < N\) , if the element \(a_{i,j}\) is stored in a specific location in memory, the element \(a_{i,j+1}\) is stored in the next location, and, for \(1 \leq i < M\) , the element \(a_{i,N}\) is stored in the location previous to element \(a_{i+1,1}\) . So the matrix elements are located in memory according to this sequence:
\[\{ a_{1,1}~a_{1,2}~\dots~a_{1,N}~a_{2,1}~a_{2,2}~\dots~a_{2,N}~\dots~a_{M,1}~a_{M,2}~\dots~a_{M,N} \}\]
Leading Dimension Parameter
A leading dimension parameter allows use of LAPACK routines on a submatrix of a larger matrix. For example, the submatrix \(B\) can be extracted from the original matrix \(A\) defined previously:
\[B = {\scriptstyle \left [ \begin{matrix} a_{i_0+1,j_0+1} & a_{i_0+1,j_0+2} & \ldots & a_{i_0+1,j_0+L-1} & a_{i_0+1,j_0+L} \\ a_{i_0+2,j_0+1} & a_{i_0+2,j_0+2} & \ldots & a_{i_0+2,j_0+L-1} & a_{i_0+2,j_0+L} \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ a_{i_0+K-1,j_0+1} & a_{i_0+K-1,j_0+2} & \ldots & a_{i_0+K-1,j_0+L-1} & a_{i_0+K-1,j_0+L} \\ a_{i_0+K ,j_0+1} & a_{i_0+K ,j_0+2} & \ldots & a_{i_0+K ,j_0+L-1} & a_{i_0+K ,j_0+L} \end{matrix} \right ]}\]
\(B\) is formed from rows with indices \(i_{0} + 1\) to \(i_{0} + K\) and columns \(j_{0} + 1\) to \(j_{0} + L\) of matrix \(A\) . To specify matrix \(B\) , LAPACK routines require four parameters:
the number of rows \(K\) ;
the number of columns \(L\) ;
a pointer to the start of the array containing elements of \(B\) ;
the leading dimension of the array containing elements of \(B\) .
The leading dimension depends on the layout of the matrix:
Column major layout
Leading dimension \(ldb = M\) , the number of rows of matrix \(A\).
Starting address: offset by \(i_{0} + j_{0} \cdot ldb\) from \(a_{1,1}\) .

Row major layout
Leading dimension \(ldb = N\) , the number of columns of matrix \(A\) .
Starting address: offset by \(i_{0} \cdot ldb + j_{0}\) from \(a_{1,1}\) .
