Contents

# 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
: ## 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
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
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
...
a
M
,1
a
1,2
a
2,2
...
a
M
,2
... ...
a
1,
N
a
2,
N
...
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
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
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
...
a
1,
N
a
2,1
a
2,2
...
a
2,
N
... ...
a
N
,1
a
N
,2
...
a
M
,
N
}

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
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
ldb
=
M
, the number of rows of matrix
A
.
i
0
+
j
0
*
ldb
from
a
1,1
. • Row major layout
ldb
=
N
, the number of columns of matrix
A
.
i
0
*
ldb
+
j
0
from
a
1,1
. #### Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.