Skip to content

Commit 10eb202

Browse files
committed
add rawupdateindex! and dropzeros! methods
1 parent b802504 commit 10eb202

File tree

7 files changed

+113
-11
lines changed

7 files changed

+113
-11
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ jobs:
4343
- uses: julia-actions/julia-buildpkg@v1
4444
- uses: julia-actions/julia-runtest@v1
4545
- uses: julia-actions/julia-processcoverage@v1
46-
# - uses: codecov/codecov-action@v1
47-
# with:
48-
# file: lcov.info
46+
- uses: codecov/codecov-action@v1
47+
with:
48+
file: lcov.info
4949
docs:
5050
name: Documentation
5151
runs-on: ubuntu-latest

Project.toml

Lines changed: 1 addition & 1 deletion
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 <juergen.fuhrmann@wias-berlin.de>"]
4-
version = "0.4.1"
4+
version = "0.4.2"
55

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

src/ExtendableSparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using LinearAlgebra
66
include("sparsematrixcsc.jl")
77
include("sparsematrixlnk.jl")
88
include("extendable.jl")
9-
export SparseMatrixLNK,ExtendableSparseMatrix,flush!,nnz, updateindex!, colptrs
9+
export SparseMatrixLNK,ExtendableSparseMatrix,flush!,nnz, updateindex!, rawupdateindex!, colptrs
1010

1111
include("preconditioners.jl")
1212
export JacobiPreconditioner, ILU0Preconditioner, ParallelJacobiPreconditioner

src/extendable.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Base.similar(m::ExtendableSparseMatrix{Tv,Ti}) where {Tv,Ti}=ExtendableSparseMat
8080
"""
8181
$(SIGNATURES)
8282
83-
Update element of the matrix with operation `op`.
83+
Update element of the matrix with operation `op`.
8484
This can replace the following code and save one index
8585
search during acces:
8686
@@ -98,6 +98,8 @@ A=ExtendableSparseMatrix(3,3)
9898
updateindex!(A,+,0.1,1,2)
9999
A
100100
```
101+
102+
If `v` is zero, no new entry is created.
101103
"""
102104
function updateindex!(ext::ExtendableSparseMatrix{Tv,Ti}, op,v, i,j) where {Tv,Ti<:Integer}
103105
k=findindex(ext.cscmatrix,i,j)
@@ -113,11 +115,31 @@ function updateindex!(ext::ExtendableSparseMatrix{Tv,Ti}, op,v, i,j) where {Tv,T
113115
end
114116

115117

118+
"""
119+
$(SIGNATURES)
120+
Like [`updateindex!`](@ref) but without
121+
checking if v is zero.
122+
123+
"""
124+
function rawupdateindex!(ext::ExtendableSparseMatrix{Tv,Ti}, op,v, i,j) where {Tv,Ti<:Integer}
125+
k=findindex(ext.cscmatrix,i,j)
126+
if k>0
127+
ext.cscmatrix.nzval[k]=op(ext.cscmatrix.nzval[k],v)
128+
else
129+
if ext.lnkmatrix==nothing
130+
ext.lnkmatrix=SparseMatrixLNK{Tv, Ti}(ext.cscmatrix.m, ext.cscmatrix.n)
131+
end
132+
rawupdateindex!(ext.lnkmatrix,op,v,i,j)
133+
end
134+
ext
135+
end
136+
137+
116138
"""
117139
$(SIGNATURES)
118140
119141
Find index in CSC matrix and set value if it exists. Otherwise,
120-
set index in extension.
142+
set index in extension if `v` is nonzero.
121143
"""
122144

123145
function Base.setindex!(ext::ExtendableSparseMatrix{Tv,Ti}, v, i,j) where {Tv,Ti}
@@ -189,7 +211,7 @@ function flush!(ext::ExtendableSparseMatrix)
189211
if ext.lnkmatrix!=nothing && nnz(ext.lnkmatrix)>0
190212
ext.cscmatrix=ext.lnkmatrix+ext.cscmatrix
191213
ext.lnkmatrix=nothing
192-
ext.pattern_timestamp
214+
ext.pattern_timestamp=time()
193215
end
194216
return ext
195217
end
@@ -380,3 +402,12 @@ function Base.:-(csc::SparseMatrixCSC,ext::ExtendableSparseMatrix)
380402
@inbounds flush!(ext)
381403
return csc - ext.cscmatrix
382404
end
405+
406+
407+
"""
408+
$(SIGNATURES)
409+
"""
410+
function SparseArrays.dropzeros!(ext::ExtendableSparseMatrix)
411+
@inbounds flush!(ext)
412+
dropzeros!(ext.cscmatrix)
413+
end

src/sparsematrixcsc.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ end
2323
"""
2424
$(SIGNATURES)
2525
26-
Update element of the matrix with operation `op`.
26+
Update element of the matrix with operation `op` whithout
27+
introducing new nonzero elements.
28+
2729
This can replace the following code and save one index
2830
search during acces:
2931
@@ -42,17 +44,19 @@ A=spzeros(3,3)
4244
updateindex!(A,+,0.1,1,2)
4345
A
4446
```
47+
4548
"""
4649
function updateindex!(csc::SparseMatrixCSC{Tv,Ti}, op, v,i, j) where{Tv,Ti<:Integer}
4750
k=findindex(csc,i,j)
4851
if k>0 # update existing value
4952
csc.nzval[k]=op(csc.nzval[k],v)
5053
else # insert new value
51-
csc[i,j]= op(zero(Tv),v)
54+
csc[i,j]=op(zero(Tv),v)
5255
end
5356
csc
5457
end
5558

59+
5660
"""
5761
$(SIGNATURES)
5862

src/sparsematrixlnk.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ end
211211
$(SIGNATURES)
212212
213213
Update element of the matrix with operation `op`.
214-
It assumes that `op(0,0)==0`
214+
It assumes that `op(0,0)==0`. If `v` is zero, no new
215+
entry is created.
215216
"""
216217
function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, v, i,j) where {Tv,Ti}
217218
# Set the first column entry if it was not yet set.
@@ -233,6 +234,31 @@ function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, v, i,j) where {Tv,Ti}
233234
lnk
234235
end
235236

237+
"""
238+
$(SIGNATURES)
239+
240+
Update element of the matrix with operation `op`.
241+
It assumes that `op(0,0)==0`. If `v` is zero a new entry
242+
is created nevertheless.
243+
"""
244+
function rawupdateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, v, i,j) where {Tv,Ti}
245+
# Set the first column entry if it was not yet set.
246+
if lnk.rowval[j]==0
247+
lnk.rowval[j]=i
248+
lnk.nzval[j]=op(lnk.nzval[j],v)
249+
lnk.nnz+=1
250+
return lnk
251+
end
252+
k,k0=findindex(lnk,i,j)
253+
if k>0
254+
lnk.nzval[k]=op(lnk.nzval[k],v)
255+
else
256+
k=addentry!(lnk,i,j,k,k0)
257+
lnk.nzval[k]=op(zero(Tv),v)
258+
end
259+
lnk
260+
end
261+
236262

237263
"""
238264
$(SIGNATURES)

test/runtests.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ using BenchmarkTools
2121
@test test_constructors()
2222
end
2323

24+
#################################################################
25+
26+
@testset "Updates" begin
27+
A=ExtendableSparseMatrix(10,10)
28+
@test nnz(A)==0
29+
A[1,3]=5
30+
updateindex!(A,+,6.0,4,5)
31+
updateindex!(A,+,0.0,2,3)
32+
@test nnz(A)==2
33+
rawupdateindex!(A,+,0.0,2,3)
34+
@test nnz(A)==3
35+
dropzeros!(A)
36+
@test nnz(A)==2
37+
rawupdateindex!(A,+,0.1,2,3)
38+
@test nnz(A)==3
39+
dropzeros!(A)
40+
@test nnz(A)==3
41+
end
2442
#################################################################
2543
function test_timing(k,l,m)
2644
t1=@belapsed fdrand($k,$l,$m,matrixtype=$SparseMatrixCSC) seconds=1
@@ -171,6 +189,26 @@ end
171189
@test test_fdrand_coo(10,10,10)
172190
end
173191

192+
193+
194+
##############################################
195+
function test_fdrand_update(k,l,m)
196+
A1=fdrand(k,l,m,rand=()->1,matrixtype=ExtendableSparseMatrix,update = (A,v,i,j)-> A[i,j]+=v)
197+
A2=fdrand(k,l,m,rand=()->1,matrixtype=ExtendableSparseMatrix,update = (A,v,i,j)-> rawupdateindex!(A,+,v,i,j))
198+
A3=fdrand(k,l,m,rand=()->1,matrixtype=ExtendableSparseMatrix,update = (A,v,i,j)-> updateindex!(A,+,v,i,j))
199+
200+
A1A2 && A1 A3
201+
end
202+
203+
@testset "fdrand_update" begin
204+
@test test_fdrand_update(1000,1,1)
205+
@test test_fdrand_update(20,20,1)
206+
@test test_fdrand_update(10,10,10)
207+
end
208+
209+
210+
211+
174212
##############################################
175213

176214
function test_precon(Precon,k,l,m;maxiter=10000)
@@ -236,3 +274,6 @@ end
236274
@test test_hermitian(300,:U)
237275
@test test_hermitian(300,:L)
238276
end
277+
278+
279+

0 commit comments

Comments
 (0)