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

add segbuf to quadgkjl #151

Closed
wants to merge 1 commit into from
Closed

add segbuf to quadgkjl #151

wants to merge 1 commit into from

Conversation

lxvm
Copy link
Collaborator

@lxvm lxvm commented Feb 22, 2023

Sometimes quadgk is used in the inner loop of a larger calculation (such as a nested integral) and it is desirable to reduce the number of allocations it makes. This pr adds a segbuf field to the QuadGKJL algorithm to allow the user to pass in a segment buffer that reduces the amount of memory allocated by the routine when reused across calls with compatible domain, integral, and error types.

julia> using Integrals, QuadGK

julia> g(x, (k,)) = sin(k*x)
g (generic function with 1 method)

julia> alg = QuadGKJL(; segbuf=QuadGK.alloc_segbuf(Float64, Float64, Float64))
QuadGKJL{typeof(LinearAlgebra.norm), Vector{QuadGK.Segment{Float64, Float64, Float64}}}(7, LinearAlgebra.norm, QuadGK.Segment{Float64, Float64, Float64}[QuadGK.Segment{Float64, Float64, Float64}(0.0, 0.0, 0.0, 0.0)])

julia> solve(IntegralProblem(g, 0, 1, 80.0), QuadGKJL(; segbuf=QuadGK.alloc_segbuf(Float64, Float64, Float64)))^C

julia> @allocated solve(IntegralProblem(g, 0, 1, 80.0))
2800

julia> @allocated solve(IntegralProblem(g, 0, 1, 80.0), alg)
1072

@ChrisRackauckas
Copy link
Member

ChrisRackauckas commented Mar 8, 2023

I would think this would be best to do at init time?

@lxvm
Copy link
Collaborator Author

lxvm commented Mar 8, 2023

I don't follow what you mean by init time: is it when init.jl load extensions?
In #135 it seemed like it would be a good idea to add solver keywords to the algorithm structs.

Perhaps an issue holding back this pr is that the QuadGK.jl version is restricted to 2.1 in the Project.toml, while segbufs aren't added to QuadGK.jl until version 2.6 by JuliaMath/QuadGK.jl#59

Is the idea to define QuadGKJL in init.jl because the segbuf keyword may or may not exist depending on the QuadGK.jl version? So something like:

@static if isdefined(QuadGK, :alloc_segbuf)
# define QuadGKJL with segbuf
else
# define current version of QuadGKJL
end

order::Int
norm::F
segbuf::S
Copy link
Member

Choose a reason for hiding this comment

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

Need to add the keyword to the docstring.

@ArnoStrouwen
Copy link
Member

I think this is what is meant by init:
https://docs.sciml.ai/SciMLBase/stable/interfaces/Init_Solve/#init-and-the-Iterator-Interface
Currently this is not set up properly in Integrals.jl, but should be added.

@lxvm
Copy link
Collaborator Author

lxvm commented Mar 15, 2023

Thank you for the link. I see how it would be a good idea to cache the quadrature rule, or at least allocate memory for it. It's hard to predict how general-purpose a caching interface for quadratures can be, since there are quadratures tailored to all sorts of problems, but if we set up the init interface then at least each algorithm could have a corresponding cache.

It seems like I could close this pr and open a new one for properly providing the init interface as soon as I have some time for it

@ChrisRackauckas
Copy link
Member

It seems like I could close this pr and open a new one for properly providing the init interface as soon as I have some time for it

Yeah I think this is probably the right direction here. In the end, we really just need the init part of the interface in this package, and then need to just cache everything.

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.

None yet

3 participants