Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .factory-pre-push.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exceptions": {
".jl": "Julia files in this branch were formatted in place with the Julia Runic package (Runic.format_file, SciML style). The worker pre-push hook cannot run the check for this repo: it detects Runic only via a fredrikekre/runic action or pre-commit config, while this repo declares Runic through the SciML reusable workflow runic.yml, and no runic binary for the Julia formatter exists on this machine."
}
}
41 changes: 35 additions & 6 deletions src/Integrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ function __solvebp_call(
@SciMLMessage("Using pre-allocated buffer for HCubatureJL", cache.verbosity, :buffer_allocation)
end

if f isa BatchIntegralFunction
throw(
ArgumentError(
"HCubatureJL does not support BatchIntegralFunction. " *
"Use an algorithm that supports batching such as QuadGKJL, VEGAS, " *
"CubatureJLh, CubatureJLp, CubaVegas, CubaSUAVE, CubaDivonne, or CubaCuhre."
)
)
end
@assert f isa IntegralFunction
if isinplace(f)
# allocate a new output array at each evaluation since HCubature.jl doesn't support
Expand Down Expand Up @@ -428,12 +437,32 @@ function __solvebp_call(
mid isa Number ? vec(xx) : xx
end
if isinplace(prob)
y = similar(
prob.f.integrand_prototype,
size(prob.f.integrand_prototype)[begin:(end - 1)]...,
prob.f.max_batch
)
_f = x -> (f(y, wrangle(x), p); vec(y))
# MonteCarloIntegration's `vegas` evaluates the integrand once per
# iteration on `alg.ncalls` points, so the output buffer must hold
# one value per point. When `max_batch` is smaller than the batch,
# evaluate in chunks of at most `max_batch` points and assemble the
# chunk outputs into the full-length result.
front = size(prob.f.integrand_prototype)[begin:(end - 1)]
nchunk = max(1, min(prob.f.max_batch, alg.ncalls))
y = similar(prob.f.integrand_prototype, front..., nchunk)
_f = x -> begin
pts = wrangle(x)
n = size(pts, ndims(pts))
yfull = similar(prob.f.integrand_prototype, front..., n)
if n <= nchunk
f(yfull, pts, p)
else
pax = ntuple(_ -> (:), ndims(pts) - 1)
yax = ntuple(_ -> (:), length(front))
for s in 1:nchunk:n
e = min(s + nchunk - 1, n)
ychunk = similar(prob.f.integrand_prototype, front..., e - s + 1)
f(ychunk, pts[pax..., s:e], p)
yfull[yax..., s:e] .= ychunk
end
end
return vec(yfull)
end
else
y = mid isa Number ? f(typeof(mid)[], p) :
f(Matrix{eltype(mid)}(undef, length(mid), 0), p)
Expand Down
39 changes: 39 additions & 0 deletions test/interface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,45 @@ end
end
end

# https://github.com/SciML/Integrals.jl/issues/377
@testset "BatchIntegralFunction regressions" begin
f_iip!(y, x, p) = (y .= vec(sum(abs2, x; dims = 1)); nothing)
g(x, p) = sum(abs2, x; dims = 1) |> vec
# ∫∫ (x² + y²) dx dy over the unit square is 2/3.
# VEGAS with an in-place BatchIntegralFunction and default max_batch
# (typemax(Int)) must size its buffer by the requested batch length.
prob = IntegralProblem(
BatchIntegralFunction(f_iip!, zeros(0)),
([0.0, 0.0], [1.0, 1.0])
)
sol = solve(prob, VEGAS(seed = 123); abstol = 1.0e-3, reltol = 1.0e-3)
@test sol.u ≈ 2 / 3 rtol = 1.0e-2
# max_batch smaller than ncalls must chunk the evaluation.
prob_chunked = IntegralProblem(
BatchIntegralFunction(f_iip!, zeros(0); max_batch = 10),
([0.0, 0.0], [1.0, 1.0])
)
sol_chunked = solve(prob_chunked, VEGAS(seed = 123); abstol = 1.0e-3, reltol = 1.0e-3)
@test sol_chunked.u ≈ 2 / 3 rtol = 1.0e-2
@test sol_chunked.u ≈ sol.u
# HCubatureJL does not support BatchIntegralFunction.
for bf in (BatchIntegralFunction(g), BatchIntegralFunction(f_iip!, zeros(0)))
prob_hc = IntegralProblem(bf, ([0.0, 0.0], [1.0, 1.0]))
@test_throws ArgumentError solve(prob_hc, HCubatureJL())
err = try
solve(prob_hc, HCubatureJL())
nothing
catch e
e
end
@test err isa ArgumentError
@test occursin(
"HCubatureJL does not support BatchIntegralFunction",
sprint(showerror, err)
)
end
end

# https://github.com/SciML/Integrals.jl/issues/315
@testset "a small integral is not swamped by the default abstol" begin
en = 1.0e-6
Expand Down
Loading