Array of rows compared to the main arrangement of the columns of the matrices

When programming dense matrix calculations, is there any reason to choose the layout of the main row above the layout of the column?

I know that depending on the layout of the selected matrix, we need to write the appropriate code in order to effectively use cache memory for speed purposes.

The layout of the line string seems more natural and simple (at least for me). But major libraries, such as LAPACK, which are written in Fortran, use a basic column layout, so there is a definite reason for this.

+4
source share
2 answers

FORTRAN was developed to solve scientific and technical problems. A repository with a large number of columns is more natural from a scientific point of view, since the general rule of linear algebra uses column vectors and often treats matrices as concatenations of column vectors. In matrix vector multiplications, column vectors are located on the right side (after multiplication), with subsequent matrices being added further on the left side, for example. B*(A*x) . Languages โ€‹โ€‹such as COBOL, PL / 1, and C treat matrices as collections of row entries, so row ordering is more natural for them.

In linear algebra, a vector is represented by its coordinates: x = x[1]*e1 + x[2]*e2 + ... + x[n]*en where x[i] are vector coordinates, and ei is the i th basic vector. In the matrix representation, the basis vectors are column vectors. Then the linear operator A acting on x gives:

 y = A*x = A*{x[1]*e1 + x[2]*e2 + ... x[n]*en} = x[1]*(A*e1) + x[2]*(A*e2) + ... x[n]*(A*en) 

In the matrix representation, the linear operator A consists of columns n , column i is the result of A acting on the ith basis vector, and A*x then just a linear combination of columns A with coefficients for x coordinates. In FORTRAN, this will be:

 ! Zero out the result vector DO k = 1,n y(k) = 0.0 END DO ! Iterate over the columns of A DO i = 1,n ! Add the i-th column to the linear combination with a weight of x(i) w = x(i) DO k = 1,n y(k) = y(k) + w*A(k,i) END DO END DO 

This automatically takes advantage of column storage A This may seem uncomfortable, but back in the 50s, when FORTRAN was born, the FMAC hardware and register optimization were not at all as popular as they are now.

+16
source

I do not see any difference. Or this is a great way to convert a multidimensional matrix into a linear memory order. The equations for the transformation are very similar.

There is also / and \. And the big and small Indian. Someone made a choice, and later another made a different choice.

0
source

All Articles