You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working with symmetric, upper-triangular, and lower-triangular matrices in LinearAlgebra, it is necessary to allocate $N^2$ numbers, but there are only at most $N(N+1)/2$ distinct entries. This is in contrast to common Fortran linear algebra packages.
Consider the following code:
using LinearAlgebra;
N =5;
x =zeros(N,N);
z =0.;
for j in1:N
for i in1:j
z+=1;
x[i,j] = z;
endend
xut =UpperTriangular(x);
xsym =Symmetric(x);
x === xut.data # returns true
x === xysm.data # returns true
One can see that UpperTriangular and Symmetric are just wrapping the memory allocated in x which means that if you don't actually care about the lower-triangular part of x, you're unnecessarily storing $N(N-1)/2 = O(N^2/2)$ zeroes! The purpose of these wrappers is so that when some routine from LinearAlgebra is called, the appropriate method optimized for the matrix type is called. In BLAS, LAPACK, and CuBLAS, there are equivalent routines which support a "packed" format which essentially stores the non-zero/necessary entries as a vector in column-major-like form. In this packed format, xut.data would be stored as [1.0, 2.0, 3.0, ..., 15.0]. Here are some links to the documentation where some of these functions and features are discussed:
I think exposing these routines in LinearAlgebra and CUDA.jl would be useful and improve memory efficiency by a factor of two, approximately. I imagine there might be some new types, like LowerTriangularPacked, UpperTriangularPacked, SymmetricPacked, whose data fields would be the packed format used in these linear algebra routines. Some implementations that might be nice besides the access to the BLAS, LAPACK, and CuBlas routines could be:
A constructor that takes a Matrix-typed variable like x and converts it to a packed-type variable by extracting and flattening the appropriate entries
A constructor that directly takes a Vector-typed variable in a packed format
Appropriate indexing: provide a row-column index like xut[i,j] and access the appropriate linear index in the packed format
Iterate over relevant indices in an efficient way
A method to convert these packed types back to dense matrices
I would greatly appreciate it if this were implemented, and I believe it would improve Julia as a whole. Thanks!
The text was updated successfully, but these errors were encountered:
This is nice. Thank you so much for sharing. I initially became interested in this because I am memory-limited on a GPU for a project, so ideally this would work on the GPU with CUDA.jl. I also would enjoy seeing this implemented in the default LinearAlgebra with appropriate method and in a packed format with pretty-printing, but I understand that this might not happen.
When working with symmetric, upper-triangular, and lower-triangular matrices in LinearAlgebra, it is necessary to allocate$N^2$ numbers, but there are only at most $N(N+1)/2$ distinct entries. This is in contrast to common Fortran linear algebra packages.
Consider the following code:
For reference,
x
looks like:One can see that$N(N-1)/2 = O(N^2/2)$ zeroes! The purpose of these wrappers is so that when some routine from LinearAlgebra is called, the appropriate method optimized for the matrix type is called. In BLAS, LAPACK, and CuBLAS, there are equivalent routines which support a "packed" format which essentially stores the non-zero/necessary entries as a vector in column-major-like form. In this packed format,
UpperTriangular
andSymmetric
are just wrapping the memory allocated inx
which means that if you don't actually care about the lower-triangular part ofx
, you're unnecessarily storingxut.data
would be stored as[1.0, 2.0, 3.0, ..., 15.0]
. Here are some links to the documentation where some of these functions and features are discussed:I think exposing these routines in LinearAlgebra and CUDA.jl would be useful and improve memory efficiency by a factor of two, approximately. I imagine there might be some new types, like
LowerTriangularPacked
,UpperTriangularPacked
,SymmetricPacked
, whosedata
fields would be the packed format used in these linear algebra routines. Some implementations that might be nice besides the access to the BLAS, LAPACK, and CuBlas routines could be:Matrix
-typed variable likex
and converts it to a packed-type variable by extracting and flattening the appropriate entriesVector
-typed variable in a packed formatxut[i,j]
and access the appropriate linear index in the packed formatI would greatly appreciate it if this were implemented, and I believe it would improve Julia as a whole. Thanks!
The text was updated successfully, but these errors were encountered: