This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Reuse of Krylov preconditioner

+2 votes

I am trying to solve a problem several consecutive times using the same matrix. Therefore it is useful to reuse as much as possible from one solve to the next one. For direct solvers I use:

solver = LUSolver()
solver.parameters['reuse_factorization'] = True

This works fine.

I was now trying to do the same with a Krylov solver. I searched around in FEniCS and I found this (very similar to the direct solver):

solver = KrylovSolver('gmres', 'hypre_amg')
solver.parameters['preconditioner']['reuse'] = True

But this returns an error

KeyError: "'reuse' is not a parameter"

Therefore I guess that either this option was removed or the way to use it changed. Can anyone clarify this to me?

Thank you.

asked Mar 19, 2015 by apalha FEniCS Novice (440 points)

1 Answer

+2 votes
 
Best answer

Try with

solver.parameters['preconditioner']['structure'] = 'same'

or, if the matrix changes, but the nonzero pattern does not:

solver.parameters['preconditioner']['structure'] = 'same_nonzero_pattern'

I have not found any documentation of this, but you can see it in use for example in Oasis:
https://github.com/mikaem/Oasis/blob/9d0ffaebd4bc8f58fb3bf2fd174f09b48e2c5d73/solvers/NSfracStep/IPCS_ABCN.py#L107

answered Mar 19, 2015 by Tormod Landet FEniCS User (5,130 points)
selected Mar 19, 2015 by apalha

Thank you Tormod!

I tried that but the times did not change. Do you know why that might be?

-artur palha

It seems you are right! I tested the below code with both FEniCS 1.4 and FEniCS 1.5. The results are as follows:

Results from running with FEniCS 1.4. The output shows preconditioner structure and timings in seconds:

same 2.22
different_nonzero_pattern 3.51

With 1.5:

same 2.43
different_nonzero_pattern 2.34

So, with the older version the preconditioner structure statement had an effect, while with the newest version it does not. If this is a bug or a feature I do not know.

The code:

from dolfin import *

import logging
logging.getLogger('FFC').setLevel(logging.WARNING)
logging.getLogger('UFL').setLevel(logging.WARNING)
set_log_level(WARNING)

def solve_poisson(N, M, structure):
    mesh = UnitSquareMesh(N, N)
    V = FunctionSpace(mesh, 'CG', 1)

    # Source term
    f = Expression('-200*pi*pi*sin(pi*x[0])*sin(pi*x[1])')

    # Define Poisson equation bilinear and linear form with du/dn=0
    u = TrialFunction(V)
    v = TestFunction(V)
    eq = inner(nabla_grad(u), nabla_grad(v))*dx - f*v*dx
    a, L = system(eq)

    # Boundary condition
    bc = DirichletBC(V, Constant(0.0), DomainBoundary())

    # Create matrix and vector with appropriate BCs
    A, b = assemble_system(a, L)
    bc.apply(A, b)

    # Solve system
    solution = Function(V)
    solver = KrylovSolver("gmres", "hypre_amg")
    solver.parameters['preconditioner']['structure'] = structure

    for i in range(M):
        solver.solve(A, solution.vector(), b)

import time
for structure in ('same', 'different_nonzero_pattern'):
    t1 = time.time()
    solve_poisson(400, 10, structure)
    print structure, round(time.time() - t1, 2)

See the mailing list for the follow up on this:
http://fenicsproject.org/pipermail/fenics/2015-March/002605.html

Thanks for the help.

I took some time because I was setting up a code to benchmark the different solvers. I attach just for curiosity (results for version 1.5).

For this benchmark I have tried several solvers (direct and iterative). What I realised is that if the solver is setup in the following way:

solver = dolfin.LUSolver(solver_name)
solver.set_operator(operator_matrix)
solver.solve(vector, right_hand_side)

Then all the reuse will be performed automatically and, as mentioned in the mailing list, once a new operator is set, the pre-processing will be done again for the new operator.

Once again thank you for your help!

Unsorted timmings
Sorted timmings

Hi apalha,

Can you kindly take a look at my question and help me out please.

http://fenicsproject.org/qa/8548/error-during-implementation-of-krylovsolver?show=8553#a8553

Thanks a lot!

Hi Tormod Landet,

Can you kindly take a look at my question and help me out please.

http://fenicsproject.org/qa/8548/error-during-implementation-of-krylovsolver?show=8553#a8553

Thanks a lot!

...