Skip to content

Commit

Permalink
[SparseArraysBase] Rename SparseArrayInterface to SparseArraysBase (
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Nov 16, 2024
1 parent 4e6552d commit 9ef8ae2
Show file tree
Hide file tree
Showing 78 changed files with 248 additions and 251 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TODO: Define in `SparseArrayInterface`.
# TODO: Define in `SparseArraysBase`.
using ..SparseArrayDOKs: SparseArrayDOK

# TODO: This is inefficient, need to optimize.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using .SparseArrayInterface: densearray
using .SparseArraysBase: densearray
using .DiagonalArrays: DiagIndex, diaglength
using .TypeParameterAccessors: unwrap_array_type

Expand Down
2 changes: 1 addition & 1 deletion NDTensors/src/imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ for lib in [
:SymmetrySectors,
:TensorAlgebra,
:NestedPermutedDimsArrays,
:SparseArrayInterface,
:SparseArraysBase,
:SparseArrayDOKs,
:DiagonalArrays,
:BlockSparseArrays,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using NDTensors.GradedAxes:
gradedrange,
isdual
using NDTensors.LabelledNumbers: label
using NDTensors.SparseArrayInterface: stored_length
using NDTensors.SparseArraysBase: stored_length
using NDTensors.SymmetrySectors: U1
using NDTensors.TensorAlgebra: fusedims, splitdims
using LinearAlgebra: adjoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Test: @test, @testset
using NDTensors.BlockSparseArrays: BlockSparseArray
using NDTensors.TensorAlgebra: contract
using NDTensors.SparseArrayInterface: densearray
using NDTensors.SparseArraysBase: densearray
@testset "BlockSparseArraysTensorAlgebraExt (eltype=$elt)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
)
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, stored_length, stored_indices
using ..SparseArraysBase: SparseArraysBase, 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,7 +534,7 @@ function Base.setindex!(a::BlockView{<:Any,N}, value, index::Vararg{Int,N}) wher
return a
end

function SparseArrayInterface.stored_length(a::BlockView)
function SparseArraysBase.stored_length(a::BlockView)
# TODO: Store whether or not the block is stored already as
# a Bool in `BlockView`.
I = CartesianIndex(Int.(a.block))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using BlockArrays: AbstractBlockArray, BlocksView
using ..SparseArrayInterface: SparseArrayInterface, stored_length
using ..SparseArraysBase: SparseArraysBase, stored_length

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

# TODO: Handle `BlocksView` wrapping a sparse array?
function SparseArrayInterface.storage_indices(a::BlocksView)
function SparseArraysBase.storage_indices(a::BlocksView)
return CartesianIndices(a)
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ include("abstractblocksparsearray/linearalgebra.jl")
include("abstractblocksparsearray/cat.jl")
include("blocksparsearray/defaults.jl")
include("blocksparsearray/blocksparsearray.jl")
include("BlockArraysSparseArrayInterfaceExt/BlockArraysSparseArrayInterfaceExt.jl")
include("BlockArraysSparseArraysBaseExt/BlockArraysSparseArraysBaseExt.jl")
include("../ext/BlockSparseArraysTensorAlgebraExt/src/BlockSparseArraysTensorAlgebraExt.jl")
include("../ext/BlockSparseArraysGradedAxesExt/src/BlockSparseArraysGradedAxesExt.jl")
include("../ext/BlockSparseArraysAdaptExt/src/BlockSparseArraysAdaptExt.jl")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BlockArrays:
BlockArrays, AbstractBlockArray, Block, BlockIndex, BlockedUnitRange, blocks
using ..SparseArrayInterface: sparse_getindex, sparse_setindex!
using ..SparseArraysBase: sparse_getindex, sparse_setindex!

# TODO: Delete this. This function was replaced
# by `stored_length` but is still used in `NDTensors`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ArrayLayouts: ArrayLayouts, DualLayout, MemoryLayout, MulAdd
using BlockArrays: BlockLayout
using ..SparseArrayInterface: SparseLayout
using ..SparseArraysBase: SparseLayout
using ..TypeParameterAccessors: parenttype, similartype

function ArrayLayouts.MemoryLayout(arraytype::Type{<:AnyAbstractBlockSparseArray})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ArrayLayouts: LayoutArray
using BlockArrays: blockisequal
using LinearAlgebra: Adjoint, Transpose
using ..SparseArrayInterface:
SparseArrayInterface,
using ..SparseArraysBase:
SparseArraysBase,
SparseArrayStyle,
sparse_map!,
sparse_copy!,
Expand Down Expand Up @@ -62,7 +62,7 @@ end
# is used to determine `union_stored_blocked_cartesianindices(...)`).
# `reblock` is a partial solution to that, but a bit ad-hoc.
# TODO: Move to `blocksparsearrayinterface/map.jl`.
function SparseArrayInterface.sparse_map!(
function SparseArraysBase.sparse_map!(
::BlockSparseArrayStyle, f, a_dest::AbstractArray, a_srcs::Vararg{AbstractArray}
)
a_dest, a_srcs = reblock(a_dest), reblock.(a_srcs)
Expand All @@ -89,7 +89,7 @@ function SparseArrayInterface.sparse_map!(
end

# TODO: Implement this.
# function SparseArrayInterface.sparse_mapreduce(::BlockSparseArrayStyle, f, a_dest::AbstractArray, a_srcs::Vararg{AbstractArray})
# function SparseArraysBase.sparse_mapreduce(::BlockSparseArrayStyle, f, a_dest::AbstractArray, a_srcs::Vararg{AbstractArray})
# end

function Base.map!(f, a_dest::AbstractArray, a_srcs::Vararg{AnyAbstractBlockSparseArray})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BlockArrays: Block
using ..SparseArrayInterface: SparseArrayInterface, sparse_storage, stored_indices
using ..SparseArraysBase: SparseArraysBase, sparse_storage, stored_indices

# Structure storing the block sparse storage
struct BlockSparseStorage{Arr<:AbstractBlockSparseArray}
Expand Down Expand Up @@ -29,10 +29,10 @@ function Base.iterate(s::BlockSparseStorage, args...)
return iterate(values(s), args...)
end

function SparseArrayInterface.sparse_storage(a::AbstractBlockSparseArray)
function SparseArraysBase.sparse_storage(a::AbstractBlockSparseArray)
return BlockSparseStorage(a)
end

function SparseArrayInterface.stored_length(a::AnyAbstractBlockSparseArray)
function SparseArraysBase.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
@@ -1,6 +1,6 @@
using ArrayLayouts: ArrayLayouts, Dot, MatMulMatAdd, MatMulVecAdd, MulAdd
using BlockArrays: BlockLayout
using ..SparseArrayInterface: SparseLayout
using ..SparseArraysBase: SparseLayout
using LinearAlgebra: dot, mul!

function blocksparse_muladd!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using BlockArrays:
blocks,
findblockindex
using LinearAlgebra: Adjoint, Transpose
using ..SparseArrayInterface: perm, iperm, stored_length, sparse_zero!
using ..SparseArraysBase: perm, iperm, stored_length, sparse_zero!

blocksparse_blocks(a::AbstractArray) = error("Not implemented")

Expand Down Expand Up @@ -142,8 +142,7 @@ end

# BlockArrays

using ..SparseArrayInterface:
SparseArrayInterface, AbstractSparseArray, AbstractSparseMatrix
using ..SparseArraysBase: SparseArraysBase, AbstractSparseArray, AbstractSparseMatrix

_perm(::PermutedDimsArray{<:Any,<:Any,perm}) where {perm} = perm
_invperm(::PermutedDimsArray{<:Any,<:Any,<:Any,invperm}) where {invperm} = invperm
Expand All @@ -170,16 +169,16 @@ function Base.getindex(
blocks(parent(a.array))[_getindices(index, _invperm(a.array))...], _perm(a.array)
)
end
function SparseArrayInterface.stored_indices(a::SparsePermutedDimsArrayBlocks)
function SparseArraysBase.stored_indices(a::SparsePermutedDimsArrayBlocks)
return map(I -> _getindices(I, _perm(a.array)), stored_indices(blocks(parent(a.array))))
end
# TODO: Either make this the generic interface or define
# `SparseArrayInterface.sparse_storage`, which is used
# `SparseArraysBase.sparse_storage`, which is used
# to defined this.
function SparseArrayInterface.stored_length(a::SparsePermutedDimsArrayBlocks)
function SparseArraysBase.stored_length(a::SparsePermutedDimsArrayBlocks)
return length(stored_indices(a))
end
function SparseArrayInterface.sparse_storage(a::SparsePermutedDimsArrayBlocks)
function SparseArraysBase.sparse_storage(a::SparsePermutedDimsArrayBlocks)
return error("Not implemented")
end

Expand Down Expand Up @@ -241,22 +240,22 @@ function Base.isassigned(a::SparseSubArrayBlocks{<:Any,N}, I::Vararg{Int,N}) whe
# TODO: Implement this properly.
return true
end
function SparseArrayInterface.stored_indices(a::SparseSubArrayBlocks)
function SparseArraysBase.stored_indices(a::SparseSubArrayBlocks)
return stored_indices(view(blocks(parent(a.array)), blockrange(a)...))
end
# TODO: Either make this the generic interface or define
# `SparseArrayInterface.sparse_storage`, which is used
# `SparseArraysBase.sparse_storage`, which is used
# to defined this.
SparseArrayInterface.stored_length(a::SparseSubArrayBlocks) = length(stored_indices(a))
SparseArraysBase.stored_length(a::SparseSubArrayBlocks) = length(stored_indices(a))

## struct SparseSubArrayBlocksStorage{Array<:SparseSubArrayBlocks}
## array::Array
## end
function SparseArrayInterface.sparse_storage(a::SparseSubArrayBlocks)
function SparseArraysBase.sparse_storage(a::SparseSubArrayBlocks)
return map(I -> a[I], stored_indices(a))
end

function SparseArrayInterface.getindex_zero_function(a::SparseSubArrayBlocks)
function SparseArraysBase.getindex_zero_function(a::SparseSubArrayBlocks)
# TODO: Base it off of `getindex_zero_function(blocks(parent(a.array))`, but replace the
# axes with `axes(a.array)`.
return BlockZero(axes(a.array))
Expand All @@ -272,4 +271,4 @@ function blocksparse_blocks(
end

using BlockArrays: BlocksView
SparseArrayInterface.stored_length(a::BlocksView) = length(a)
SparseArraysBase.stored_length(a::BlocksView) = length(a)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using BlockArrays: AbstractBlockedUnitRange, blockedrange, blocklengths
using NDTensors.SparseArrayInterface: SparseArrayInterface, allocate_cat_output, sparse_cat!
using NDTensors.SparseArraysBase: SparseArraysBase, allocate_cat_output, sparse_cat!

# TODO: Maybe move to `SparseArrayInterfaceBlockArraysExt`.
# TODO: Handle dual graded unit ranges, for example in a new `SparseArrayInterfaceGradedAxesExt`.
function SparseArrayInterface.axis_cat(
# TODO: Maybe move to `SparseArraysBaseBlockArraysExt`.
# TODO: Handle dual graded unit ranges, for example in a new `SparseArraysBaseGradedAxesExt`.
function SparseArraysBase.axis_cat(
a1::AbstractBlockedUnitRange, a2::AbstractBlockedUnitRange
)
return blockedrange(vcat(blocklengths(a1), blocklengths(a2)))
Expand Down
2 changes: 1 addition & 1 deletion NDTensors/src/lib/BlockSparseArrays/test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using NDTensors.BlockSparseArrays:
blocktype,
view!
using NDTensors.GPUArraysCoreExtensions: cpu
using NDTensors.SparseArrayInterface: stored_length
using NDTensors.SparseArraysBase: stored_length
using NDTensors.SparseArrayDOKs: SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK
using NDTensors.TensorAlgebra: contract
using Test: @test, @test_broken, @test_throws, @testset, @inferred
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using ..SparseArrayInterface: AbstractSparseArray
using ..SparseArraysBase: AbstractSparseArray

abstract type AbstractDiagonalArray{T,N} <: AbstractSparseArray{T,N} end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ArrayLayouts: ArrayLayouts
using ..SparseArrayInterface: AbstractSparseLayout
using ..SparseArraysBase: AbstractSparseLayout

abstract type AbstractDiagonalLayout <: AbstractSparseLayout end
struct DiagonalLayout <: AbstractDiagonalLayout end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ..SparseArrayInterface: SparseArrayInterface, StorageIndex, StorageIndices
using ..SparseArraysBase: SparseArraysBase, StorageIndex, StorageIndices

SparseArrayInterface.StorageIndex(i::DiagIndex) = StorageIndex(index(i))
SparseArraysBase.StorageIndex(i::DiagIndex) = StorageIndex(index(i))

function Base.getindex(a::AbstractDiagonalArray, i::DiagIndex)
return a[StorageIndex(i)]
Expand All @@ -11,7 +11,7 @@ function Base.setindex!(a::AbstractDiagonalArray, value, i::DiagIndex)
return a
end

SparseArrayInterface.StorageIndices(i::DiagIndices) = StorageIndices(indices(i))
SparseArraysBase.StorageIndices(i::DiagIndices) = StorageIndices(indices(i))

function Base.getindex(a::AbstractDiagonalArray, i::DiagIndices)
return a[StorageIndices(i)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Compat: Returns, allequal
using ..SparseArrayInterface: SparseArrayInterface
using ..SparseArraysBase: SparseArraysBase

# `SparseArrayInterface` interface
function SparseArrayInterface.index_to_storage_index(
# `SparseArraysBase` interface
function SparseArraysBase.index_to_storage_index(
a::AbstractDiagonalArray{<:Any,N}, I::CartesianIndex{N}
) where {N}
!allequal(Tuple(I)) && return nothing
return first(Tuple(I))
end

function SparseArrayInterface.storage_index_to_index(a::AbstractDiagonalArray, I)
function SparseArraysBase.storage_index_to_index(a::AbstractDiagonalArray, I)
return CartesianIndex(ntuple(Returns(I), ndims(a)))
end

## # 1-dimensional case can be `AbstractDiagonalArray`.
## function SparseArrayInterface.sparse_similar(
## function SparseArraysBase.sparse_similar(
## a::AbstractDiagonalArray, elt::Type, dims::Tuple{Int}
## )
## # TODO: Handle preserving zero element function.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ..SparseArrayInterface: Zero, getindex_zero_function
using ..SparseArraysBase: Zero, getindex_zero_function
# TODO: Put into `DiagonalArraysSparseArrayDOKsExt`?
using ..SparseArrayDOKs: SparseArrayDOKs, SparseArrayDOK

Expand Down Expand Up @@ -88,20 +88,20 @@ function Base.similar(a::DiagonalArray, elt::Type, dims::Tuple{Vararg{Int}})
return DiagonalArray{elt}(undef, dims, getindex_zero_function(a))
end

# Minimal `SparseArrayInterface` interface
SparseArrayInterface.sparse_storage(a::DiagonalArray) = a.diag
# Minimal `SparseArraysBase` interface
SparseArraysBase.sparse_storage(a::DiagonalArray) = a.diag

# `SparseArrayInterface`
# `SparseArraysBase`
# Defines similar when the output can't be `DiagonalArray`,
# such as in `reshape`.
# TODO: Put into `DiagonalArraysSparseArrayDOKsExt`?
# TODO: Special case 2D to output `SparseMatrixCSC`?
function SparseArrayInterface.sparse_similar(
function SparseArraysBase.sparse_similar(
a::DiagonalArray, elt::Type, dims::Tuple{Vararg{Int}}
)
return SparseArrayDOK{elt}(undef, dims, getindex_zero_function(a))
end

function SparseArrayInterface.getindex_zero_function(a::DiagonalArray)
function SparseArraysBase.getindex_zero_function(a::DiagonalArray)
return a.zero
end
2 changes: 1 addition & 1 deletion NDTensors/src/lib/DiagonalArrays/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Test: @test, @testset, @test_broken
using NDTensors.DiagonalArrays: DiagonalArrays, DiagonalArray, DiagonalMatrix, diaglength
using NDTensors.SparseArrayDOKs: SparseArrayDOK
using NDTensors.SparseArrayInterface: stored_length
using NDTensors.SparseArraysBase: stored_length
@testset "Test NDTensors.DiagonalArrays" begin
@testset "README" begin
@test include(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module NamedDimsArraysSparseArraysBaseExt
include("densearray.jl")
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ..NamedDimsArrays: AbstractNamedDimsArray, dimnames, named, unname
using ...SparseArrayInterface: SparseArrayInterface, densearray
using ...SparseArraysBase: SparseArraysBase, densearray

# TODO: Use `Adapt` or some kind of rewrap function like in
# ArrayInterface.jl (https://github.com/JuliaArrays/ArrayInterface.jl/issues/136)
function SparseArrayInterface.densearray(na::AbstractNamedDimsArray)
function SparseArraysBase.densearray(na::AbstractNamedDimsArray)
return named(densearray(unname(na)), dimnames(na))
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
@eval module $(gensym())
using LinearAlgebra: Diagonal
using Test: @test, @testset
using NDTensors.SparseArrayInterface: densearray
using NDTensors.SparseArraysBase: densearray
using NDTensors.NamedDimsArrays: named, unname
@testset "NamedDimsArraysSparseArrayInterfaceExt (eltype=$elt)" for elt in
(Float32, Float64)
@testset "NamedDimsArraysSparseArraysBaseExt (eltype=$elt)" for elt in (Float32, Float64)
na = named(Diagonal(randn(2)), ("i", "j"))
na_dense = densearray(na)
@test na na_dense
Expand Down
2 changes: 1 addition & 1 deletion NDTensors/src/lib/NamedDimsArrays/src/NamedDimsArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ include("broadcast.jl")
# Extensions
include("../ext/NamedDimsArraysAdaptExt/src/NamedDimsArraysAdaptExt.jl")
include(
"../ext/NamedDimsArraysSparseArrayInterfaceExt/src/NamedDimsArraysSparseArrayInterfaceExt.jl",
"../ext/NamedDimsArraysSparseArraysBaseExt/src/NamedDimsArraysSparseArraysBaseExt.jl"
)
include("../ext/NamedDimsArraysTensorAlgebraExt/src/NamedDimsArraysTensorAlgebraExt.jl")
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Test: @testset

@testset "NamedDimsArrays $(@__FILE__)" begin
include("../ext/NamedDimsArraysSparseArrayInterfaceExt/test/runtests.jl")
include("../ext/NamedDimsArraysSparseArraysBaseExt/test/runtests.jl")
end
Loading

0 comments on commit 9ef8ae2

Please sign in to comment.