Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BlockSparseArrays] Use new SparseArrayDOK type in BlockSparseArrays #1272

Merged
merged 34 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6401807
[NDTensors] Start SparseArrayDOKs module
mtfishman Nov 25, 2023
9e4142c
Reorganization
mtfishman Nov 26, 2023
0677dfa
Format
mtfishman Nov 26, 2023
995c3f9
Start making SparseArrayInterface module, to be used by SparseArrayDO…
mtfishman Nov 28, 2023
b696a7e
Update interface
mtfishman Nov 28, 2023
e984cbe
Add another test
mtfishman Nov 28, 2023
475ee31
Improved map
mtfishman Nov 28, 2023
36961f8
New SparseArrayDOKs using SparseArrayInterface
mtfishman Nov 28, 2023
25f8229
Fix namespace issues
mtfishman Nov 29, 2023
3b3c50d
One more namespace issue
mtfishman Nov 29, 2023
a20e819
More namespace issues
mtfishman Nov 29, 2023
d571bd9
Julia 1.6 backwards compatibility
mtfishman Nov 29, 2023
410df32
Use SparseArrayInterface in DiagonalArrays
mtfishman Nov 29, 2023
41c9816
Format
mtfishman Nov 29, 2023
420692a
Fix loading issue
mtfishman Nov 29, 2023
c6dcefe
Missing include, improve README
mtfishman Nov 29, 2023
2478166
[BlockSparseArrays] Start using SparseArrayDOK
mtfishman Nov 29, 2023
43929be
Small fixes
mtfishman Nov 30, 2023
f70a520
Merge branch 'main' into NDTensors_new_BlockSparseArrays
mtfishman Nov 30, 2023
41b92b8
Change SparseArray to SparseArrayDOK
mtfishman Nov 30, 2023
af5ff02
Format
mtfishman Nov 30, 2023
a1733f6
Temporarily remove broken tests
mtfishman Nov 30, 2023
1be1d00
Introduct AbstractSparseArray, start rewriting BlockSparseArray
mtfishman Dec 1, 2023
97f3df4
Move AbstractSparseArray to SparseArrayInterface
mtfishman Dec 1, 2023
32d375a
Improve testing and organization
mtfishman Dec 1, 2023
0ea3eee
DiagonalArrays reorganization and simplification
mtfishman Dec 1, 2023
f918aee
Get more BlockSparseArrays tests passing
mtfishman Dec 1, 2023
fc0ff14
Move arraytensor code to backup files
mtfishman Dec 1, 2023
8487d0c
Move arraystorage code to backup files
mtfishman Dec 1, 2023
fc9ff82
Try fixing tests
mtfishman Dec 1, 2023
b5b643d
Comment
mtfishman Dec 1, 2023
796f33d
Merge main
mtfishman Dec 1, 2023
4d1453d
Fix namespace issue
mtfishman Dec 1, 2023
1f04d9c
Remove arraytensor test
mtfishman Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve testing and organization
  • Loading branch information
mtfishman committed Dec 1, 2023
commit 32d375af44ac8ed20e330740f1e7b0b9793a8d36
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ include("sparsearrayinterface/broadcast.jl")
include("sparsearrayinterface/conversion.jl")
include("sparsearrayinterface/wrappers.jl")
include("sparsearrayinterface/zero.jl")
include("sparsearrayinterface/SparseArrayInterfaceLinearAlgebraExt.jl")
include("abstractsparsearray/abstractsparsearray.jl")
include("abstractsparsearray/sparsearrayinterface.jl")
include("abstractsparsearray/base.jl")
include("abstractsparsearray/broadcast.jl")
include("abstractsparsearray/map.jl")
include("abstractsparsearray/baseinterface.jl")
include("abstractsparsearray/convert.jl")
include("abstractsparsearray/SparseArrayDOKsSparseArraysExt.jl")
include("abstractsparsearray/SparseArrayInterfaceSparseArraysExt.jl")
include("abstractsparsearray/SparseArrayInterfaceLinearAlgebraExt.jl")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using LinearAlgebra: LinearAlgebra

LinearAlgebra.norm(a::AbstractSparseArray, p::Real=2) = sparse_norm(a, p)
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,13 @@ using ..SparseArrayInterface: SparseArrayInterface

SparseArrayInterface.sparse_storage(::AbstractSparseArray) = error("Not implemented")

# TODO: Move to `SparseArrayInterface`?
getindex_zero_function(::AbstractSparseArray) = error("Not implemented")

function SparseArrayInterface.index_to_storage_index(
a::AbstractSparseArray{<:Any,N}, I::CartesianIndex{N}
) where {N}
!isassigned(SparseArrayInterface.sparse_storage(a), I) && return nothing
return I
end

function SparseArrayInterface.getindex_notstored(
a::AbstractSparseArray{<:Any,N}, I::CartesianIndex{N}
) where {N}
return getindex_zero_function(a)(a, I)
end

## # TODO: Generalize with `is_wrapped_array` trait.
## function SparseArrayInterface.getindex_notstored(
## a::PermutedDimsArray{<:Any,N,<:AbstractSparseArray}, I::CartesianIndex{N}
## ) where {N}
## # TODO: Need to permute `I`?
## return getindex_zero_function(a)(a, I)
## end

function SparseArrayInterface.setindex_notstored!(
a::AbstractSparseArray{<:Any,N}, value, I::CartesianIndex{N}
) where {N}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using LinearAlgebra: norm

sparse_norm(a::AbstractArray, p::Real=2) = norm(sparse_storage(a))
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Optional interface.
# Access a zero value.
getindex_zero_function(::AbstractArray) = Zero()

function getindex_notstored(a::AbstractArray, I)
return zero(eltype(a))
return getindex_zero_function(a)(a, I)
end

# Optional interface.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module AbstractSparseArrays
using NDTensors.SparseArrayInterface: SparseArrayInterface, AbstractSparseArray

struct SparseArray{T,N} <: AbstractSparseArray{T,N}
data::Vector{T}
dims::Tuple{Vararg{Int,N}}
index_to_dataindex::Dict{CartesianIndex{N},Int}
dataindex_to_index::Vector{CartesianIndex{N}}
end
function SparseArray{T,N}(dims::Tuple{Vararg{Int,N}}) where {T,N}
return SparseArray{T,N}(
T[], dims, Dict{CartesianIndex{N},Int}(), Vector{CartesianIndex{N}}()
)
end
SparseArray{T,N}(dims::Vararg{Int,N}) where {T,N} = SparseArray{T,N}(dims)
SparseArray{T}(dims::Tuple{Vararg{Int}}) where {T} = SparseArray{T,length(dims)}(dims)
SparseArray{T}(dims::Vararg{Int}) where {T} = SparseArray{T}(dims)

# AbstractArray interface
Base.size(a::SparseArray) = a.dims
function Base.similar(a::SparseArray, elt::Type, dims::Tuple{Vararg{Int}})
return SparseArray{elt}(dims)
end

# Minimal interface
SparseArrayInterface.sparse_storage(a::SparseArray) = a.data
function SparseArrayInterface.index_to_storage_index(
a::SparseArray{<:Any,N}, I::CartesianIndex{N}
) where {N}
return get(a.index_to_dataindex, I, nothing)
end
SparseArrayInterface.storage_index_to_index(a::SparseArray, I) = a.dataindex_to_index[I]
function SparseArrayInterface.setindex_notstored!(
a::SparseArray{<:Any,N}, value, I::CartesianIndex{N}
) where {N}
push!(a.data, value)
push!(a.dataindex_to_index, I)
a.index_to_dataindex[I] = length(a.data)
return a
end

# Empty the storage, helps with efficiency in `map!` to drop
# zeros.
function SparseArrayInterface.dropall!(a::SparseArray)
empty!(a.data)
empty!(a.index_to_dataindex)
empty!(a.dataindex_to_index)
return a
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module SparseArrayInterfaceTestUtils
include("SparseArrays.jl")
include("AbstractSparseArrays.jl")
include("DiagonalArrays.jl")
include("SparseArrays.jl")
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ SparseArray{T}(dims::Vararg{Int}) where {T} = SparseArray{T}(dims)

# AbstractArray interface
Base.size(a::SparseArray) = a.dims
function Base.similar(a::SparseArray, elt::Type, dims::Tuple{Vararg{Int}})
return SparseArray{elt}(dims)
end

function Base.getindex(a::SparseArray, I...)
return SparseArrayInterface.sparse_getindex(a, I...)
end
function Base.setindex!(a::SparseArray, I...)
return SparseArrayInterface.sparse_setindex!(a, I...)
end
function Base.similar(a::SparseArray, elt::Type, dims::Tuple{Vararg{Int}})
return SparseArray{elt}(dims)
end

# Minimal interface
SparseArrayInterface.sparse_storage(a::SparseArray) = a.data
Expand Down
6 changes: 3 additions & 3 deletions NDTensors/src/lib/SparseArrayInterface/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@eval module $(gensym())
include("test_array.jl")
include("test_diagonalarray.jl")
include("test_sparsearray.jl")
for filename in ["abstractsparsearray", "array", "diagonalarray"]
include("test_$filename.jl")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
using LinearAlgebra: norm
using NDTensors.SparseArrayInterface: SparseArrayInterface
include("SparseArrayInterfaceTestUtils/SparseArrayInterfaceTestUtils.jl")
using .SparseArrayInterfaceTestUtils.SparseArrays: SparseArray
using .SparseArrayInterfaceTestUtils.AbstractSparseArrays: AbstractSparseArrays
using .SparseArrayInterfaceTestUtils.SparseArrays: SparseArrays
using Test: @test, @testset
@testset "SparseArray (eltype=$elt)" for elt in (Float32, ComplexF32, Float64, ComplexF64)
@testset "AbstractSparseArray (arraytype=$SparseArray, eltype=$elt)" for SparseArray in (
AbstractSparseArrays.SparseArray, SparseArrays.SparseArray
),
elt in (Float32, ComplexF32, Float64, ComplexF64)

a = SparseArray{elt}(2, 3)
@test size(a) == (2, 3)
@test axes(a) == (1:2, 1:3)
Expand Down