MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::itl::detail::scaled_rhs< VecX, VecB > Class Template Reference

The right-hand side a Krylov solver works with, for the length of one solve. More...

#include <scaled_rhs.hpp>

Public Types

using value_type = typename VecX::value_type
using size_type = typename VecX::size_type
using vector_type = vec::dense_vector<value_type>
using mag_type = decltype(mtl::two_norm(std::declval<const vector_type&>()))

Public Member Functions

 scaled_rhs (VecX &x, const VecB &b, bool scale)
 scaled_rhs (const scaled_rhs &)=delete
scaled_rhs & operator= (const scaled_rhs &)=delete
 ~scaled_rhs ()
const vector_type & b () const
 s * b, in storage of its own.
mag_type unscale (const mag_type &r) const
 A residual norm of the scaled system, in the units of b.
template<typename V>
mag_type norm (const V &r) const

Detailed Description

template<typename VecX, typename VecB>
class mtl::itl::detail::scaled_rhs< VecX, VecB >

The right-hand side a Krylov solver works with, for the length of one solve.

A copy of b. The solvers read b again after they have written x: every true-residual check, and gmres on every restart. A call cg(A, x, x, M, iter) therefore read its own iterates as the right-hand side and returned x ~ 6e12 for a 4x4 SPD system (issue 924). The copy is taken before x is touched, so x may alias b, or share its storage through a view. It is also b evaluated once, when b is an expression.

Scaling. With scale, the system is solved as A (s x) = s b, where s is an exact power of two chosen so that ||s b|| is near 1. This happens only when ||b|| is outside [2^-E, 2^E], with E a quarter of the type's max_exponent (32 for float, 256 for double). Inside that band nothing changes, bit for bit. The dot products of cg, bicg, bicgstab, cgs, qmr, tfqmr and bicgstab_ell are quadratic in the residual. In float, b = 1e-30 * ones made dot(p, A p) underflow to 0, so cg and bicgstab reported a breakdown with x = 0. b = 1e20 made the dots overflow and x = NaN (issue 924). minres and idr_s scale their own dot products (#671, #874) and take the copy without the scaling.

Every operation of these solvers is homogeneous in (x, b) when the preconditioner is linear, and multiplying by 2^k is exact in a binary type away from overflow and the subnormals. So the scaled iterates are the unscaled ones times s. x is multiplied by s here and divided by it again when this object is destroyed, on every return and on an exception.

The iteration object sees the user's scale. Pass a residual norm through norm(r) or unscale(nr): the tolerance and norm_r0 are in the units of b.

Member Typedef Documentation

◆ mag_type

template<typename VecX, typename VecB>
using mtl::itl::detail::scaled_rhs< VecX, VecB >::mag_type = decltype(mtl::two_norm(std::declval<const vector_type&>()))

◆ size_type

template<typename VecX, typename VecB>
using mtl::itl::detail::scaled_rhs< VecX, VecB >::size_type = typename VecX::size_type

◆ value_type

template<typename VecX, typename VecB>
using mtl::itl::detail::scaled_rhs< VecX, VecB >::value_type = typename VecX::value_type

◆ vector_type

template<typename VecX, typename VecB>
using mtl::itl::detail::scaled_rhs< VecX, VecB >::vector_type = vec::dense_vector<value_type>

Constructor & Destructor Documentation

◆ scaled_rhs() [1/2]

template<typename VecX, typename VecB>
mtl::itl::detail::scaled_rhs< VecX, VecB >::scaled_rhs ( VecX & x,
const VecB & b,
bool scale )
inline

◆ scaled_rhs() [2/2]

template<typename VecX, typename VecB>
mtl::itl::detail::scaled_rhs< VecX, VecB >::scaled_rhs ( const scaled_rhs< VecX, VecB > & )
delete

◆ ~scaled_rhs()

template<typename VecX, typename VecB>
mtl::itl::detail::scaled_rhs< VecX, VecB >::~scaled_rhs ( )
inline

Member Function Documentation

◆ b()

template<typename VecX, typename VecB>
const vector_type & mtl::itl::detail::scaled_rhs< VecX, VecB >::b ( ) const
inline

s * b, in storage of its own.

◆ norm()

template<typename VecX, typename VecB>
template<typename V>
mag_type mtl::itl::detail::scaled_rhs< VecX, VecB >::norm ( const V & r) const
inline

◆ operator=()

template<typename VecX, typename VecB>
scaled_rhs & mtl::itl::detail::scaled_rhs< VecX, VecB >::operator= ( const scaled_rhs< VecX, VecB > & )
delete

◆ unscale()

template<typename VecX, typename VecB>
mag_type mtl::itl::detail::scaled_rhs< VecX, VecB >::unscale ( const mag_type & r) const
inline

A residual norm of the scaled system, in the units of b.


The documentation for this class was generated from the following file: