Skip to content

Commit 0e95c6d

Browse files
authored
Merge pull request #368 from JuliaRobotics/maint/1Q20/copyusd
update solver data by field and test
2 parents 7d55fde + 143c0fb commit 0e95c6d

File tree

3 files changed

+74
-27
lines changed

3 files changed

+74
-27
lines changed

src/services/DFGVariable.jl

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ end
224224
"""
225225
$SIGNATURES
226226
227-
Return full dictionary of PPEs in a variable, recommended to rather use CRUD: [`getPPE`](@ref),
227+
Return full dictionary of PPEs in a variable, recommended to rather use CRUD: [`getPPE`](@ref),
228228
"""
229229
getVariablePPEDict(vari::VariableDataLevel1) = getPPEDict(vari)
230230

@@ -361,34 +361,67 @@ addVariableSolverData!(dfg::AbstractDFG, sourceVariable::DFGVariable, solvekey::
361361
"""
362362
$(SIGNATURES)
363363
Update variable solver data if it exists, otherwise add it.
364+
365+
Notes:
366+
- `useCopy=true` to copy solver data and keep separate memory.
367+
- Use `fields` to updated only a few VND.fields while adhering to `useCopy`.
368+
369+
Related
370+
371+
mergeVariableSolverData!
364372
"""
365-
function updateVariableSolverData!(dfg::AbstractDFG, variablekey::Symbol, vnd::VariableNodeData, solvekey::Symbol=:default)::VariableNodeData
373+
function updateVariableSolverData!(dfg::AbstractDFG,
374+
variablekey::Symbol,
375+
vnd::VariableNodeData,
376+
solvekey::Symbol=:default,
377+
useCopy::Bool=true,
378+
fields::Vector{Symbol}=Symbol[] )::VariableNodeData
379+
#
366380
#This is basically just setSolverData
367381
var = getVariable(dfg, variablekey)
368382
if !haskey(var.solverDataDict, solvekey)
369383
@warn "VariableNodeData '$(solvekey)' does not exist, adding"
370384
end
371-
#for InMemoryDFGTypes, cloud would update here
372-
var.solverDataDict[solvekey] = vnd
373-
return vnd
374-
end
375385

376-
"""
377-
$(SIGNATURES)
378-
Update variable solver data if it exists, otherwise add it.
379-
NOTE: Copies the solver data.
380-
"""
381-
updateVariableSolverData!(dfg::AbstractDFG, sourceVariable::DFGVariable, solvekey::Symbol=:default) =
382-
updateVariableSolverData!(dfg, sourceVariable.label, deepcopy(getSolverData(sourceVariable, solvekey)), solvekey)
386+
# for InMemoryDFGTypes do memory copy or repointing, for cloud this would be an different kind of update.
387+
usevnd = useCopy ? deepcopy(vnd) : vnd
388+
# should just one, or many pointers be updated?
389+
if haskey(var.solverDataDict, solvekey) && isa(var.solverDataDict[solvekey], VariableNodeData) && length(fields) != 0
390+
# change multiple pointers inside the VND var.solverDataDict[solvekey]
391+
for field in fields
392+
destField = getfield(var.solverDataDict[solvekey], field)
393+
srcField = getfield(usevnd, field)
394+
if isa(destField, Array) && size(destField) == size(srcField)
395+
# use broadcast (in-place operation)
396+
destField .= srcField
397+
else
398+
# change pointer of destination VND object member
399+
setfield!(var.solverDataDict[solvekey], field, srcField)
400+
end
401+
end
402+
else
403+
# change a single pointer in var.solverDataDict
404+
var.solverDataDict[solvekey] = usevnd
405+
end
383406

384-
"""
385-
$(SIGNATURES)
386-
Update variable solver data if it exists, otherwise add it.
387-
"""
388-
function updateVariableSolverData!(dfg::AbstractDFG, sourceVariables::Vector{<:DFGVariable}, solvekey::Symbol=:default)
407+
return var.solverDataDict[solvekey]
408+
end
409+
410+
updateVariableSolverData!(dfg::AbstractDFG,
411+
sourceVariable::DFGVariable,
412+
solvekey::Symbol=:default,
413+
useCopy::Bool=true,
414+
fields::Vector{Symbol}=Symbol[] ) =
415+
updateVariableSolverData!(dfg, sourceVariable.label, getSolverData(sourceVariable, solvekey), solvekey, useCopy, fields)
416+
417+
function updateVariableSolverData!(dfg::AbstractDFG,
418+
sourceVariables::Vector{<:DFGVariable},
419+
solvekey::Symbol=:default,
420+
useCopy::Bool=true,
421+
fields::Vector{Symbol}=Symbol[] )
389422
#I think cloud would do this in bulk for speed
390423
for var in sourceVariables
391-
updateVariableSolverData!(dfg, var.label, getSolverData(var, solvekey), solvekey)
424+
updateVariableSolverData!(dfg, var.label, getSolverData(var, solvekey), solvekey, useCopy, fields)
392425
end
393426
end
394427

test/iifInterfaceTests.jl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,6 @@ end
281281
#make a copy and simulate external changes
282282
newvar = deepcopy(var1)
283283
getVariablePPEDict(newvar)[:default] = MeanMaxPPE(:default, [150.0], [100.0], [50.0])
284-
#update
285-
# mergeUpdateVariableSolverData!(dfg, newvar)
286-
# mergeVariableSolverData!(getVariable(dfg,getLabel(newvar)), newvar)
287-
# mergeVariableData!(getVariable(dfg,getLabel(newvar)), newvar)
288284
mergeVariableData!(dfg, newvar)
289285

290286
#Check if variable is updated
@@ -297,8 +293,6 @@ end
297293
# Confirm they're different
298294
@test getVariablePPEDict(newvar) != getVariablePPEDict(var1)
299295
# Persist it.
300-
# mergeUpdateVariableSolverData!(dfg, newvar)
301-
# mergeVariableSolverData!(getVariable(dfg, getLabel(newvar)), newvar)
302296
mergeVariableData!(dfg, newvar)
303297
# Get the latest
304298
var1 = getVariable(dfg, :a)
@@ -313,8 +307,7 @@ end
313307
#confirm delete
314308
@test symdiff(collect(keys(getVariablePPEDict(newvar))), [:second]) == Symbol[]
315309
# Persist it., and test
316-
mergeVariableData!(dfg, newvar)
317-
# mergeUpdateVariableSolverData!(dfg, newvar) #357 #358
310+
mergeVariableData!(dfg, newvar) #357 #358
318311

319312
# Get the latest and confirm they're the same, :second
320313
var1 = getVariable(dfg, :a)

test/testBlocks.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,30 @@ function VSDTestBlock!(fg, v1)
568568

569569
# Update update it
570570
@test updateVariableSolverData!(fg, :a, vnd, :parametric) == vnd
571+
# test without deepcopy
572+
@test updateVariableSolverData!(fg, :a, vnd, :parametric, false) == vnd
571573
# Bulk copy update x0
572574
@test updateVariableSolverData!(fg, [v1], :default) == nothing
573575

576+
altVnd = vnd |> deepcopy
577+
keepVnd = getSolverData(getVariable(fg, :a), :parametric) |> deepcopy
578+
altVnd.inferdim = -99.0
579+
retVnd = updateVariableSolverData!(fg, :a, altVnd, :parametric, false, [:inferdim;])
580+
@test retVnd == altVnd
581+
582+
fill!(altVnd.bw, -1.0)
583+
retVnd = updateVariableSolverData!(fg, :a, altVnd, :parametric, false, [:bw;])
584+
@test retVnd == altVnd
585+
586+
altVnd.inferdim = -98.0
587+
@test retVnd != altVnd
588+
589+
# restore without copy
590+
# @show vnd.inferdim
591+
@test updateVariableSolverData!(fg, :a, keepVnd, :parametric, false, [:inferdim;:bw]) == vnd
592+
@test getSolverData(getVariable(fg, :a), :parametric).inferdim != altVnd.inferdim
593+
@test getSolverData(getVariable(fg, :a), :parametric).bw != altVnd.bw
594+
574595
# Delete parametric from v1
575596
@test deleteVariableSolverData!(fg, :a, :parametric) == vnd
576597

0 commit comments

Comments
 (0)