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

Override Base.sum! #368

Merged
merged 2 commits into from
Aug 22, 2024
Merged

Override Base.sum! #368

merged 2 commits into from
Aug 22, 2024

Conversation

LuEdRaMo
Copy link
Contributor

@LuEdRaMo LuEdRaMo commented Aug 3, 2024

This PR leverages TS.add! to reduce allocations when summing an array of Taylors. For instance:

# Generate a vector of random Taylor1s
order = 25
n = 10_000
v = [Taylor1(randn(order)) for _ in 1:n]
# New sum! method
y = zero(v[1])
sum!(y, v)
# Compare with existing methods
y == +(v...) # true
y  sum(v) # true
# Benchmark
@time +(v...)
# 0.002061 seconds (30.00 k allocations: 3.357 MiB)
@time sum(v)
# 0.001163 seconds (10.00 k allocations: 2.441 MiB)
@time begin
    y = zero(v[1])
    sum!(y, v)
end
# 0.000688 seconds (3 allocations: 320 bytes)

@lbenet
Copy link
Member

lbenet commented Aug 3, 2024

Thanks a lot for this addition! I just restarted the problematic job (in the tests); I guess it will run ok. Please ping me when you want me to review this PR.

@@ -405,6 +405,18 @@ for (f, fc) in ((:+, :(add!)), (:-, :(subst!)))
end
end

for T in (:Taylor1, :TaylorN)
@eval begin
function sum!(v::$T{S}, a::AbstractArray{$T{S}}) where {S <: Number}
Copy link
Member

Choose a reason for hiding this comment

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

Outside from this for-loop, I suggest to add a method for sum! with the signaturesum!(v::TaylorN{S}, a::AbstractArray{HomogeneousPolynomial{S}}), which is used in some evaluate and/or related functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! I've added the corresponding method, although I don't know which method of evaluate will benefit the most from it.

@lbenet
Copy link
Member

lbenet commented Aug 22, 2024

Thanks a lot @LuEdRaMo. I'll go ahead and merge it; in a separate PR, I shall use sum! to optimise some parts of the code.

@lbenet lbenet merged commit ae47f13 into JuliaDiff:master Aug 22, 2024
13 checks passed
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.

2 participants