Skip to content

Commit 8b70aaf

Browse files
authored
Merge pull request #1716 from JuliaRobotics/23Q2/twig/varValsAll_ref
rework ccw.varValsAll memory for static arrays
2 parents e07607a + 874825e commit 8b70aaf

30 files changed

+492
-269
lines changed

src/Deprecated.jl

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,64 @@ function solveGraphParametric2(
119119
return d, result, flatvar.idx, Σ
120120
end
121121

122+
##==============================================================================
123+
## Deprecate code below before v0.35
124+
##==============================================================================
125+
126+
127+
@deprecate _prepCCW(w...;kw...) _createCCW(w...;kw...)
128+
129+
predictbelief(w...;asPartial::Bool=false,kw...) = begin
130+
@warn("predictbelief is deprecated, use propagateBelief instead")
131+
bel,ipc = propagateBelief(w...;asPartial,kw...)
132+
getPoints(bel), ipc
133+
end
134+
135+
# """
136+
# $SIGNATURES
137+
138+
# This is an old function name that will be replaced by [`propagateBelief`](@ref).
139+
# """
140+
# function predictbelief(
141+
# dfg::AbstractDFG,
142+
# destvert::DFGVariable,
143+
# factors::AbstractVector; #{<:DFGFactor};
144+
# asPartial::Bool = false,
145+
# kw...,
146+
# )
147+
# #
148+
# # new
149+
# mkd, ifd = propagateBelief(dfg, destvert, factors; kw...)
150+
151+
# # legacy interface
152+
# return getPoints(mkd, asPartial), ifd
153+
# end
154+
155+
# function predictbelief(
156+
# dfg::AbstractDFG,
157+
# destlbl::Symbol,
158+
# fctlbls::AbstractVector{Symbol};
159+
# kw...,
160+
# )
161+
# return predictbelief(
162+
# dfg,
163+
# getVariable(dfg, destlbl),
164+
# map(x -> getFactor(dfg, x), fctlbls);
165+
# kw...,
166+
# )
167+
# end
168+
# #
169+
170+
# function predictbelief(dfg::AbstractDFG, destlbl::Symbol, ::Colon; kw...)
171+
# return predictbelief(dfg, destlbl, getNeighbors(dfg, destlbl); kw...)
172+
# end
173+
#
122174

123175
##==============================================================================
124176
## Deprecate code below before v0.34
125177
##==============================================================================
126178

179+
127180
# function CommonConvWrapper(
128181
# usrfnc::T,
129182
# fullvariables, #::Tuple ::Vector{<:DFGVariable};
@@ -204,15 +257,27 @@ function Base.getproperty(ccw::CommonConvWrapper, f::Symbol)
204257
# return SingleThreaded
205258
elseif f == :params
206259
error("CommonConvWrapper.params is deprecated, use .varValsAll instead")
207-
return ccw.varValsAll
260+
return ccw.varValsAll[]
208261
elseif f == :vartypes
209262
@warn "CommonConvWrapper.vartypes is deprecated, use typeof.(getVariableType.(ccw.fullvariables) instead" maxlog=3
210263
return typeof.(getVariableType.(ccw.fullvariables))
264+
elseif f == :hypotheses
265+
@warn "CommonConvWrapper.hypotheses is now under ccw.hyporecipe.hypotheses" maxlog=5
266+
return ccw.hyporecipe.hypotheses
267+
elseif f == :certainhypo
268+
@warn "CommonConvWrapper.certainhypo is now under ccw.hyporecipe.certainhypo" maxlog=5
269+
return ccw.hyporecipe.certainhypo
270+
elseif f == :activehypo
271+
@warn "CommonConvWrapper.activehypo is now under ccw.hyporecipe.activehypo" maxlog=5
272+
return ccw.hyporecipe.activehypo
211273
else
212274
return getfield(ccw, f)
213275
end
214276
end
215277

278+
279+
280+
216281
##==============================================================================
217282
## Deprecate code below before v0.35
218283
##==============================================================================

src/ExportAPI.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export CSMHistory,
153153
getNumPts,
154154
getBWVal,
155155
setBW!,
156+
setBelief!,
156157
setValKDE!,
157158
buildCliqSubgraph,
158159

@@ -240,7 +241,6 @@ export CSMHistory,
240241
getCliqNumAssocFactorsPerVar,
241242

242243
# user functions
243-
predictbelief,
244244
propagateBelief,
245245
getCliqMat,
246246
getCliqAssocMat,

src/Factors/LinearRelative.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Default linear offset between two scalar variables.
1010
X_2 = X_1 + η_Z
1111
```
1212
"""
13-
struct LinearRelative{N, T <: SamplableBelief} <: AbstractRelativeRoots
13+
struct LinearRelative{N, T <: SamplableBelief} <: AbstractRelativeMinimize
1414
Z::T
1515
end
1616

@@ -41,6 +41,10 @@ getDimension(::InstanceType{LinearRelative{N}}) where {N} = N
4141
# new and simplified interface for both nonparametric and parametric
4242
function (s::CalcFactor{<:LinearRelative})(z, x1, x2)
4343
# TODO convert to distance(distance(x2,x1),z) # or use dispatch on `-` -- what to do about `.-`
44+
# if s._sampleIdx < 5
45+
# @info "LinearRelative" s._sampleIdx "$z" "$x1" "$x2" s.solvefor getLabel.(s.fullvariables)
46+
# @info "in variables" pointer(getVal(s.fullvariables[s.solvefor])) getVal(s.fullvariables[s.solvefor])[1]
47+
# end
4448
return z .- (x2 .- x1)
4549
end
4650

src/IncrementalInference.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ include("ManifoldsExtentions.jl")
125125
# regular
126126
include("entities/SolverParams.jl")
127127

128+
include("entities/HypoRecipe.jl")
128129
include("entities/FactorOperationalMemory.jl")
129130

130131
include("Factors/GenericMarginal.jl")

src/Serialization/services/DispatchPackedConversions.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ function reconstFactorData(
3737
vars = map(f -> getVariable(dfg, f), varOrder)
3838
userCache = preambleCache(dfg, vars, usrfnc)
3939

40-
# TODO -- improve _prepCCW for hypotheses and certainhypo field recovery when deserializing
40+
# TODO -- improve _createCCW for hypotheses and certainhypo field recovery when deserializing
4141
# reconstitute from stored data
4242
# FIXME, add threadmodel=threadmodel
4343
# FIXME https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/590#issuecomment-776838053
4444
# FIXME dont know what manifolds to use in ccw
45-
ccw = _prepCCW(
45+
ccw = _createCCW(
4646
vars,
4747
usrfnc;
4848
multihypo,
@@ -145,7 +145,7 @@ function rebuildFactorMetadata!(
145145
end
146146

147147
#... Copying neighbor data into the factor?
148-
# JT TODO it looks like this is already updated in getDefaultFactorData -> _prepCCW
148+
# JT TODO it looks like this is already updated in getDefaultFactorData -> _createCCW
149149
# factormetadata.variableuserdata is deprecated, remove when removing deprecation
150150
# for i in 1:Threads.nthreads()
151151
# ccw_new.fnc.cpt[i].factormetadata.variableuserdata = deepcopy(neighborUserData)

src/entities/FactorOperationalMemory.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ $(TYPEDEF)
6363
"""
6464
struct MultiThreaded <: _AbstractThreadModel end
6565

66+
67+
6668
"""
6769
$(TYPEDEF)
6870
@@ -83,11 +85,10 @@ Related
8385
Base.@kwdef struct CommonConvWrapper{
8486
T <: AbstractFactor,
8587
VT <: Tuple,
86-
NTP <: Tuple,
88+
TP <: Base.RefValue{<:Tuple},
8789
CT,
8890
AM <: AbstractManifold,
89-
HP <: Union{Nothing, <:Distributions.Categorical{Float64, Vector{Float64}}},
90-
CH <: Union{Nothing, Vector{Int}},
91+
HR <: HypoRecipeCompute,
9192
MT,
9293
G
9394
} <: FactorOperationalMemory
@@ -98,8 +99,9 @@ Base.@kwdef struct CommonConvWrapper{
9899
fullvariables::VT
99100
# shortcuts to numerical containers
100101
""" Numerical containers for all connected variables. Hypo selection needs to be passed
101-
to each hypothesis evaluation event on user function via CalcFactor, #1321 """
102-
varValsAll::NTP
102+
to each hypothesis evaluation event on user function via CalcFactor, #1321.
103+
Points directly at the variable VND.val (not a deepcopy). """
104+
varValsAll::TP
103105
""" dummy cache value to be deep copied later for each of the CalcFactor instances """
104106
dummyCache::CT = nothing
105107
# derived config parameters for this factor
@@ -113,13 +115,8 @@ Base.@kwdef struct CommonConvWrapper{
113115
nullhypo::Float64 = 0.0
114116
""" inflationSpread particular to this factor (by how much to dispurse the belief initial values before numerical optimization is run). Analogous to stochastic search """
115117
inflation::Float64 = SolverParams().inflation
116-
# multihypo specific field containers for recipe of hypotheses to compute
117-
""" multi hypothesis settings #NOTE no need for a parameter as type is known from `parseusermultihypo` """
118-
hypotheses::HP = nothing
119-
""" categorical to select which hypothesis is being considered during convolution operation """
120-
certainhypo::CH = nothing
121-
""" subsection indices to select which params should be used for this hypothesis evaluation """
122-
activehypo::Vector{Int} = collect(1:length(varValsAll))
118+
""" multihypo specific field containers for recipe of hypotheses to compute """
119+
hyporecipe::HR = HypoRecipeCompute(;activehypo=collect(1:length(varValsAll)))
123120
# buffers and indices to point numerical computations to specific memory locations
124121
""" user defined measurement values for each approxConv operation
125122
FIXME make type stable, JT should now be type stable if rest works.

src/entities/HypoRecipe.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
4+
Base.@kwdef struct HypoRecipe
5+
certainidx::Vector{Int}
6+
allelements::Vector{Vector{Int}}
7+
activehypo::Vector{Tuple{Int,Vector{Int}}}
8+
mhidx::Vector{Int}
9+
end
10+
11+
Base.@kwdef struct HypoRecipeCompute{
12+
HP <: Union{Nothing, <:Distributions.Categorical{Float64, <:AbstractVector{Float64}}},
13+
CH <: Union{Nothing, <:AbstractVector{<:Integer}},
14+
}
15+
""" multi hypothesis settings #NOTE no need for a parameter as type is known from `parseusermultihypo` """
16+
hypotheses::HP = nothing
17+
""" categorical to select which hypothesis is being considered during convolution operation """
18+
certainhypo::CH = nothing
19+
""" subsection indices to select which params should be used for this hypothesis evaluation """
20+
activehypo::Vector{Int} = Int[]
21+
end

src/services/ApproxConv.jl

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,36 @@ function approxConvBelief(
1212
skipSolve::Bool = false,
1313
)
1414
#
15-
v1 = getVariable(dfg, target)
16-
N = N == 0 ? getNumPts(v1; solveKey = solveKey) : N
15+
v_trg = getVariable(dfg, target)
16+
N = N == 0 ? getNumPts(v_trg; solveKey = solveKey) : N
17+
# approxConv should push its result into duplicate memory destination, NOT the variable.VND.val itself. ccw.varValsAll always points directly to variable.VND.val
1718
# points and infoPerCoord
18-
pts, ipc = evalFactor(dfg, fc, v1.label, measurement; solveKey, N, skipSolve, nullSurplus)
19+
20+
pts, ipc = evalFactor(
21+
dfg,
22+
fc,
23+
v_trg.label,
24+
measurement;
25+
solveKey,
26+
N,
27+
skipSolve,
28+
nullSurplus
29+
)
1930

2031
len = length(ipc)
2132
mask = 1e-14 .< abs.(ipc)
2233
partl = collect(1:len)[mask]
2334

2435
# is the convolution infoPerCoord full or partial
25-
if sum(mask) == len
36+
res = if sum(mask) == len
2637
# not partial
27-
return manikde!(getManifold(getVariable(dfg, target)), pts; partial = nothing)
38+
manikde!(getManifold(getVariable(dfg, target)), pts; partial = nothing)
2839
else
2940
# is partial
30-
return manikde!(getManifold(getVariable(dfg, target)), pts; partial = partl)
41+
manikde!(getManifold(getVariable(dfg, target)), pts; partial = partl)
3142
end
43+
44+
return res
3245
end
3346

3447
approxConv(w...; kw...) = getPoints(approxConvBelief(w...; kw...), false)
@@ -66,7 +79,7 @@ function approxConvBelief(
6679
measurement::AbstractVector = Tuple[];
6780
solveKey::Symbol = :default,
6881
N::Int = length(measurement),
69-
tfg::AbstractDFG = initfg(),
82+
tfg::AbstractDFG = LocalDFG(;solverParams=getSolverParams(dfg)),
7083
setPPEmethod::Union{Nothing, Type{<:AbstractPointParametricEst}} = nothing,
7184
setPPE::Bool = setPPEmethod !== nothing,
7285
path::AbstractVector{Symbol} = Symbol[],
@@ -96,7 +109,9 @@ function approxConvBelief(
96109
neMsk = exists.(tfg, varLbls) .|> x -> xor(x, true)
97110
# put the non-existing variables into the temporary graph `tfg`
98111
# bring all the solveKeys too
99-
addVariable!.(tfg, getVariable.(dfg, varLbls[neMsk]))
112+
for v in getVariable.(dfg, varLbls[neMsk])
113+
addVariable!(tfg, v.label, getVariableType(v))
114+
end
100115
# variables adjacent to the shortest path should be initialized from dfg
101116
setdiff(varLbls, path[xor.(fctMsk, true)]) .|>
102117
x -> initVariable!(tfg, x, getBelief(dfg, x))

0 commit comments

Comments
 (0)