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

Less allocs in resid2DLinear and faster mean #261

Merged
merged 1 commit into from
Oct 26, 2023
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
9 changes: 5 additions & 4 deletions src/CommonUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@


function resid2DLinear(μ, mus, Lambdas; diffop::Function=-) # '-' exploits EuclideanManifold commutativity a-b = b-a
dμ = broadcast(diffop, μ, mus) # mus .- μ ## μ .\ mus
# dμ = broadcast(diffop, μ, mus) # mus .- μ ## μ .\ mus
# @show round.(dμ, digits=4)
ret = sum( Lambdas.*dμ )
return ret
# ret = sum( Lambdas.*dμ )
r = map((mu, lam) -> diffop(μ[], mu) * lam, mus, Lambdas)
Copy link
Member Author

Choose a reason for hiding this comment

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

@dehann, here is a version without the extra dispatch and allocations.
Is μ always of length 1?

Copy link
Member

Choose a reason for hiding this comment

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

I think so, will double check bit later today.

Copy link
Member

Choose a reason for hiding this comment

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

looks right

return sum(r)
end

function solveresid2DLinear!(res, x, mus, Lambdas; diffop::Function=-)::Nothing
Expand Down Expand Up @@ -71,7 +72,7 @@ function _getManifoldFullOrPart(mkd::ManifoldKernelDensity, aspartial::Bool=true
end

function Statistics.mean(mkd::ManifoldKernelDensity, aspartial::Bool=true; kwargs...)
return mean(_getManifoldFullOrPart(mkd,aspartial), getPoints(mkd, aspartial); kwargs...)
return mean(_getManifoldFullOrPart(mkd,aspartial), getPoints(mkd, aspartial), GeodesicInterpolation(); kwargs...)
Copy link
Member Author

Choose a reason for hiding this comment

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

GeodesicInterpolation is currently way faster but still has dynamic dispatch.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ExtrinsicEstimation should usually be the fastest way to compute mean. It doesn't work well for some manifolds but for Lie groups it is worth trying out when you prefer speed over accuracy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I'll give it a try. With all the performance enhancements you helped with so far we are seeing a major improvement already.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great! Feel free to report any further issues you think I could help with.

Copy link
Member Author

Choose a reason for hiding this comment

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

ExtrinsicEstimation gives an error:

MethodError: no method matching get_embedding(::GroupManifold{ℝ, ProductManifold{ℝ, Tuple{TranslationGroup{ManifoldsBase.TypeParameter{Tuple{2}}, ℝ}, SpecialOrthogonal{ManifoldsBase.TypeParameter{Tuple{2}}}}}, Manifolds.SemidirectProductOperation{RotationAction{LeftAction, TranslationGroup{ManifoldsBase.TypeParameter{Tuple{2}}, ℝ}, SpecialOrthogonal{ManifoldsBase.TypeParameter{Tuple{2}}}}}})

Don't worry about it now though. I think there are other optimizations that will make a bigger difference.

end
"""
$SIGNATURES
Expand Down
Loading