I would like to assess how large Armadillo sparse matrices are. The question is related to this answer regarding dense matrices.
Consider the following example:
void some_function(unsigned int matrix_size) {
arma::sp_mat x(matrix_size, matrix_size);
// Steps entering some non-zero values
std::cout << sizeof(x) << std::endl;
}
Unfortunately, sizeof
does, as in the dense matrix case, not return the size of the matrix itself, but rather the size of a pointer or some other small object. The size of the sparse matrix should not simply be the number of non-zero elements times the data type's size. Armadillo stores sparse matrices in a compressed format. And on top of the cell values, there should also be a matrix or vectors storing the cell indices. And I guess that the matrix also has a header storing information about the object.
CodePudding user response:
There are three key properties:
- n_rows
- n_cols and
- n_nonzero
The last value represents the number of cells 0 <= n_nonzero <= (n_rows*n_cols)
which have a value.
You can use this to know the density (which is also displayed as a percentage with .print
, e.g.
[matrix size: 3x3; n_nonzero: 4; density: 44.44%]
(1, 0) 0.2505
(0, 1) 0.9467
(0, 2) 0.2513
(2, 2) 0.5206
I used these properties to implement sp_matrix serialization before: How to serialize sparse matrix in Armadillo and use with mpi implementation of boost?
The actual number of bytes allocated will be roughly correlating to n_nonzero
, but you have to account for /some/ overhead. In practice the best way to measure actual allocations is by using instrumented allocators, or (the logical extension of that idea) memory profilers. See e.g. How to find the memory occupied by a boost::dynamic_bitset?