Sparse matrix memory usage clarification

조회 수: 5 (최근 30일)
Carl LEe
Carl LEe 2020년 11월 14일
댓글: Carl LEe 2020년 11월 14일
I am trying to understand the memory usage of a sparse matrix.
I have read the following:
Strategies for Efficient Use of Memory:
Sub-Topic : Make Arrays Sparse When Possible
Basic calculation for memory consumption :
In general, for a sparse double array with nnz nonzero elements and ncol columns, the memory required is
- 16 * nnz + 8 * ncol + 8 bytes (on a 64 bit machine)
- 12 * nnz + 4 * ncol + 4 bytes (on a 32 bit machine)
where
nnz = Number of non-zeros
ncol = Number of columns of the Sparse Matrix
I have a 64bit machine running 2019b.
I have the following code that does not follow the basic calculation for the above, namely, nnz does not seem to matter here.
For a sparse matrix of 6x6, after more than 1 element, the memory usage seem to remain stagnant at 232 bytes with increasing elements (until a dense enough matrix).
It seems that the calculation above does not match what I have obtained via whos for the memory of the sparse matrix.
Please advise on what I am missing.
Thank you!
a = sparse(6,6);
a(1,1)=1;
a(2,1) = 2;
a(1,2)=2;
a(2,2)=-1;
a(3,2)=2;
a(2,3)=2;
a(3,3)=-1;
a(4,3)=2;
a(3,4)=2;
a(4,4)=1;
a(6,6)=3;
whos("a")
Name Size Bytes Class Attributes
a 6x6 232 double sparse
a = sparse(6,6);
a(1,1)=1;
whos("a")
Name Size Bytes Class Attributes
a 6x6 72 double sparse
a = sparse(6,6);
a(1,1)=1;
a(2,1) = 2;
whos("a")
Name Size Bytes Class Attributes
a 6x6 232 double sparse

채택된 답변

Bruno Luong
Bruno Luong 2020년 11월 14일
MATLAB allocates a larger memory than nnz for sparse matrix, it's call nzmax, with some strategy that is not documented. You should not count with nnz but with nzmax.
>> a=sparse(6,6);
>> a(1,1)=1
a =
(1,1) 1
>> whos a
Name Size Bytes Class Attributes
a 6x6 72 double sparse
>> nzmax(a)
ans =
1
>> a(2,1)=2
a =
(1,1) 1
(2,1) 2
>> whos a
Name Size Bytes Class Attributes
a 6x6 232 double sparse
>> nzmax(a)
ans =
11
>>
  댓글 수: 1
Carl LEe
Carl LEe 2020년 11월 14일
Thank you! This helps me to somewhat compute the memory requirements of my large matrix, before execution.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2020년 11월 14일
편집: James Tursa 2020년 11월 14일
Sparse matrices can be allocated with more than the minimum memory to hold the non-zero elements and their row indexes (see nzmax). When extra non-zero elements are inserted, they can use this extra memory in the background without increasing the sparse matrix memory footprint until the extra memory is used up, at which point a reallocation of memory is needed. The reallocation can be for more than one element in the background, giving the sparse matrix some new extra memory. The rules for how MATLAB does this in the background and how much extra memory will be allocated are not published. The memory formulas you see are generic and only refer to the minimum amount needed ... not necessarily the same as the actual amount allocated.
  댓글 수: 1
Carl LEe
Carl LEe 2020년 11월 14일
Thanks for your prompt reply, and pointing me in the right direction.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by