MasonProtter / Bumper.jl

Bring Your Own Stack
MIT License
152 stars 6 forks source link

StackOverflow with eigen #35

Closed mesonepigreco closed 2 weeks ago

mesonepigreco commented 6 months ago

Dear developers, I found that the following code gives rise to a stack overflow:

using Bumper
using LinearAlgebra
 function trial(x)
       @no_escape begin
          T = @alloc(eltype(x), 2, 2)
          T .= 0
          T[1,1] = x
          T[2,2] = x
          eigval, eigvects = eigen(T)
          sum(eigval)
       end
end

julia> trial(2)

Generates the following error:

ERROR: StackOverflowError:
Stacktrace:
 [1] AbstractPtrArray
   @ ~/.julia/packages/StrideArraysCore/VyBzA/src/ptr_array.jl:199 [inlined]
 [2] AbstractPtrArray
   @ ~/.julia/packages/StrideArraysCore/VyBzA/src/ptr_array.jl:456 [inlined]
 [3] AbstractPtrArray
   @ ~/.julia/packages/StrideArraysCore/VyBzA/src/ptr_array.jl:481 [inlined]
 [4] view(A::StrideArraysCore.PtrArray{Int64, 2, (1, 2), Tuple{Int64, Int64}, Tuple{Nothing, Nothing}, Tuple{Static.StaticInt{1}, Static.StaticInt{1}}}, i::StepRange{Int64, Int64}) (repeats 79984 times)
   @ StrideArraysCore ~/.julia/packages/StrideArraysCore/VyBzA/src/stridearray.jl:263

Am I using Bumper in the wrong way? My understanding is that the memory allocated inside @no_escape should not escape the block. Still, here, the block returns a scalar reduction of the allocated array, so the memory should not escape.

Is there another way to diagonalize a matrix allocated on the Bumper stack?

EDIT: Also, the error occurs in the line that calls eigen(T).

MasonProtter commented 2 weeks ago

@mesonepigreco sorry for the late reply. This no longer errors on version 0.7, but it's unlikely to have any performance advantages relative to just doing a regular eigen decomposition (eigen will just allocate new Arrays).