-
Notifications
You must be signed in to change notification settings - Fork 53
Further allocation reduction in power computations #361
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
Changes from 12 commits
dbdbcc1
a34ce7b
c04d9f8
5d3d57d
a9c770b
5bcccf4
6cce90a
a205dcd
e44811c
2e4e60a
f581b21
27c3d0e
dc6a68c
364faf7
99e3061
efa02a1
d9af8ca
68bf845
8610e21
ba30e7a
ea15252
21c876b
702fbac
6264eae
4f613d6
ad18057
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,8 @@ function resize_coeffs1!(coeffs::Array{T,1}, order::Int) where {T<:Number} | |
| lencoef = length(coeffs) | ||
| resize!(coeffs, order+1) | ||
| if order > lencoef-1 | ||
| z = zero(coeffs[1]) | ||
| @simd for ord in lencoef+1:order+1 | ||
| @inbounds coeffs[ord] = z | ||
| @inbounds coeffs[ord] = zero(coeffs[1]) | ||
|
||
| end | ||
| end | ||
| return nothing | ||
|
|
@@ -39,9 +38,8 @@ function resize_coeffsHP!(coeffs::Array{T,1}, order::Int) where {T<:Number} | |
| @assert order ≤ get_order() && lencoef ≤ num_coeffs | ||
| num_coeffs == lencoef && return nothing | ||
| resize!(coeffs, num_coeffs) | ||
| z = zero(coeffs[1]) | ||
| @simd for ord in lencoef+1:num_coeffs | ||
| @inbounds coeffs[ord] = z | ||
| @inbounds coeffs[ord] = zero(coeffs[1]) | ||
|
||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,7 +156,7 @@ struct TaylorN{T<:Number} <: AbstractSeries{T} | |
| order :: Int | ||
|
|
||
| function TaylorN{T}(v::Array{HomogeneousPolynomial{T},1}, order::Int) where T<:Number | ||
| coeffs = zeros(HomogeneousPolynomial{T}, order) | ||
| coeffs = isempty(v) ? zeros(HomogeneousPolynomial{T}, order) : zeros(v[1], order) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we would need similar fixes in |
||
| @inbounds for i in eachindex(v) | ||
| ord = v[i].order | ||
| if ord ≤ order | ||
|
|
||
lbenet marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,37 +58,98 @@ end | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ^(a::Taylor1{TaylorN{T}}, r::Rational) where {T<:NumberNotSeries} = a^(r.num/r.den) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # power_by_squaring; slightly modified from base/intfuncs.jl | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Licensed under MIT "Expat" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval function power_by_squaring(x::$T, p::Integer) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if p == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return copy(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elseif p == 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return one(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elseif p == 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return square(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # in-place form of power_by_squaring | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # this method assumes `y`, `x` and `aux` are of same order | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for T in (:Taylor1, :TaylorN) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval function power_by_squaring!(y::$T{T}, x::$T{T}, aux::$T{T}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| p::Integer) where {T<:NumberNotSeries} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| t = trailing_zeros(p) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| p >>= t | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # aux = x | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in eachindex(aux) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| identity!(aux, x, k) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while (t -= 1) > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| x = square(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # aux = square(aux) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in reverse(eachindex(aux)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sqr!(aux, k) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # y = aux | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in eachindex(y) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| identity!(y, aux, k) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| y = x | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while p > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| t = trailing_zeros(p) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| p >>= t | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while (t -= 1) ≥ 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # aux = square(aux) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in reverse(eachindex(aux)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sqr!(aux, k) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # y = y * aux | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mul!(y, aux) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # power_by_squaring; slightly modified from base/intfuncs.jl | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Licensed under MIT "Expat" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval power_by_squaring(x::$T, p::Integer) = power_by_squaring(x, Val(p)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval power_by_squaring(x::$T, ::Val{0}) = one(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval power_by_squaring(x::$T, ::Val{1}) = copy(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval power_by_squaring(x::$T, ::Val{2}) = square(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @eval function power_by_squaring(x::$T, ::Val{P}) where P | ||||||||||||||||||||||||||||||||||||||||||||||||||
| p = P # copy static parameter `P` into local variable `p` | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if $T != HomogeneousPolynomial | ||||||||||||||||||||||||||||||||||||||||||||||||||
| y = zero(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| aux = zero(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| power_by_squaring!(y, x, aux, p) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||
| t = trailing_zeros(p) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| p >>= t | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while (t -= 1) > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| x = square(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| y *= x | ||||||||||||||||||||||||||||||||||||||||||||||||||
| y = x | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while p > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| t = trailing_zeros(p) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| p >>= t | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while (t -= 1) ≥ 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| x = square(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| y *= x | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if $T != HomogeneousPolynomial | |
| y = zero(x) | |
| aux = zero(x) | |
| power_by_squaring!(y, x, aux, p) | |
| else | |
| t = trailing_zeros(p) + 1 | |
| p >>= t | |
| while (t -= 1) > 0 | |
| x = square(x) | |
| end | |
| y *= x | |
| y = x | |
| while p > 0 | |
| t = trailing_zeros(p) + 1 | |
| p >>= t | |
| while (t -= 1) ≥ 0 | |
| x = square(x) | |
| end | |
| y *= x | |
| end | |
| end | |
| y = zero(x) | |
| aux = zero(x) | |
| power_by_squaring!(y, x, aux, p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for noticing this @lbenet! I agree there's better ways to deal with this if block, I'm just wondering if it makes sense to have a power_by_squaring! for HomogeneousPolynomial, since inside such methods, there's a lot of in-place squaring going around, which would require a different treatment, since squaring a HomogeneousPolynomial changes its order, so some extra auxiliaries might be needed, I think? Anyway, I'm pushing a commit where the power_by_squaring! method for HomogeneousPolynomial is declared separately, so that this if does not occur inside the method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it is worth keeping both methods for HomogeneousPolynomials. I think they can be useful in methods that exploit evaluate, which is worth checking, though perhaps in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I added a comment with a TODO for another PR
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these methods are defined above in lines 104-106...
lbenet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
lbenet marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the following solves the problem...
| (kodd == 0) && ( @inbounds c[k] = c[k >> 1]^2 ) | |
| (kodd == 0) && ( @inbounds c[k] += c[k >> 1]^2 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! It does the trick, many thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An equivalent method
mul!(a::Taylor1{T}, b::Taylor1{T}) where {T<:Number})could be useful for the extension ofpow!forTaylor1{TaylorN{T}}?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would indeed by useful, I've added the corresponding method, thanks!