matrixfunctions / GraphMatFun.jl

Computation graphs for matrix functions
MIT License
13 stars 0 forks source link

One too many allocations in generated julia code #60

Closed jarlebring closed 3 years ago

jarlebring commented 3 years ago

At some point start_j disappeared in this code gen_code/gen_julia_code.jl and was replaced by a hard-coded 1.

    push_code!(code, "memslots=Vector{Matrix{T}}(undef,max_memslots)")
    push_code!(code, "n=size(A,1)")
    push_code!(code, "for j=1:max_memslots")
    push_code!(code, "memslots[j]=Matrix{T}(undef,n,n)", ind_lvl = 2)
    push_code!(code, "end")

This generates code with an additional allocation since the code becomes

    max_memslots=8
    memslots=Vector{Matrix{T}}(undef,max_memslots)
    n=size(A,1)
    for j=1:max_memslots
        memslots[j]=Matrix{T}(undef,n,n)
    end
    # The first slots are precomputed nodes [:A]
    memslots[1]=A # overwrite A

The first memslot should just be A which can be overwritten.

When I wanted to fix the memslots (in the previous issue) I copied that problem so the generated code looks a bit different now.