StochasticDiffEq.jl icon indicating copy to clipboard operation
StochasticDiffEq.jl copied to clipboard

using StaticArrays increases allocations by a factor of 4

Open isaacsas opened this issue 3 years ago • 3 comments

This gives about 40MB of allocations, with the allocations scaling linearly in the number of trajectories. @ChrisRackauckas said on Slack this is likely due to allocations in random number generation from using arrays, and could be fixed by using a scalar RNG to fill SVectors here.

using DiffEqBase, StochasticDiffEq, LinearAlgebra, StaticArrays

function fs(u,p,t)
	@inbounds a1 = u[1]-u[3]
	@inbounds a2 = u[2]-u[4]
	du = SVector{4,Float64}(a1,a2,-a1,-a2)
	du *= 2000.0 * exp(-10.0*(a1*a1 + a2*a2))
	du
end
const sq2 = sqrt(2.0)
function gs(u,p,t) 
	SVector{4,Float64}(sq2,sq2,sq2,sq2)
end

const tspan    = (0.0,0.01)
const nsims    = 1000
const outs = (sol,i) -> (sqrt( (sol[1,end]-sol[3,end])^2 + (sol[2,end]-sol[4,end])^2),false)
const u₀s      = @SVector zeros(4)
sdes           = SDEProblem(fs,gs,u₀s,tspan)
sde_sims_svec  = EnsembleProblem(sdes,output_func=outs)

# shows 518K allocations with a size of 40MB for 1000 simulations
@time sols = solve(sde_sims_svec, SOSRA(), trajectories=nsims, saveat=tspan[2]) 

isaacsas avatar Nov 14 '20 18:11 isaacsas

Since I'm not 100% certain it's from here, let's move it to StochasticDiffEq where it might get more eyeballs.

ChrisRackauckas avatar Nov 14 '20 18:11 ChrisRackauckas

Is this what gets called?

https://github.com/SciML/DiffEqNoiseProcess.jl/blob/3130f51e6acdad1502574dd16b1220b362f8bfe1/src/wiener.jl#L74

I guess a dispatch is needed on REAL_WHITE_NOISE_DIST? (Though I can imagine this might require changes here too that make sure dW and W are also StaticArrays?)

isaacsas avatar Nov 14 '20 19:11 isaacsas

Yes, that's probably what needs to get a specialization for StaticArrays.

ChrisRackauckas avatar Nov 14 '20 19:11 ChrisRackauckas