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

Improve Taylor1{TaylorN{T}} mixtures #333

Merged
merged 22 commits into from
Jan 31, 2024
Merged

Improve Taylor1{TaylorN{T}} mixtures #333

merged 22 commits into from
Jan 31, 2024

Conversation

lbenet
Copy link
Member

@lbenet lbenet commented Aug 11, 2023

This PR defines some specialized methods on Taylor1{TaylorN{T}}. This specialization is aimed to improve jet transport Taylor integrations.

@coveralls
Copy link

coveralls commented Aug 11, 2023

Coverage Status

coverage: 98.097% (+0.9%) from 97.198%
when pulling 088c1c3 on lb/improve_mixtures2
into 36586d7 on master.

Copy link
Contributor

@PerezHz PerezHz left a comment

Choose a reason for hiding this comment

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

Many thanks @lbenet for adding these improvements to the handling of mixtures! Looks like this will really help TaylorIntegration.jl and NEOs.jl. I left a couple of comments about some minor stuff, but otherwise LGTM!

src/arithmetic.jl Outdated Show resolved Hide resolved
Comment on lines 351 to 377
# function ($f)(a::Taylor1{TaylorN{T}}, b::Taylor1{TaylorN{S}}) where
# {T<:NumberNotSeries, S<:NumberNotSeries}
# R = promote_type(T,S)
# return ($f)(convert(Taylor1{TaylorN{R}}, a), convert(Taylor1{TaylorN{R}}, b))
# end

# function ($f)(a::Taylor1{TaylorN{T}}, b::Taylor1{TaylorN{T}}) where
# {T<:NumberNotSeries}
# if (a.order != b.order) || any(get_order.(a[:]) .!= get_order.(b[:]))
# a, b = fixorder(a, b)
# end
# res = Taylor1(zero( constant_term(a)), a.order )
# for ordT in eachindex(a)
# ($fc)(res, a, b, ordT)
# end
# return res
# end

# function ($fc)(res::Taylor1{TaylorN{T}}, a::Taylor1{TaylorN{T}},
# b::Taylor1{TaylorN{T}}, ordT::Int) where {T<:NumberNotSeries}
# # Sanity
# zero!(res, a, ordT)
# @inbounds for ordQ in eachindex(a[ordT])
# $(fc)(res[ordT], a[ordT], b[ordT], ordQ)
# end
# return nothing
# end
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these lines intended to stay commented as they are, or does it make sense for them to be deleted?

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 think I wanted to have more specific methods, but something didn't work (ambiguities?). For the time being I'll leave them there, but if they are not needed, I'll remove them.

src/arithmetic.jl Outdated Show resolved Hide resolved
Comment on lines +595 to +571
# v = Array{typeof(aux)}(undef, length(a.coeffs))
# @__dot__ v = a.coeffs / b
# return $T(v, a.order)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like these commented lines are meant to be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, they should be eventually removed. I think I kept them to check performance. I shall keep this conversation open, to remember eventually remove them if not needed.

Comment on lines +627 to +604
# R = typeof(aux)
# coeffs = Array{R}(undef, length(b.coeffs))
# @__dot__ coeffs = b.coeffs / a
# return Taylor1(coeffs, b.order)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above

src/arithmetic.jl Outdated Show resolved Hide resolved
src/arithmetic.jl Outdated Show resolved Hide resolved
src/auxiliary.jl Outdated Show resolved Hide resolved
@PerezHz
Copy link
Contributor

PerezHz commented Sep 4, 2023

If I remember correctly, I think it was suggested in an offline discussion that it probably makes sense to add specialized evaluate methods for mixtures, is that still the case?

@lbenet
Copy link
Member Author

lbenet commented Dec 6, 2023

If I remember correctly, I think it was suggested in an offline discussion that it probably makes sense to add specialized evaluate methods for mixtures, is that still the case?

Sorry for the long delay to get back to your comments... I have implemented most of your suggestions. About the comments, I think I left them there because I was testing improving performance, but either didn't get better results, or got some sort of ambiguities/errors. I left those conversations unresolved, to come back later to them.

@PerezHz
Copy link
Contributor

PerezHz commented Dec 6, 2023

Many thanks for addressing the comments! I agree that probably there's room left to improve performance, probably we can merge this as a first step and then try to get further performance gains in future PRs. Thus, as it stands now, it's probably worth bumping at least the patch version?

@lbenet lbenet force-pushed the lb/improve_mixtures2 branch 2 times, most recently from 9e17ecc to 09e773e Compare December 14, 2023 16:36
@lbenet
Copy link
Member Author

lbenet commented Jan 28, 2024

I've removed some methods of evaluate, restricting their application, that I think had some impact in performance, but that is difficult to judge that. I think I still need to add some tests, and then it should be ready.

In pass, I also checked if there were invalidations triggered by using TaylorSeries; there are none. Once this is merged, I'll check the impact in Taylorintegration.jl

@lbenet
Copy link
Member Author

lbenet commented Jan 29, 2024

This follows the discussion from PerezHz/PlanetaryEphemeris.jl#30 (comment)

The following is the example used, using this branch

julia> using TaylorSeries

julia> x = Taylor1.([rand(3) for _ in 1:5, __ in 1:2])
5×2 Matrix{Taylor1{Float64}}:
    0.0991832863555554 + 0.6683889089955399 t + 0.7967925292561205+ 𝒪(t³)        0.489775369583631 + 0.1409952219760241 t + 0.7373398449922951+ 𝒪(t³)
...

julia> dq = TaylorSeries.set_variables("dq", order=2, numvars=2);

julia> x(3.04 + dq[1])
5×2 Matrix{TaylorN{Float64}}:
      9.494723408075362 + 5.512887486872753 dq₁ + 0.7967925292561205 dq₁² + 𝒪(‖x‖³)      7.732600755871539 + 4.624021479529178 dq₁ + 0.7373398449922951 dq₁² + 𝒪(‖x‖³)
...

julia> @code_warntype x(3.04 + dq[1])
MethodInstance for (::Matrix{Taylor1{Float64}})(::TaylorN{Float64})
  from (p::Array{Taylor1{T}})(x) where T<:Number @ TaylorSeries ~/.julia/dev/TaylorSeries/src/evaluate.jl:102
Static Parameters
  T = Float64
Arguments
  p::Matrix{Taylor1{Float64}}
  x::TaylorN{Float64}
Body::Any
1%1 = Base.broadcasted(TaylorSeries.evaluate, p, x)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2}, Nothing, typeof(evaluate), Tuple{Matrix{Taylor1{Float64}}, TaylorN{Float64}}}
│   %2 = Base.materialize(%1)::Any
└──      return %2

julia> @which evaluate(x, 3.04 + dq[1])
evaluate(x::AbstractArray{Taylor1{T}}, δt::S) where {T<:Number, S<:Number}
     @ TaylorSeries ~/.julia/dev/TaylorSeries/src/evaluate.jl:42

The method quoted uses broadcasting, so I guess that is the problem.

@lbenet
Copy link
Member Author

lbenet commented Jan 31, 2024

@PerezHz I think I have solved the issue you reported. I have checked that all the changes do not induce invalidations, and that tests pass in TaylorIntegration. In my opinion this is ready to be merged, except for tagging the new version; I am in favor of bumping a new minor version. Do you see anything that still has to be addressed?

@PerezHz
Copy link
Contributor

PerezHz commented Jan 31, 2024

@lbenet LGTM! I think there's further improvements to be made in the in-place methods like mul! for mixtures, but we can address this in future PRs (I have an idea regarding this in the works, will try to upload a very WIP proof-of-concept PR today)

@lbenet
Copy link
Member Author

lbenet commented Jan 31, 2024

Great! So I'll bump the patch version, merge, and then release the new version!

@lbenet lbenet merged commit f5587a7 into master Jan 31, 2024
13 checks passed
@lbenet lbenet deleted the lb/improve_mixtures2 branch February 4, 2024 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants