-
-
Notifications
You must be signed in to change notification settings - Fork 30
Wrap GSL.jl in extension #213
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module IntegralsGSLExt | ||
|
||
using GSL | ||
using Integrals | ||
using Integrals: IntegralCache | ||
|
||
mutable struct GSLCache{T} | ||
value::T | ||
end | ||
getvalue(cache::GSLCache) = cache.value | ||
|
||
function Integrals.init_cacheval(alg::GSLIntegration{typeof(integration_cquad)}, prob::IntegralProblem) | ||
ws = integration_cquad_workspace_alloc(alg.kws.wssize) | ||
gslcache = GSLCache(ws) | ||
finalizer(integration_cquad_workspace_free∘getvalue, gslcache) | ||
result = Cdouble[0] | ||
abserr = Cdouble[0] | ||
nevals = C_NULL # Csize_t[0] | ||
return (; gslcache, result, abserr, nevals) | ||
end | ||
|
||
function Integrals.__solvebp_call(cache::IntegralCache, alg::GSLIntegration{typeof(integration_cquad)}, sensealg, domain, p; | ||
reltol = 1e-8, abstol = 1e-8, maxiters = nothing) | ||
|
||
prob = Integrals.build_problem(cache) | ||
|
||
if !all(isone∘length, domain) | ||
error("GSLIntegration only accepts one-dimensional quadrature problems.") | ||
end | ||
@assert prob.f isa IntegralFunction | ||
|
||
f = if isinplace(prob) | ||
@assert isone(length(prob.f.integrand_prototype)) "GSL only supports scalar, real-valued integrands" | ||
y = similar(prob.f.integrand_prototype, Cdouble) | ||
x -> (prob.f(y, x, p); only(y)) | ||
else | ||
x -> Cdouble(only(prob.f(x, p))) | ||
end | ||
# gslf = @gsl_function(f) # broken, see: https://github.com/JuliaMath/GSL.jl/pull/128 | ||
ptr = @cfunction($((x,p) -> f(x)), Cdouble, (Cdouble, Ptr{Cvoid})) | ||
gslf = GC.@preserve ptr gsl_function(Base.unsafe_convert(Ptr{Cvoid},ptr), 0) | ||
a, b = map(Cdouble∘only, domain) | ||
(; gslcache, result, abserr, nevals) = cache.cacheval | ||
integration_cquad(gslf, a, b, abstol, reltol, getvalue(gslcache), result, abserr, nevals) | ||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return SciMLBase.build_solution(prob, alg, only(result), only(abserr), retcode = ReturnCode.Success) | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,3 +370,16 @@ end | |
function ArblibJL(; check_analytic=false, take_prec=false, warn_on_no_convergence=false, opts=C_NULL) | ||
return ArblibJL(check_analytic, take_prec, warn_on_no_convergence, opts) | ||
end | ||
|
||
|
||
""" | ||
GSLIntegration(routine; kws...) | ||
|
||
One-dimensional quadrature of Float64-valued function using `routine` from GSL with | ||
additional arguments. For example `using Integrals, GSL; GSLIntegration(integration_cquad; wssize=100)` | ||
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. We should probably document the available functions. How many are there? 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. There are quite a few, since the GSL includes a rewrite of quadpack. I'll put a full list below and I hope that most of them can be wrapped metaprogramatically 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.
|
||
""" | ||
struct GSLIntegration{F,A<:NamedTuple} <: SciMLBase.AbstractIntegralAlgorithm | ||
f::F | ||
kws::A | ||
end | ||
GSLIntegration(f; kws...) = GSLIntegration(f, NamedTuple(kws)) |
Uh oh!
There was an error while loading. Please reload this page.