gko::stop::ResidualNorm#
Residual-norm tolerance criteria. The baseline factory parameter
picks how the threshold is interpreted (relative to the right-hand
side, relative to the initial residual, or absolute); see the class
docstring for the three modes.
Two flavours:
ResidualNorm: uses the 2-norm of the residual, taken from the norm the solver provides or computed from the solver’s residual vector with one global reduction per check. Solvers that pass neither (MINRES, Multigrid) make it compute \(b - A x\), an extra SpMV per check.ImplicitResidualNorm: uses the squared-norm estimate the solver maintains internally (typically an inner product that is already computed each iteration), avoiding that reduction at the cost of a quantity that can drift from the true residual norm on long runs.
Helper functions build these criteria without spelling out the value type:
relative_residual_norm(tol) (baseline rhs_norm),
initial_residual_norm(tol) (initial_resnorm),
absolute_residual_norm(tol) (absolute), and
relative_implicit_residual_norm, initial_implicit_residual_norm and
absolute_implicit_residual_norm for ImplicitResidualNorm.
-
template<typename ValueType = default_precision>
class ResidualNorm # Inherits from
public gko::stop::ResidualNormBase<default_precision>
Stopping criterion based on the explicit residual norm \( \| r_k \| = \| b - A x_k \| \). The iteration halts once \( \| r_k \| \le \tau \cdot \beta \), where \(\tau\) is the factory parameter
reduction_factorand the baseline \(\beta\) is selected by thebaselinefactory parameter:mode::rhs_norm(default) — relative to the right-hand side: \( \beta = \| b \| \), so the condition is \( \| r_k \| \le \tau \| b \| \).mode::initial_resnorm— relative to the initial residual: \( \beta = \| r_0 \| \), so the condition is \( \| r_k \| \le \tau \| r_0 \| \).mode::absolute— absolute threshold: \( \beta = 1 \), so the condition is \( \| r_k \| \le \tau \).
Per-iteration, the criterion prefers a pre-computed residual norm passed by the solver (via
Updater::residual_norm); otherwise it falls back to computing the 2-norm of the residual vector itself (viaUpdater::residual), which costs one global reduction. When even cheaper checks are needed and the solver maintains an internal squared-norm estimate, use ImplicitResidualNorm instead.Note
Baseline prerequisites at construction time:
mode::rhs_normrequires the right-hand side \( b \).mode::initial_resnormrequires either the initial residual \( r_0 \) explicitly, or the triple \( (A, b, x_0) \) from which it is computed as \( r_0 = b - A x_0 \).mode::absoluterequires \( b \) as well — to determine the number of right-hand sides; its baseline is then filled with ones. If the required arguments are missing, ::gko::NotSupported() is thrown.
-
struct parameters_type#
-
template<typename ValueType = default_precision>
class ImplicitResidualNorm # Inherits from
public gko::stop::ResidualNormBase<default_precision>
Stopping criterion based on a solver-maintained squared residual-norm estimate \( \rho_k^2 \approx \| r_k \|^2 \). Several Krylov methods (CG, BiCGSTAB, …) already compute this quantity per iteration as a by-product of their inner-product recurrences, so the criterion can check convergence without computing a norm of its own — saving the global reduction the explicit ResidualNorm form needs when the solver only passes \( r_k \). The iteration halts once \( \rho_k \le \tau \cdot \beta \), with the same
reduction_factorandbaselinefactory parameters as ResidualNorm:mode::rhs_norm(default) — \( \rho_k \le \tau \| b \| \).mode::initial_resnorm— \( \rho_k \le \tau \| r_0 \| \).mode::absolute— \( \rho_k \le \tau \).
Because \( \rho_k^2 \) is updated by short recurrences rather than recomputed from \( b - A x_k \) each step, it can drift from the true residual norm on long runs in finite precision; pair this criterion with an Iteration cap when that matters.
Note
The solver must pass the squared estimate through
Updater::implicit_sq_residual_normon every check — there is no fallback path. If it is missing, ::gko::NotSupported() is thrown at check time.Note
Baseline prerequisites at construction time mirror ResidualNorm:
mode::rhs_normrequires \( b \).mode::initial_resnormrequires either \( r_0 \) explicitly or the triple \( (A, b, x_0) \) to derive it.mode::absoluterequires \( b \) for sizing the per-RHS baseline. If the required arguments are missing, ::gko::NotSupported() is thrown.
-
struct parameters_type#
-
template<typename ValueType>
class ResidualNormBase # Inherits from
public gko::stop::Criterion
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm. These criteria differ in the way they initialize starting_tau_, so in the value they compare the residual norm against. The provided check_impl uses the actual residual to check for convergence.
- deferred_factory_parameter<CriterionFactory> gko::stop::relative_residual_norm(
- double tolerance,
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with
.with_criteria(...)function calls when building a solver factory. This stopping criterion will stop the iteration after the residual norm has decreased below the specified value or by the specified amount.Full usage example: Stop after 100 iterations or when the absolute residual norm is below \(10^{-10}\), whichever happens first.
auto factory = gko::solver::Cg<double>::build() .with_criteria( gko::stop::max_iters(100), gko::stop::absolute_residual_norm(1e-10)) .on(exec);
- Parameters:
tolerance – the value the residual norm needs to be below. With residual \(r\), initial guess \(x_0\), right-hand side \(b\) and matrix \(A\),
absolutemeans the exact value of the norm \(||r||\),relativemeans the norm relative to the right-hand side \(||r||/||b||\),initialmeans the norm relative to the initial residual \(||r||/||b - A x_0||\). An implicit stopping criterion is only available with some solvers, and refers to either the energy norm \(||r||_A\) in short-recurrence solvers like Cg or the euclidian norm \(||r||\) in solvers like GMRES. Implicit residual norms are cheaper to compute, but may be less precise due to accumulating rounding errors.- Returns:
a deferred_factory_parameter that can be passed to the
with_criteriafunction when building a solver.
- deferred_factory_parameter<CriterionFactory> gko::stop::initial_residual_norm(
- double tolerance,
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with
.with_criteria(...)function calls when building a solver factory. This stopping criterion will stop the iteration after the residual norm has decreased below the specified value or by the specified amount.Full usage example: Stop after 100 iterations or when the absolute residual norm is below \(10^{-10}\), whichever happens first.
auto factory = gko::solver::Cg<double>::build() .with_criteria( gko::stop::max_iters(100), gko::stop::absolute_residual_norm(1e-10)) .on(exec);
- Parameters:
tolerance – the value the residual norm needs to be below. With residual \(r\), initial guess \(x_0\), right-hand side \(b\) and matrix \(A\),
absolutemeans the exact value of the norm \(||r||\),relativemeans the norm relative to the right-hand side \(||r||/||b||\),initialmeans the norm relative to the initial residual \(||r||/||b - A x_0||\). An implicit stopping criterion is only available with some solvers, and refers to either the energy norm \(||r||_A\) in short-recurrence solvers like Cg or the euclidian norm \(||r||\) in solvers like GMRES. Implicit residual norms are cheaper to compute, but may be less precise due to accumulating rounding errors.- Returns:
a deferred_factory_parameter that can be passed to the
with_criteriafunction when building a solver.
- deferred_factory_parameter<CriterionFactory> gko::stop::absolute_residual_norm(
- double tolerance,
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with
.with_criteria(...)function calls when building a solver factory. This stopping criterion will stop the iteration after the residual norm has decreased below the specified value or by the specified amount.Full usage example: Stop after 100 iterations or when the absolute residual norm is below \(10^{-10}\), whichever happens first.
auto factory = gko::solver::Cg<double>::build() .with_criteria( gko::stop::max_iters(100), gko::stop::absolute_residual_norm(1e-10)) .on(exec);
- Parameters:
tolerance – the value the residual norm needs to be below. With residual \(r\), initial guess \(x_0\), right-hand side \(b\) and matrix \(A\),
absolutemeans the exact value of the norm \(||r||\),relativemeans the norm relative to the right-hand side \(||r||/||b||\),initialmeans the norm relative to the initial residual \(||r||/||b - A x_0||\). An implicit stopping criterion is only available with some solvers, and refers to either the energy norm \(||r||_A\) in short-recurrence solvers like Cg or the euclidian norm \(||r||\) in solvers like GMRES. Implicit residual norms are cheaper to compute, but may be less precise due to accumulating rounding errors.- Returns:
a deferred_factory_parameter that can be passed to the
with_criteriafunction when building a solver.
- deferred_factory_parameter<CriterionFactory> gko::stop::relative_implicit_residual_norm(
- double tolerance,
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with
.with_criteria(...)function calls when building a solver factory. This stopping criterion will stop the iteration after the residual norm has decreased below the specified value or by the specified amount.Full usage example: Stop after 100 iterations or when the absolute residual norm is below \(10^{-10}\), whichever happens first.
auto factory = gko::solver::Cg<double>::build() .with_criteria( gko::stop::max_iters(100), gko::stop::absolute_residual_norm(1e-10)) .on(exec);
- Parameters:
tolerance – the value the residual norm needs to be below. With residual \(r\), initial guess \(x_0\), right-hand side \(b\) and matrix \(A\),
absolutemeans the exact value of the norm \(||r||\),relativemeans the norm relative to the right-hand side \(||r||/||b||\),initialmeans the norm relative to the initial residual \(||r||/||b - A x_0||\). An implicit stopping criterion is only available with some solvers, and refers to either the energy norm \(||r||_A\) in short-recurrence solvers like Cg or the euclidian norm \(||r||\) in solvers like GMRES. Implicit residual norms are cheaper to compute, but may be less precise due to accumulating rounding errors.- Returns:
a deferred_factory_parameter that can be passed to the
with_criteriafunction when building a solver.
- deferred_factory_parameter<CriterionFactory> gko::stop::initial_implicit_residual_norm(
- double tolerance,
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with
.with_criteria(...)function calls when building a solver factory. This stopping criterion will stop the iteration after the residual norm has decreased below the specified value or by the specified amount.Full usage example: Stop after 100 iterations or when the absolute residual norm is below \(10^{-10}\), whichever happens first.
auto factory = gko::solver::Cg<double>::build() .with_criteria( gko::stop::max_iters(100), gko::stop::absolute_residual_norm(1e-10)) .on(exec);
- Parameters:
tolerance – the value the residual norm needs to be below. With residual \(r\), initial guess \(x_0\), right-hand side \(b\) and matrix \(A\),
absolutemeans the exact value of the norm \(||r||\),relativemeans the norm relative to the right-hand side \(||r||/||b||\),initialmeans the norm relative to the initial residual \(||r||/||b - A x_0||\). An implicit stopping criterion is only available with some solvers, and refers to either the energy norm \(||r||_A\) in short-recurrence solvers like Cg or the euclidian norm \(||r||\) in solvers like GMRES. Implicit residual norms are cheaper to compute, but may be less precise due to accumulating rounding errors.- Returns:
a deferred_factory_parameter that can be passed to the
with_criteriafunction when building a solver.
- deferred_factory_parameter<CriterionFactory> gko::stop::absolute_implicit_residual_norm(
- double tolerance,
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with
.with_criteria(...)function calls when building a solver factory. This stopping criterion will stop the iteration after the residual norm has decreased below the specified value or by the specified amount.Full usage example: Stop after 100 iterations or when the absolute residual norm is below \(10^{-10}\), whichever happens first.
auto factory = gko::solver::Cg<double>::build() .with_criteria( gko::stop::max_iters(100), gko::stop::absolute_residual_norm(1e-10)) .on(exec);
- Parameters:
tolerance – the value the residual norm needs to be below. With residual \(r\), initial guess \(x_0\), right-hand side \(b\) and matrix \(A\),
absolutemeans the exact value of the norm \(||r||\),relativemeans the norm relative to the right-hand side \(||r||/||b||\),initialmeans the norm relative to the initial residual \(||r||/||b - A x_0||\). An implicit stopping criterion is only available with some solvers, and refers to either the energy norm \(||r||_A\) in short-recurrence solvers like Cg or the euclidian norm \(||r||\) in solvers like GMRES. Implicit residual norms are cheaper to compute, but may be less precise due to accumulating rounding errors.- Returns:
a deferred_factory_parameter that can be passed to the
with_criteriafunction when building a solver.