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

column-major access and other perf improvements for generic triangular solves #14475

Merged
merged 1 commit into from
Jan 10, 2016
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
69 changes: 31 additions & 38 deletions base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -763,69 +763,62 @@ function A_mul_Bc!(A::StridedMatrix, B::UnitLowerTriangular)
end

#Generic solver using naive substitution
function naivesub!(A::UpperTriangular, b::AbstractVector, x::AbstractVector=b)
# manually hoisting x[j] significantly improves performance as of Dec 2015
# manually eliding bounds checking significantly improves performance as of Dec 2015
# directly indexing A.data rather than A significantly improves performance as of Dec 2015
# replacing repeated references to A.data with [Adata = A.data and references to Adata] does not significantly impact performance as of Dec 2015
# replacing repeated references to A.data[j,j] with [Ajj = A.data[j,j] and references to Ajj] does not significantly impact performance as of Dec 2015
function naivesub!(A::UpperTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)) must be equal"))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
for j = n:-1:1
xj = b[j]
for k = j+1:1:n
xj -= A[j,k] * x[k]
end
Ajj = A[j,j]
if Ajj == zero(Ajj)
throw(SingularException(j))
else
x[j] = Ajj\xj
@inbounds for j in n:-1:1
A.data[j,j] == zero(A.data[j,j]) && throw(SingularException(j))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might help to do Ajj = A.data[j,j] once instead of 3x

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so as well. So I benchmarked. No significant performance impact. I suppose the compiler is intelligent enough to perform that optimization automagically?

For benefit of future readers, alongside the other optimization notes I added sentences addressing this optimization and manually hoisting A.data out of the loops.

Thanks!

xj = x[j] = A.data[j,j] \ b[j]
for i in j-1:-1:1 # counterintuitively 1:j-1 performs slightly better
b[i] -= A.data[i,j] * xj
end
end
x
end
function naivesub!(A::UnitUpperTriangular, b::AbstractVector, x::AbstractVector=b)
function naivesub!(A::UnitUpperTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)) must be equal"))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
for j = n:-1:1
xj = b[j]
for k = j+1:1:n
xj -= A[j,k] * x[k]
@inbounds for j in n:-1:1
xj = x[j] = b[j]
for i in j-1:-1:1 # counterintuitively 1:j-1 performs slightly better
b[i] -= A.data[i,j] * xj
end
x[j] = xj
end
x
end
function naivesub!(A::LowerTriangular, b::AbstractVector, x::AbstractVector=b)
function naivesub!(A::LowerTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)) must be equal"))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
for j = 1:n
xj = b[j]
for k = 1:j-1
xj -= A[j,k] * x[k]
end
Ajj = A[j,j]
if Ajj == zero(Ajj)
throw(SingularException(j))
else
x[j] = Ajj\xj
@inbounds for j in 1:n
A.data[j,j] == zero(A.data[j,j]) && throw(SingularException(j))
xj = x[j] = A.data[j,j] \ b[j]
for i in j+1:n
b[i] -= A.data[i,j] * xj
end
end
x
end
function naivesub!(A::UnitLowerTriangular, b::AbstractVector, x::AbstractVector=b)
function naivesub!(A::UnitLowerTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)) must be equal"))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
for j = 1:n
xj = b[j]
for k = 1:j-1
xj -= A[j,k] * x[k]
@inbounds for j in 1:n
xj = x[j] = b[j]
for i in j+1:n
b[i] -= A.data[i,j] * xj
end
x[j] = xj
end
x
end
Expand Down