Skip to content

Commit eba3b38

Browse files
committed
towards consolidated FactorCompute constructor
1 parent c45ba31 commit eba3b38

6 files changed

Lines changed: 106 additions & 55 deletions

File tree

src/Deprecated.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,39 @@ function updateVariableSolverData!(
190190
end
191191
end
192192

193+
function FactorCompute(
194+
label::Symbol,
195+
timestamp::Union{DateTime, ZonedDateTime},
196+
nstime::Nanosecond,
197+
tags::Set{Symbol},
198+
solverData::GenericFunctionNodeData,
199+
solvable::Int,
200+
variableOrder::Union{Vector{Symbol}, Tuple};
201+
observation = getFactorType(solverData),
202+
state::FactorState = FactorState(),
203+
workmem::Base.RefValue{<:FactorOperationalMemory} = Ref{FactorOperationalMemory}(),
204+
id::Union{UUID, Nothing} = nothing,
205+
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
206+
)
207+
error(
208+
"This constructor is deprecated, use FactorCompute(label, variableOrder, solverData; ...) instead",
209+
)
210+
return FactorCompute(
211+
id,
212+
label,
213+
tags,
214+
Tuple(variableOrder),
215+
timestamp,
216+
nstime,
217+
Ref(solverData),
218+
Ref(solvable),
219+
smallData,
220+
observation,
221+
state,
222+
workmem,
223+
)
224+
end
225+
193226
## ================================================================================
194227
## Deprecated in v0.25
195228
##=================================================================================

src/entities/DFGFactor.jl

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ $(TYPEDFIELDS)
253253
"""
254254
Base.@kwdef struct FactorCompute{FT <: AbstractFactor, N} <: AbstractDFGFactor
255255
"""The ID for the factor"""
256-
id::Union{UUID, Nothing} = nothing
256+
id::Union{UUID, Nothing} = nothing #TODO deprecate id
257257
"""Factor label, e.g. :x1f1.
258258
Accessor: [`getLabel`](@ref)"""
259259
label::Symbol
@@ -279,55 +279,62 @@ Base.@kwdef struct FactorCompute{FT <: AbstractFactor, N} <: AbstractDFGFactor
279279
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}()
280280

281281
#refactor fields
282+
"""Observation function or measurement for this factor.
283+
Accessors: [`getObservation`](@ref)(@ref)"""
282284
observation::FT
285+
"""Describes the current state of the factor. Persisted in serialization.
286+
Accessors: [`getFactorState`](@ref)"""
283287
state::FactorState
284-
computeMem::Base.RefValue{<:FactorOperationalMemory} #TODO easy of use vs. performance as container is abstract in any case.
288+
"""Temporary, non-persistent memory used internally by the solver for intermediate numerical computations and buffers.
289+
`workmem` is lazily allocated and only used during factor operations; it is not serialized or retained after solving.
290+
Accessors: [`getWorkmem`](@ref), [`setWorkmem!`](@ref)"""
291+
workmem::Base.RefValue{<:FactorOperationalMemory} #TODO easy of use vs. performance as container is abstract in any case.
285292
end
286293

287294
##------------------------------------------------------------------------------
288295
## Constructors
289296

290-
#TODO consolidate constructors, currently IIF calls only
291-
# DFGFactor(
292-
# Symbol(namestring),
293-
# varOrderLabels,
294-
# solverData;
295-
# tags = Set(union(tags, [:FACTOR])),
296-
# solvable,
297-
# timestamp = _zonedtime(timestamp),
298-
# )
299-
297+
# TODO standardize new fields in kw constructors, .id
300298
function FactorCompute(
301299
label::Symbol,
302-
timestamp::Union{DateTime, ZonedDateTime},
303-
nstime::Nanosecond,
304-
tags::Set{Symbol},
305-
solverData::GenericFunctionNodeData,
306-
solvable::Int,
307-
variableOrder::Union{Vector{Symbol}, Tuple};
308-
observation = getFactorType(solverData),
300+
variableOrder::Union{Vector{Symbol}, Tuple},
301+
observation::AbstractFactor,
309302
state::FactorState = FactorState(),
310-
computeMem::Base.RefValue{<:FactorOperationalMemory} = Ref{FactorOperationalMemory}(),
303+
workmem = Ref{FactorOperationalMemory}();
304+
tags::Set{Symbol} = Set{Symbol}(),
305+
timestamp::Union{DateTime, ZonedDateTime} = now(localzone()),
306+
solvable::Int = 1,
307+
nstime::Nanosecond = Nanosecond(0),
311308
id::Union{UUID, Nothing} = nothing,
312309
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
310+
solverData = nothing
313311
)
312+
if isnothing(solverData)
313+
refsolverData = Ref{GenericFunctionNodeData}() #TODO solverData deprecated in v0.27 WIP
314+
else
315+
Base.depwarn(
316+
"`FactorCompute` solverData is deprecated",
317+
:FactorCompute,
318+
)
319+
refsolverData = Ref(solverData)
320+
end
321+
314322
return FactorCompute(
315323
id,
316324
label,
317325
tags,
318326
Tuple(variableOrder),
319327
timestamp,
320328
nstime,
321-
Ref(solverData),
329+
refsolverData,
322330
Ref(solvable),
323331
smallData,
324332
observation,
325333
state,
326-
computeMem,
334+
workmem,
327335
)
328336
end
329337

330-
# TODO standardize new fields in kw constructors, .id
331338
function FactorCompute(
332339
label::Symbol,
333340
variableOrder::Union{Vector{Symbol}, Tuple},
@@ -339,6 +346,10 @@ function FactorCompute(
339346
id::Union{UUID, Nothing} = nothing,
340347
smallData::Dict{Symbol, SmallDataTypes} = Dict{Symbol, SmallDataTypes}(),
341348
)
349+
Base.depwarn(
350+
"`FactorCompute` constructor with `GenericFunctionNodeData` is deprecated. observation, state, and workmem should be provided explicitly.",
351+
:FactorCompute,
352+
)
342353
observation = getFactorType(solverData)
343354
state = FactorState(
344355
solverData.eliminated,
@@ -351,24 +362,24 @@ function FactorCompute(
351362
)
352363

353364
if solverData.fnc isa FactorOperationalMemory
354-
computeMem = Ref(solverData.fnc)
365+
workmem = Ref(solverData.fnc)
355366
else
356-
computeMem = Ref{FactorOperationalMemory}()
367+
workmem = Ref{FactorOperationalMemory}()
357368
end
358369

359370
return FactorCompute(
360371
label,
372+
Tuple(variableOrder),
373+
observation,
374+
state,
375+
workmem;
376+
id,
361377
timestamp,
362378
nstime,
363379
tags,
364380
solverData,
365-
solvable,
366-
Tuple(variableOrder);
367-
observation,
368-
computeMem,
369-
id,
370381
smallData,
371-
state,
382+
solvable,
372383
)
373384
end
374385

src/services/CompareUtils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const GeneratedCompareUnion = Union{
3434
}
3535

3636
@generated function ==(x::T, y::T) where {T <: GeneratedCompareUnion}
37-
ignored = [:computeMem]
37+
ignored = [:workmem]
3838
return mapreduce(
3939
n -> :(x.$n == y.$n),
4040
(a, b) -> :($a && $b),
@@ -321,7 +321,7 @@ function compareFactor(
321321
:attributes,
322322
:solverData,
323323
:observation,
324-
:computeMem,
324+
:workmem,
325325
:_variableOrderSymbols,
326326
:_gradients,
327327
],

src/services/DFGFactor.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,20 @@ getFactorType(f::FactorDFG) = getTypeFromSerializationModule(f.fnctype)() # TODO
3939
getFactorType(dfg::AbstractDFG, lbl::Symbol) = getFactorType(getFactor(dfg, lbl))
4040

4141
getState(f::AbstractDFGFactor) = f.state
42+
43+
getFactorState(f::AbstractDFGFactor) = f.state
44+
getFactorState(dfg::AbstractDFG, lbl::Symbol) = getFactorState(getFactor(dfg, lbl))
45+
4246
getObservation(f::FactorCompute) = f.observation
4347
function getObservation(f::FactorDFG)
4448
#FIXME completely refactor to not need getTypeFromSerializationModule and just use StructTypes
4549
packtype = DFG.getTypeFromSerializationModule("Packed" * f.fnctype)
4650
return packtype(; JSON3.read(f.observJSON)...)
4751
end
4852

53+
getWorkmem(f::FactorCompute) = f.workmem[]
54+
setWorkmem!(f::FactorCompute, workmem::FactorOperationalMemory) = f.workmem[] = workmem
55+
4956
"""
5057
$SIGNATURES
5158
@@ -155,12 +162,12 @@ end
155162
function setTimestamp(f::FactorCompute, ts::ZonedDateTime)
156163
return FactorCompute(
157164
f.label,
158-
ts,
159-
f.nstime,
160-
f.tags,
161-
f.solverData,
162-
f.solvable,
163-
getfield(f, :_variableOrderSymbols);
165+
getfield(f, :_variableOrderSymbols),
166+
f.solverData;
167+
timestamp = ts,
168+
nstime = f.nstime,
169+
tags = f.tags,
170+
solvable = f.solvable,
164171
id = f.id,
165172
)
166173
end

src/services/Serialization.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,23 +403,23 @@ function unpackFactor(dfg::AbstractDFG, factor::FactorDFG; skipVersionCheck::Boo
403403
metadata = JSON3.read(base64decode(factor.metadata), Dict{Symbol, DFG.SmallDataTypes})
404404

405405
if fullFactorData.fnc isa FactorOperationalMemory
406-
computeMem = Ref(fullFactorData.fnc)
406+
workmem = Ref(fullFactorData.fnc)
407407
else
408-
computeMem = Ref{FactorOperationalMemory}()
408+
workmem = Ref{FactorOperationalMemory}()
409409
end
410410
return FactorCompute(
411411
factor.label,
412-
factor.timestamp,
413-
Nanosecond(factor.nstime),
414-
Set(factor.tags),
415-
fullFactorData,
416-
factor.solvable,
417-
Tuple(factor._variableOrderSymbols);
412+
Tuple(factor._variableOrderSymbols),
413+
observation,
414+
factor.state,
415+
workmem;
416+
solverData = fullFactorData,
417+
nstime = Nanosecond(factor.nstime),
418+
tags = Set(factor.tags),
419+
solvable = factor.solvable,
420+
timestamp = factor.timestamp,
418421
id = factor.id,
419422
smallData = metadata,
420-
state = factor.state,
421-
observation,
422-
computeMem,
423423
)
424424
end
425425

test/testBlocks.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,12 @@ function VariablesandFactorsCRUD_SET!(fg, v1, v2, v3, f0, f1, f2)
493493
if f2 isa FactorCompute
494494
f2_mod = FactorCompute(
495495
f2.label,
496-
f2.timestamp,
497-
f2.nstime,
498-
f2.tags,
499-
f2.solverData,
500-
f2.solvable,
501496
(:a,),
497+
f2.solverData;
498+
timestamp = f2.timestamp,
499+
nstime = f2.nstime,
500+
tags = f2.tags,
501+
solvable = f2.solvable,
502502
)
503503
else
504504
f2_mod = deepcopy(f2)

0 commit comments

Comments
 (0)