Skip to content

Why jacobian! allocates despite using pre-allocated memory ? #402

Closed
@j-fu

Description

@j-fu

Hi,
I am greatly enjoying the functionality of ForwardDiff. I use the module in a finite volume
solver VoronoiFVM.jl for user callback functions describing fluxes between control volumes etc. This means that I have lots of calls on small amounts of data.
However, each call to jacobian! has 4 allocations.
Here is a test example:

using ForwardDiff, DiffResults
using LinearAlgebra

function tfwd()

    n=2
    function f(y,u)
        y[1]=u[1]*u[2]
        y[2]=u[2]-u[1]^3
    end
    # Create pre-allocated data storage
    result=DiffResults.DiffResult(Vector{Float64}(undef,n),Matrix{Float64}(undef,n,n))

    U=[1.0,1.0]
    Y=zeros(n)

    # Call jacobian! with pre-allocated result and Y array. Why do we see allocations here ?
    @time ForwardDiff.jacobian!(result,f,Y,U)

    
    res=DiffResults.value(result)
    jac=DiffResults.jacobian(result)
end


tfwd()
tfwd()

The output is:

0.476762 seconds (1.44 M allocations: 71.990 MiB, 2.81% gc time)
0.000017 seconds (4 allocations: 336 bytes)

Is this unavoidable ?

Jürgen

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions