AG

January 31, 2022by Dr Abhinav Gupta

Every researcher must learn about sparse matrices.

You can not even imagine solving medium to large-scale systems without sparse matrices.

The problem

When students learn to code FEM, they are usually not taught about sparse matrices. This is done to keep the code as well as the learning simple. Students are first taught how to code FEM, and then once they have to handle real problems, they are taught about sparse matrix operations. But unfortunately, most of the time, the semester ends without introducing sparse matrices, their importance and how to code them.

You can not even imagine solving medium to large-scale systems without sparse matrices.

Real world systems are usually somewhere between a few thousand to well over a million degrees of freedom (nn). What this means is that the resulting matrix in the mathematical model of the system will be n×nn \times n. Luckily in the FEM, the matrices are usually sparse and we can solve for a big system. But most researchers do not understand how the size of the system is related to the computers ability to solve it.

background

Lets try to understand this:

  • Every number takes a certain amount of RAM space. Remember 1 byte = 8 bits and
Data TypeSizeDescription
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
boolean1 bitStores true or false values
char2 bytesStores a single character/letter or ASCII values
  • In general we work with double (8 bytes) data type to store data and with int (4 bytes) to store indexes.

  • 1MB = 1e6 Bytes

  • 1GB = 1e9 Bytes = 1000 MB

  • 1TB = 1e12 Bytes = 1000 GB

  • Considering a tridiagonal system where we have non zero entities only on the main diagonal and adjacent to it here is a comparison of RAM requirement for dense v/s sparse system.

    RAM for dense storage: dof2×8109\frac{dof^2 \times 8}{10^9}GB

    RAM for sparse storage: 3×(dof×8+dof×4+dof×4)109\frac{3 \times (dof \times 8+dof \times 4+dof \times 4)}{10^9}GB

    dofNum itemsRAM for DenseRAM for Sparse
    10001 Million8 MB0.048 MB
    10,0001e8800 MB0.48 MB
    1 Lakh1e1080 GB 🧐4.8 MB
    1 Million1e128 TB 😨48 MB
    10 Million1e14800 TB 😱480 MB
    100 Million1e160.8 Million TB 🤯4.8 GB
    1 Billion1e1880 Million TB ☠️48 GB
  • The above table is formed by considering that every cell of the matrix will hold a double value (8 bytes) and the indices are stored as integers (4 bytes) for the sparse matrix. A sparse matrix is stored as a 3 column matrix with first column being data the second being row number and the third being column number.

  • Thus you can see that by using a dense matrix you will exhaust you laptop memory even with a 1Lakh dof system.

The solution

If you are a researcher working with matrices, you should learn and understand how to work with sparse matices. It is the only way to solve large systems on your computer. There is no other alternative.

  • You can find the code for this blog here
  • Start learning about sparse matrix algebra here and here

References