# How to solve this Ax=b faster?

**URL:** https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693
**Category:** Numerics
**Tags:** linearalgebra, linearsolve
**Created:** [May 26, 2022, 5:34am UTC](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693 "2022-05-26T05:34:42Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 28, 2022, 10:05pm UTC](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693/21 "2022-05-28T22:05:02Z")

</div>

> [@rayegun](#):
>
> It looks like it’s calling CHOLMOD, by dispatching on `ishermitian` . Should we duplicate this codepath @ChrisRackauckas? I’m not even sure if we wrap CHOLMOD very well in LinearSolve.jl yet.

Oh, that completely changes the game. Yes, Cholesky factorizations are going to a way faster than any other factorization if the matrix is SPD! I didn’t catch that detail (now that I re-read, there was a quick mention of SPD).

Yes, we should wrap CHOLMOD for this case. Also, for SPD you shouldn’t use GMRES, instead you should use CG (`KrylovJL_CG`).

---

<div class="post-metadata">

### Author: ![Andrea\_Vigliotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_vigliotti/32/7635_2.png) [@Andrea\_Vigliotti](https://discourse.julialang.org/u/Andrea_Vigliotti)
#### Post date: [May 28, 2022, 10:47pm UTC](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693/22 "2022-05-28T22:47:11Z")

</div>

[here](https://drive.google.com/drive/folders/16M8BBqg0odWY2CkKAw7e8yNbosihTNe1?usp=sharing) I uploaded a `JLD2` file and a small script that runs the tests.

following the output of the script

```julia
loading variables ... 
A, r, 
size(A) = (162372, 162372)
ishermitian(A) = true
norm(r) = 9027.184755276294

doing u = A\r ...
  3.878 s (52 allocations: 1.39 GiB)
norm(r - A * u) = 5.766109468498555e-12

doing u = lu(A)\r ...
  13.797 s (72 allocations: 2.39 GiB)
norm(r - A * u) = 4.227052608619862e-12

doing LinearSolve.solve(prob, UMFPACKFactorization()) ...
  12.981 s (94 allocations: 2.71 GiB)
norm(r - A * sol.u) = 4.227052608619862e-12

doing LinearSolve.solve(prob, KrylovJL_CG()) ...
  70.360 s (50 allocations: 179.79 MiB)
norm(r - A * sol.u) = 0.00013389304531825536

```

you can tell `KrylovJL_CG()` is a conjugate gradient method by looking at how little memory it allocates, I would also have expected a CG method would have been faster on a SPD problem (probably the the matrix is not big enough to outperform a very well optimized direct method)

---

<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: [June 7, 2022, 2:43am UTC](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693/23 "2022-06-07T02:43:47Z")

</div>

@Andrea_Vigliotti, the convergence rate of CG hightly depends on the distribution of the eigenvalues of `A`.  
If you have a good preconditioner, the Krylov method could only requires a few iterations.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 7, 2022, 2:50am UTC](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693/24 "2022-06-07T02:50:54Z")

</div>

The `\` is probably running `cholesky`, which is about 3x - 4x faster than `lu`.

---

<div class="post-metadata">

### Author: ![Andrea\_Vigliotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_vigliotti/32/7635_2.png) [@Andrea\_Vigliotti](https://discourse.julialang.org/u/Andrea_Vigliotti)
#### Post date: [June 7, 2022, 8:40am UTC](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693/25 "2022-06-07T08:40:32Z")

</div>

you are right, `u_ch=cholesky(A)\r` and `u=A\r` on this matrix take the same time, do the same number of allocation and find the same solution, `norm(u_ch-u)` is `0.0`

[Previous page](https://discourse.julialang.org/t/how-to-solve-this-ax-b-faster/81693.md?page=1)
