JuliaMath / JuliaMath/HCubature.jl

slow hcubature for large vector-valued integrands

Open
#30 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
168
Forks
28
PR merge metrics
No merged PRs in 30d

Description

I am integrating `L(f) = \int K.(f .- g(x)) dx` where f is a vector of 100-1000 elements, `g` and `K` are scalar, the integration is in R^3. The dimension of the codomain of `K` is the same as `f` because it is easy to vectorize the computation of `K`.

`HCubature` does worse than `Cubature`, the memory allocation being 100 times that of Cubature (`SVector`s do not perform well for large vectors, although it may not have anything to do with `SVector`) and 20 times slower. I am not sure if part of the problem is also that Cubature allows one to copy in place the result of the computation.

Here is a MWE with the timings on my machine. Is there a workaround or will passing an integrand with an extra argument that accepts an in-place return value be considered?

```
using Cubature
using HCubature
using StaticArrays: SVector
using BenchmarkTools

function test_HCubature(sf)
cnt = 0

function integr(pt)
cnt += 1
inv.(1 .+ (sf .+ pt[1]*cos(pt[2])*sin(pt[3])).^2)
end
xmin, xmax = [0.0,0,-2], [0.5, 2π,+2]
r, e = HCubature.hcubature(integr, xmin, xmax; rtol=1e-1, atol=1e-1)
#println("iter: $cnt")
r, e
end

function test_Cubature(f)
cnt = 0
function integr(pt, res)
cnt += 1
res .= inv.(1 .+ (f .+ pt[1]*cos(pt[2])*sin(pt[3])).^2)
end
xmin, xmax = [0.0,0,-2], [0.5, 2π,+2]
r, e = Cubature.hcubature(length(f), integr, xmin, xmax; reltol=1e-1, abstol=1e-1)
#println("iter: $cnt")
r, e
end

f = collect(range(-3,3,length=101))
sf = SVector{length(f)}(f)

@benchmark test_HCubature($sf)
BenchmarkTools.Trial:
memory estimate: 1.18 MiB
allocs estimate: 60213
--------------
minimum time: 2.439 ms (0.00% GC)
median time: 2.578 ms (0.00% GC)
mean time: 2.689 ms (1.55% GC)
maximum time: 4.703 ms (34.03% GC)
--------------
samples: 1858
evals/sample: 1

julia> @benchmark test_Cubature($f)
BenchmarkTools.Trial:
memory estimate: 8.94 KiB
allocs estimate: 160
--------------
minimum time: 15.500 μs (0.00% GC)
median time: 16.800 μs (0.00% GC)
mean time: 18.832 μs (2.05% GC)
maximum time: 2.109 ms (98.33% GC)
--------------
samples: 10000
evals/sample: 1
```
**EDIT:** timing with `@benchmark`.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the MWE comparing HCubature.hcubature and Cubature.hcubature, including the vector-valued integrand and allocation benchmarks. Trace how HCubature.hcubature handles vector results versus the in-place callback accepted by Cubature.hcubature. Done means the performance cause is identified and either a documented workaround or supported in-place approach is demonstrated with comparable benchmarks.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.