Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CPT #1615

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 69 additions & 101 deletions src/entities/FactorOperationalMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,73 +22,41 @@ DevNotes

Related

[`CalcFactorMahalanobis`](@ref), [`CommonConvWrapper`](@ref), [`ConvPerThread`](@ref)
[`CalcFactorMahalanobis`](@ref), [`CommonConvWrapper`](@ref)
"""
struct CalcFactor{T <: AbstractFactor, P <: Union{<:Tuple,Nothing,AbstractVector}, X, C}
""" the interface compliant user object functor containing the data and logic """
factor::T
""" what is the sample (particle) id for which the residual is being calculated """
_sampleIdx::Int
""" legacy support when concerned with how many measurement tuple elements are used by user """
_measCount::Int
""" legacy suport for measurement sample values of old functor residual functions """
_legacyMeas::P
""" legacy support for variable values old functor residual functions """
_legacyParams::X
""" allow threading for either sampling or residual calculations (workaround for thread yield issue) """
_allowThreads::Bool
""" user cache of arbitrary type, overload the [`preambleCache`](@ref) function. NOT YET THREADSAFE """
cache::C

## TODO Consolidation WIP with FactorMetadata
# full list of variables connected to the factor
fullvariables::Vector{DFGVariable}
# which index is being solved for?
solvefor::Int
struct CalcFactor{T<:AbstractFactor,P<:Union{<:Tuple,Nothing,AbstractVector},X,C}
""" the interface compliant user object functor containing the data and logic """
factor::T
""" what is the sample (particle) id for which the residual is being calculated """
_sampleIdx::Int
""" legacy support when concerned with how many measurement tuple elements are used by user """
_measCount::Int
""" legacy suport for measurement sample values of old functor residual functions """
_legacyMeas::P
""" legacy support for variable values old functor residual functions """
_legacyParams::X
""" allow threading for either sampling or residual calculations (workaround for thread yield issue) """
_allowThreads::Bool
""" user cache of arbitrary type, overload the [`preambleCache`](@ref) function. NOT YET THREADSAFE """
cache::C

## TODO Consolidation WIP with FactorMetadata
# full list of variables connected to the factor
fullvariables::Vector{DFGVariable}
# which index is being solved for?
solvefor::Int
end



abstract type _AbstractThreadModel end

"""
$(TYPEDEF)
"""
struct SingleThreaded <: _AbstractThreadModel
end
struct SingleThreaded <: _AbstractThreadModel end
"""
$(TYPEDEF)
"""
struct MultiThreaded <: _AbstractThreadModel
end



"""
$(TYPEDEF)

DevNotes
- FIXME consolidate with CalcFactor
- TODO consolidate with CCW, FMd, CalcFactor
- TODO consider renaming `.p` to `.decisionDims`
- TODO `.decisionDims::DD where DD <: Union{<:AbstractVector{Int},Colon}` -- ensure type stability
- TODO figure out if we want static parameter THRID
- TODO make static params {XDIM, ZDIM, P}
- TODO make immutable
"""
mutable struct ConvPerThread{R,P}
thrid_::Int
# the actual particle being solved at this moment
particleidx::Int
# subsection indices to select which params should be used for this hypothesis evaluation
activehypo::Vector{Int}
# slight numerical perturbation for degenerate solver cases such as division by zero
perturb::Vector{Float64}
# working memory location for optimization routines on target decision variables
X::Vector{P}
# working memory to store residual for optimization routines
res::R # was Vector{Float64}
end
struct MultiThreaded <: _AbstractThreadModel end



Expand All @@ -109,52 +77,52 @@ DevNotes

Related

[`CalcFactor`](@ref), [`CalcFactorMahalanobis`](@ref), [`ConvPerThread`](@ref)
[`CalcFactor`](@ref), [`CalcFactorMahalanobis`](@ref)
"""
mutable struct CommonConvWrapper{ T<:AbstractFactor,
H<:Union{Nothing, Distributions.Categorical},
C<:Union{Nothing, Vector{Int}},
NTP <: Tuple,
G,
MT,
CT} <: FactorOperationalMemory
#
### Values consistent across all threads during approx convolution
usrfnc!::T # user factor / function
# general setup
xDim::Int
zDim::Int
# is this a partial constraint as defined by the existance of factor field `.partial::Tuple`
partial::Bool
# multi hypothesis settings
hypotheses::H
# categorical to select which hypothesis is being considered during convolution operation
certainhypo::C
nullhypo::Float64
# parameters passed to each hypothesis evaluation event on user function, #1321
params::NTP # TODO rename to varValsLink::NTP
# which index is being solved for in params?
varidx::Int
# FIXME make type stable, JT should now be type stable if rest works
# user defined measurement values for each approxConv operation
measurement::Vector{MT}
# TODO refactor and deprecate this old approach, Union{Type{SingleThreaded}, Type{MultiThreaded}}
threadmodel::Type{<:_AbstractThreadModel}
# FIXME, deprecate for only `readonly` and build CalcFactor objects on stack instead
## will be obsolete: particular convolution computation values per particle idx (varies by thread)
cpt::Vector{<:ConvPerThread}
# inflationSpread
inflation::Float64
# Which dimensions does this factor influence. Sensitive (mutable) to both which 'solvefor index' variable and whether the factor is partial dimension
partialDims::Vector{<:Integer}
# variable types for points in params
vartypes::Vector{DataType}
# experimental feature to embed gradient calcs with ccw
_gradients::G
# type used for cache
dummyCache::CT
#
fullvariables::Vector{DFGVariable}
mutable struct CommonConvWrapper{T<:AbstractFactor,NTP<:Tuple,G,MT,CT} <: FactorOperationalMemory
#
### Values consistent across all threads during approx convolution
usrfnc!::T # user factor / function
# general setup
xDim::Int
zDim::Int
# is this a partial constraint as defined by the existance of factor field `.partial::Tuple`
partial::Bool
# multi hypothesis settings #NOTE no need for a parameter as type is known from `parseusermultihypo`
hypotheses::Union{Nothing,Distributions.Categorical{Float64,Vector{Float64}}}
# categorical to select which hypothesis is being considered during convolution operation
certainhypo::Union{Nothing,Vector{Int}}
nullhypo::Float64
# parameters passed to each hypothesis evaluation event on user function, #1321
params::NTP # TODO rename to varValsLink::NTP
# which index is being solved for in params?
varidx::Int
# FIXME make type stable, JT should now be type stable if rest works
# user defined measurement values for each approxConv operation
measurement::Vector{MT}
# TODO refactor and deprecate this old approach, Union{Type{SingleThreaded}, Type{MultiThreaded}}
threadmodel::Type{<:_AbstractThreadModel}
# inflationSpread
inflation::Float64
# Which dimensions does this factor influence. Sensitive (mutable) to both which 'solvefor index' variable and whether the factor is partial dimension
partialDims::Vector{<:Integer}
# variable types for points in params
vartypes::Vector{DataType}
# experimental feature to embed gradient calcs with ccw
_gradients::G
# type used for cache
dummyCache::CT

#Consolidation from FMD
fullvariables::Vector{DFGVariable}

#Consolidation from CPT
# the actual particle being solved at this moment
particleidx::Int
dehann marked this conversation as resolved.
Show resolved Hide resolved
# subsection indices to select which params should be used for this hypothesis evaluation
activehypo::Vector{Int}
# working memory to store residual for optimization routines
res::Vector{Float64}
end


Expand Down
49 changes: 10 additions & 39 deletions src/services/CalcFactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,40 +188,21 @@ end
## FactorOperationalMemory helper constructors
## =============================================================================================


function ConvPerThread( X::AbstractVector{P},
zDim::Int;
particleidx::Int=1,
activehypo= 1:length(params),
perturb=zeros(zDim),
res=zeros(zDim),
thrid_ = 0 ) where P
#
return ConvPerThread{typeof(res), Any}( thrid_,
particleidx,
Int[activehypo;],
perturb,
X,
res )
end


function CommonConvWrapper( usrfnc::T,
X::AbstractVector{P},
X::AbstractVector{P}, #TODO remove X completely
zDim::Int,
varValsLink::Tuple,
fullvariables::Vector{DFGVariable};
partial::Bool=false,
hypotheses::H=nothing,
certainhypo=nothing,
activehypo= 1:length(varValsLink),
activehypo=collect(1:length(varValsLink)),
nullhypo::Real=0,
varidx::Int=1,
measurement::AbstractVector=Vector(Vector{Float64}(),),
particleidx::Int=1,
xDim::Int=size(X,1),
partialDims::AbstractVector{<:Integer}=1:length(X),
perturb=zeros(zDim),
res::AbstractVector{<:Real}=zeros(zDim),
threadmodel::Type{<:_AbstractThreadModel}=MultiThreaded,
inflation::Real=3.0,
Expand All @@ -241,15 +222,16 @@ function CommonConvWrapper( usrfnc::T,
varidx,
measurement,
threadmodel,
(i->ConvPerThread(X, zDim, particleidx=particleidx,
activehypo=activehypo,
perturb=perturb, res=res )).(1:Threads.nthreads()),
inflation,
partialDims,
DataType[vartypes...],
gradients,
userCache,
fullvariables )
fullvariables,
particleidx,
activehypo,
res,
)
end


Expand Down Expand Up @@ -345,19 +327,6 @@ function _setCCWDecisionDimsConv!(ccwl::Union{CommonConvWrapper{F},
nothing
end


function _updateCPTs!(ccwl, sfidx)
for thrid in 1:Threads.nthreads()
cpt_ = ccwl.cpt[thrid]
cpt_.X = ccwl.params[sfidx]
# used in ccw functor for AbstractRelativeMinimize
# TODO JT - Confirm it should be updated here. Testing in _prepCCW
resize!(cpt_.res, ccwl.zDim)
fill!(cpt_.res, 0.0)
end
nothing
end

function attemptGradientPrep( varTypes, usrfnc, varParamsAll, multihypo, meas_single, _blockRecursion )
# prepare new cached gradient lambdas (attempt)
try
Expand Down Expand Up @@ -525,7 +494,9 @@ function _updateCCW!( F_::Type{<:AbstractRelative},
end

# set each CPT
_updateCPTs!(ccwl, sfidx)
# used in ccw functor for AbstractRelativeMinimize
resize!(ccwl.res, ccwl.zDim)
fill!(ccwl.res, 0.0)

# calculate new gradients perhaps
# J = ccwl.gradients(measurement..., pts...)
Expand Down
5 changes: 1 addition & 4 deletions src/services/DeconvUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@ function approxDeconv(fcto::DFGFactor,

for idx in 1:N
# towards each particle in their own thread (not 100% ready yet, factors should be separate memory)
thrid = Threads.threadid()
cpt_ = ccw.cpt[thrid]
targeti_ = makeTarget(idx)

# TODO must first resolve hypothesis selection before unrolling them -- deferred #1096
cpt_.activehypo = hyporecipe.activehypo[2][2]
ccw.activehypo = hyporecipe.activehypo[2][2]

onehypo!, _ = _buildCalcFactorLambdaSample( ccw,
idx,
cpt_,
targeti_,
measurement )
#
Expand Down
20 changes: 10 additions & 10 deletions src/services/EvalFactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ function approxConvOnElements!( ccwl::Union{CommonConvWrapper{F},
::Type{<:MultiThreaded},
_slack=nothing ) where {N_,F<:AbstractRelative,S,T}
#
Threads.@threads for n in elements
# ccwl.thrid_ = Threads.threadid()
ccwl.cpt[Threads.threadid()].particleidx = n
error("MultiThreaded `approxConvOnElements!` is depricated and will soon be replaced")
# Threads.@threads for n in elements
# # ccwl.thrid_ = Threads.threadid()
# ccwl.cpt[Threads.threadid()].particleidx = n

# ccall(:jl_, Nothing, (Any,), "starting loop, thrid_=$(Threads.threadid()), partidx=$(ccwl.cpt[Threads.threadid()].particleidx)")
_solveCCWNumeric!( ccwl, _slack=_slack)
end
nothing
# # ccall(:jl_, Nothing, (Any,), "starting loop, thrid_=$(Threads.threadid()), partidx=$(ccwl.cpt[Threads.threadid()].particleidx)")
# _solveCCWNumeric!( ccwl, _slack=_slack)
# end
# nothing
end


Expand All @@ -36,7 +37,7 @@ function approxConvOnElements!( ccwl::Union{CommonConvWrapper{F},
_slack=nothing ) where {N_,F<:AbstractRelative,S,T}
#
for n in elements
ccwl.cpt[Threads.threadid()].particleidx = n
ccwl.particleidx = n
_solveCCWNumeric!( ccwl, _slack=_slack)
end
nothing
Expand Down Expand Up @@ -170,7 +171,6 @@ function computeAcrossHypothesis!(ccwl::Union{<:CommonConvWrapper{F},
activehypo = hyporecipe.activehypo
certainidx = hyporecipe.certainidx

cpt_ = ccwl.cpt[Threads.threadid()]

@assert ccwl.certainhypo == hyporecipe.certainidx "expected hyporecipe.certainidx to be the same as cached in ccw"
for (hypoidx, vars) in activehypo
Expand All @@ -179,7 +179,7 @@ function computeAcrossHypothesis!(ccwl::Union{<:CommonConvWrapper{F},
# now do hypothesis specific
if sfidx in certainidx && hypoidx != 0 || hypoidx in certainidx || hypoidx == sfidx
# hypo case hypoidx, sfidx = $hypoidx, $sfidx
for i in 1:Threads.nthreads() ccwl.cpt[i].activehypo = vars; end
for i in 1:Threads.nthreads() ccwl.activehypo = vars; end

addEntr = view(ccwl.params[sfidx], allelements[count])
# dynamic estimate with user requested speadNH of how much noise to inject (inflation or nullhypo)
Expand Down
Loading