Skip to content
19 changes: 6 additions & 13 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <support@itensor.org> and contributors"]
version = "0.10.15"
version = "0.10.16"

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

[compat]
Adapt = "4.1.1"
Aqua = "0.8.9"
ArrayLayouts = "1.10.4"
BlockArrays = "1.2"
DerivableInterfaces = "0.5.3"
DiagonalArrays = "0.3"
Dictionaries = "0.4.3"
FillArrays = "1.13"
FunctionImplementations = "0.3.1"
GPUArraysCore = "0.1, 0.2"
LinearAlgebra = "1.10"
MacroTools = "0.5.13"
MapBroadcast = "0.1.5"
MatrixAlgebraKit = "0.6"
SparseArraysBase = "0.7.1"
SparseArraysBase = "0.8.3"
SplitApplyCombine = "1.2.3"
TensorAlgebra = "0.6.2"
Test = "1.10"
TypeParameterAccessors = "0.4.1"
julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Test"]
[workspace]
projects = ["benchmark", "dev", "docs", "examples", "test"]
3 changes: 3 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ BlockSparseArrays = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"

[sources]
BlockSparseArrays = {path = ".."}

[compat]
BlockArrays = "1"
BlockSparseArrays = "0.10"
Expand Down
3 changes: 3 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
BlockSparseArrays = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[sources]
BlockSparseArrays = {path = ".."}

[compat]
BlockArrays = "1"
BlockSparseArrays = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion src/BlockArraysExtensions/BlockArraysExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Base.view(S::BlockIndices, i) = S[i]
# @view b[Block(1, 1)[1:2, 2:2]]
# ```
# This is similar to the definition:
# @interface interface(a) to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
# style(a)(to_indices)(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
function Base.getindex(
a::NonBlockedVector{<:Integer, <:BlockIndices}, I::UnitRange{<:Integer}
)
Expand Down
11 changes: 6 additions & 5 deletions src/abstractblocksparsearray/abstractblocksparsearray.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BlockArrays:
BlockArrays, AbstractBlockArray, Block, BlockIndex, BlockedUnitRange, blocks
using FunctionImplementations: style

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

Expand All @@ -19,12 +20,12 @@ end

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

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.getindex(a::AbstractBlockSparseArray{<:Any, 0})
return @interface interface(a) getindex(a)
return style(a)(getindex)(a)
end

## # Fix ambiguity error with `BlockArrays`.
Expand All @@ -39,21 +40,21 @@ end
##
## # Fix ambiguity error with `BlockArrays`.
## function Base.getindex(a::AbstractBlockSparseArray, I::Vararg{AbstractVector})
## ## return @interface interface(a) getindex(a, I...)
## ## return style(a)(getindex)(a, I...)
## return ArrayLayouts.layout_getindex(a, I...)
## end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.setindex!(
a::AbstractBlockSparseArray{<:Any, N}, value, I::Vararg{Int, N}
) where {N}
@interface interface(a) setindex!(a, value, I...)
style(a)(setindex!)(a, value, I...)
return a
end

# Fix ambiguity error.
function Base.setindex!(a::AbstractBlockSparseArray{<:Any, 0}, value)
@interface interface(a) setindex!(a, value)
style(a)(setindex!)(a, value)
return a
end

Expand Down
29 changes: 13 additions & 16 deletions src/abstractblocksparsearray/broadcast.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using BlockArrays: AbstractBlockedUnitRange, BlockSlice
using Base.Broadcast: Broadcast, BroadcastStyle
using Base.Broadcast: BroadcastStyle

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

# Fix ambiguity error with `BlockArrays`.
function Broadcast.BroadcastStyle(
function Base.Broadcast.BroadcastStyle(
arraytype::Type{
<:SubArray{
<:Any,
Expand All @@ -16,9 +16,9 @@ function Broadcast.BroadcastStyle(
},
},
)
return BlockSparseArrayStyle{ndims(arraytype)}()
return Broadcast.BlockSparseArrayStyle{ndims(arraytype)}()
end
function Broadcast.BroadcastStyle(
function Base.Broadcast.BroadcastStyle(
arraytype::Type{
<:SubArray{
<:Any,
Expand All @@ -32,9 +32,9 @@ function Broadcast.BroadcastStyle(
},
},
)
return BlockSparseArrayStyle{ndims(arraytype)}()
return Broadcast.BlockSparseArrayStyle{ndims(arraytype)}()
end
function Broadcast.BroadcastStyle(
function Base.Broadcast.BroadcastStyle(
arraytype::Type{
<:SubArray{
<:Any,
Expand All @@ -44,25 +44,22 @@ function Broadcast.BroadcastStyle(
},
},
)
return BlockSparseArrayStyle{ndims(arraytype)}()
return Broadcast.BlockSparseArrayStyle{ndims(arraytype)}()
end

# These catch cases that aren't caught by the standard
# `BlockSparseArrayStyle` definition, and also fix
# ambiguity issues.
function Base.copyto!(dest::AnyAbstractBlockSparseArray, bc::Broadcasted)
copyto_blocksparse!(dest, bc)
return dest
return copyto!_blocksparse(dest, bc)
end
function Base.copyto!(
dest::AnyAbstractBlockSparseArray, bc::Broadcasted{<:Base.Broadcast.AbstractArrayStyle{0}}
)
copyto_blocksparse!(dest, bc)
return dest
return copyto!_blocksparse(dest, bc)
end
function Base.copyto!(
dest::AnyAbstractBlockSparseArray{<:Any, N}, bc::Broadcasted{BlockSparseArrayStyle{N}}
dest::AnyAbstractBlockSparseArray{<:Any, N}, bc::Broadcasted{<:Broadcast.BlockSparseArrayStyle{N}}
) where {N}
copyto_blocksparse!(dest, bc)
return dest
return copyto!_blocksparse(dest, bc)
end
3 changes: 1 addition & 2 deletions src/abstractblocksparsearray/cat.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DerivableInterfaces: @interface, interface
using DerivableInterfaces.Concatenate: concatenate
using FunctionImplementations.Concatenate: concatenate

function Base._cat(dims, as::AnyAbstractBlockSparseArray...)
return concatenate(dims, as...)
Expand Down
39 changes: 26 additions & 13 deletions src/abstractblocksparsearray/map.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ArrayLayouts: LayoutArray
using BlockArrays: AbstractBlockVector, Block
using FunctionImplementations: style
using LinearAlgebra: Adjoint, Transpose

# TODO: Make this more general, independent of `AbstractBlockSparseArray`.
Expand Down Expand Up @@ -36,17 +37,17 @@ function reblock(
end

function Base.map!(f, a_dest::AbstractArray, a_srcs::AnyAbstractBlockSparseArray...)
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
style(a_dest, a_srcs...)(map!)(f, a_dest, a_srcs...)
return a_dest
end
function Base.map!(f, a_dest::AnyAbstractBlockSparseArray, a_srcs::AbstractArray...)
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
style(a_dest, a_srcs...)(map!)(f, a_dest, a_srcs...)
return a_dest
end
function Base.map!(
f, a_dest::AnyAbstractBlockSparseArray, a_srcs::AnyAbstractBlockSparseArray...
)
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
style(a_dest, a_srcs...)(map!)(f, a_dest, a_srcs...)
return a_dest
end

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

function Base.copy!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
return @interface interface(a_src) copy!(a_dest, a_src)
return style(a_src)(copy!)(a_dest, a_src)
end

function Base.copyto!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
return @interface interface(a_src) copyto!(a_dest, a_src)
return style(a_src)(copyto!)(a_dest, a_src)
end

# Fix ambiguity error
function Base.copyto!(a_dest::LayoutArray, a_src::AnyAbstractBlockSparseArray)
return @interface interface(a_src) copyto!(a_dest, a_src)
return style(a_src)(copyto!)(a_dest, a_src)
end

function Base.copyto!(
a_dest::AbstractMatrix, a_src::Transpose{T, <:AbstractBlockSparseMatrix{T}}
) where {T}
return @interface interface(a_src) copyto!(a_dest, a_src)
return style(a_src)(copyto!)(a_dest, a_src)
end

function Base.copyto!(
a_dest::AbstractMatrix, a_src::Adjoint{T, <:AbstractBlockSparseMatrix{T}}
) where {T}
return @interface interface(a_src) copyto!(a_dest, a_src)
return style(a_src)(copyto!)(a_dest, a_src)
end

const copyto!_blocksparse = blocksparse_style(copyto!)
function copyto!_blocksparse(dst::AbstractArray, src::AbstractArray)
# return sparse_style(copyto!)(dst, src)
return map!(identity, dst, src)
end

const copy!_blocksparse = blocksparse_style(copy!)
function copy!_blocksparse(dst::AbstractArray, src::AbstractArray)
# return sparse_style(copy!)(dst, src)
return copyto!(dst, src)
end

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

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

function Base.mapreduce(f, op, as::AnyAbstractBlockSparseArray...; kwargs...)
return @interface interface(as...) mapreduce(f, op, as...; kwargs...)
return style(as...)(mapreduce)(f, op, as...; kwargs...)
end

function Base.iszero(a::AnyAbstractBlockSparseArray)
return @interface interface(a) iszero(a)
return style(a)(iszero)(a)
end

function Base.isreal(a::AnyAbstractBlockSparseArray)
return @interface interface(a) isreal(a)
return style(a)(isreal)(a)
end

# Helps with specialization of block operations by avoiding
Expand Down
1 change: 0 additions & 1 deletion src/abstractblocksparsearray/sparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ end
## return BlockSparseStorage(a)
## end

# TODO: Turn this into an `@interface ::AbstractBlockSparseArrayInterface` function.
function SparseArraysBase.storedlength(a::AnyAbstractBlockSparseArray)
return sum(storedlength, storedvalues(blocks(a)); init = zero(Int))
end
19 changes: 15 additions & 4 deletions src/abstractblocksparsearray/unblockedsubarray.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ArrayLayouts: ArrayLayouts, MemoryLayout
using Base.Broadcast: Broadcast, BroadcastStyle
using Base.Broadcast: BroadcastStyle
using BlockArrays: BlockArrays, Block, BlockIndexRange, BlockSlice
using TypeParameterAccessors: TypeParameterAccessors, parenttype, similartype

Expand All @@ -18,18 +18,25 @@ function BlockArrays.blocks(a::UnblockedSubArray)
return SingleBlockView(a)
end

function DerivableInterfaces.interface(arraytype::Type{<:UnblockedSubArray})
return interface(blocktype(parenttype(arraytype)))
using FunctionImplementations: FunctionImplementations, Style
function FunctionImplementations.Style(arraytype::Type{<:UnblockedSubArray})
return Style(blocktype(parenttype(arraytype)))
end

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

function Broadcast.BroadcastStyle(arraytype::Type{<:UnblockedSubArray})
function Base.Broadcast.BroadcastStyle(arraytype::Type{<:UnblockedSubArray})
return BroadcastStyle(blocktype(parenttype(arraytype)))
end

function Base.similar(a::UnblockedSubArray)
return similar(a, eltype(a))
end
function Base.similar(a::UnblockedSubArray, elt::Type)
return similar(a, elt, axes(a))
end
function Base.similar(
a::UnblockedSubArray, elt::Type, axes::Tuple{Base.OneTo, Vararg{Base.OneTo}}
)
Expand All @@ -39,6 +46,10 @@ function Base.similar(a::UnblockedSubArray, elt::Type, size::Tuple{Int, Vararg{I
return similar(a, elt, Base.OneTo.(size))
end

function Base.copyto!(dst::AbstractArray, src::UnblockedSubArray)
return @invoke copyto!(dst::AbstractArray, src::AbstractArray)
end

function ArrayLayouts.sub_materialize(a::UnblockedSubArray)
a_cpu = adapt(Array, a)
a_cpu′ = similar(a_cpu)
Expand Down
Loading
Loading