Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficiency improvement of exp(::StridedMatrix) with UniformScaling and mul! #40668

Merged
merged 5 commits into from
May 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
end
ilo, ihi, scale = LAPACK.gebal!('B', A) # modifies A
nA = opnorm(A, 1)
Inn = Matrix{T}(I, n, n)
## For sufficiently small nA, use lower order Padé-Approximations
if (nA <= 2.1)
if nA > 0.95
Expand All @@ -634,17 +633,21 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
C = T[120.,60.,12.,1.]
end
A2 = A * A
P = copy(Inn)
U = C[2] * P
V = C[1] * P
for k in 1:(div(size(C, 1), 2) - 1)
# Compute U and V: Even/odd terms in Padé numerator & denom
# Expansion of k=1 in for loop
P = A2
U = C[2]*I + C[4]*P
V = C[1]*I + C[3]*P
for k in 2:(div(size(C, 1), 2) - 1)
k2 = 2 * k
P *= A2
U += C[k2 + 2] * P
V += C[k2 + 1] * P
mul!(U, C[k2 + 2], P, true, true) # U += C[k2+2]*P
mul!(V, C[k2 + 1], P, true, true) # V += C[k2+1]*P
end

U = A * U
X = V + U
# Padé approximant: (V-U)\(V+U)
LAPACK.gesv!(V-U, X)
else
s = log2(nA/5.4) # power of 2 later reversed by squaring
Expand All @@ -660,10 +663,27 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
A2 = A * A
A4 = A2 * A2
A6 = A2 * A4
U = A * (A6 * (CC[14].*A6 .+ CC[12].*A4 .+ CC[10].*A2) .+
CC[8].*A6 .+ CC[6].*A4 .+ CC[4].*A2 .+ CC[2].*Inn)
V = A6 * (CC[13].*A6 .+ CC[11].*A4 .+ CC[9].*A2) .+
CC[7].*A6 .+ CC[5].*A4 .+ CC[3].*A2 .+ CC[1].*Inn
Ut = CC[4]*A2
Ut[diagind(Ut)] .+= CC[2]
# Allocation economical version of:
#U = A * (A6 * (CC[14].*A6 .+ CC[12].*A4 .+ CC[10].*A2) .+
# CC[8].*A6 .+ CC[6].*A4 .+ Ut)
U = mul!(CC[8].*A6 .+ CC[6].*A4 .+ Ut,
A6,
CC[14].*A6 .+ CC[12].*A4 .+ CC[10].*A2,
true, true)
U = A*U

# Allocation economical version of: Vt = CC[3]*A2 (recycle Ut)
Vt = mul!(Ut, CC[3], A2, true, false)
Vt[diagind(Vt)] .+= CC[1]
# Allocation economical version of:
#V = A6 * (CC[13].*A6 .+ CC[11].*A4 .+ CC[9].*A2) .+
# CC[7].*A6 .+ CC[5].*A4 .+ Vt
V = mul!(CC[7].*A6 .+ CC[5].*A4 .+ Vt,
A6,
CC[13].*A6 .+ CC[11].*A4 .+ CC[9].*A2,
true, true)

X = V + U
LAPACK.gesv!(V-U, X)
Expand Down