Skip to content

Commit

Permalink
Add default hash function for reco parameter
Browse files Browse the repository at this point in the history
nHackel committed Dec 13, 2023
1 parent b3e4942 commit e766439
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/AbstractImageReconstruction.jl
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ using ThreadPools
using Scratch
using RegularizedLeastSquares

import Base: put!, take!, fieldtypes, fieldtype, ismissing, propertynames, parent
import Base: put!, take!, fieldtypes, fieldtype, ismissing, propertynames, parent, hash

include("AlgorithmInterface.jl")
include("StructTransforms.jl")
35 changes: 32 additions & 3 deletions src/RecoPlans/Cache.jl
Original file line number Diff line number Diff line change
@@ -4,10 +4,26 @@ Base.@kwdef mutable struct CachedProcessParameter{P <: AbstractImageReconstructi
cache::Dict{UInt64, Any} = Dict{UInt64, Any}()
lock::ReentrantLock = ReentrantLock()
end
function process(algo, param::CachedProcessParameter, inputs...)
function process(algo::AbstractImageReconstructionAlgorithm, param::CachedProcessParameter, inputs...)
lock(param.lock) do
id = hash(param, hash(inputs))
result = get(param.cache, id, process(algo, param.param, inputs...))
id = hash(param.param, hash(inputs))
if haskey(param.cache, id)
result = param.cache[id]
else
result = process(algo, param.param, inputs...)
end
param.cache[id] = result
return result
end
end
function process(algo::Type{<:AbstractImageReconstructionAlgorithm}, param::CachedProcessParameter, inputs...)
lock(param.lock) do
id = hash(param.param, hash(inputs))
if haskey(param.cache, id)
result = param.cache[id]
else
result = process(algo, param.param, inputs...)
end
param.cache[id] = result
return result
end
@@ -38,4 +54,17 @@ function Base.empty!(cache::CachedProcessParameter)
lock(cache.lock) do
empty!(cache.cache)
end
end

"""
hash(parameter::AbstractImageReconstructionParameters, h)
Default hash function for image reconstruction paramters. Uses `nameof` the parameter and all fields not starting with `_` to compute the hash.
"""
function Base.hash(parameter::T, h::UInt64) where T <: AbstractImageReconstructionParameters
h = hash(nameof(T), h)
for field in filter(f -> !startswith(string(f), "_"), fieldnames(T))
h = hash(hash(getproperty(parameter, field)), h)
end
return h
end

0 comments on commit e766439

Please sign in to comment.