Skip to content

Commit 4183ddd

Browse files
authored
Abstractsparse (#25)
Implement AbstractSparseMatrixCSC interface. Remove extension of LinearSolve methods LinearSolve since 1.37 can handle this.
1 parent ff970f4 commit 4183ddd

File tree

8 files changed

+76
-45
lines changed

8 files changed

+76
-45
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ExtendableSparse"
22
uuid = "95c220a8-a1cf-11e9-0c77-dbfce5f500b3"
33
authors = ["Juergen Fuhrmann <[email protected]>"]
4-
version = "0.9.6"
4+
version = "0.9.7"
55

66
[deps]
77
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"

docs/src/changes.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## v0.9.7, Jan 22, 2023
4+
5+
- support of AbstractSparseMatrixCSC interface
6+
37
## v0.9.6, Jan 22, 2023
48

59
- support for LinearSolve.jl

docs/src/linearsolve.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
Starting with version 0.9.6, ExtendableSparse is compatible
44
with [LinearSolve.jl](https://github.com/SciML/LinearSolve.jl).
5-
For this purpose, it extends the `LinearProblem` constructor and the
6-
`set_A` function by methods specific for `ExtendableSparseMatrix`.
5+
Since version 0.9.7, this is facilitated via the
6+
AbstractSparseMatrixCSC interface.
77

88
```@autodocs
99
Modules = [ExtendableSparse]

src/ExtendableSparse.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using Requires
1616

1717
using DocStringExtensions
1818

19-
import SparseArrays: rowvals, getcolptr, nonzeros
19+
import SparseArrays: AbstractSparseMatrixCSC, rowvals, getcolptr, nonzeros
2020

2121
include("sparsematrixcsc.jl")
2222
include("sparsematrixlnk.jl")
@@ -47,7 +47,6 @@ function __init__()
4747
@require Pardiso="46dd5b70-b6fb-5a00-ae2d-e8fea33afaf2" include("pardiso_lu.jl")
4848
@require IncompleteLU="40713840-3770-5561-ab4c-a76e7d0d7895" include("ilut.jl")
4949
@require AlgebraicMultigrid="2169fc97-5a83-5252-b627-83903c6c433c" include("amg.jl")
50-
@require LinearSolve="7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" include("linearsolve.jl")
5150
end
5251

5352
export ILUTPreconditioner, AMGPreconditioner

src/amg.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ function AMGPreconditioner(; valuetype::Type = Float64, indextype::Type = Int64,
2323
AMGPreconditioner{valuetype, indextype}(; kwargs...)
2424
end
2525

26-
@eval begin @makefrommatrix AMGPreconditioner end
26+
@eval begin
27+
@makefrommatrix AMGPreconditioner
28+
end
2729

2830
function update!(precon::AMGPreconditioner{Tv, Ti}) where {Tv, Ti}
2931
@inbounds flush!(precon.A)

src/extendable.jl

+61-38
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ either in cscmatrix, or in lnkmatrix, never in both.
77
88
$(TYPEDFIELDS)
99
"""
10-
mutable struct ExtendableSparseMatrix{Tv, Ti <: Integer} <: AbstractSparseMatrix{Tv, Ti}
10+
mutable struct ExtendableSparseMatrix{Tv, Ti <: Integer} <: AbstractSparseMatrixCSC{Tv, Ti}
1111
"""
1212
Final matrix data
1313
"""
@@ -34,6 +34,7 @@ Create empty ExtendableSparseMatrix. This is equivalent to `spzeros(m,n)` for
3434
`SparseMartrixCSC`.
3535
3636
"""
37+
3738
function ExtendableSparseMatrix{Tv, Ti}(m, n) where {Tv, Ti <: Integer}
3839
ExtendableSparseMatrix{Tv, Ti}(spzeros(Tv, Ti, m, n), nothing, 0)
3940
end
@@ -56,6 +57,7 @@ $(SIGNATURES)
5657
5758
Create ExtendableSparseMatrix from SparseMatrixCSC
5859
"""
60+
5961
function ExtendableSparseMatrix(csc::SparseMatrixCSC{Tv, Ti}) where {Tv, Ti <: Integer}
6062
return ExtendableSparseMatrix{Tv, Ti}(csc, nothing, phash(csc))
6163
end
@@ -154,6 +156,7 @@ A
154156
155157
If `v` is zero, no new entry is created.
156158
"""
159+
157160
function updateindex!(ext::ExtendableSparseMatrix{Tv, Ti},
158161
op,
159162
v,
@@ -199,7 +202,10 @@ $(SIGNATURES)
199202
Find index in CSC matrix and set value if it exists. Otherwise,
200203
set index in extension if `v` is nonzero.
201204
"""
202-
function Base.setindex!(ext::ExtendableSparseMatrix{Tv, Ti}, v, i, j) where {Tv, Ti}
205+
function Base.setindex!(ext::ExtendableSparseMatrix{Tv, Ti},
206+
v,
207+
i::Integer,
208+
j::Integer) where {Tv, Ti}
203209
k = findindex(ext.cscmatrix, i, j)
204210
if k > 0
205211
ext.cscmatrix.nzval[k] = v
@@ -217,7 +223,9 @@ $(SIGNATURES)
217223
Find index in CSC matrix and return value, if it exists.
218224
Otherwise, return value from extension.
219225
"""
220-
function Base.getindex(ext::ExtendableSparseMatrix{Tv, Ti}, i, j) where {Tv, Ti <: Integer}
226+
function Base.getindex(ext::ExtendableSparseMatrix{Tv, Ti},
227+
i::Integer,
228+
j::Integer) where {Tv, Ti <: Integer}
221229
k = findindex(ext.cscmatrix, i, j)
222230
if k > 0
223231
return ext.cscmatrix.nzval[k]
@@ -303,26 +311,28 @@ end
303311
"""
304312
$(SIGNATURES)
305313
314+
Return element type.
315+
"""
316+
Base.eltype(::ExtendableSparseMatrix{Tv, Ti}) where {Tv, Ti} = Tv
317+
318+
"""
319+
$(SIGNATURES)
320+
306321
[`flush!`](@ref) and return rowvals in ext.cscmatrix.
307322
"""
308323
function SparseArrays.rowvals(ext::ExtendableSparseMatrix)
309324
flush!(ext)
310325
rowvals(ext.cscmatrix)
311326
end
312327

313-
function SparseArrays.getrowval(S::ExtendableSparseMatrix)
314-
flush!(S)
315-
getfield(S.cscmatrix, :rowval)
316-
end
317-
318328
"""
319329
$(SIGNATURES)
320330
321331
[`flush!`](@ref) and return colptr of in ext.cscmatrix.
322332
"""
323333
function SparseArrays.getcolptr(ext::ExtendableSparseMatrix)
324334
flush!(ext)
325-
return ext.cscmatrix.colptr
335+
return getcolptr(ext.cscmatrix)
326336
end
327337

328338
"""
@@ -335,15 +345,12 @@ function SparseArrays.findnz(ext::ExtendableSparseMatrix)
335345
return findnz(ext.cscmatrix)
336346
end
337347

338-
if USE_GPL_LIBS
339-
for (Tv) in (:Float64, :ComplexF64)
340-
@eval begin function LinearAlgebra.:\(ext::ExtendableSparseMatrix{$Tv, Ti},
341-
B::AbstractVecOrMat{$Tv}) where {Ti}
342-
flush!(ext)
343-
ext.cscmatrix \ B
344-
end end
348+
@static if VERSION >= v"1.7"
349+
function SparseArrays._checkbuffers(ext::ExtendableSparseMatrix)
350+
flush!(ext)
351+
SparseArrays._checkbuffers(ext.cscmatrix)
345352
end
346-
end # USE_GPL_LIBS
353+
end
347354

348355
"""
349356
A\b
@@ -353,7 +360,7 @@ are allowed in the Julia sysimage and the floating point type of the matrix is
353360
In that case, Julias standard `\` is called, which is realized via UMFPACK.
354361
"""
355362
function LinearAlgebra.:\(ext::ExtendableSparseMatrix{Tv, Ti},
356-
b::AbstractVector{Tv}) where {Tv, Ti}
363+
b::AbstractVector) where {Tv, Ti}
357364
flush!(ext)
358365
SparspakLU(ext) \ b
359366
end
@@ -364,16 +371,8 @@ $(SIGNATURES)
364371
[`\\`](@ref) for Symmetric{ExtendableSparse}
365372
"""
366373
function LinearAlgebra.:\(symm_ext::Symmetric{Tm, ExtendableSparseMatrix{Tm, Ti}},
367-
B::AbstractVector{T} where {T}) where {Tm, Ti}
368-
flush!(symm_ext.data)
369-
symm_csc = Symmetric(symm_ext.data.cscmatrix, Symbol(symm_ext.uplo))
370-
symm_csc \ B
371-
end
372-
function LinearAlgebra.:\(symm_ext::Symmetric{Tm, ExtendableSparseMatrix{Tm, Ti}},
373-
B::AbstractMatrix{T} where {T}) where {Tm, Ti}
374-
flush!(symm_ext.data)
375-
symm_csc = Symmetric(symm_ext.data.cscmatrix, Symbol(symm_ext.uplo))
376-
symm_csc \ B
374+
b::AbstractVector) where {Tm, Ti}
375+
symm_ext.data \ b # no ldlt yet ...
377376
end
378377

379378
"""
@@ -382,18 +381,42 @@ $(SIGNATURES)
382381
[`\\`](@ref) for Hermitian{ExtendableSparse}
383382
"""
384383
function LinearAlgebra.:\(symm_ext::Hermitian{Tm, ExtendableSparseMatrix{Tm, Ti}},
385-
B::AbstractVector{T} where {T}) where {Tm, Ti}
386-
flush!(symm_ext.data)
387-
symm_csc = Hermitian(symm_ext.data.cscmatrix, Symbol(symm_ext.uplo))
388-
symm_csc \ B
389-
end
390-
function LinearAlgebra.:\(symm_ext::Hermitian{Tm, ExtendableSparseMatrix{Tm, Ti}},
391-
B::AbstractMatrix{T} where {T}) where {Tm, Ti}
392-
flush!(symm_ext.data)
393-
symm_csc = Hermitian(symm_ext.data.cscmatrix, Symbol(symm_ext.uplo))
394-
symm_csc \ B
384+
b::AbstractVector) where {Tm, Ti}
385+
symm_ext.data \ B # no ldlt yet ...
395386
end
396387

388+
if USE_GPL_LIBS
389+
for (Tv) in (:Float64, :ComplexF64)
390+
@eval begin function LinearAlgebra.:\(ext::ExtendableSparseMatrix{$Tv, Ti},
391+
B::AbstractVector) where {Ti}
392+
flush!(ext)
393+
ext.cscmatrix \ B
394+
end end
395+
396+
@eval begin function LinearAlgebra.:\(symm_ext::Symmetric{$Tv,
397+
ExtendableSparseMatrix{
398+
$Tv,
399+
Ti
400+
}},
401+
B::AbstractVector) where {Ti}
402+
flush!(symm_ext.data)
403+
symm_csc = Symmetric(symm_ext.data.cscmatrix, Symbol(symm_ext.uplo))
404+
symm_csc \ B
405+
end end
406+
407+
@eval begin function LinearAlgebra.:\(symm_ext::Hermitian{$Tv,
408+
ExtendableSparseMatrix{
409+
$Tv,
410+
Ti
411+
}},
412+
B::AbstractVector) where {Ti}
413+
flush!(symm_ext.data)
414+
symm_csc = Hermitian(symm_ext.data.cscmatrix, Symbol(symm_ext.uplo))
415+
symm_csc \ B
416+
end end
417+
end
418+
end # USE_GPL_LIBS
419+
397420
"""
398421
$(SIGNATURES)
399422

src/ilut.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ function ILUTPreconditioner(; valuetype::Type = Float64, indextype::Type = Int64
2323
ILUTPreconditioner{valuetype, indextype}(; kwargs...)
2424
end
2525

26-
@eval begin @makefrommatrix ILUTPreconditioner end
26+
@eval begin
27+
@makefrommatrix ILUTPreconditioner
28+
end
2729

2830
function update!(precon::ILUTPreconditioner)
2931
A = precon.A

test/test_symmetric.jl

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function test_symm(n, uplo)
1111
flush!(A)
1212
SA = Symmetric(A, uplo)
1313
Scsc = Symmetric(A.cscmatrix, uplo)
14+
1415
if ExtendableSparse.USE_GPL_LIBS
1516
#requires SuiteSparse which is not available on non-GPL builds
1617
SA \ b Scsc \ b

0 commit comments

Comments
 (0)