giordano / Cuba.jl

Library for multidimensional numerical integration with four independent algorithms: Vegas, Suave, Divonne, and Cuhre.
https://giordano.github.io/Cuba.jl/stable
MIT License
77 stars 9 forks source link

Lots of allocations #21

Open jw3126 opened 5 years ago

jw3126 commented 5 years ago

Cuba produces a lot of allocations, 4 per iteration in the example below. Is there a way to avoid this?

julia> using Cuba

julia> f!(x,ret ) = ret
f! (generic function with 1 method)

julia> vegas(f!); # warmup

julia> @time vegas(f!)
  0.001090 seconds (4.01 k allocations: 187.984 KiB)
Component:
 1: 0.0 ± 7.025180405943273e-18 (prob.: -999.0)
Integrand evaluations: 1000
giordano commented 5 years ago

I'll have a look but I'm not sure there is anything I can do about this. The only suggestion I can give you is to use cuhre instead of vegas as the former is more accurate and requires fewer evaluations (thus fewer allocations):

julia> using Cuba, BenchmarkTools

julia> @btime cuhre((x, f) -> f[1] = x[1])
  25.231 μs (784 allocations: 36.89 KiB)
Component:
 1: 0.5 ± 4.291960664289122e-15 (prob.: 0.0)
Integrand evaluations: 195
Fail:                  0
Number of subregions:  2

julia> @btime vegas((x, f) -> f[1] = x[1])
  21.588 ms (928004 allocations: 42.48 MiB)
Component:
 1: 0.4999999506146127 ± 4.969630652405548e-5 (prob.: 2.559630372933608e-5)
Integrand evaluations: 232000
Fail:                  0
Number of subregions:  0