-
Notifications
You must be signed in to change notification settings - Fork 1
Sparse mapping refactor #63
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2dec436
Add `map` interface for carefully checking zeros
lkdvos b4b2328
disable previous implementation
lkdvos bd91790
Update indexstyle for eachstoredindex
lkdvos 0966414
`haszero`
lkdvos 95a636e
Formatting
lkdvos 9961f4c
More careful with preserving functions
lkdvos af5fd7a
Bump v0.6.0
lkdvos 0e163b4
Revert "Bump v0.6.0"
lkdvos 80f6805
Bump v0.5.12
lkdvos 4632408
Refactor `map` to allow specifying kind of preserving
lkdvos 1aa1964
Refactor derived map functions
lkdvos f6148a0
Avoid infinite loop
lkdvos d316b9f
Revert Revert Bump v0.6.0
lkdvos 8214b8f
Refactor eachstoredindex implementation
lkdvos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "SparseArraysBase" | ||
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208" | ||
authors = ["ITensor developers <[email protected]> and contributors"] | ||
version = "0.5.11" | ||
version = "0.6.0" | ||
|
||
[deps] | ||
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
# zero-preserving Traits | ||
# ---------------------- | ||
""" | ||
abstract type ZeroPreserving <: Function end | ||
|
||
Holy Trait to indicate how a function interacts with abstract zero values: | ||
|
||
- `StrongPreserving` : output is guaranteed to be zero if **any** input is. | ||
- `WeakPreserving` : output is guaranteed to be zero if **all** inputs are. | ||
- `NonPreserving` : no guarantees on output. | ||
|
||
To attempt to automatically determine this, either `ZeroPreserving(f, A::AbstractArray...)` or | ||
`ZeroPreserving(f, T::Type...)` can be used/overloaded. | ||
|
||
!!! warning | ||
incorrectly registering a function to be zero-preserving will lead to silently wrong results. | ||
""" | ||
abstract type ZeroPreserving <: Function end | ||
|
||
struct StrongPreserving{F} <: ZeroPreserving | ||
f::F | ||
end | ||
struct WeakPreserving{F} <: ZeroPreserving | ||
f::F | ||
end | ||
struct NonPreserving{F} <: ZeroPreserving | ||
f::F | ||
end | ||
|
||
# Backport: remove in 1.12 | ||
@static if !isdefined(Base, :haszero) | ||
_haszero(T::Type) = false | ||
_haszero(::Type{<:Number}) = true | ||
else | ||
_haszero = Base.haszero | ||
end | ||
|
||
# warning: cannot automatically detect WeakPreserving since this would mean checking all values | ||
function ZeroPreserving(f, A::AbstractArray, Bs::AbstractArray...) | ||
return ZeroPreserving(f, eltype(A), eltype.(Bs)...) | ||
end | ||
# TODO: the following might not properly specialize on the types | ||
# TODO: non-concrete element types | ||
function ZeroPreserving(f, T::Type, Ts::Type...) | ||
if all(_haszero, (T, Ts...)) | ||
return iszero(f(zero(T), zero.(Ts)...)) ? WeakPreserving(f) : NonPreserving(f) | ||
else | ||
return NonPreserving(f) | ||
end | ||
end | ||
|
||
const _WEAK_FUNCTIONS = (:+, :-) | ||
for f in _WEAK_FUNCTIONS | ||
@eval begin | ||
ZeroPreserving(::typeof($f), ::Type{<:Number}, ::Type{<:Number}...) = WeakPreserving($f) | ||
end | ||
end | ||
|
||
const _STRONG_FUNCTIONS = (:*,) | ||
for f in _STRONG_FUNCTIONS | ||
@eval begin | ||
ZeroPreserving(::typeof($f), ::Type{<:Number}, ::Type{<:Number}...) = StrongPreserving( | ||
$f | ||
) | ||
end | ||
end | ||
|
||
# map(!) | ||
# ------ | ||
@interface I::AbstractSparseArrayInterface function Base.map( | ||
f, A::AbstractArray, Bs::AbstractArray... | ||
) | ||
f_pres = ZeroPreserving(f, A, Bs...) | ||
return @interface I map(f_pres, A, Bs...) | ||
end | ||
@interface I::AbstractSparseArrayInterface function Base.map( | ||
f::ZeroPreserving, A::AbstractArray, Bs::AbstractArray... | ||
) | ||
T = Base.Broadcast.combine_eltypes(f.f, (A, Bs...)) | ||
C = similar(I, T, size(A)) | ||
return @interface I map!(f, C, A, Bs...) | ||
end | ||
|
||
@interface I::AbstractSparseArrayInterface function Base.map!( | ||
f, C::AbstractArray, A::AbstractArray, Bs::AbstractArray... | ||
) | ||
f_pres = ZeroPreserving(f, A, Bs...) | ||
return @interface I map!(f_pres, C, A, Bs...) | ||
end | ||
|
||
@interface ::AbstractSparseArrayInterface function Base.map!( | ||
f::ZeroPreserving, C::AbstractArray, A::AbstractArray, Bs::AbstractArray... | ||
) | ||
checkshape(C, A, Bs...) | ||
unaliased = map(Base.Fix1(Base.unalias, C), (A, Bs...)) | ||
|
||
if f isa StrongPreserving | ||
style = IndexStyle(C, unaliased...) | ||
inds = intersect(eachstoredindex.(Ref(style), unaliased)...) | ||
zero!(C) | ||
elseif f isa WeakPreserving | ||
style = IndexStyle(C, unaliased...) | ||
inds = union(eachstoredindex.(Ref(style), unaliased)...) | ||
zero!(C) | ||
elseif f isa NonPreserving | ||
inds = eachindex(C, unaliased...) | ||
else | ||
error(lazy"unknown zero-preserving type $(typeof(f))") | ||
end | ||
|
||
@inbounds for I in inds | ||
C[I] = f.f(ith_all(I, unaliased)...) | ||
end | ||
|
||
return C | ||
end | ||
|
||
# Derived functions | ||
# ----------------- | ||
@interface I::AbstractSparseArrayInterface Base.copyto!(C::AbstractArray, A::AbstractArray) = @interface I map!( | ||
identity, C, A | ||
) | ||
|
||
# Only map the stored values of the inputs. | ||
function map_stored! end | ||
|
||
@interface interface::AbstractArrayInterface function map_stored!( | ||
f, a_dest::AbstractArray, as::AbstractArray... | ||
) | ||
@interface interface map!(WeakPreserving(f), a_dest, as...) | ||
return a_dest | ||
end | ||
|
||
# Only map all values, not just the stored ones. | ||
function map_all! end | ||
|
||
@interface interface::AbstractArrayInterface function map_all!( | ||
f, a_dest::AbstractArray, as::AbstractArray... | ||
) | ||
@interface interface map!(NonPreserving(f), a_dest, as...) | ||
return a_dest | ||
end | ||
|
||
# Utility functions | ||
# ----------------- | ||
# shape check similar to checkbounds | ||
checkshape(::Type{Bool}, A::AbstractArray) = true | ||
checkshape(::Type{Bool}, A::AbstractArray, B::AbstractArray) = size(A) == size(B) | ||
function checkshape(::Type{Bool}, A::AbstractArray, Bs::AbstractArray...) | ||
return allequal(size, (A, Bs...)) | ||
end | ||
|
||
function checkshape(A::AbstractArray, Bs::AbstractArray...) | ||
return checkshape(Bool, A, Bs...) || | ||
throw(DimensionMismatch("argument shapes must match")) | ||
end | ||
|
||
@inline ith_all(i, ::Tuple{}) = () | ||
function ith_all(i, as) | ||
@_propagate_inbounds_meta | ||
return (as[1][i], ith_all(i, Base.tail(as))...) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.