Sparse matrix memory usage clarification

5 次查看(过去 30 天)
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.

类别

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