Skip to content

Commit 56676c9

Browse files
authored
Switch over to FunctionImplementations.jl (#195)
1 parent a52e335 commit 56676c9

26 files changed

Lines changed: 274 additions & 281 deletions

Project.toml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.10.15"
4+
version = "0.10.16"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
88
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
99
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
10-
DerivableInterfaces = "6c5e35bf-e59e-4898-b73c-732dcc4ba65f"
1110
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
1211
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
1312
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
13+
FunctionImplementations = "7c7cc465-9c6a-495f-bdd1-f42428e86d0c"
1414
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
1515
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1616
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
@@ -28,29 +28,22 @@ BlockSparseArraysTensorAlgebraExt = "TensorAlgebra"
2828

2929
[compat]
3030
Adapt = "4.1.1"
31-
Aqua = "0.8.9"
3231
ArrayLayouts = "1.10.4"
3332
BlockArrays = "1.2"
34-
DerivableInterfaces = "0.5.3"
3533
DiagonalArrays = "0.3"
3634
Dictionaries = "0.4.3"
3735
FillArrays = "1.13"
36+
FunctionImplementations = "0.3.1"
3837
GPUArraysCore = "0.1, 0.2"
3938
LinearAlgebra = "1.10"
4039
MacroTools = "0.5.13"
4140
MapBroadcast = "0.1.5"
4241
MatrixAlgebraKit = "0.6"
43-
SparseArraysBase = "0.7.1"
42+
SparseArraysBase = "0.8.3"
4443
SplitApplyCombine = "1.2.3"
4544
TensorAlgebra = "0.6.2"
46-
Test = "1.10"
4745
TypeParameterAccessors = "0.4.1"
4846
julia = "1.10"
4947

50-
[extras]
51-
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
52-
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
53-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
54-
55-
[targets]
56-
test = ["Aqua", "Test"]
48+
[workspace]
49+
projects = ["benchmark", "dev", "docs", "examples", "test"]

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ BlockSparseArrays = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
44
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
55
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
66

7+
[sources]
8+
BlockSparseArrays = {path = ".."}
9+
710
[compat]
811
BlockArrays = "1"
912
BlockSparseArrays = "0.10"

examples/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
33
BlockSparseArrays = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
44
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
55

6+
[sources]
7+
BlockSparseArrays = {path = ".."}
8+
69
[compat]
710
BlockArrays = "1"
811
BlockSparseArrays = "0.10"

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Base.view(S::BlockIndices, i) = S[i]
172172
# @view b[Block(1, 1)[1:2, 2:2]]
173173
# ```
174174
# This is similar to the definition:
175-
# @interface interface(a) to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
175+
# style(a)(to_indices)(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
176176
function Base.getindex(
177177
a::NonBlockedVector{<:Integer, <:BlockIndices}, I::UnitRange{<:Integer}
178178
)

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BlockArrays:
22
BlockArrays, AbstractBlockArray, Block, BlockIndex, BlockedUnitRange, blocks
3+
using FunctionImplementations: style
34

45
abstract type AbstractBlockSparseArray{T, N} <: AbstractBlockArray{T, N} end
56

@@ -19,12 +20,12 @@ end
1920

2021
# Specialized in order to fix ambiguity error with `BlockArrays`.
2122
function Base.getindex(a::AbstractBlockSparseArray{<:Any, N}, I::Vararg{Int, N}) where {N}
22-
return @interface interface(a) getindex(a, I...)
23+
return style(a)(getindex)(a, I...)
2324
end
2425

2526
# Specialized in order to fix ambiguity error with `BlockArrays`.
2627
function Base.getindex(a::AbstractBlockSparseArray{<:Any, 0})
27-
return @interface interface(a) getindex(a)
28+
return style(a)(getindex)(a)
2829
end
2930

3031
## # Fix ambiguity error with `BlockArrays`.
@@ -39,21 +40,21 @@ end
3940
##
4041
## # Fix ambiguity error with `BlockArrays`.
4142
## function Base.getindex(a::AbstractBlockSparseArray, I::Vararg{AbstractVector})
42-
## ## return @interface interface(a) getindex(a, I...)
43+
## ## return style(a)(getindex)(a, I...)
4344
## return ArrayLayouts.layout_getindex(a, I...)
4445
## end
4546

4647
# Specialized in order to fix ambiguity error with `BlockArrays`.
4748
function Base.setindex!(
4849
a::AbstractBlockSparseArray{<:Any, N}, value, I::Vararg{Int, N}
4950
) where {N}
50-
@interface interface(a) setindex!(a, value, I...)
51+
style(a)(setindex!)(a, value, I...)
5152
return a
5253
end
5354

5455
# Fix ambiguity error.
5556
function Base.setindex!(a::AbstractBlockSparseArray{<:Any, 0}, value)
56-
@interface interface(a) setindex!(a, value)
57+
style(a)(setindex!)(a, value)
5758
return a
5859
end
5960

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using BlockArrays: AbstractBlockedUnitRange, BlockSlice
2-
using Base.Broadcast: Broadcast, BroadcastStyle
2+
using Base.Broadcast: BroadcastStyle
33

4-
function Broadcast.BroadcastStyle(arraytype::Type{<:AnyAbstractBlockSparseArray})
5-
return BlockSparseArrayStyle(BroadcastStyle(blocktype(arraytype)))
4+
function Base.Broadcast.BroadcastStyle(arraytype::Type{<:AnyAbstractBlockSparseArray})
5+
return Broadcast.BlockSparseArrayStyle(BroadcastStyle(blocktype(arraytype)))
66
end
77

88
# Fix ambiguity error with `BlockArrays`.
9-
function Broadcast.BroadcastStyle(
9+
function Base.Broadcast.BroadcastStyle(
1010
arraytype::Type{
1111
<:SubArray{
1212
<:Any,
@@ -16,9 +16,9 @@ function Broadcast.BroadcastStyle(
1616
},
1717
},
1818
)
19-
return BlockSparseArrayStyle{ndims(arraytype)}()
19+
return Broadcast.BlockSparseArrayStyle{ndims(arraytype)}()
2020
end
21-
function Broadcast.BroadcastStyle(
21+
function Base.Broadcast.BroadcastStyle(
2222
arraytype::Type{
2323
<:SubArray{
2424
<:Any,
@@ -32,9 +32,9 @@ function Broadcast.BroadcastStyle(
3232
},
3333
},
3434
)
35-
return BlockSparseArrayStyle{ndims(arraytype)}()
35+
return Broadcast.BlockSparseArrayStyle{ndims(arraytype)}()
3636
end
37-
function Broadcast.BroadcastStyle(
37+
function Base.Broadcast.BroadcastStyle(
3838
arraytype::Type{
3939
<:SubArray{
4040
<:Any,
@@ -44,25 +44,22 @@ function Broadcast.BroadcastStyle(
4444
},
4545
},
4646
)
47-
return BlockSparseArrayStyle{ndims(arraytype)}()
47+
return Broadcast.BlockSparseArrayStyle{ndims(arraytype)}()
4848
end
4949

5050
# These catch cases that aren't caught by the standard
5151
# `BlockSparseArrayStyle` definition, and also fix
5252
# ambiguity issues.
5353
function Base.copyto!(dest::AnyAbstractBlockSparseArray, bc::Broadcasted)
54-
copyto_blocksparse!(dest, bc)
55-
return dest
54+
return copyto!_blocksparse(dest, bc)
5655
end
5756
function Base.copyto!(
5857
dest::AnyAbstractBlockSparseArray, bc::Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}}
5958
)
60-
copyto_blocksparse!(dest, bc)
61-
return dest
59+
return copyto!_blocksparse(dest, bc)
6260
end
6361
function Base.copyto!(
64-
dest::AnyAbstractBlockSparseArray{<:Any, N}, bc::Broadcasted{BlockSparseArrayStyle{N}}
62+
dest::AnyAbstractBlockSparseArray{<:Any, N}, bc::Broadcasted{<:Broadcast.BlockSparseArrayStyle{N}}
6563
) where {N}
66-
copyto_blocksparse!(dest, bc)
67-
return dest
64+
return copyto!_blocksparse(dest, bc)
6865
end

src/abstractblocksparsearray/cat.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using DerivableInterfaces: @interface, interface
2-
using DerivableInterfaces.Concatenate: concatenate
1+
using FunctionImplementations.Concatenate: concatenate
32

43
function Base._cat(dims, as::AnyAbstractBlockSparseArray...)
54
return concatenate(dims, as...)

src/abstractblocksparsearray/map.jl

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ArrayLayouts: LayoutArray
22
using BlockArrays: AbstractBlockVector, Block
3+
using FunctionImplementations: style
34
using LinearAlgebra: Adjoint, Transpose
45

56
# TODO: Make this more general, independent of `AbstractBlockSparseArray`.
@@ -36,17 +37,17 @@ function reblock(
3637
end
3738

3839
function Base.map!(f, a_dest::AbstractArray, a_srcs::AnyAbstractBlockSparseArray...)
39-
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
40+
style(a_dest, a_srcs...)(map!)(f, a_dest, a_srcs...)
4041
return a_dest
4142
end
4243
function Base.map!(f, a_dest::AnyAbstractBlockSparseArray, a_srcs::AbstractArray...)
43-
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
44+
style(a_dest, a_srcs...)(map!)(f, a_dest, a_srcs...)
4445
return a_dest
4546
end
4647
function Base.map!(
4748
f, a_dest::AnyAbstractBlockSparseArray, a_srcs::AnyAbstractBlockSparseArray...
4849
)
49-
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
50+
style(a_dest, a_srcs...)(map!)(f, a_dest, a_srcs...)
5051
return a_dest
5152
end
5253

@@ -55,28 +56,40 @@ function Base.map(f, as::Vararg{AnyAbstractBlockSparseArray})
5556
end
5657

5758
function Base.copy!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
58-
return @interface interface(a_src) copy!(a_dest, a_src)
59+
return style(a_src)(copy!)(a_dest, a_src)
5960
end
6061

6162
function Base.copyto!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
62-
return @interface interface(a_src) copyto!(a_dest, a_src)
63+
return style(a_src)(copyto!)(a_dest, a_src)
6364
end
6465

6566
# Fix ambiguity error
6667
function Base.copyto!(a_dest::LayoutArray, a_src::AnyAbstractBlockSparseArray)
67-
return @interface interface(a_src) copyto!(a_dest, a_src)
68+
return style(a_src)(copyto!)(a_dest, a_src)
6869
end
6970

7071
function Base.copyto!(
7172
a_dest::AbstractMatrix, a_src::Transpose{T, <:AbstractBlockSparseMatrix{T}}
7273
) where {T}
73-
return @interface interface(a_src) copyto!(a_dest, a_src)
74+
return style(a_src)(copyto!)(a_dest, a_src)
7475
end
7576

7677
function Base.copyto!(
7778
a_dest::AbstractMatrix, a_src::Adjoint{T, <:AbstractBlockSparseMatrix{T}}
7879
) where {T}
79-
return @interface interface(a_src) copyto!(a_dest, a_src)
80+
return style(a_src)(copyto!)(a_dest, a_src)
81+
end
82+
83+
const copyto!_blocksparse = blocksparse_style(copyto!)
84+
function copyto!_blocksparse(dst::AbstractArray, src::AbstractArray)
85+
# return sparse_style(copyto!)(dst, src)
86+
return map!(identity, dst, src)
87+
end
88+
89+
const copy!_blocksparse = blocksparse_style(copy!)
90+
function copy!_blocksparse(dst::AbstractArray, src::AbstractArray)
91+
# return sparse_style(copy!)(dst, src)
92+
return copyto!(dst, src)
8093
end
8194

8295
# This avoids going through the generic version that calls `Base.permutedims!`,
@@ -85,7 +98,7 @@ end
8598
# `PermutedDimsArray`).
8699
# TODO: Handle slicing better in `map!` so that this can be removed.
87100
function Base.permutedims(a::AnyAbstractBlockSparseArray, perm)
88-
return @interface interface(a) permutedims(a, perm)
101+
return style(a)(permutedims)(a, perm)
89102
end
90103

91104
# The `::AbstractBlockSparseArrayInterface` version
@@ -96,19 +109,19 @@ end
96109
# ```
97110
# TODO: Handle slicing better in `map!` so that this can be removed.
98111
function Base.permutedims!(a_dest, a_src::AnyAbstractBlockSparseArray, perm)
99-
return @interface interface(a_src) permutedims!(a_dest, a_src, perm)
112+
return style(a_src)(permutedims!)(a_dest, a_src, perm)
100113
end
101114

102115
function Base.mapreduce(f, op, as::AnyAbstractBlockSparseArray...; kwargs...)
103-
return @interface interface(as...) mapreduce(f, op, as...; kwargs...)
116+
return style(as...)(mapreduce)(f, op, as...; kwargs...)
104117
end
105118

106119
function Base.iszero(a::AnyAbstractBlockSparseArray)
107-
return @interface interface(a) iszero(a)
120+
return style(a)(iszero)(a)
108121
end
109122

110123
function Base.isreal(a::AnyAbstractBlockSparseArray)
111-
return @interface interface(a) isreal(a)
124+
return style(a)(isreal)(a)
112125
end
113126

114127
# Helps with specialization of block operations by avoiding

src/abstractblocksparsearray/sparsearrayinterface.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ end
3636
## return BlockSparseStorage(a)
3737
## end
3838

39-
# TODO: Turn this into an `@interface ::AbstractBlockSparseArrayInterface` function.
4039
function SparseArraysBase.storedlength(a::AnyAbstractBlockSparseArray)
4140
return sum(storedlength, storedvalues(blocks(a)); init = zero(Int))
4241
end

src/abstractblocksparsearray/unblockedsubarray.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ArrayLayouts: ArrayLayouts, MemoryLayout
2-
using Base.Broadcast: Broadcast, BroadcastStyle
2+
using Base.Broadcast: BroadcastStyle
33
using BlockArrays: BlockArrays, Block, BlockIndexRange, BlockSlice
44
using TypeParameterAccessors: TypeParameterAccessors, parenttype, similartype
55

@@ -18,18 +18,25 @@ function BlockArrays.blocks(a::UnblockedSubArray)
1818
return SingleBlockView(a)
1919
end
2020

21-
function DerivableInterfaces.interface(arraytype::Type{<:UnblockedSubArray})
22-
return interface(blocktype(parenttype(arraytype)))
21+
using FunctionImplementations: FunctionImplementations, Style
22+
function FunctionImplementations.Style(arraytype::Type{<:UnblockedSubArray})
23+
return Style(blocktype(parenttype(arraytype)))
2324
end
2425

2526
function ArrayLayouts.MemoryLayout(arraytype::Type{<:UnblockedSubArray})
2627
return MemoryLayout(blocktype(parenttype(arraytype)))
2728
end
2829

29-
function Broadcast.BroadcastStyle(arraytype::Type{<:UnblockedSubArray})
30+
function Base.Broadcast.BroadcastStyle(arraytype::Type{<:UnblockedSubArray})
3031
return BroadcastStyle(blocktype(parenttype(arraytype)))
3132
end
3233

34+
function Base.similar(a::UnblockedSubArray)
35+
return similar(a, eltype(a))
36+
end
37+
function Base.similar(a::UnblockedSubArray, elt::Type)
38+
return similar(a, elt, axes(a))
39+
end
3340
function Base.similar(
3441
a::UnblockedSubArray, elt::Type, axes::Tuple{Base.OneTo, Vararg{Base.OneTo}}
3542
)
@@ -39,6 +46,10 @@ function Base.similar(a::UnblockedSubArray, elt::Type, size::Tuple{Int, Vararg{I
3946
return similar(a, elt, Base.OneTo.(size))
4047
end
4148

49+
function Base.copyto!(dst::AbstractArray, src::UnblockedSubArray)
50+
return @invoke copyto!(dst::AbstractArray, src::AbstractArray)
51+
end
52+
4253
function ArrayLayouts.sub_materialize(a::UnblockedSubArray)
4354
a_cpu = adapt(Array, a)
4455
a_cpu′ = similar(a_cpu)

0 commit comments

Comments
 (0)