|
| 1 | +# Solvers for the Stein equation X - G X Diagonal(w) = B, i.e. (1 - wᵢ G) xᵢ = bᵢ per column, of the |
| 2 | +# truncated pullbacks. svd: G = P Pᴴ or Pᴴ P (the smaller), wᵢ = 1/σᵢ²; eigh: G = P, wᵢ = 1/λᵢ; with |
| 3 | +# P the part of A outside the kept vectors and γᵢ < 1 the spectral radius of wᵢ G. |
| 4 | + |
| 5 | +""" |
| 6 | + accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) |
| 7 | +
|
| 8 | +Solve `X = B + G * X * Diagonal(w)` by summing the Neumann series |
| 9 | +`X = Σₖ Gᵏ * B * Diagonal(w)ᵏ` by doubling (Smith's method), i.e. by repeatedly adding |
| 10 | +`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until the norm of that increment drops below `atol`, |
| 11 | +for at most `maxiter` steps. |
| 12 | +
|
| 13 | +On entry, `X` contains `B`, and it is overwritten with the result. `Xₙ` is used as a buffer, |
| 14 | +and `G` and `w` are overwritten. `w` is normalized such that `maximum(abs, w) == 1`, so that |
| 15 | +squaring it can only shrink it; `G` is scaled by the inverse factor to compensate. |
| 16 | +
|
| 17 | +Reference: https://doi.org/10.1016/j.aml.2009.01.012. |
| 18 | +""" |
| 19 | +function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter) |
| 20 | + Gₙ = similar(G) |
| 21 | + wmax = maximum(abs, w) |
| 22 | + w ./= wmax |
| 23 | + G .*= wmax |
| 24 | + for k in 1:maxiter |
| 25 | + Xₙ = rmul!(mul!(Xₙ, G, X), Diagonal(w)) |
| 26 | + if maximum(abs, Xₙ) < atol |
| 27 | + break |
| 28 | + end |
| 29 | + X .+= Xₙ |
| 30 | + if k == maxiter |
| 31 | + @warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(maximum(abs, X))" |
| 32 | + break |
| 33 | + end |
| 34 | + w .= w .^ 2 |
| 35 | + Gₙ = mul!(Gₙ, G, G) |
| 36 | + G, Gₙ = Gₙ, G |
| 37 | + end |
| 38 | + return X |
| 39 | +end |
| 40 | + |
| 41 | +# smallest Ritz value: smallest eigenvalue of the Lanczos tridiagonal from the CG coefficients `α`, `β` |
| 42 | +function _cg_ritz_min(α, β) |
| 43 | + l = length(α) |
| 44 | + d = similar(α) |
| 45 | + d[1] = 1 / α[1] |
| 46 | + for j in 2:l |
| 47 | + d[j] = 1 / α[j] + β[j - 1] / α[j - 1] |
| 48 | + end |
| 49 | + e = sqrt.(β[1:(l - 1)]) ./ α[1:(l - 1)] |
| 50 | + return LinearAlgebra.eigmin(LinearAlgebra.SymTridiagonal(d, e)) |
| 51 | +end |
| 52 | + |
| 53 | +# (1 - wᵢ G) applied to the columns of Z |
| 54 | +_stein_op(G, w, Z) = Z .- (G * Z) .* transpose(w) |
| 55 | + |
| 56 | +# iterations until the squared residual norm `r` drops below `tol²`, at the faster of its rates over |
| 57 | +# the last `l` iterations (from `r₀`) and all `m` (from `rᵢ`) |
| 58 | +function _cg_remaining(r, r₀, rᵢ, tol, l, m) |
| 59 | + q = min(log(r / r₀) / l, log(r / rᵢ) / m) # logarithm of the reduction per iteration |
| 60 | + return q < 0 ? log(tol^2 / r) / q : oftype(float(r), Inf) |
| 61 | +end |
| 62 | + |
| 63 | +# real parts of the column-wise inner products of A and B, on the device of A and B. CPU arrays use |
| 64 | +# `dot` per column, which avoids the temporary. This is restricted to `Matrix` rather than |
| 65 | +# `StridedMatrix`, which GPU arrays also are: there it would return a CPU vector, one `dot` call |
| 66 | +# each, which the GPU broadcasts in `hermitian_stein_cg!` cannot mix with the device arrays. |
| 67 | +_coldots(A, B) = vec(real(sum(conj.(A) .* B; dims = 1))) |
| 68 | +const _CPUMatrix = Union{Matrix, SubArray{<:Any, 2, <:Matrix}} |
| 69 | +_coldots(A::_CPUMatrix, B::_CPUMatrix) = [real(LinearAlgebra.dot(view(A, :, j), view(B, :, j))) for j in axes(A, 2)] |
| 70 | + |
| 71 | +""" |
| 72 | + hermitian_stein_cg!(X, applyG!, formG, w, atol, maxiter; cost_apply, cost_apply_formed, cost_form, cost_square = nothing) |
| 73 | +
|
| 74 | +Solve `X - G * X * Diagonal(w) = B` for Hermitian `G` by conjugate gradients on all columns at |
| 75 | +once, in at most `maxiter` iterations: O(n² k) per iteration for k columns, against O(n³) per |
| 76 | +doubling step. Only the products wᵢ G enter, so neither needs normalizing. `X` contains `B` on |
| 77 | +entry and is overwritten with the result. |
| 78 | +
|
| 79 | +`applyG!(Y, Z)` sets `Y = G * Z` without forming `G`, and `formG()` returns `G`. `G` is formed |
| 80 | +once the predicted remaining applications save more than `cost_form`, given the costs per column |
| 81 | +`cost_apply` (by `applyG!`) and `cost_apply_formed` (by `G`); `cost_form = 0` forms it at once. |
| 82 | +With `cost_square`, for indefinite `G` (eigh), the solver restarts on the residual equation |
| 83 | +multiplied by 1 + wᵢ G, (1 - wᵢ² G²) xᵢ = (1 + wᵢ G) bᵢ, once half the predicted remaining cost |
| 84 | +exceeds `cost_square` (forming G² and the new right-hand side): its spectrum [1 - γᵢ², 1] needs |
| 85 | +about half the iterations. A column stops once its residual is below `atol` times the smallest |
| 86 | +Ritz value of the slowest column, so that its error is below about `atol`. |
| 87 | +""" |
| 88 | +function hermitian_stein_cg!( |
| 89 | + X, applyG!, formG, w, atol, maxiter; |
| 90 | + cost_apply, cost_apply_formed = cost_apply, cost_form, cost_square = nothing, nprobe::Int = 5 |
| 91 | + ) |
| 92 | + RT = real(eltype(X)) |
| 93 | + G = iszero(cost_form) ? formG() : nothing |
| 94 | + tol = RT(atol) # stopping residual 2-norm, refined by the Ritz values |
| 95 | + ρ = _coldots(X, X) |
| 96 | + cols = findall(ρ .> tol^2) # active columns, kept contiguous at the front |
| 97 | + nact = length(cols) |
| 98 | + iszero(nact) && return fill!(X, zero(eltype(X))) |
| 99 | + R = X[:, cols] # X keeps B until the end |
| 100 | + P = zero(R) |
| 101 | + Q = similar(R) |
| 102 | + Xc = zero(R) |
| 103 | + wc = w[cols] |
| 104 | + ρ = ρ[cols] |
| 105 | + β = zero(ρ) |
| 106 | + ρ₀ = copy(ρ) # squared residual norms at the previous check |
| 107 | + ρᵢ = copy(ρ) # and at the start |
| 108 | + αs = [RT[] for _ in 1:nact] |
| 109 | + βs = [RT[] for _ in 1:nact] |
| 110 | + for numiter in 1:maxiter |
| 111 | + if numiter > 1 && (numiter - 1) % nprobe == 0 |
| 112 | + c = argmax(abs.(view(wc, 1:nact))) # the slowest column |
| 113 | + tol = atol * _cg_ritz_min(αs[c], βs[c]) |
| 114 | + nact = _cg_compact!( |
| 115 | + view(ρ, 1:nact), tol, view(R, :, 1:nact), view(P, :, 1:nact), view(Xc, :, 1:nact), view(wc, 1:nact), |
| 116 | + view(β, 1:nact), view(ρ₀, 1:nact), view(ρᵢ, 1:nact), view(cols, 1:nact), view(αs, 1:nact), view(βs, 1:nact) |
| 117 | + ) |
| 118 | + iszero(nact) && break |
| 119 | + # predicted column applications |
| 120 | + napply = sum(_cg_remaining.(view(ρ, 1:nact), view(ρ₀, 1:nact), view(ρᵢ, 1:nact), tol, nprobe, numiter - 1)) |
| 121 | + if isnothing(G) && napply * (cost_apply - cost_apply_formed) > cost_form |
| 122 | + G = formG() |
| 123 | + end |
| 124 | + if !isnothing(cost_square) && napply * cost_apply / 2 > cost_square # restart on the squared equation |
| 125 | + isnothing(G) && (G = formG()) |
| 126 | + X₁ = zero(X) # the current solution |
| 127 | + X₁[:, cols] .= Xc |
| 128 | + R = X .- _stein_op(G, w, X₁) |
| 129 | + X .= R .+ (G * R) .* transpose(w) |
| 130 | + G² = G * G |
| 131 | + hermitian_stein_cg!(X, nothing, () -> G², w .^ 2, atol, maxiter; cost_apply, cost_form = 0, nprobe) |
| 132 | + return X .+= X₁ |
| 133 | + end |
| 134 | + ρ₀ .= ρ |
| 135 | + end |
| 136 | + Pₐ, Rₐ, Qₐ, wₐ = view(P, :, 1:nact), view(R, :, 1:nact), view(Q, :, 1:nact), view(wc, 1:nact) |
| 137 | + ρₐ, βₐ = view(ρ, 1:nact), view(β, 1:nact) |
| 138 | + Pₐ .= Rₐ .+ Pₐ .* transpose(βₐ) |
| 139 | + isnothing(G) ? applyG!(Qₐ, Pₐ) : mul!(Qₐ, G, Pₐ) |
| 140 | + Qₐ .= Pₐ .- Qₐ .* transpose(wₐ) # q = (1 - wᵢ G) p |
| 141 | + α = ρₐ ./ _coldots(Pₐ, Qₐ) |
| 142 | + view(Xc, :, 1:nact) .+= Pₐ .* transpose(α) |
| 143 | + Rₐ .-= Qₐ .* transpose(α) |
| 144 | + βₐ .= ρₐ # ρold |
| 145 | + ρₐ .= _coldots(Rₐ, Rₐ) |
| 146 | + βₐ .= ρₐ ./ βₐ |
| 147 | + for (j, a, b) in zip(1:nact, Array(α), Array(βₐ)) |
| 148 | + push!(αs[j], a) |
| 149 | + push!(βs[j], b) |
| 150 | + end |
| 151 | + nact = _cg_compact!( |
| 152 | + ρₐ, tol, Rₐ, Pₐ, view(Xc, :, 1:nact), wₐ, |
| 153 | + βₐ, view(ρ₀, 1:nact), view(ρᵢ, 1:nact), view(cols, 1:nact), view(αs, 1:nact), view(βs, 1:nact) |
| 154 | + ) |
| 155 | + iszero(nact) && break |
| 156 | + end |
| 157 | + iszero(nact) || @warn "conjugate gradients did not converge in $maxiter iterations, largest residual norm: $(sqrt(maximum(view(ρ, 1:nact))))" |
| 158 | + fill!(X, zero(eltype(X))) |
| 159 | + X[:, cols] .= Xc |
| 160 | + return X |
| 161 | +end |
| 162 | + |
| 163 | +# move the columns with `ρ > tol²` to the front of all arrays (in order) and return their number |
| 164 | +function _cg_compact!(ρ, tol, arrays...) |
| 165 | + keep = Array(ρ) .> tol^2 |
| 166 | + all(keep) && return length(keep) |
| 167 | + perm = vcat(findall(keep), findall(.!keep)) |
| 168 | + for a in (ρ, arrays...) |
| 169 | + if a isa AbstractMatrix |
| 170 | + a .= a[:, perm] |
| 171 | + else |
| 172 | + a .= a[perm] |
| 173 | + end |
| 174 | + end |
| 175 | + return count(keep) |
| 176 | +end |
0 commit comments