Skip to content

Commit 25ae137

Browse files
committed
[WIP] Rewrite for latest Derive.jl and SparseArraysBase.jl
1 parent 63bb180 commit 25ae137

File tree

20 files changed

+159
-105
lines changed

20 files changed

+159
-105
lines changed

Project.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
88
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
99
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
1010
BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2"
11+
Derive = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a"
1112
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
1213
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
1314
GradedUnitRanges = "e2de450a-8a67-46c7-b59c-01d5a3d041c5"
@@ -21,19 +22,17 @@ TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
2122
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
2223

2324
[sources]
24-
BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"}
2525
GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"}
2626
LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"}
2727
NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"}
28-
SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"}
2928
TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"}
30-
TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"}
3129

3230
[compat]
3331
Adapt = "4.1.1"
3432
Aqua = "0.8.9"
3533
ArrayLayouts = "1.10.4"
3634
BlockArrays = "1.2.0"
35+
Derive = "0.3.0"
3736
Dictionaries = "0.4.3"
3837
GPUArraysCore = "0.1.0"
3938
LinearAlgebra = "1.10"

TODO.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
- Rename `block_storedlength` to `blockstoredlength`.
2+
- Rename `BlockZero` to `getunstoredblock`.
3+
- Use `Derive.@array_aliases`.
4+
- Use `@derive AbstractArrayInterface` to define `abstractblocksparsearray/map.jl` functions.
5+
6+
17
- Add Aqua tests.
28
- Turn the package extensions into actual package extensions:
39
- BlockSparseArraysAdaptExt

docs/Project.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
1212
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
1313

1414
[sources]
15-
BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"}
1615
GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"}
1716
LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"}
1817
NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"}
19-
SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"}
2018
TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"}
21-
TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"}

examples/Project.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1313
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
1414

1515
[sources]
16-
BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"}
1716
GradedUnitRanges = {url = "https://github.com/ITensor/GradedUnitRanges.jl"}
1817
LabelledNumbers = {url = "https://github.com/ITensor/LabelledNumbers.jl"}
1918
NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"}
20-
SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"}
2119
TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"}
22-
TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"}

examples/README.jl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@
1515
```julia
1616
julia> using Pkg: Pkg
1717
18-
julia> Pkg.add(url="https://github.com/ITensor/BroadcastMapConversion.jl")
19-
2018
julia> Pkg.add(url="https://github.com/ITensor/NestedPermutedDimsArrays.jl")
2119
22-
julia> Pkg.add(url="https://github.com/ITensor/TypeParameterAccessors.jl")
23-
2420
julia> Pkg.add(url="https://github.com/ITensor/LabelledNumbers.jl")
2521
2622
julia> Pkg.add(url="https://github.com/ITensor/GradedUnitRanges.jl")
2723
28-
julia> Pkg.add(url="https://github.com/ITensor/SparseArraysBase.jl")
29-
3024
julia> Pkg.add(url="https://github.com/ITensor/TensorAlgebra.jl")
3125
3226
julia> Pkg.add(url="https://github.com/ITensor/BlockSparseArrays.jl")
@@ -36,7 +30,7 @@ julia> Pkg.add(url="https://github.com/ITensor/BlockSparseArrays.jl")
3630
# ## Examples
3731

3832
using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
39-
using BlockSparseArrays: BlockSparseArray, block_stored_length
33+
using BlockSparseArrays: BlockSparseArray, block_storedlength
4034
using Test: @test, @test_broken
4135

4236
function main()
@@ -63,13 +57,13 @@ function main()
6357
]
6458
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
6559

66-
@test block_stored_length(b) == 2
60+
@test block_storedlength(b) == 2
6761

6862
## Blocks with discontiguous underlying data
6963
d_blocks = randn.(nz_block_sizes)
7064
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
7165

72-
@test block_stored_length(b) == 2
66+
@test block_storedlength(b) == 2
7367

7468
## Access a block
7569
@test b[Block(1, 1)] == d_blocks[1]
@@ -93,7 +87,7 @@ function main()
9387
@test b + b Array(b) + Array(b)
9488
@test b + b isa BlockSparseArray
9589
## TODO: Fix this, broken.
96-
@test_broken block_stored_length(b + b) == 2
90+
@test_broken block_storedlength(b + b) == 2
9791

9892
scaled_b = 2b
9993
@test scaled_b 2Array(b)

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using BlockArrays:
2121
findblockindex
2222
using Dictionaries: Dictionary, Indices
2323
using GradedUnitRanges: blockedunitrange_getindices, to_blockindices
24-
using SparseArraysBase: SparseArraysBase, stored_length, stored_indices
24+
using SparseArraysBase: SparseArraysBase, storedlength, eachstoredindex
2525

2626
# A return type for `blocks(array)` when `array` isn't blocked.
2727
# Represents a vector with just that single block.
@@ -533,13 +533,13 @@ function Base.setindex!(a::BlockView{<:Any,N}, value, index::Vararg{Int,N}) wher
533533
return a
534534
end
535535

536-
function SparseArraysBase.stored_length(a::BlockView)
536+
function SparseArraysBase.storedlength(a::BlockView)
537537
# TODO: Store whether or not the block is stored already as
538538
# a Bool in `BlockView`.
539539
I = CartesianIndex(Int.(a.block))
540540
# TODO: Use `block_stored_indices`.
541-
if I stored_indices(blocks(a.array))
542-
return stored_length(blocks(a.array)[I])
541+
if I eachstoredindex(blocks(a.array))
542+
return storedlength(blocks(a.array)[I])
543543
end
544544
return 0
545545
end
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using BlockArrays: AbstractBlockArray, BlocksView
2-
using SparseArraysBase: SparseArraysBase, stored_length
2+
using SparseArraysBase: SparseArraysBase, storedlength
33

4-
function SparseArraysBase.stored_length(a::AbstractBlockArray)
5-
return sum(b -> stored_length(b), blocks(a); init=zero(Int))
4+
function SparseArraysBase.storedlength(a::AbstractBlockArray)
5+
return sum(b -> storedlength(b), blocks(a); init=zero(Int))
66
end
77

88
# TODO: Handle `BlocksView` wrapping a sparse array?
9-
function SparseArraysBase.storage_indices(a::BlocksView)
9+
function SparseArraysBase.eachstoredindex(a::BlocksView)
1010
return CartesianIndices(a)
1111
end

src/BlockSparseArrays.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module BlockSparseArrays
22
include("BlockArraysExtensions/BlockArraysExtensions.jl")
3+
include("blocksparsearrayinterface/abstractblocksparsearrayinterface.jl")
34
include("blocksparsearrayinterface/blocksparsearrayinterface.jl")
45
include("blocksparsearrayinterface/linearalgebra.jl")
56
include("blocksparsearrayinterface/blockzero.jl")
@@ -16,7 +17,7 @@ include("abstractblocksparsearray/views.jl")
1617
include("abstractblocksparsearray/arraylayouts.jl")
1718
include("abstractblocksparsearray/sparsearrayinterface.jl")
1819
include("abstractblocksparsearray/broadcast.jl")
19-
include("abstractblocksparsearray/map.jl")
20+
## include("abstractblocksparsearray/map.jl")
2021
include("abstractblocksparsearray/linearalgebra.jl")
2122
include("abstractblocksparsearray/cat.jl")
2223
include("blocksparsearray/defaults.jl")

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ function nonzero_keys end
88

99
abstract type AbstractBlockSparseArray{T,N} <: AbstractBlockArray{T,N} end
1010

11+
using Derive: @array_aliases
12+
# Define AbstractSparseVector, AnyAbstractSparseArray, etc.
13+
@array_aliases AbstractBlockSparseArray
14+
15+
using Derive: Derive
16+
function Derive.interface(::Type{<:AbstractBlockSparseArray})
17+
return BlockSparseArrayInterface()
18+
end
19+
20+
using Derive: @derive
21+
22+
# TODO: These need to be loaded since `AbstractArrayOps`
23+
# includes overloads of functions from these modules.
24+
# Ideally that wouldn't be needed and can be circumvented
25+
# with `GlobalRef`.
26+
using ArrayLayouts: ArrayLayouts
27+
using LinearAlgebra: LinearAlgebra
28+
29+
# Derive `Base.getindex`, `Base.setindex!`, etc.
30+
# TODO: Define `AbstractMatrixOps` and overload for
31+
# `AnyAbstractSparseMatrix` and `AnyAbstractSparseVector`,
32+
# which is where matrix multiplication and factorizations
33+
# shoudl go.
34+
@derive AnyAbstractBlockSparseArray AbstractArrayOps
35+
1136
## Base `AbstractArray` interface
1237

1338
Base.axes(::AbstractBlockSparseArray) = error("Not implemented")

src/abstractblocksparsearray/arraylayouts.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ using BlockArrays: BlockLayout
33
using SparseArraysBase: SparseLayout
44
using TypeParameterAccessors: parenttype, similartype
55

6-
function ArrayLayouts.MemoryLayout(arraytype::Type{<:AnyAbstractBlockSparseArray})
7-
outer_layout = typeof(MemoryLayout(blockstype(arraytype)))
8-
inner_layout = typeof(MemoryLayout(blocktype(arraytype)))
9-
return BlockLayout{outer_layout,inner_layout}()
10-
end
6+
## TODO: Bring this back.
7+
## function ArrayLayouts.MemoryLayout(arraytype::Type{<:AnyAbstractBlockSparseArray})
8+
## outer_layout = typeof(MemoryLayout(blockstype(arraytype)))
9+
## inner_layout = typeof(MemoryLayout(blocktype(arraytype)))
10+
## return BlockLayout{outer_layout,inner_layout}()
11+
## end
1112

1213
# TODO: Generalize to `BlockSparseVectorLike`/`AnyBlockSparseVector`.
1314
function ArrayLayouts.MemoryLayout(

0 commit comments

Comments
 (0)