Skip to content

Commit 7f25694

Browse files
committed
Improve getSparsityPattern
1 parent 3d89532 commit 7f25694

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/parametric/services/ParametricManoptDev.jl

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ end
8080
struct CostF_RLM!{T}
8181
points::Vector{T}
8282
costfuns::Vector{<:CalcFactorManopt}
83+
# varLabels::Vector{Symbol} @TODO add
8384
end
8485

8586
function CostF_RLM!(costfuns::Vector{<:CalcFactorManopt}, frontals_p::Vector{T}, separators_p::Vector{T}) where T
@@ -154,8 +155,6 @@ end
154155
# function JacF_RLM!(M, costF!; basis_domain::AbstractBasis = DefaultOrthonormalBasis())
155156
function JacF_RLM!(M, costF!, p, fg=nothing; basis_domain::AbstractBasis = DefaultOrthogonalBasis(), sparse=!isnothing(fg))
156157

157-
# p = costF!.points
158-
159158
#Why does this error?
160159
# res = Vector(mapreduce(f -> f(p), vcat, costF!.costfuns))
161160
res = reduce(vcat, map(f -> f(p), Vector(costF!.costfuns)))
@@ -168,7 +167,7 @@ function JacF_RLM!(M, costF!, p, fg=nothing; basis_domain::AbstractBasis = Defau
168167

169168
if sparse
170169
factLabels = collect(getproperty.(costF!.costfuns, :faclbl))
171-
sparsity = eltype(res).(getSparsityPattern(fg, ls(fg), factLabels))
170+
sparsity = eltype(res).(getSparsityPattern(fg, costF!.varLabels, factLabels))
172171
colorvec = matrix_colors(sparsity)
173172
else
174173
sparsity = nothing
@@ -247,26 +246,32 @@ end
247246

248247
# TODO this function is not the sparsity pattern yet, it just fills in all entries from the biadjacency matrix
249248
# TODO allow getting sparcity pattern for a subfg
249+
# OLD 0.424040 seconds (940.11 k allocations: 45.512 MiB)
250+
# NEW 0.001552 seconds (2.04 k allocations: 1.816 MiB)
250251
function getSparsityPattern(fg, varLabels, factLabels)
251252
biadj = getBiadjacencyMatrix(fg; varLabels, factLabels)
252253

253254
vdims = getDimension.(getVariable.(fg, biadj.varLabels))
254255
fdims = getDimension.(getFactor.(fg, biadj.facLabels))
256+
257+
c_end = cumsum(vdims)
258+
r_end = cumsum(fdims)
255259

256-
sm = map(eachindex(biadj.B)) do i
257-
vdim = vdims[i[2]]
258-
fdim = fdims[i[1]]
259-
if biadj.B[i] > 0
260-
trues(fdim,vdim)
261-
else
262-
falses(fdim,vdim)
263-
end
264-
end
260+
C_range = range.(c_end - vdims .+1, c_end)
261+
R_range = range.(r_end - fdims .+1, r_end)
265262

266-
return SparseMatrixCSC(mortar(sm))
263+
ROWS, COLS, _ = findnz(biadj.B)
267264

265+
iter = reduce(vcat, map(zip(ROWS, COLS)) do (R,C)
266+
vec(CartesianIndices((R_range[R], C_range[C])))
267+
end)
268+
269+
vec(CartesianIndices((R_range[2], C_range[1])))
270+
271+
return sparse(getindex.(iter,1), getindex.(iter,2), ones(Bool, length(iter)))
268272
end
269273

274+
270275
function solve_RLM(
271276
fg,
272277
frontals::Vector{Symbol} = ls(fg);
@@ -446,7 +451,8 @@ end
446451

447452
#HEX solve
448453
# sparse J 0.025235 seconds (133.65 k allocations: 9.964 MiB
449-
# new 0.013486 seconds (36.16 k allocations: 2.593 MiB)
454+
# new1 0.013486 seconds (36.16 k allocations: 2.593 MiB)
455+
# new2 0.010764 seconds (34.61 k allocations: 3.111 MiB)
450456
# dense J 0.022079 seconds (283.54 k allocations: 18.146 MiB)
451457

452458
function solve_RLM_sparse(fg; kwargs...)

0 commit comments

Comments
 (0)