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

StackOverflowError: infinite call loop between LazyArrays and ArrayLayouts

Open putianyi889 opened this issue 4 years ago • 2 comments

julia> A = zeros(1,1) * BandedMatrix(0=>Zeros(∞))[1:1,1:1] * zeros(1,1)
(1×1 Array{Float64,2}) * (1×1 BandedMatrix{Float64} with bandwidths (0, 0) with data (1×0 view(::Array{Float64,2}, 1:1, 1:0) with eltype Float64) * (0×1 view(::LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}}, 1:0, 1:1) with eltype Float64)) * (1×1 Array{Float64,2}):
 0.0

julia> Matrix(A)
ERROR: StackOverflowError:
Stacktrace:
 [1] simplify at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\linalg\mul.jl:290 [inlined]
 [2] simplify(::ArrayLayouts.Mul{LazyArrays.ApplyLayout{typeof(*)},ArrayLayouts.DenseColumnMajor,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{Array{Float64,2},BandedMatrix{Float64,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},SubArray{Float64,2,LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}},Base.OneTo{Int64}}}},Array{Float64,2}}) at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\linalg\mul.jl:300       
 [3] copy at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\linalg\mul.jl:305 [inlined]
 [4] materialize at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\mul.jl:105 [inlined]
 [5] mul at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\mul.jl:106 [inlined]
 [6] * at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\mul.jl:170 [inlined]
 [7] * at .\operators.jl:538 [inlined]
 [8] _default_materialize at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:86 [inlined]
 [9] copy at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:89 [inlined]
 [10] materialize at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:81 [inlined]
 [11] copyto! at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:92 [inlined]
 [12] _copyto! at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:334 [inlined]
 [13] _copyto!(::Array{Float64,2}, ::LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{Array{Float64,2},BandedMatrix{Float64,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},SubArray{Float64,2,LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}},Base.OneTo{Int64}},Array{Float64,2}}}) at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\ArrayLayouts.jl:169
 [14] copyto!(::Array{Float64,2}, ::LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{Array{Float64,2},BandedMatrix{Float64,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},SubArray{Float64,2,LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}},Base.OneTo{Int64}},Array{Float64,2}}}) at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\ArrayLayouts.jl:171
 ... (the last 4 lines are repeated 21758 more times)
 [87047] copyto! at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:92 [inlined]
 [87048] _copyto! at C:\Users\pty\.julia\packages\LazyArrays\6zNYO\src\lazyapplying.jl:334 [inlined]
 [87049] _copyto!(::Array{Float64,2}, ::LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{Array{Float64,2},BandedMatrix{Float64,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},SubArray{Float64,2,LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}},Base.OneTo{Int64}},Array{Float64,2}}}) at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\ArrayLayouts.jl:169
 [87050] copyto! at C:\Users\pty\.julia\packages\ArrayLayouts\ATRic\src\ArrayLayouts.jl:171 [inlined]
 [87051] copyto_axcheck! at .\abstractarray.jl:946 [inlined]
 [87052] Array{Float64,2}(::LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{Array{Float64,2},BandedMatrix{Float64,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},SubArray{Float64,2,LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}},Base.OneTo{Int64}},Array{Float64,2}}}) at .\array.jl:562
 [87053] Array{T,2} where T(::LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{Array{Float64,2},BandedMatrix{Float64,LazyArrays.ApplyArray{Float64,2,typeof(*),Tuple{SubArray{Float64,2,Array{Float64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},SubArray{Float64,2,LazyArrays.ApplyArray{Float64,2,typeof(vcat),Tuple{Zeros{Float64,2,Tuple{Base.OneTo{Int64},InfiniteArrays.OneToInf{Int64}}}}},Tuple{UnitRange{Int64},UnitRange{Int64}},false}}},Base.OneTo{Int64}},Array{Float64,2}}}) at .\boot.jl:428

In the above example, the error won't happen with one of the following modifications:

  • Delete zeros(1,1), the first or the last or both.
  • Change BandedMatrix(0=>Zeros(∞))[1:1,1:1] to `BandedMatrix(0=>Zeros(∞)[1:1]).

putianyi889 avatar Feb 07 '21 21:02 putianyi889

I guess there’s two things going on here:

  1. the stack overflow
  2. That BandedMatrix(0=>Zeros(∞))[1:1,1:1] is left lazy instead of materialising

(2) is an easy fix: just overload sub_materialize .

dlfivefifty avatar Feb 07 '21 22:02 dlfivefifty

OK the stack overflow is caused by:

https://github.com/JuliaArrays/LazyArrays.jl/blob/56e264b89ea50156eb06a88b67588e8dad6657ed/src/lazyapplying.jl#L334

which lowers ApplyMatrix to Applied and

https://github.com/JuliaArrays/LazyArrays.jl/blob/56e264b89ea50156eb06a88b67588e8dad6657ed/src/lazyapplying.jl#L92

which lowers back to ApplyMatrix. I think we can catch the case where type doesn't change under materializeand go to the_base_copyto!` fallback.

dlfivefifty avatar Feb 09 '21 14:02 dlfivefifty