Skip to content

Commit b846fe1

Browse files
committed
Simplify reduce! and add dropzeros!
1 parent b57acef commit b846fe1

File tree

2 files changed

+24
-47
lines changed

2 files changed

+24
-47
lines changed

src/systems/alias_elimination.jl

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ function Base.setindex!(ag::AliasGraph, v::Integer, i::Integer)
241241
return 0 => 0
242242
end
243243

244-
function Base.setindex!(ag::AliasGraph, p::Pair{Int, Int}, i::Integer)
244+
function Base.setindex!(ag::AliasGraph, p::Union{Pair{Int, Int}, Tuple{Int, Int}}, i::Integer)
245245
(c, v) = p
246246
if c == 0 || v == 0
247247
ag[i] = 0
@@ -271,49 +271,9 @@ function Base.in(i::Int, agk::AliasGraphKeySet)
271271
end
272272

273273
function reduce!(mm::SparseMatrixCLIL, ag::AliasGraph)
274-
dels = Int[]
275-
for (i, rs) in enumerate(mm.row_cols)
276-
rvals = mm.row_vals[i]
277-
j = 1
278-
while j <= length(rs)
279-
c = rs[j]
280-
_alias = get(ag, c, nothing)
281-
if _alias !== nothing
282-
coeff, alias = _alias
283-
if alias == c
284-
i = searchsortedfirst(rs, alias)
285-
rvals[i] *= coeff
286-
else
287-
push!(dels, j)
288-
iszero(coeff) && (j += 1; continue)
289-
inc = coeff * rvals[j]
290-
i = searchsortedfirst(rs, alias)
291-
if i > length(rs) || rs[i] != alias
292-
# if we add a variable to what we already visited, make sure
293-
# to bump the cursor.
294-
j += i <= j
295-
for (i, e) in enumerate(dels)
296-
e >= i && (dels[i] += 1)
297-
end
298-
insert!(rs, i, alias)
299-
insert!(rvals, i, inc)
300-
else
301-
rvals[i] += inc
302-
end
303-
end
304-
end
305-
j += 1
306-
end
307-
unique!(sort!(dels))
308-
deleteat!(rs, dels)
309-
deleteat!(rvals, dels)
310-
empty!(dels)
311-
for (j, v) in enumerate(rvals)
312-
iszero(v) && push!(dels, j)
313-
end
314-
deleteat!(rs, dels)
315-
deleteat!(rvals, dels)
316-
empty!(dels)
274+
for i in 1:size(mm, 1)
275+
adj_row = @view mm[i, :]
276+
locally_structure_simplify!(adj_row, nothing, ag)
317277
end
318278
mm
319279
end
@@ -896,8 +856,12 @@ function exactdiv(a::Integer, b)
896856
end
897857

898858
function locally_structure_simplify!(adj_row, pivot_var, ag)
899-
pivot_val = adj_row[pivot_var]
900-
iszero(pivot_val) && return false
859+
if pivot_var === nothing
860+
pivot_val = nothing
861+
else
862+
pivot_val = adj_row[pivot_var]
863+
iszero(pivot_val) && return false
864+
end
901865

902866
nirreducible = 0
903867
# When this row only as the pivot element, the pivot is zero by homogeneity
@@ -940,10 +904,21 @@ function locally_structure_simplify!(adj_row, pivot_var, ag)
940904
end
941905
end
942906

907+
if pivot_var === nothing
908+
if iszero(nirreducible)
909+
zero!(adj_row)
910+
else
911+
dropzeros!(adj_row)
912+
end
913+
return true
914+
end
943915
# If there were only one or two terms left in the equation (including the
944916
# pivot variable). We can eliminate the pivot variable. Note that when
945917
# `nirreducible <= 1`, `alias_candidate` is uniquely determined.
946-
nirreducible <= 1 || return false
918+
if nirreducible > 1
919+
dropzeros!(adj_row)
920+
return false
921+
end
947922

948923
if alias_candidate isa Pair
949924
alias_val, alias_var = alias_candidate

src/systems/sparsematrixclil.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ end
6565
zero!(a::AbstractArray{T}) where {T} = a[:] .= zero(T)
6666
zero!(a::SparseVector) = (empty!(a.nzind); empty!(a.nzval))
6767
zero!(a::CLILVector) = zero!(a.vec)
68+
SparseArrays.dropzeros!(a::CLILVector) = SparseArrays.dropzeros!(a.vec)
6869

6970
struct NonZeros{T <: AbstractArray}
7071
v::T
@@ -75,6 +76,7 @@ struct NonZerosPairs{T <: AbstractArray}
7576
v::T
7677
end
7778

79+
Base.IteratorSize(::Type{<:NonZerosPairs}) = Base.SizeUnknown()
7880
# N.B.: Because of how we're using this, this must be robust to modification of
7981
# the underlying vector. As such, we treat this as an iteration over indices
8082
# that happens to short cut using the sparse structure and sortedness of the

0 commit comments

Comments
 (0)