diff --git a/.factory-pre-push.json b/.factory-pre-push.json new file mode 100644 index 00000000..f2ac56ed --- /dev/null +++ b/.factory-pre-push.json @@ -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." + } +} diff --git a/src/Integrals.jl b/src/Integrals.jl index 958a788e..9b715249 100644 --- a/src/Integrals.jl +++ b/src/Integrals.jl @@ -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 @@ -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) diff --git a/test/interface_tests.jl b/test/interface_tests.jl index d56d9111..871b08fe 100644 --- a/test/interface_tests.jl +++ b/test/interface_tests.jl @@ -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