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 Namespace Reference

Classes

struct  owned_csr
 The owned compressed2D copy, as a base constructed before the smoother. More...
struct  gmres_workspace
 gmres's vectors, allocated once per solve rather than per restart cycle and per Arnoldi step: kmax + 1 basis vectors were zero-filled on every cycle (about 250 MB at n = 1e6 and restart 30), and two vectors were allocated on every step (issue 924). More...
struct  gmres_no_unscale
 The identity, for a gmres_inner called without a scaled right-hand side. More...
struct  idrs_is_complex
struct  idrs_is_complex< std::complex< T > >
class  scaled_rhs
 The right-hand side a Krylov solver works with, for the length of one solve. More...
struct  spd_accumulator_real
struct  spd_accumulator_real< std::complex< R > >

Functions

template<typename Value, typename Parameters>
mat::compressed2D< Value > to_compressed (const mat::coordinate2D< Value, Parameters > &A)
template<typename Value, typename Parameters>
mat::compressed2D< Value > to_compressed (const mat::ell_matrix< Value, Parameters > &A)
template<typename Row, typename F>
void for_common_columns (const Row &ri, const Row &rj, std::size_t limit, F &&f)
 Calls f(ri_k, rj_k, k) for every column k < limit stored in both rows, in ascending k.
void throw_zero_diagonal (const char *who, std::size_t row)
 Through throw_or_abort, so a build with exceptions disabled still compiles and aborts instead (#870 review).
void throw_nonpositive_pivot (const char *who, std::size_t row)
 A pivot that is not positive where the factorization needs it to be: IC(0) takes the square root of each pivot, so a zero or negative one (A not positive definite, or IC(0) breaking down on it) gave NaN in solve() (issue 925).
template<typename Matrix>
void require_square (const Matrix &A, const char *who)
 A matrix that must be square, checked once at construction in every build (issue 925; the index-check policy of #825).
template<typename Accumulator, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter, typename Value, typename Unscale>
int gmres_cycle (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, int kmax, gmres_workspace< Value > &ws, const Unscale &unscale)
 One GMRES cycle in the workspace ws, for the right-hand side b.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int gmres_inner (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, int kmax)
 Single GMRES cycle (inner iteration) – up to kmax Arnoldi steps.
template<typename Accumulator, typename Value>
void idrs_orthonormalize (std::vector< vec::dense_vector< Value > > &P)
 Orthonormalize P in place (modified Gram-Schmidt, run twice, which keeps P orthonormal to working precision).
template<typename Value>
std::vector< vec::dense_vector< Value > > idrs_random_shadow (std::size_t n, std::size_t s, std::uint64_t seed)
 s vectors of length n with entries uniform in [-1, 1), drawn from seed in the value type itself: 24-bit integers scaled by 2^-23, exact in any type with a 24-bit significand, with no detour through double.
template<typename Value>
bool is_normal_magnitude (const Value &d)
 Whether |d| is a normal number: neither zero, subnormal, infinite nor NaN.
template<typename Value>
Value by_pow2 (const Value &v, int k)
 v * 2^k, exact also where 2^k itself is not representable (a subnormal v scaled up by 2^1074, which as a factor would be inf).
template<typename Vec>
int max_abs_exponent (const Vec &v)
 The e with 2^e <= max_i |v_i| < 2^(e+1); 0 for a zero or non-finite maximum (NaN cannot win the max).
template<typename Value>
int value_exponent (const Value &v)
 The e with 2^e <= |v| < 2^(e+1), |v| the larger of a complex value's parts; 0 for a zero or non-finite v.
template<typename Value>
Value scaled_quotient (const Value &num, const Value &den, int k)
 2^k num / den without an intermediate leaving the range: the quotient of the two mantissas, near 1, then one exact scaling by 2^(k + e_num - e_den).
template<typename Vec, typename Out>
void scale_by_pow2 (const Vec &v, int k, Out &out)
 out = 2^k v, value by value.
template<typename Accumulator, typename Mag>
Mag dot_rounding_unit ()
 The rounding unit of a dot product delivered in a value of magnitude type Mag and summed in Accumulator (void: in the value type, as dot()'s default).
template<typename Accumulator = void, typename T, typename Norms>
bool not_positive_definite (const T &d, std::size_t n, Norms &&norms)
 True when d, a computed v^H M^{-1} v (MINRES's ||v||_{M^-1}^2, CG's rho = r^H M^{-1} r), shows that M is not symmetric (Hermitian) positive definite: its real part is negative beyond the rounding of an n-term dot product, n u ||v||_2 ||M^{-1} v||_2, with u the rounding unit of the arithmetic that formed d (dot_rounding_unit).

Variables

constexpr int not_positive_definite_code = 7
 The error code of a preconditioner that is not positive definite, as basic_iteration::not_positive_definite (7).

Function Documentation

◆ by_pow2()

template<typename Value>
Value mtl::itl::detail::by_pow2 ( const Value & v,
int k )

v * 2^k, exact also where 2^k itself is not representable (a subnormal v scaled up by 2^1074, which as a factor would be inf).

A complex value is scaled part by part.

◆ dot_rounding_unit()

template<typename Accumulator, typename Mag>
Mag mtl::itl::detail::dot_rounding_unit ( )

The rounding unit of a dot product delivered in a value of magnitude type Mag and summed in Accumulator (void: in the value type, as dot()'s default).

The larger of the two epsilons: a float accumulator rounds a double dot product at float's. An accumulator without numeric_limits, or one whose limits say it is exact (a quire, whose epsilon() is a quire that the delivery type may not convert from), is taken to round no worse than the delivery type. So is one whose epsilon() does not convert to Mag.

◆ for_common_columns()

template<typename Row, typename F>
void mtl::itl::detail::for_common_columns ( const Row & ri,
const Row & rj,
std::size_t limit,
F && f )

Calls f(ri_k, rj_k, k) for every column k < limit stored in both rows, in ascending k.

Each row is a sequence of (column, value) pairs sorted by column.

IC(0) and ILDL form L(i,j) from the sum over k < j of L(i,k) L(j,k). They scanned row j from its start for every k of row i, which costs O(len_i * len_j) per entry and is cubic in the row length: a banded SPD matrix with n = 2000 took 0.056, 0.41 and 2.71 s at bandwidths 50, 100 and 200 (issue 925). One merge of the two sorted rows costs O(len_i + len_j), and it visits the same pairs in the same order, so the sums are bit-identical.

◆ gmres_cycle()

template<typename Accumulator, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter, typename Value, typename Unscale>
int mtl::itl::detail::gmres_cycle ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
int kmax,
gmres_workspace< Value > & ws,
const Unscale & unscale )

One GMRES cycle in the workspace ws, for the right-hand side b.

unscale maps a residual norm to the units of the iteration object (scaled_rhs.hpp). See gmres_inner for the return value.

◆ gmres_inner()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::detail::gmres_inner ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
int kmax )

Single GMRES cycle (inner iteration) – up to kmax Arnoldi steps.

Returns 0 once iter is finished (converged, at its limit, or broken down: its error code says which), 1 if kmax was exhausted first (restart needed). It does not call iter.error_code(), which a cyclic_iteration answers by printing its summary (issue 866).

◆ idrs_orthonormalize()

template<typename Accumulator, typename Value>
void mtl::itl::detail::idrs_orthonormalize ( std::vector< vec::dense_vector< Value > > & P)

Orthonormalize P in place (modified Gram-Schmidt, run twice, which keeps P orthonormal to working precision).

Throws std::invalid_argument when P does not have full rank.

◆ idrs_random_shadow()

template<typename Value>
std::vector< vec::dense_vector< Value > > mtl::itl::detail::idrs_random_shadow ( std::size_t n,
std::size_t s,
std::uint64_t seed )

s vectors of length n with entries uniform in [-1, 1), drawn from seed in the value type itself: 24-bit integers scaled by 2^-23, exact in any type with a 24-bit significand, with no detour through double.

Complex value types get complex entries, the recommended shadow space for a complex system.

◆ is_normal_magnitude()

template<typename Value>
bool mtl::itl::detail::is_normal_magnitude ( const Value & d)

Whether |d| is a normal number: neither zero, subnormal, infinite nor NaN.

◆ max_abs_exponent()

template<typename Vec>
int mtl::itl::detail::max_abs_exponent ( const Vec & v)

The e with 2^e <= max_i |v_i| < 2^(e+1); 0 for a zero or non-finite maximum (NaN cannot win the max).

std::ilogb, which the floating-point specialization calls, gives INT_MIN-like and INT_MAX values there, and -e would overflow. A zero v then gives a zero rescaled product, the solver's breakdown; an infinite one a non-finite step.

◆ not_positive_definite()

template<typename Accumulator = void, typename T, typename Norms>
bool mtl::itl::detail::not_positive_definite ( const T & d,
std::size_t n,
Norms && norms )

True when d, a computed v^H M^{-1} v (MINRES's ||v||_{M^-1}^2, CG's rho = r^H M^{-1} r), shows that M is not symmetric (Hermitian) positive definite: its real part is negative beyond the rounding of an n-term dot product, n u ||v||_2 ||M^{-1} v||_2, with u the rounding unit of the arithmetic that formed d (dot_rounding_unit).

norms() returns the pair (||v||_2, ||M^{-1} v||_2); it is called only when the real part is negative, so the test costs nothing on an SPD M's ordinary steps. The small factor n u is applied before the two norms are multiplied, so their product cannot overflow while d itself is finite.

The imaginary part of a complex d is not tested. It also points to an M that is not Hermitian, but rounding in the preconditioner's solve can give an HPD M one of order cond(M) eps ||v|| ||M^{-1} v||, which no tolerance independent of M separates from that.

◆ require_square()

template<typename Matrix>
void mtl::itl::detail::require_square ( const Matrix & A,
const char * who )

A matrix that must be square, checked once at construction in every build (issue 925; the index-check policy of #825).

The preconditioners checked it with assert only, and a non-square A then indexed past the work arrays, which are sized by rows and indexed by column.

◆ scale_by_pow2()

template<typename Vec, typename Out>
void mtl::itl::detail::scale_by_pow2 ( const Vec & v,
int k,
Out & out )

out = 2^k v, value by value.

◆ scaled_quotient()

template<typename Value>
Value mtl::itl::detail::scaled_quotient ( const Value & num,
const Value & den,
int k )

2^k num / den without an intermediate leaving the range: the quotient of the two mantissas, near 1, then one exact scaling by 2^(k + e_num - e_den).

The rescaled step's coefficient is 2^k rho / d with rho and d themselves in range but rho / d not, as for A = [2^-200] with M^-1 = 2^900: rho = 2^900, d = 2^-200, rho / d = 2^1100 is inf where 2^-900 rho / d = 2^200 is not (#967 review). Division rounds the same on values scaled by powers of two, so where rho / d is in range this is 2^k (rho / d) exactly.

◆ throw_nonpositive_pivot()

void mtl::itl::detail::throw_nonpositive_pivot ( const char * who,
std::size_t row )
inline

A pivot that is not positive where the factorization needs it to be: IC(0) takes the square root of each pivot, so a zero or negative one (A not positive definite, or IC(0) breaking down on it) gave NaN in solve() (issue 925).

Checked at construction, as the zero diagonal is.

◆ throw_zero_diagonal()

void mtl::itl::detail::throw_zero_diagonal ( const char * who,
std::size_t row )
inline

Through throw_or_abort, so a build with exceptions disabled still compiles and aborts instead (#870 review).

◆ to_compressed() [1/2]

template<typename Value, typename Parameters>
mat::compressed2D< Value > mtl::itl::detail::to_compressed ( const mat::coordinate2D< Value, Parameters > & A)

◆ to_compressed() [2/2]

template<typename Value, typename Parameters>
mat::compressed2D< Value > mtl::itl::detail::to_compressed ( const mat::ell_matrix< Value, Parameters > & A)

◆ value_exponent()

template<typename Value>
int mtl::itl::detail::value_exponent ( const Value & v)

The e with 2^e <= |v| < 2^(e+1), |v| the larger of a complex value's parts; 0 for a zero or non-finite v.

Variable Documentation

◆ not_positive_definite_code

int mtl::itl::detail::not_positive_definite_code = 7
inlineconstexpr

The error code of a preconditioner that is not positive definite, as basic_iteration::not_positive_definite (7).