Skip to content

Commit eba3489

Browse files
committed
Base.sum for ExtendableSparse; depend on Julia 1.10
1 parent 7a33584 commit eba3489

6 files changed

Lines changed: 48 additions & 23 deletions

Project.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ ILUZero = "0.2"
3535
IncompleteLU = "^0.2.1"
3636
InteractiveUtils = "1.11.0"
3737
IterativeSolvers = "0.9"
38-
LinearAlgebra = "1.9"
38+
LinearAlgebra = "1.10"
3939
LinearSolve = "2.36.0, 3.7.1"
4040
Metis = "1"
4141
MultiFloats = "1, 2"
4242
OhMyThreads = "0.6, 0.7, 0.8"
43-
Printf = "1.9"
44-
Random = "1.9"
43+
Printf = "1.10"
44+
Random = "1.10"
4545
RecursiveFactorization = "0.2"
4646
SciMLPublic = "1.0.1"
47-
SparseArrays = "1.9"
47+
SparseArrays = "1.10"
4848
Sparspak = "0.3.6"
4949
StaticArrays = "1"
50-
Test = "1.9"
51-
julia = "1.9"
50+
Test = "1.10"
51+
julia = "1.10"
5252

5353
[extras]
5454
AMGCLWrap = "4f76b812-4ba5-496d-b042-d70715554288"

src/ExtendableSparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module ExtendableSparse
88
using DocStringExtensions: DocStringExtensions, SIGNATURES, TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES
99
using ILUZero: ILUZero
1010
using LinearAlgebra: LinearAlgebra, Diagonal, Hermitian, Symmetric, Tridiagonal, mul!, ldiv!
11-
using SparseArrays: SparseArrays, AbstractSparseMatrix, AbstractSparseMatrixCSC, SparseMatrixCSC
11+
using SparseArrays: SparseArrays, AbstractSparseMatrix, AbstractSparseMatrixCSC, SparseMatrixCSC, indtype
1212
using SparseArrays: dropzeros!, findnz, nzrange, sparse, spzeros, rowvals, getcolptr, nonzeros, nnz
1313
using Sparspak: sparspaklu
1414
using SciMLPublic: @public

src/abstractextendablesparsematrixcsc.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ Return element type.
106106
"""
107107
Base.eltype(::AbstractExtendableSparseMatrixCSC{Tv, Ti}) where {Tv, Ti} = Tv
108108

109+
"""
110+
SparseArrays.indtype(A::AbstractExtendableSparseMatrixCSC{Tv, Ti})
111+
112+
Return element type.
113+
"""
114+
SparseArrays.indtype(A::AbstractExtendableSparseMatrixCSC{Tv, Ti}) where {Tv, Ti} = Ti
109115

110116
"""
111117
SparseArrays.SparseMatrixCSC(A::AbstractExtendableSparseMatrixCSC)
@@ -360,6 +366,37 @@ function Base.:-(csc::SparseMatrixCSC, ext::AbstractExtendableSparseMatrixCSC)
360366
return csc - sparse(ext)
361367
end
362368

369+
"""
370+
Base.sum(M::AbstractVector{TM}) where TM<:AbstractExtendableSparseMatrixCSC
371+
372+
Efficient sum of ExtendableSparse matrices.
373+
"""
374+
function Base.sum(M::AbstractVector{TM}) where TM<:AbstractExtendableSparseMatrixCSC
375+
Ti = promote_type(indtype.(M)...)
376+
Tv = promote_type(eltype.(M)...)
377+
return _sum(M,Tv,Ti)
378+
end
379+
380+
function _sum(M, ::Type{Tv}, ::Type{Ti}) where {Tv, Ti}
381+
l = sum(nnz, M)
382+
I = Vector{Ti}(undef, l)
383+
J = Vector{Ti}(undef, l)
384+
V = Vector{Tv}(undef, l)
385+
386+
i = 1
387+
@time for m in M
388+
(; colptr, nzval, rowval) = sparse(m)
389+
for icsc in 1:(length(colptr) - 1)
390+
for j in colptr[icsc]:(colptr[icsc + 1] - 1)
391+
I[i] = rowval[j]
392+
J[i] = icsc
393+
V[i] = nzval[j]
394+
i = i + 1
395+
end
396+
end
397+
end
398+
return SparseArrays.sparse!(I, J, V)
399+
end
363400

364401
"""
365402
$(TYPEDSIGNATURES)

src/sparsematrixdict.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,7 @@ function SparseArrays.sparse(mat::SparseMatrixDict{Tv, Ti}) where {Tv, Ti}
135135
V[i] = v
136136
i = i + 1
137137
end
138-
@static if VERSION >= v"1.10"
139-
return SparseArrays.sparse!(I, J, V, size(mat)..., +)
140-
else
141-
return SparseArrays.sparse(I, J, V, size(mat)..., +)
142-
end
138+
return SparseArrays.sparse!(I, J, V, size(mat)..., +)
143139
end
144140

145141
"""
@@ -172,11 +168,7 @@ function Base.:+(dictmatrix::SparseMatrixDict{Tv, Ti}, cscmatrix::SparseMatrixCS
172168
end
173169

174170
@assert l == i - 1
175-
@static if VERSION >= v"1.10"
176-
return SparseArrays.sparse!(I, J, V, m, n, +)
177-
else
178-
return SparseArrays.sparse(I, J, V, m, n, +)
179-
end
171+
return SparseArrays.sparse!(I, J, V, m, n, +)
180172
end
181173
return cscmatrix
182174
end

src/sparsematrixdilnkc.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,7 @@ function Base.sum(lnkdictmatrices::Vector{SparseMatrixDILNKC{Tv, Ti}}, cscmatrix
443443
end
444444
end
445445
@assert l == i - 1
446-
@static if VERSION >= v"1.10"
447-
return SparseArrays.sparse!(I, J, V, m, n, +)
448-
else
449-
return SparseArrays.sparse(I, J, V, m, n, +)
450-
end
446+
return SparseArrays.sparse!(I, J, V, m, n, +)
451447
end
452448
return cscmatrix
453449
end

src/sparsematrixlnk.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ can be conveniently updated via `push!`. No copying of existing data is necessa
1818
1919
Via the type aliases [`STExtendableSparseMatrixCSC`](@ref), [`ExtendableSparseMatrixCSC`](@ref),
2020
and [`ExtendableSparseMatrix`](@ref) this extension is used as default for handling
21-
scalar assembly.
21+
sequential assembly.
2222
2323
2424
$(TYPEDFIELDS)

0 commit comments

Comments
 (0)