Skip to content

Commit 6192106

Browse files
committed
Lazy creation of extmatrix.
Once the sparsity structure is fixed it makes no sense to keep the memory for eventual additions.
1 parent 91714de commit 6192106

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/extendable.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mutable struct ExtendableSparseMatrix{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv
1616
"""
1717
Intermediate structure holding data of extension
1818
"""
19-
extmatrix::SparseMatrixExtension{Tv,Ti}
19+
extmatrix::Union{SparseMatrixExtension{Tv,Ti},Nothing}
2020
end
2121

2222

@@ -27,7 +27,7 @@ Create empty ExtendablSparseMatrix.
2727
2828
This is a pendant to spzeros.
2929
"""
30-
ExtendableSparseMatrix{Tv,Ti}(m::Integer, n::Integer) where {Tv,Ti<:Integer}=ExtendableSparseMatrix{Tv,Ti}(spzeros(Tv,Ti,m,n),SparseMatrixExtension{Tv, Ti}(m,n))
30+
ExtendableSparseMatrix{Tv,Ti}(m::Integer, n::Integer) where{Tv,Ti<:Integer}=ExtendableSparseMatrix{Tv,Ti}(spzeros(Tv,Ti,m,n),nothing)
3131

3232

3333
"""
@@ -60,11 +60,14 @@ $(SIGNATURES)
6060
Find index in CSC matrix and set value if it exists. Otherwise,
6161
set index in extension.
6262
"""
63-
function Base.setindex!(M::ExtendableSparseMatrix, v, i::Integer, j::Integer)
63+
function Base.setindex!(M::ExtendableSparseMatrix{Tv,Ti}, v, i::Integer, j::Integer) where{Tv,Ti<:Integer}
6464
k=findindex(M.cscmatrix,i,j)
6565
if k>0
6666
M.cscmatrix.nzval[k]=v
6767
else
68+
if M.extmatrix==nothing
69+
M.extmatrix=SparseMatrixExtension{Tv, Ti}(M.cscmatrix.m, M.cscmatrix.n)
70+
end
6871
M.extmatrix[i,j]=v
6972
end
7073
end
@@ -79,10 +82,12 @@ $(SIGNATURES)
7982
Find index in CSC matrix and return value, if it exists.
8083
Otherwise, return value from extension.
8184
"""
82-
function Base.getindex(M::ExtendableSparseMatrix,i::Integer, j::Integer)
85+
function Base.getindex(M::ExtendableSparseMatrix{Tv,Ti},i::Integer, j::Integer) where{Tv,Ti<:Integer}
8386
k=findindex(M.cscmatrix,i,j)
8487
if k>0
8588
return M.cscmatrix.nzval[k]
89+
elseif M.extmatrix==nothing
90+
return zero(Tv)
8691
else
8792
return M.extmatrix[i,j]
8893
end
@@ -101,7 +106,13 @@ $(SIGNATURES)
101106
102107
Number of nonzeros of ExtendableSparseMatrix.
103108
"""
104-
SparseArrays.nnz(E::ExtendableSparseMatrix)=(nnz(E.cscmatrix)+nnz(E.extmatrix))
109+
function SparseArrays.nnz(E::ExtendableSparseMatrix)
110+
ennz=0
111+
if E.extmatrix!=nothing
112+
ennz=nnz(E.extmatrix)
113+
end
114+
return nnz(E.cscmatrix)+ennz
115+
end
105116

106117

107118

@@ -206,9 +217,9 @@ If there are new entries in extension, create new CSC matrix
206217
and reset extension.
207218
"""
208219
function flush!(M::ExtendableSparseMatrix{Tv,Ti}) where {Tv, Ti<:Integer}
209-
if nnz(M.extmatrix)>0
220+
if M.extmatrix!=nothing && nnz(M.extmatrix)>0
210221
M.cscmatrix=_splice(M.extmatrix,M.cscmatrix)
211-
M.extmatrix=SparseMatrixExtension{Tv,Ti}(M.cscmatrix.m, M.cscmatrix.n)
222+
M.extmatrix=nothing
212223
end
213224
return M
214225
end

0 commit comments

Comments
 (0)