Skip to content

Commit

Permalink
Merge branch 'main' into BlockSparseArrays_more_constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Nov 14, 2024
2 parents 8656a56 + 7faad33 commit cabfea7
Show file tree
Hide file tree
Showing 33 changed files with 281 additions and 272 deletions.
2 changes: 1 addition & 1 deletion NDTensors/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NDTensors"
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
authors = ["Matthew Fishman <[email protected]>"]
version = "0.3.65"
version = "0.3.67"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
8 changes: 4 additions & 4 deletions NDTensors/src/lib/BlockSparseArrays/examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# `BlockArrays` reinterprets the `SparseArray` as a blocked data structure.

using BlockArrays: BlockArrays, PseudoBlockVector, blockedrange
using NDTensors.BlockSparseArrays: BlockSparseArray, block_nstored
using NDTensors.BlockSparseArrays: BlockSparseArray, block_stored_length
using Test: @test, @test_broken

function main()
Expand Down Expand Up @@ -36,13 +36,13 @@ function main()
]
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)

@test block_nstored(b) == 2
@test block_stored_length(b) == 2

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

@test block_nstored(b) == 2
@test block_stored_length(b) == 2

## Access a block
@test b[Block(1, 1)] == d_blocks[1]
Expand All @@ -65,7 +65,7 @@ function main()

@test b + b Array(b) + Array(b)
@test b + b isa BlockSparseArray
@test block_nstored(b + b) == 2
@test block_stored_length(b + b) == 2

scaled_b = 2b
@test scaled_b 2Array(b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Test: @test, @testset
using BlockArrays:
AbstractBlockArray, Block, BlockedOneTo, blockedrange, blocklengths, blocksize
using NDTensors.BlockSparseArrays: BlockSparseArray, block_nstored
using NDTensors.BlockSparseArrays: BlockSparseArray, block_stored_length
using NDTensors.GradedAxes:
GradedAxes,
GradedOneTo,
Expand All @@ -13,7 +13,7 @@ using NDTensors.GradedAxes:
gradedrange,
isdual
using NDTensors.LabelledNumbers: label
using NDTensors.SparseArrayInterface: nstored
using NDTensors.SparseArrayInterface: stored_length
using NDTensors.SymmetrySectors: U1
using NDTensors.TensorAlgebra: fusedims, splitdims
using LinearAlgebra: adjoint
Expand All @@ -40,8 +40,8 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
@test size(b) == (4, 4, 4, 4)
@test blocksize(b) == (2, 2, 2, 2)
@test blocklengths.(axes(b)) == ([2, 2], [2, 2], [2, 2], [2, 2])
@test nstored(b) == 32
@test block_nstored(b) == 2
@test stored_length(b) == 32
@test block_stored_length(b) == 2
for i in 1:ndims(a)
@test axes(b, i) isa GradedOneTo
end
Expand All @@ -58,8 +58,8 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
@test size(b) == (4, 4, 4, 4)
@test blocksize(b) == (2, 2, 2, 2)
@test blocklengths.(axes(b)) == ([2, 2], [2, 2], [2, 2], [2, 2])
@test nstored(b) == 256
@test block_nstored(b) == 16
@test stored_length(b) == 256
@test block_stored_length(b) == 16
for i in 1:ndims(a)
@test axes(b, i) isa BlockedOneTo{Int}
end
Expand All @@ -71,8 +71,8 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
b = a[2:3, 2:3, 2:3, 2:3]
@test size(b) == (2, 2, 2, 2)
@test blocksize(b) == (2, 2, 2, 2)
@test nstored(b) == 2
@test block_nstored(b) == 2
@test stored_length(b) == 2
@test block_stored_length(b) == 2
for i in 1:ndims(a)
@test axes(b, i) isa GradedOneTo
end
Expand Down Expand Up @@ -156,7 +156,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
a[i] = randn(elt, size(a[i]))
end
b = 2 * a
@test block_nstored(b) == 2
@test block_stored_length(b) == 2
@test Array(b) == 2 * Array(a)
for i in 1:2
@test axes(b, i) isa GradedOneTo
Expand All @@ -177,7 +177,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
a[i] = randn(elt, size(a[i]))
end
b = 2 * a
@test block_nstored(b) == 2
@test block_stored_length(b) == 2
@test Array(b) == 2 * Array(a)
for i in 1:2
@test axes(b, i) isa GradedUnitRange
Expand All @@ -204,7 +204,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
a[i] = randn(elt, size(a[i]))
end
b = 2 * a
@test block_nstored(b) == 2
@test block_stored_length(b) == 2
@test Array(b) == 2 * Array(a)
for i in 1:2
@test axes(b, i) isa GradedUnitRangeDual
Expand All @@ -229,7 +229,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
a[i] = randn(elt, size(a[i]))
end
b = 2 * a
@test block_nstored(b) == 2
@test block_stored_length(b) == 2
@test Array(b) == 2 * Array(a)
for i in 1:2
@test axes(b, i) isa GradedUnitRangeDual
Expand All @@ -255,7 +255,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
a[i] = randn(elt, size(a[i]))
end
b = 2 * a
@test block_nstored(b) == 2
@test block_stored_length(b) == 2
@test Array(b) == 2 * Array(a)
@test a[:, :] isa BlockSparseArray
for i in 1:2
Expand All @@ -280,7 +280,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
a[i] = randn(elt, size(a[i]))
end
b = 2 * a'
@test block_nstored(b) == 2
@test block_stored_length(b) == 2
@test Array(b) == 2 * Array(a)'
for ax in axes(b)
@test ax isa typeof(dual(r))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using BlockArrays:
using Compat: allequal
using Dictionaries: Dictionary, Indices
using ..GradedAxes: blockedunitrange_getindices, to_blockindices
using ..SparseArrayInterface: SparseArrayInterface, nstored, stored_indices
using ..SparseArrayInterface: SparseArrayInterface, stored_length, stored_indices

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

function SparseArrayInterface.nstored(a::BlockView)
function SparseArrayInterface.stored_length(a::BlockView)
# TODO: Store whether or not the block is stored already as
# a Bool in `BlockView`.
I = CartesianIndex(Int.(a.block))
# TODO: Use `block_stored_indices`.
if I stored_indices(blocks(a.array))
return nstored(blocks(a.array)[I])
return stored_length(blocks(a.array)[I])
end
return 0
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using BlockArrays: AbstractBlockArray, BlocksView
using ..SparseArrayInterface: SparseArrayInterface, nstored
using ..SparseArrayInterface: SparseArrayInterface, stored_length

function SparseArrayInterface.nstored(a::AbstractBlockArray)
return sum(b -> nstored(b), blocks(a); init=zero(Int))
function SparseArrayInterface.stored_length(a::AbstractBlockArray)
return sum(b -> stored_length(b), blocks(a); init=zero(Int))
end

# TODO: Handle `BlocksView` wrapping a sparse array?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using BlockArrays:
using ..SparseArrayInterface: sparse_getindex, sparse_setindex!

# TODO: Delete this. This function was replaced
# by `nstored` but is still used in `NDTensors`.
# by `stored_length` but is still used in `NDTensors`.
function nonzero_keys end

abstract type AbstractBlockSparseArray{T,N} <: AbstractBlockArray{T,N} end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using BlockArrays: BlockLayout
using ..SparseArrayInterface: SparseLayout
using ..TypeParameterAccessors: parenttype, similartype

function ArrayLayouts.MemoryLayout(arraytype::Type{<:BlockSparseArrayLike})
function ArrayLayouts.MemoryLayout(arraytype::Type{<:AnyAbstractBlockSparseArray})
outer_layout = typeof(MemoryLayout(blockstype(arraytype)))
inner_layout = typeof(MemoryLayout(blocktype(arraytype)))
return BlockLayout{outer_layout,inner_layout}()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BlockArrays: AbstractBlockedUnitRange, BlockSlice
using Base.Broadcast: Broadcast

function Broadcast.BroadcastStyle(arraytype::Type{<:BlockSparseArrayLike})
function Broadcast.BroadcastStyle(arraytype::Type{<:AnyAbstractBlockSparseArray})
return BlockSparseArrayStyle{ndims(arraytype)}()
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TODO: Change to `AnyAbstractBlockSparseArray`.
function Base.cat(as::BlockSparseArrayLike...; dims)
function Base.cat(as::AnyAbstractBlockSparseArray...; dims)
# TODO: Use `sparse_cat` instead, currently
# that erroneously allocates too many blocks that are
# zero and shouldn't be stored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,27 @@ end
# function SparseArrayInterface.sparse_mapreduce(::BlockSparseArrayStyle, f, a_dest::AbstractArray, a_srcs::Vararg{AbstractArray})
# end

function Base.map!(f, a_dest::AbstractArray, a_srcs::Vararg{BlockSparseArrayLike})
function Base.map!(f, a_dest::AbstractArray, a_srcs::Vararg{AnyAbstractBlockSparseArray})
sparse_map!(f, a_dest, a_srcs...)
return a_dest
end

function Base.map(f, as::Vararg{BlockSparseArrayLike})
function Base.map(f, as::Vararg{AnyAbstractBlockSparseArray})
return f.(as...)
end

function Base.copy!(a_dest::AbstractArray, a_src::BlockSparseArrayLike)
function Base.copy!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
sparse_copy!(a_dest, a_src)
return a_dest
end

function Base.copyto!(a_dest::AbstractArray, a_src::BlockSparseArrayLike)
function Base.copyto!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
sparse_copyto!(a_dest, a_src)
return a_dest
end

# Fix ambiguity error
function Base.copyto!(a_dest::LayoutArray, a_src::BlockSparseArrayLike)
function Base.copyto!(a_dest::LayoutArray, a_src::AnyAbstractBlockSparseArray)
sparse_copyto!(a_dest, a_src)
return a_dest
end
Expand All @@ -131,21 +131,21 @@ function Base.copyto!(
return a_dest
end

function Base.permutedims!(a_dest, a_src::BlockSparseArrayLike, perm)
function Base.permutedims!(a_dest, a_src::AnyAbstractBlockSparseArray, perm)
sparse_permutedims!(a_dest, a_src, perm)
return a_dest
end

function Base.mapreduce(f, op, as::Vararg{BlockSparseArrayLike}; kwargs...)
function Base.mapreduce(f, op, as::Vararg{AnyAbstractBlockSparseArray}; kwargs...)
return sparse_mapreduce(f, op, as...; kwargs...)
end

# TODO: Why isn't this calling `mapreduce` already?
function Base.iszero(a::BlockSparseArrayLike)
function Base.iszero(a::AnyAbstractBlockSparseArray)
return sparse_iszero(blocks(a))
end

# TODO: Why isn't this calling `mapreduce` already?
function Base.isreal(a::BlockSparseArrayLike)
function Base.isreal(a::AnyAbstractBlockSparseArray)
return sparse_isreal(blocks(a))
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ function SparseArrayInterface.sparse_storage(a::AbstractBlockSparseArray)
return BlockSparseStorage(a)
end

function SparseArrayInterface.nstored(a::BlockSparseArrayLike)
return sum(nstored, sparse_storage(blocks(a)); init=zero(Int))
function SparseArrayInterface.stored_length(a::AnyAbstractBlockSparseArray)
return sum(stored_length, sparse_storage(blocks(a)); init=zero(Int))
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ end

# Override the default definition of `BlockArrays.blocksize`,
# which is incorrect for certain slices.
function BlockArrays.blocksize(a::SubArray{<:Any,<:Any,<:BlockSparseArrayLike})
function BlockArrays.blocksize(a::SubArray{<:Any,<:Any,<:AnyAbstractBlockSparseArray})
return blocklength.(axes(a))
end
function BlockArrays.blocksize(a::SubArray{<:Any,<:Any,<:BlockSparseArrayLike}, i::Int)
function BlockArrays.blocksize(
a::SubArray{<:Any,<:Any,<:AnyAbstractBlockSparseArray}, i::Int
)
# TODO: Maybe use `blocklength(axes(a, i))` which would be a bit faster.
return blocksize(a)[i]
end
Expand All @@ -33,22 +35,22 @@ end
# which don't handle subslices of blocks properly.
function Base.view(
a::SubArray{
<:Any,N,<:BlockSparseArrayLike,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
<:Any,N,<:AnyAbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
},
I::Block{N},
) where {N}
return blocksparse_view(a, I)
end
function Base.view(
a::SubArray{
<:Any,N,<:BlockSparseArrayLike,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
<:Any,N,<:AnyAbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
},
I::Vararg{Block{1},N},
) where {N}
return blocksparse_view(a, I...)
end
function Base.view(
V::SubArray{<:Any,1,<:BlockSparseArrayLike,<:Tuple{BlockSlice{<:BlockRange{1}}}},
V::SubArray{<:Any,1,<:AnyAbstractBlockSparseArray,<:Tuple{BlockSlice{<:BlockRange{1}}}},
I::Block{1},
)
return blocksparse_view(a, I)
Expand Down Expand Up @@ -154,7 +156,7 @@ function BlockArrays.viewblock(
return viewblock(a, to_tuple(block)...)
end

# Fixes ambiguity error with `BlockSparseArrayLike` definition.
# Fixes ambiguity error with `AnyAbstractBlockSparseArray` definition.
function Base.view(
a::SubArray{
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
Expand All @@ -163,7 +165,7 @@ function Base.view(
) where {T,N}
return viewblock(a, block)
end
# Fixes ambiguity error with `BlockSparseArrayLike` definition.
# Fixes ambiguity error with `AnyAbstractBlockSparseArray` definition.
function Base.view(
a::SubArray{
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
Expand Down
Loading

0 comments on commit cabfea7

Please sign in to comment.