How to extract intermediate results from GMRES of Krylov.jl

Hi, I am using gmres from Krylov.jl as follows

x,stat=gmres(A,b,itmax=3000,memory=500,history=true)

which will return some information about the iteration, e.g., stat.residuals. Is there a way to also return the intermediate x’s for diagnostic purposes? By changing the verbose keyword does not seem to do it. I looked through documentation here but couldn’t find anything relevant.
Does anyone have experience with this GMRES implementation?

you could register a callback. the reason this doesn’t exist by default is that saving the intermediate values is pretty expensive.

Thank you for your answer. I looked at Callbacks but cannot see how the callback function can access intermediate variables from the solver. Can you elaborate a little more?

Hi @jinml!

FOM, GMRES and GPMR are the only methods in Krylov.jl that compute the solution x at the last iteration.
We can’t update x because x = V * y where y is the solution of a triangular system Ry = z. At each iteration, the coefficients of y are different if we perform the backward substitution.

However, you can create a callback that compute each xk. The initial goal of a callback is to add new stopping conditions but you can also use it to see the current state of the KrylovSolver (workspace).

T = eltype(b)
global X = Vector{T}[]

function jinml_callback(solver)
   z = solver.z
   k = solver.inner_iter
   nr = sum(1:k)
   V = solver.V
   R = solver.R
    y = copy(z)
    for i = k : -1 : 1
      pos = nr + i - k               # position of rᵢ.ₖ
      for j = k : -1 : i+1
        y[i] = y[i] - R[pos] * y[j]  # yᵢ ← yᵢ - rᵢⱼyⱼ
        pos = pos - j + 1            # position of rᵢ.ⱼ₋₁
      end
      y[i] = y[i] / R[pos]  # yᵢ ← yᵢ / rᵢᵢ
    end
    xk = sum(V[i] * y[i] for i = 1:k)
    push!(X, xk)
    return false
end
x, stats = gmres(A, b, itmax=3000, memory=500, history=true, callback=jinml_callback)
stats.residuals
residuals2 = [norm(b - A * X[i]) for i = 1:stats.niter]
3 Likes

Thanks @Oscar_Smith @amontoison for the detailed description. I was able to generate a convergence animation of a solution for your entertainment :smiley:
scatt2Intconv

4 Likes

That’s awesome!

It’s great @jinml! What do you plot exactly?

@amontoison It is the intermediate solutions to a wave equation using GMRES converging to the final result. The wave equation describes a wave traveling through two penetrable obstacles, and was focused by the convex shape of the obstacle.

1 Like