How to calculate memory required to do mldivide?

13 views (last 30 days)
Hi, I am using mldivide to solve systems of linear equations (finding x in Ax = b). In some cases I run out of memory if solving for too many outputs, is there a way to estimate the memory requirement to do mldivide in order to not attempt it in case there's not enough memory available?
A is square symmetric positive definite sparse matrix of a size currently ranging from 10,000^2 to 50,000^2 but could possibly increase in size in the future.
b is also a sparse of a size length(A) x nColumns where nColumns can range from 1 to 200;
I have tried using
profile -memory on;
but it didn't show any memory usage for the mldivide.
I can find the amount of memory available which is 23.2GB in my case.
mem = memory;
mem.MaxPossibleArrayBytes / (1024^3)
ans =
23.2091
But I can't figure out a way to relate matrix size to memory required.
Thanks,
Titas

Accepted Answer

John D'Errico
John D'Errico on 30 May 2018
You can't do it in general if the matrix is sparse, because the sparsity pattern of the matrix is the important factor. How many non-zeros there are, as well as where do they lie? Fill-in is the killer. So there is no simple formula to know the memory required, and there is no easy way to predict how much memory it will use in advance.
If all of your matrices have a consistent sparsity pattern, for example, they are all discretizations of the same PDE, just for different size meshes on a square grid, then you could run an experiment. You would need to see the memory used while you ran the code. If necessary, use an external monitoring tool to watch the memory used by MATLAB, as you steadily increase the size of your problem. Then estimate a simple model of the memory consumed, something of this form:
Memory = a + b*n^p
But even then, remember that you will have memory problems if MATLAB cannot find a contiguous block of memory of the required size. So depending on what else lies in memory, you may or may not be able to solve the problem.
Perhaps a better solution is to not use mldivide at all. Instead, switch to using one of the iterative solvers in MATLAB, as they do not need to factorize the matrix. In some cases, this may require you to supply a preconditioner to improve the performance of the method. It has been a while since I used those tools (15-20 years since I was using them regularly), but you may want to consider an incomplete LU (ilu) or Cholesky (ichol) as a preconditioner.
  4 Comments
Titas Bucelis
Titas Bucelis on 30 May 2018
Exactly. Your suggestion was briliant by the way. Iterative solver has reduced the time to calculate 50,000 result from 6 minutes to 5 seconds! Although it does not outperform mldivide for 100 x 10,000 solutions so I'll still have to figure out a way to choose a way to solve depending on a case. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!