Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackOverflowError: infinite call loop between LazyArrays and ArrayLayouts #55

Open
putianyi889 opened this issue Feb 7, 2021 · 2 comments

Comments

@putianyi889
Copy link
Contributor

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]).
@dlfivefifty
Copy link
Member

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants