# OutOfMemoryError() while solving a linear system

**URL:** <https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921>\
**Category:** Performance\
**Created:** [January 29, 2020, 9:58am UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921 "2020-01-29T09:58:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 29, 2020, 9:58am UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/1 "2020-01-29T09:58:19Z")

</div>

I am trying to solve a linear system with the direct solver (backslash operator). My matrix is sparse,square and unsymmetric. The system is defined as `Ax=b`, where A is defined to be a SparseMatrixCSC with dimensions = 803800 x 803800 `nnz(A_matrix)=808613995`. the `b` vector is dense and defined to be like that. My A matrix is very close to be a banded matrix, but it is not, due to some elements on the corners, so I believe that the solver is LU factorizing the matrix and then solving the system. I am running my code on a 16 GB RAM laptop. My question is how to know the size limit of my linear system which I can solve on this machine given the sparsity pattern of my matrix?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 29, 2020, 10:33am UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/2 "2020-01-29T10:33:53Z")

</div>

Think of the memory consumption of such a system: you would need

```julia
julia> (808613995 * 16) / (2^30)
12.049287483096123

```

gigabytes to represent the nonzero entries and the row indexes (`Float64`, and `Int`).

Maybe someone can suggest something, but I am not sure that doing this in 16GB is feasible.

Consider even with iterative methods with lower memory requirements, eg

> **[GitHub - JuliaLinearAlgebra/IterativeSolvers.jl: Iterative algorithms for...](https://github.com/JuliaLinearAlgebra/IterativeSolvers.jl)**
>
> Iterative algorithms for solving linear systems, eigensystems, and singular value problems - GitHub - JuliaLinearAlgebra/IterativeSolvers.jl: Iterative algorithms for solving linear systems, eigens...

but even these would need more RAM I assume.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 29, 2020, 10:44am UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/3 "2020-01-29T10:44:10Z")

</div>

Thanks !

> [@Tamas\_Papp](#):
>
> but even these would need more RAM I assume.

This is true. Unless you have a good preconditioner, IterativeSolver will take ages and also massive memory.

---

<div class="post-metadata">

**Author:** ![amontoison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amontoison/32/218741_2.png) [@amontoison](https://discourse.julialang.org/u/amontoison)\
**Post date:** [February 10, 2020, 5:57am UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/4 "2020-02-10T05:57:54Z")

</div>

Normally, a Krylov method only allocates few n-vectors, where n is the dimension of your matrix. With n = 803800, a n-vector only needs 6.13Mo. So iterative methods could be relevant in your case.

I can recommend you to test DQGMRES method, you can define the number of n-vectors that you want to use during the Arnoldi process. It’s a kind of limited memory variant of GMRES.

> **[GitHub - JuliaSmoothOptimizers/Krylov.jl: A Julia Basket of Hand-Picked...](https://github.com/JuliaSmoothOptimizers/Krylov.jl)**
>
> A Julia Basket of Hand-Picked Krylov Methods. Contribute to JuliaSmoothOptimizers/Krylov.jl development by creating an account on GitHub.

```plaintext
(x, stats) = dqgmres(A, b, memory=400, itmax=10000, verbose=true)

```

You can adjust parameters (set verbose to false and/or reduce/increase memory).

Do you have your linear system available somewhere or a way to generate it that I can give a try too ?

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [February 10, 2020, 1:13pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/5 "2020-02-10T13:13:32Z")

</div>

Thanks for your suggestion! I tried it but it is not actually working with A being sparse. I tried to convert A back to the dense form using `Matrix(A)` (which I know is not a good idea), and I got an OutOfMemoryError.

---

<div class="post-metadata">

**Author:** ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)\
**Post date:** [February 10, 2020, 1:26pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/6 "2020-02-10T13:26:06Z")

</div>

Have you tried with Pardiso.jl and MKL?

---

<div class="post-metadata">

**Author:** ![amontoison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amontoison/32/218741_2.png) [@amontoison](https://discourse.julialang.org/u/amontoison)\
**Post date:** [February 10, 2020, 1:35pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/7 "2020-02-10T13:35:57Z")

</div>

@AhmedAlreweny, method should work with A being sparse. What is the error that is returned?

```julia
using SparseArrays
A = spzeros(100,100)
for i = 1 : 100
    A[i,i] = 100
end
b = ones(100)
dqgmres(A, b)

([0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 … 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01], 
Simple stats
  solved: true
  inconsistent: false
  residuals: [1.0e+01 5.4e-16]
  Aresiduals: []
  status: solution good enough given atol and rtol
)

```

Otherwise, you can use MUMPS that has been developed for linear systems of high dimensions.

> **[GitHub - JuliaSmoothOptimizers/MUMPS.jl: A Julia Interface to MUMPS](https://github.com/JuliaSmoothOptimizers/MUMPS.jl)**
>
> A Julia Interface to MUMPS. Contribute to JuliaSmoothOptimizers/MUMPS.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [February 10, 2020, 1:50pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/8 "2020-02-10T13:50:15Z")

</div>

Thanks again! This is the error I got

> dqgmres(A\_matrix, b\_vector)  
> ERROR: MethodError: no method matching dqgmres(::SparseMatrixCSC{Float64,Int64}, ::Array{Float64,2})  
> Closest candidates are:  
> dqgmres(::AbstractArray{T\<:AbstractFloat,2}, ::SparseVector{T\<:AbstractFloat,Ti} where Ti\<:Integer, ::Any…; kwargs…) where T\<:AbstractFloat

---

<div class="post-metadata">

**Author:** ![amontoison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amontoison/32/218741_2.png) [@amontoison](https://discourse.julialang.org/u/amontoison)\
**Post date:** [February 10, 2020, 2:51pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/9 "2020-02-10T14:51:44Z")

</div>

Ok, I see your problem.  
Your right-hand side b is not a vector but a matrix `Array{Float64,2}` with one column.  
You can convert it to a vector with `b = b[:]`.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [February 10, 2020, 3:56pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/10 "2020-02-10T15:56:31Z")

</div>

> [@carlomontec](#):
>
> MKL

Nope! I will go through the documentation thanks!

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [February 10, 2020, 3:59pm UTC](https://discourse.julialang.org/t/outofmemoryerror-while-solving-a-linear-system/33921/11 "2020-02-10T15:59:06Z")

</div>

It seems that a preconditioner is required. It takes very long time without converging to the solution. Thanks for pointing out!
