Skip to content

Commit 6905338

Browse files
committed
Put Accumulate filldata! behind a function barrier; Fix stackoverflow/BoundsError issue with cacheddata storing a Vcat+scalar
1 parent f0e6ca6 commit 6905338

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/lazyapplying.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ function show(io::IO, A::Applied)
329329
print(io, ')')
330330
end
331331

332+
# BroadcastStyle(::Type{<:LinearAlgebra.QRCompactWYQ}) = DefaultArrayStyle{2}()
333+
# BroadcastStyle(::Type{<:LinearAlgebra.AdjointQ}) = DefaultArrayStyle{2}()
334+
332335
applybroadcaststyle(::Type{<:AbstractArray{<:Any,N}}, _2) where N = DefaultArrayStyle{N}()
333336
applybroadcaststyle(::Type{<:AbstractArray{<:Any,N}}, ::AbstractLazyLayout) where N = LazyArrayStyle{N}()
334337
applybroadcaststyle(::Type{<:ApplyArray{<:Any,N,<:Any,Args}}, ::AbstractLazyLayout) where {N,Args<:Tuple} = result_style(LazyArrayStyle{N}(), tuple_type_broadcastlayout(Args))

src/lazyconcat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function vcat_copyto!(arr::AbstractVector, arrays...)
256256
i = firstindex(arr)
257257
for a in arrays
258258
m = length(a)
259-
copyto!(view(arr, range(i, length=m)), a)
259+
m > 0 && copyto!(view(arr, range(i, length=m)), a)
260260
i += m
261261
end
262262
arr

src/lazyoperations.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,25 +438,35 @@ copy(Q::Accumulate) = Accumulate(Q.op, copy(Q.data), copy(Q.v), Q.dims, Q.datasi
438438
copyto!(x::AbstractArray{<:Any,N}, C::Cumsum{<:Any,N}) where N = cumsum!(x, C.v)
439439

440440
# How we populate the data
441-
function cache_filldata!(K::Accumulate{<:Any,1}, inds)
442-
copyto!(view(K.data,inds), view(K.v,inds))
441+
function _accumulate_cache_filldata_1d!(data, v, op::F, inds) where {F}
442+
copyto!(view(data,inds), view(v,inds))
443443
@inbounds for k in inds
444-
K.data[k] = K.op(K.data[k-1], K.data[k])
444+
data[k] = op(data[k-1], v[k])
445445
end
446446
end
447447

448-
function cache_filldata!(K::Accumulate{<:Any,2}, kr, jr)
449-
if K.dims == 1
448+
function cache_filldata!(K::Accumulate{<:Any,1}, inds)
449+
_accumulate_cache_filldata_1d!(K.data, K.v, K.op, inds)
450+
end
451+
452+
function _accumulate_cache_filldata_2d!(data, v, op::F, kr, jr, dims) where {F}
453+
if dims == 1
454+
copyto!(view(data, kr, jr), view(v, kr, jr))
450455
@inbounds for j in jr, k in kr
451-
K.data[k,j] = K.op(K.data[k-1,j], K.v[k,j])
456+
data[k,j] = op(data[k-1,j], v[k,j])
452457
end
453-
else # K.dims == 2
458+
else # dims == 2
459+
copyto!(view(data, kr, jr), view(v, kr, jr))
454460
@inbounds for j in jr, k in kr
455-
K.data[k,j] = K.op(K.data[k,j-1], K.v[k,j])
461+
data[k,j] = op(data[k,j-1], v[k,j])
456462
end
457463
end
458464
end
459465

466+
function cache_filldata!(K::Accumulate{<:Any,2}, kr, jr)
467+
_accumulate_cache_filldata_2d!(K.data, K.v, K.op, kr, jr, K.dims)
468+
end
469+
460470

461471
# keep lazy
462472
cumsum(a::LazyArray; kwds...) = Cumsum(a; kwds...)

test/concattests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,15 @@ import Base.Broadcast: BroadcastStyle
729729
@test BroadcastStyle(typeof(Vcat())) == LazyArrayStyle{1}()
730730
@test BroadcastStyle(typeof(Hcat())) == LazyArrayStyle{2}()
731731
end
732+
733+
@testset "Previously broken case with cacheddata storing a Vcat" begin
734+
v = Accumulate(*, Vcat(cache(1:3), 5));
735+
resizedata!(v, 4)
736+
@test v.datasize == (4,)
737+
@test v == [1, 2, 6, 30]
738+
@test v[4] == 30
739+
@test Base.isassigned(v, 4)
740+
end
732741
end
733742

734743
end # module

0 commit comments

Comments
 (0)