KSPSolve#

Solves linear system.

Synopsis#

#include "petscksp.h" 
#include "petscmat.h" 
PetscErrorCode KSPSolve(KSP ksp, Vec b, Vec x)

Collective

Input Parameters#

  • ksp - iterative context obtained from KSPCreate()

  • b - the right-hand side vector

  • x - the solution (this may be the same vector as b, then b will be overwritten with answer)

Options Database Keys#

  • -ksp_view_eigenvalues - compute preconditioned operators eigenvalues

  • -ksp_view_eigenvalues_explicit - compute the eigenvalues by forming the dense operator and using LAPACK

  • -ksp_view_mat binary - save matrix to the default binary viewer

  • -ksp_view_pmat binary - save matrix used to build preconditioner to the default binary viewer

  • -ksp_view_rhs binary - save right-hand side vector to the default binary viewer

  • -ksp_view_solution binary - save computed solution vector to the default binary viewer (can be read later with src/ksp/tutorials/ex10.c for testing solvers)

  • -ksp_view_mat_explicit - for matrix-free operators, computes the matrix entries and views them

  • -ksp_view_preconditioned_operator_explicit - computes the product of the preconditioner and matrix as an explicit matrix and views it

  • -ksp_converged_reason - print reason for converged or diverged, also prints number of iterations

  • -ksp_view_final_residual - print 2-norm of true linear system residual at the end of the solution process

  • -ksp_error_if_not_converged - stop the program as soon as an error is detected in a KSPSolve()

  • -ksp_view_pre - print the ksp data structure before the system solution

  • -ksp_view - print the ksp data structure at the end of the system solution

Notes#

If one uses KSPSetDM() then x or b need not be passed. Use KSPGetSolution() to access the solution in this case.

The operator is specified with KSPSetOperators().

KSPSolve() will normally return without generating an error regardless of whether the linear system was solved or if constructing the preconditioner failed. Call KSPGetConvergedReason() to determine if the solver converged or failed and why. The option -ksp_error_if_not_converged or function KSPSetErrorIfNotConverged() will cause KSPSolve() to error as soon as an error occurs in the linear solver. In inner KSPSolve() KSP_DIVERGED_ITS is not treated as an error because when using nested solvers it may be fine that inner solvers in the preconditioner do not converge during the solution process.

The number of iterations can be obtained from KSPGetIterationNumber().

If you provide a matrix that has a MatSetNullSpace() and MatSetTransposeNullSpace() this will use that information to solve singular systems in the least squares sense with a norm minimizing solution.

\(A x = b \) where \(b = b_p + b_t\) where \(b_t\) is not in the range of A (and hence by the fundamental theorem of linear algebra is in the nullspace(A’), see MatSetNullSpace()).

KSP first removes b_t producing the linear system A x = b_p (which has multiple solutions) and solves this to find the ||x|| minimizing solution (and hence it finds the solution x orthogonal to the nullspace(A). The algorithm is simply in each iteration of the Krylov method we remove the nullspace(A) from the search direction thus the solution which is a linear combination of the search directions has no component in the nullspace(A).

We recommend always using KSPGMRES for such singular systems. If nullspace(A) = nullspace(A’) (note symmetric matrices always satisfy this property) then both left and right preconditioning will work If nullspace(A) != nullspace(A’) then left preconditioning will work but right preconditioning may not work (or it may).

Developer Notes#

The reason we cannot always solve nullspace(A) != nullspace(A’) systems with right preconditioning is because we need to remove at each iteration the nullspace(AB) from the search direction. While we know the nullspace(A) the nullspace(AB) equals B^-1 times the nullspace(A) but except for trivial preconditioners such as diagonal scaling we cannot apply the inverse of the preconditioner to a vector and thus cannot compute the nullspace(AB).

If using a direct method (e.g., via the KSP solver KSPPREONLY and a preconditioner such as PCLU or PCILU, then its=1. See KSPSetTolerances() and KSPConvergedDefault() for more details.

Understanding Convergence: The routines KSPMonitorSet(), KSPComputeEigenvalues(), and KSPComputeEigenvaluesExplicitly() provide information on additional options to monitor convergence and print eigenvalue information.

See Also#

KSP: Linear System Solvers, KSPCreate(), KSPSetUp(), KSPDestroy(), KSPSetTolerances(), KSPConvergedDefault(), KSPSolveTranspose(), KSPGetIterationNumber(), MatNullSpaceCreate(), MatSetNullSpace(), MatSetTransposeNullSpace(), KSP, KSPConvergedReasonView(), KSPCheckSolve(), KSPSetErrorIfNotConverged()

Level#

beginner

Location#

src/ksp/ksp/interface/itfunc.c

Examples#

src/ksp/pc/tutorials/ex2.c
src/dm/impls/stag/tutorials/ex1.c
src/ksp/pc/tutorials/ex1.c
src/ksp/pc/tutorials/ex4.c
src/dm/impls/stag/tutorials/ex2.c
src/dm/impls/stag/tutorials/ex3.c
src/dm/impls/stag/tutorials/ex8.c
src/dm/impls/stag/tutorials/ex4.c
src/ksp/pc/tutorials/ex3.c
src/ksp/ksp/tutorials/ex15f.F90

Implementations#

KSPSolve_BCGS() in src/ksp/ksp/impls/bcgs/bcgs.c
KSPSolve_FBCGS() in src/ksp/ksp/impls/bcgs/fbcgs/fbcgs.c
KSPSolve_FBCGSR() in src/ksp/ksp/impls/bcgs/fbcgsr/fbcgsr.c
KSPSolve_PIPEBCGS() in src/ksp/ksp/impls/bcgs/pipebcgs/pipebcgs.c
KSPSolve_QMRCGS() in src/ksp/ksp/impls/bcgs/qmrcgs/qmrcgs.c
KSPSolve_BCGSL() in src/ksp/ksp/impls/bcgsl/bcgsl.c
KSPSolve_BiCG() in src/ksp/ksp/impls/bicg/bicg.c
KSPSolve_CG() in src/ksp/ksp/impls/cg/cg.c
KSPSolve_CGLS() in src/ksp/ksp/impls/cg/cgls.c
KSPSolve_CGNE() in src/ksp/ksp/impls/cg/cgne/cgne.c
KSPSolve_GROPPCG() in src/ksp/ksp/impls/cg/groppcg/groppcg.c
KSPSolve_PIPECG() in src/ksp/ksp/impls/cg/pipecg/pipecg.c
KSPSolve_PIPECG2() in src/ksp/ksp/impls/cg/pipecg2/pipecg2.c
KSPSolve_PIPECGRR() in src/ksp/ksp/impls/cg/pipecgrr/pipecgrr.c
KSPSolve_PIPELCG() in src/ksp/ksp/impls/cg/pipelcg/pipelcg.c
KSPSolve_PIPEPRCG() in src/ksp/ksp/impls/cg/pipeprcg/pipeprcg.c
KSPSolve_CGS() in src/ksp/ksp/impls/cgs/cgs.c
KSPSolve_CR() in src/ksp/ksp/impls/cr/cr.c
KSPSolve_PIPECR() in src/ksp/ksp/impls/cr/pipecr/pipecr.c
KSPSolve_FCG() in src/ksp/ksp/impls/fcg/fcg.c
KSPSolve_PIPEFCG() in src/ksp/ksp/impls/fcg/pipefcg/pipefcg.c
KSPSolve_FETIDP() in src/ksp/ksp/impls/fetidp/fetidp.c
KSPSolve_GCR() in src/ksp/ksp/impls/gcr/gcr.c
KSPSolve_PIPEGCR() in src/ksp/ksp/impls/gcr/pipegcr/pipegcr.c
KSPSolve_AGMRES() in src/ksp/ksp/impls/gmres/agmres/agmres.c
KSPSolve_DGMRES() in src/ksp/ksp/impls/gmres/dgmres/dgmres.c
KSPSolve_FGMRES() in src/ksp/ksp/impls/gmres/fgmres/fgmres.c
KSPSolve_GMRES() in src/ksp/ksp/impls/gmres/gmres.c
KSPSolve_LGMRES() in src/ksp/ksp/impls/gmres/lgmres/lgmres.c
KSPSolve_PGMRES() in src/ksp/ksp/impls/gmres/pgmres/pgmres.c
KSPSolve_PIPEFGMRES() in src/ksp/ksp/impls/gmres/pipefgmres/pipefgmres.c
KSPSolve_HPDDM() in src/ksp/ksp/impls/hpddm/hpddm.cxx
KSPSolve_IBCGS() in src/ksp/ksp/impls/ibcgs/ibcgs.c
KSPSolve_LCD() in src/ksp/ksp/impls/lcd/lcd.c
KSPSolve_LSQR() in src/ksp/ksp/impls/lsqr/lsqr.c
KSPSolve_MINRES() in src/ksp/ksp/impls/minres/minres.c
KSPSolve_PREONLY() in src/ksp/ksp/impls/preonly/preonly.c
KSPSolve_QCG() in src/ksp/ksp/impls/qcg/qcg.c
KSPSolve_Richardson() in src/ksp/ksp/impls/rich/rich.c
KSPSolve_SYMMLQ() in src/ksp/ksp/impls/symmlq/symmlq.c
KSPSolve_TCQMR() in src/ksp/ksp/impls/tcqmr/tcqmr.c
KSPSolve_TFQMR() in src/ksp/ksp/impls/tfqmr/tfqmr.c
KSPSolve_TSIRM() in src/ksp/ksp/impls/tsirm/tsirm.c


Index of all KSP routines
Table of Contents for all manual pages
Index of all manual pages