-
Notifications
You must be signed in to change notification settings - Fork 31
Fix tests, implement copyto! for #383 #385
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -334,6 +334,8 @@ MemoryLayout(C::Type{CachedArray{T,N,DAT,ARR}}) where {T,N,DAT,ARR} = cachedlayo | |
| ###### | ||
|
|
||
| struct CachedArrayStyle{N} <: AbstractLazyArrayStyle{N} end | ||
| CachedArrayStyle(::Val{N}) where N = CachedArrayStyle{N}() | ||
| CachedArrayStyle{M}(::Val{N}) where {N,M} = CachedArrayStyle{N}() | ||
|
|
||
| BroadcastStyle(::Type{<:AbstractCachedArray{<:Any,N}}) where N = CachedArrayStyle{N}() | ||
| BroadcastStyle(::Type{<:SubArray{<:Any,N,<:AbstractCachedArray{<:Any,M}}}) where {N,M} = CachedArrayStyle{M}() | ||
|
|
@@ -381,7 +383,36 @@ for op in (:*, :\, :+, :-) | |
| @eval layout_broadcasted(::ZerosLayout, ::CachedLayout, ::typeof($op), a::AbstractVector, b::AbstractVector) = broadcast(DefaultArrayStyle{1}(), $op, a, b) | ||
| end | ||
|
|
||
| function resize_bcargs(bc::Broadcasted{<:CachedArrayStyle}, dest) | ||
| rsz_args = let len = length(dest) | ||
| map(bc.args) do arg | ||
| resizedata!(arg, len) | ||
| iscached = arg isa AbstractCachedArray || (arg isa SubArray && parent(arg) isa AbstractCachedArray) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be all based on memory layouts not types. |
||
| iscached ? cacheddata(arg) : arg | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems very unlikely to be type stable. I think we want to rewrite this whole map in a functional programming style, something like: _bc_resizecacheddata!(n) = ()
_bc_resizecacheddata!(n, a, b...) = __bc_resizecacheddata!(n, MemoryLayout(a), a, b...)
__bc_resizecacheddata!(n, _, a, b...) = (a, _bc_resizecacheddata!(b...))
function __bc_resizecacheddata!(n, ::AbstractCachedLayout, a::AbstractVector, b...)
resizedata!(a, n)
(view(cacheddata(a), 1:n), _bc_resizecacheddata!(b...))
end
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that since |
||
| end | ||
| end | ||
| return broadcasted(bc.f, rsz_args...) | ||
| end | ||
|
|
||
| function similar(bc::Broadcasted{<:CachedArrayStyle}, ::Type{T}) where T | ||
| return CachedArray(zeros(T, axes(bc))) | ||
| end | ||
|
|
||
| function copyto!(dest::AbstractArray, bc::Broadcasted{<:CachedArrayStyle}) | ||
| #= | ||
| Without flatten, we were observing some stack overflows in some cases for nested broadcasts, e.g. | ||
| using SemiclassicalOrthogonalPolynomials, ClassicalOrthogonalPolynomials | ||
| Q = Normalized(Legendre()) | ||
| P = SemiclassicalOrthogonalPolynomials.RaisedOP(Q, 1) | ||
| A, = ClassicalOrthogonalPolynomials.recurrencecoefficients(Q) | ||
| d = -inv(A[1] * SemiclassicalOrthogonalPolynomials._p0(Q) * P.ℓ[1]) | ||
| κ = d * SemiclassicalOrthogonalPolynomials.normalizationconstant(1, P) | ||
| κ[1:2] | ||
| leads to a stack overflow. | ||
| =# | ||
| rsz_bc = resize_bcargs(Base.Broadcast.flatten(bc), dest) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced flatten is preferable to a recursive call. I.e. something like _bc_resizedata!(n, a::Broadcasted, b...) = (broadcasted(a.f, _bc_resizedata!(n, a.args...)...), _bc_resizedata!(n, b...))But I think it is fine to flatten for now and come back later if there's an issue. |
||
| copyto!(dest, rsz_bc) | ||
| end | ||
|
|
||
| ### | ||
| # norm | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.