DifferentialEquations.jl: Malloc errors in ODEProblems

k-ray-liu
Thu, 05 Jun 2025 21:23:32 +0000
General Usage

Hi there,

I’m having memory related problem when running the solver for ODEProblems, with the DifferentialEquations.jl package.

It’s very inconsistent when the problem will arise (I’m scanning across a large number of different parameters that give different RHS to the ODE), but when it does, a typical error message (from a linux cluster):

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

[2640520] signal (6.-6): Aborted
in expression starting at none:0
srun: error:  XXX: task 0: Aborted (core dumped)

The fact Julia didn’t throw the error made me question if it’s a problem with the cluster, but I’ve also encountered this on my local MacOS.

A minimal example:

function solver(parameter)

    ρ0 = genstate(parameter)
    p = geninput(parameter)

    tspan = (0, 100)
    prob = ODEProblem(lindbladian!, ρ0, tspan, p)
    method = DP8()  # supposedly stable memory wise
    sol = solve(prob, method, reltol = 1e-6, abstol = 1e-6)
end

Threads.@nthreads for parameter in parameters
    solver(parameter)
end

The function (in its original form)

function lindbladian!(du, u, p, t)

    h = p[1]
    ops = p[2]
    γs = p[3]

    du .= -1im *  commutator(h, u)

    for (i, op) in enumerate(ops)
        du .+= γs[i] * ( op * u * op' - 1/2 * anticommutator( op' * op, u))
    end
    nothing
end

I think the details for this function are not too important, but essentially all it does is doing a series of matrix multiplications in place (u is dense, but other matrices h, op are sparse, \gamma is a number) , all with the same size:

Throughout all the examples, u, h, op has dimensions 256 \times 256, which is certainly not small but not to the point of exhausting GBs of RAM (I think?)

The output are not particularly large ( sizeof(sol.u) is well below 1GB), and I’ve played around with different solvers, some definitely performed better than others, but none have been completely free of this problem.

Sorry I currently don’t have much further information to work with, since this problem itself appears somewhat randomly

7 posts - 3 participants