Skip to content

Commit 2cd76f7

Browse files
authored
Add a macro kfill! (#897)
1 parent 811870a commit 2cd76f7

40 files changed

+181
-168
lines changed

src/bicgstab.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ kwargs_bicgstab = (:c, :M, :N, :ldiv, :atol, :rtol, :itmax, :timemax, :verbose,
154154
r₀ .= b
155155
end
156156

157-
x .= zero(FC) # x₀
158-
s .= zero(FC) # s₀
159-
v .= zero(FC) # v₀
157+
@kfill!(x, zero(FC)) # x₀
158+
@kfill!(s, zero(FC)) # s₀
159+
@kfill!(v, zero(FC)) # v₀
160160
MisI || mulorldiv!(r, M, r₀, ldiv) # r₀
161161
p .= r # p₁
162162

src/bilq.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ kwargs_bilq = (:c, :transfer_to_bicg, :M, :N, :ldiv, :atol, :rtol, :itmax, :time
156156
end
157157

158158
# Initial solution x₀ and residual norm ‖r₀‖.
159-
x .= zero(FC)
159+
@kfill!(x, zero(FC))
160160
bNorm = @knrm2(n, r₀) # ‖r₀‖ = ‖b₀ - Ax₀‖
161161

162162
history && push!(rNorms, bNorm)
@@ -191,13 +191,13 @@ kwargs_bilq = (:c, :transfer_to_bicg, :M, :N, :ldiv, :atol, :rtol, :itmax, :time
191191

192192
βₖ = (abs(cᴴb)) # β₁γ₁ = cᴴ(b - Ax₀)
193193
γₖ = cᴴb / βₖ # β₁γ₁ = cᴴ(b - Ax₀)
194-
vₖ₋₁ .= zero(FC) # v₀ = 0
195-
uₖ₋₁ .= zero(FC) # u₀ = 0
194+
@kfill!(vₖ₋₁, zero(FC)) # v₀ = 0
195+
@kfill!(uₖ₋₁, zero(FC)) # u₀ = 0
196196
vₖ .= r₀ ./ βₖ # v₁ = (b - Ax₀) / β₁
197197
uₖ .= c ./ conj(γₖ) # u₁ = c / γ̄₁
198198
cₖ₋₁ = cₖ = -one(T) # Givens cosines used for the LQ factorization of Tₖ
199199
sₖ₋₁ = sₖ = zero(FC) # Givens sines used for the LQ factorization of Tₖ
200-
d̅ .= zero(FC) # Last column of D̅ₖ = Vₖ(Qₖ)ᴴ
200+
@kfill!(d̅, zero(FC)) # Last column of D̅ₖ = Vₖ(Qₖ)ᴴ
201201
ζₖ₋₁ = ζbarₖ = zero(FC) # ζₖ₋₁ and ζbarₖ are the last components of z̅ₖ = (L̅ₖ)⁻¹β₁e₁
202202
ζₖ₋₂ = ηₖ = zero(FC) # ζₖ₋₂ and ηₖ are used to update ζₖ₋₁ and ζbarₖ
203203
δbarₖ₋₁ = δbarₖ = zero(FC) # Coefficients of Lₖ₋₁ and L̅ₖ modified over the course of two iterations

src/bilqr.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ kwargs_bilqr = (:transfer_to_bicg, :atol, :rtol, :itmax, :timemax, :verbose, :hi
143143
end
144144

145145
# Initial solution x₀ and residual norm ‖r₀‖ = ‖b - Ax₀‖.
146-
x .= zero(FC) # x₀
146+
@kfill!(x, zero(FC)) # x₀
147147
bNorm = @knrm2(n, r₀) # rNorm = ‖r₀‖
148148

149149
# Initial solution t₀ and residual norm ‖s₀‖ = ‖c - Aᴴy₀‖.
150-
t .= zero(FC) # t₀
150+
@kfill!(t, zero(FC)) # t₀
151151
cNorm = @knrm2(n, s₀) # sNorm = ‖s₀‖
152152

153153
iter = 0
@@ -175,21 +175,21 @@ kwargs_bilqr = (:transfer_to_bicg, :atol, :rtol, :itmax, :timemax, :verbose, :hi
175175
# Set up workspace.
176176
βₖ = (abs(cᴴb)) # β₁γ₁ = (c - Aᴴy₀)ᴴ(b - Ax₀)
177177
γₖ = cᴴb / βₖ # β₁γ₁ = (c - Aᴴy₀)ᴴ(b - Ax₀)
178-
vₖ₋₁ .= zero(FC) # v₀ = 0
179-
uₖ₋₁ .= zero(FC) # u₀ = 0
178+
@kfill!(vₖ₋₁, zero(FC)) # v₀ = 0
179+
@kfill!(uₖ₋₁, zero(FC)) # u₀ = 0
180180
vₖ .= r₀ ./ βₖ # v₁ = (b - Ax₀) / β₁
181181
uₖ .= s₀ ./ conj(γₖ) # u₁ = (c - Aᴴy₀) / γ̄₁
182182
cₖ₋₁ = cₖ = -one(T) # Givens cosines used for the LQ factorization of Tₖ
183183
sₖ₋₁ = sₖ = zero(FC) # Givens sines used for the LQ factorization of Tₖ
184-
d̅ .= zero(FC) # Last column of D̅ₖ = Vₖ(Qₖ)ᴴ
184+
@kfill!(d̅, zero(FC)) # Last column of D̅ₖ = Vₖ(Qₖ)ᴴ
185185
ζₖ₋₁ = ζbarₖ = zero(FC) # ζₖ₋₁ and ζbarₖ are the last components of z̅ₖ = (L̅ₖ)⁻¹β₁e₁
186186
ζₖ₋₂ = ηₖ = zero(FC) # ζₖ₋₂ and ηₖ are used to update ζₖ₋₁ and ζbarₖ
187187
δbarₖ₋₁ = δbarₖ = zero(FC) # Coefficients of Lₖ₋₁ and L̅ₖ modified over the course of two iterations
188188
ψbarₖ₋₁ = ψₖ₋₁ = zero(FC) # ψₖ₋₁ and ψbarₖ are the last components of h̅ₖ = Qₖγ̄₁e₁
189189
norm_vₖ = bNorm / βₖ # ‖vₖ‖ is used for residual norm estimates
190190
ϵₖ₋₃ = λₖ₋₂ = zero(FC) # Components of Lₖ₋₁
191-
wₖ₋₃ .= zero(FC) # Column k-3 of Wₖ = Uₖ(Lₖ)⁻ᴴ
192-
wₖ₋₂ .= zero(FC) # Column k-2 of Wₖ = Uₖ(Lₖ)⁻ᴴ
191+
@kfill!(wₖ₋₃, zero(FC)) # Column k-3 of Wₖ = Uₖ(Lₖ)⁻ᴴ
192+
@kfill!(wₖ₋₂, zero(FC)) # Column k-2 of Wₖ = Uₖ(Lₖ)⁻ᴴ
193193
τₖ = zero(T) # τₖ is used for the dual residual norm estimate
194194

195195
# Stopping criterion.

src/block_krylov_utils.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ end
2020
function gs!(Q::AbstractMatrix{FC}, R::AbstractMatrix{FC}, v::AbstractVector{FC}) where FC <: FloatOrComplex
2121
n, k = size(Q)
2222
aⱼ = v
23-
R .= zero(FC)
23+
@kfill!(R, zero(FC))
2424
for j = 1:k
2525
qⱼ = view(Q,:,j)
2626
aⱼ .= qⱼ
@@ -54,7 +54,7 @@ end
5454

5555
function mgs!(Q::AbstractMatrix{FC}, R::AbstractMatrix{FC}) where FC <: FloatOrComplex
5656
n, k = size(Q)
57-
R .= zero(FC)
57+
@kfill!(R, zero(FC))
5858
for i = 1:k
5959
qᵢ = view(Q,:,i)
6060
R[i,i] = @knrm2(n, qᵢ) # rᵢᵢ = ‖qᵢ‖
@@ -90,7 +90,7 @@ end
9090

9191
function givens!(Q::AbstractMatrix{FC}, R::AbstractMatrix{FC}, C::AbstractVector{T}, S::AbstractVector{FC}) where {T <: AbstractFloat, FC <: FloatOrComplex{T}}
9292
n, k = size(Q)
93-
R .= zero(FC)
93+
@kfill!(R, zero(FC))
9494
pos = 0
9595
for j = 1:k
9696
for i = n-1:-1:j
@@ -106,7 +106,7 @@ function givens!(Q::AbstractMatrix{FC}, R::AbstractMatrix{FC}, C::AbstractVector
106106
R[i,j] = Q[i,j]
107107
end
108108
end
109-
Q .= zero(FC)
109+
@kfill!(Q, zero(FC))
110110
for i = 1:k
111111
Q[i,i] = one(FC)
112112
end
@@ -194,7 +194,7 @@ end
194194

195195
function householder!(Q::AbstractMatrix{FC}, R::AbstractMatrix{FC}, τ::AbstractVector{FC}; compact::Bool=false) where FC <: FloatOrComplex
196196
n, k = size(Q)
197-
R .= zero(FC)
197+
@kfill!(R, zero(FC))
198198
@kgeqrf!(Q, τ)
199199
copy_triangle(Q, R, k)
200200
!compact && @korgqr!(Q, τ)

src/car.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ kwargs_car = (:M, :ldiv, :atol, :rtol, :itmax, :timemax, :verbose, :history, :ca
122122
rNorms, ArNorms = stats.residuals, stats.Aresiduals
123123
reset!(stats)
124124

125-
x .= zero(FC)
125+
@kfill!(x, zero(FC))
126126
if warm_start
127127
mul!(r, A, Δx)
128128
@kaxpby!(n, one(FC), b, -one(FC), r)

src/cg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ kwargs_cg = (:M, :ldiv, :radius, :linesearch, :atol, :rtol, :itmax, :timemax, :v
133133
reset!(stats)
134134
z = MisI ? r : solver.z
135135

136-
x .= zero(FC)
136+
@kfill!(x, zero(FC))
137137
if warm_start
138138
mul!(r, A, Δx)
139139
@kaxpby!(n, one(FC), b, -one(FC), r)

src/cg_lanczos.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ kwargs_cg_lanczos = (:M, :ldiv, :check_curvature, :atol, :rtol, :itmax, :timemax
130130
v = MisI ? Mv : solver.v
131131

132132
# Initial state.
133-
x .= zero(FC)
133+
@kfill!(x, zero(FC))
134134
if warm_start
135135
mul!(Mv, A, Δx)
136136
@kaxpby!(n, one(FC), b, -one(FC), Mv)

src/cg_lanczos_shift.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ kwargs_cg_lanczos_shift = (:M, :ldiv, :check_curvature, :atol, :rtol, :itmax, :t
129129
# Initial state.
130130
## Distribute x similarly to shifts.
131131
for i = 1 : nshifts
132-
x[i] .= zero(FC) # x₀
132+
@kfill!(x[i], zero(FC)) # x₀
133133
end
134134
Mv .= b # Mv₁ ← b
135135
MisI || mulorldiv!(v, M, Mv, ldiv) # v₁ = M⁻¹ * Mv₁
@@ -142,7 +142,8 @@ kwargs_cg_lanczos_shift = (:M, :ldiv, :check_curvature, :atol, :rtol, :itmax, :t
142142
end
143143

144144
# Keep track of shifted systems with negative curvature if required.
145-
indefinite .= false
145+
# We don't want to use @kfill! here because "indefinite" is a BitVector.
146+
fill!(indefinite, false)
146147

147148
if β == 0
148149
stats.niter = 0
@@ -165,10 +166,10 @@ kwargs_cg_lanczos_shift = (:M, :ldiv, :check_curvature, :atol, :rtol, :itmax, :t
165166

166167
# Initialize some constants used in recursions below.
167168
ρ = one(T)
168-
σ .= β
169-
δhat .= zero(T)
170-
ω .= zero(T)
171-
γ .= one(T)
169+
@kfill!(σ, β)
170+
@kfill!(δhat, zero(T))
171+
@kfill!(ω, zero(T))
172+
@kfill!(γ, one(T))
172173

173174
# Define stopping tolerance.
174175
ε = atol + rtol * β

src/cgls.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ kwargs_cgls = (:M, :ldiv, :radius, :λ, :atol, :rtol, :itmax, :timemax, :verbose
146146
Mr = MisI ? r : solver.Mr
147147
Mq = MisI ? q : solver.Mr
148148

149-
x .= zero(FC)
149+
@kfill!(x, zero(FC))
150150
r .= b
151151
bNorm = @knrm2(m, r) # Marginally faster than norm(b)
152152
if bNorm == 0

src/cgls_lanczos_shift.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ kwargs_cgls_lanczos_shift = (:M, :ldiv, :atol, :rtol, :itmax, :timemax, :verbose
134134
# Initial state.
135135
## Distribute x similarly to shifts.
136136
for i = 1 : nshifts
137-
x[i] .= zero(FC) # x₀
137+
@kfill!(x[i], zero(FC)) # x₀
138138
end
139139

140140
u .= b
141-
u_prev .= zero(T)
141+
@kfill!(u_prev, zero(FC))
142142
mul!(v, Aᴴ, u) # v₁ ← Aᴴ * b
143143
β = sqrt(@kdotr(n, v, v)) # β₁ = v₁ᵀ M v₁
144144
rNorms .= β
@@ -169,9 +169,9 @@ kwargs_cgls_lanczos_shift = (:M, :ldiv, :atol, :rtol, :itmax, :timemax, :verbose
169169
# Initialize some constants used in recursions below.
170170
ρ = one(T)
171171
σ .= β
172-
δhat .= zero(T)
173-
ω .= zero(T)
174-
γ .= one(T)
172+
@kfill!(δhat, zero(T))
173+
@kfill!(ω, zero(T))
174+
@kfill!(γ, one(T))
175175

176176
# Define stopping tolerance.
177177
ε = atol + rtol * β

0 commit comments

Comments
 (0)