Skip to content

Commit e60b45f

Browse files
committed
✨ Improve allocations
1 parent cca03b4 commit e60b45f

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/spectrum.jl

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function _spectrum(
174174

175175
# Calculate |v₁> = B|ρss>
176176
ρss =
177-
solver.steadystate_solver === nothing ? mat2vec(steadystate(L)) :
177+
isnothing(solver.steadystate_solver) ? mat2vec(steadystate(L)) :
178178
mat2vec(steadystate(L; solver = solver.steadystate_solver))
179179
vₖ = (spre(B) * ρss).data
180180

@@ -219,16 +219,16 @@ function _spectrum(
219219
# Previous and next left/right Krylov vectors
220220
v₋₁ = vT(zeros(cT, D^2))
221221
v₊₁ = vT(zeros(cT, D^2))
222-
w₋₁ = vT(zeros(cT, D^2))'
223-
w₊₁ = vT(zeros(cT, D^2))'
222+
w₋₁ = transpose(vT(zeros(cT, D^2)))
223+
w₊₁ = transpose(vT(zeros(cT, D^2)))
224224

225225
# Frequency of renormalization
226226
renormFrequency::typeof(solver.maxiter) = 1
227227

228228
# Loop over the Krylov subspace(s)
229229
for k in 1:solver.maxiter
230230
# k-th diagonal element
231-
w₊₁ = wₖ * L.data
231+
mul!(w₊₁.parent, transpose(L.data), wₖ.parent) # Equivalent to: w₊₁ = wₖ * L.data
232232
αₖ = w₊₁ * vₖ
233233

234234
# Update A(k), B(k) and continuous fraction; normalization avoids overflow
@@ -239,7 +239,7 @@ function _spectrum(
239239

240240
# Renormalize Euler sequences to avoid overflow
241241
if k % renormFrequency == 0
242-
maxNorm .= max.(abs.(Aₖ), abs.(Bₖ))
242+
map!((x, y) -> max(abs(x), abs(y)), maxNorm, Aₖ, Bₖ)
243243
Aₖ ./= maxNorm
244244
Bₖ ./= maxNorm
245245
A₋₁ ./= maxNorm
@@ -248,8 +248,8 @@ function _spectrum(
248248

249249
# Check for convergence
250250
maxResidue =
251-
maximum(abs.(lanczosFactor .- lanczosFactor₋₁)) /
252-
max(maximum(abs.(lanczosFactor)), maximum(abs.(lanczosFactor₋₁)))
251+
mapreduce((x, y) -> abs(x - y), max, lanczosFactor, lanczosFactor₋₁) /
252+
max(maximum(abs, lanczosFactor), maximum(abs, lanczosFactor₋₁))
253253
if maxResidue <= solver.tol
254254
if solver.verbose > 1
255255
println("spectrum(): solver::Lanczos converged after $(k) iterations")
@@ -258,14 +258,13 @@ function _spectrum(
258258
end
259259

260260
# (k+1)-th left/right vectors, orthogonal to previous ones
261-
# Consider using explicit BLAS calls
262-
v₊₁ = L.data * vₖ
261+
mul!(v₊₁,L.data,vₖ) # Equivalent to: v₊₁ = L.data * vₖ
263262
v₊₁ .= v₊₁ .- αₖ .* vₖ .- βₖ .* v₋₁
264263
w₊₁ .= w₊₁ .- αₖ .* wₖ .- δₖ .* w₋₁
265-
v₋₁ .= vₖ
266-
w₋₁ .= wₖ
267-
vₖ .= v₊
268-
wₖ .= w₊₁
264+
v₋₁, vₖ = vₖ, v₋₁
265+
vₖ, v₊₁ = v₊₁, vₖ
266+
w₋₁, wₖ = wₖ, w₋
267+
wₖ, w₊₁ = w₊₁, wₖ
269268

270269
# k-th off-diagonal elements
271270
buf = wₖ * vₖ
@@ -277,10 +276,10 @@ function _spectrum(
277276
wₖ ./= βₖ
278277

279278
# Update everything for the next cycle
280-
A₋₂ .= A₋₁
281-
A₋₁ .= Aₖ
282-
B₋₂ .= B₋₁
283-
B₋₁ .= Bₖ
279+
A₋₂, A₋₁ = A₋₁, A₋₂
280+
A₋₁, Aₖ = Aₖ, A₋₁
281+
B₋₂, B₋₁ = B₋₁, B₋₂
282+
B₋₁, Bₖ = Bₖ, B₋₁
284283
end
285284

286285
if solver.verbose > 0 && maxResidue > solver.tol

0 commit comments

Comments
 (0)