# MPI/Distributed linear system solver?

**URL:** <https://discourse.julialang.org/t/mpi-distributed-linear-system-solver/105355>\
**Category:** New to Julia\
**Tags:** parallel, linearalgebra\
**Created:** [October 24, 2023, 6:37pm UTC](https://discourse.julialang.org/t/mpi-distributed-linear-system-solver/105355 "2023-10-24T18:37:39Z")\
**Posts on this page:** 1\
**Page:** 2

<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:** [October 26, 2023, 9:00pm UTC](https://discourse.julialang.org/t/mpi-distributed-linear-system-solver/105355/21 "2023-10-26T21:00:11Z")

</div>

Hi @Ribeiro!

I suggest the following code for your problem:

```julia
using MKL
using Krylov

n = 1000 # Dimension of your problem
T = Float64 # Precision of your problem
A = rand(T, n, n)
b = rand(T, n)

Id = Diagonal(ones(T,n))
F = lu(A)

# GMRES
solver_gmres = GmresSolver(n, n, 50, Vector{T})
solver_gmres.x .= 0.0
for i = 1 : 1000
  println(i)
  A .= A .+ Id # replace by your update of A
  b .= 2 .* b # replace by your update of b
  gmres!(solver_gmres, A, b, solver_gmres.x, N=F, atol=1e-12, rtol=0.0, ldiv=true)
  .... # Use solution solver_gmres.x, the solution of Ax = b
  if solver_gmres.stats.niter >= 50 # Tune this parameter if you recompute a preconditioner too often
    println("Recompute the preconditioner")
    F = lu(A)
  end
end

```

A few comments:

- GMRES will reuse the same memory, so you don’t need to allocate storage for every call of `gmres`.
- The previous solution is reused as an initial guess.
- GMRES stops when ||r\_k|| \<= 1e-12.
- The preconditioner is updated if it starts to be less efficient.

[Previous page](https://discourse.julialang.org/t/mpi-distributed-linear-system-solver/105355.md?page=1)
