Skip to content

Commit

Permalink
Support multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Apr 26, 2024
1 parent 6826952 commit 48d523d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SpineInterface"
uuid = "0cda1612-498a-11e9-3c92-77fa82595a4f"
authors = ["Spine Project consortium <[email protected]>"]
version = "0.13.4"
version = "0.13.5"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
10 changes: 6 additions & 4 deletions src/api/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ _find_rels(rc, rows) = (rc.relationships[row] for row in rows)
_find_rels(rc, ::Anything) = rc.relationships

function _find_rows(rc; kwargs...)
memoized_rows = get!(rc.row_map, rc.name, Dict())
get!(memoized_rows, kwargs) do
_do_find_rows(rc; kwargs...)
lock(rc.row_map_lock) do
memoized_rows = get!(rc.row_map, rc.name, Dict())
get!(memoized_rows, kwargs) do
_do_find_rows(rc; kwargs...)
end
end
end

Expand Down Expand Up @@ -714,7 +716,7 @@ function with_env(f::Function, env::Symbol)
prev_env = _active_env()
_activate_env(env)
try
f()
return f()
finally
_activate_env(prev_env)
end
Expand Down
17 changes: 14 additions & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ struct TimeSlice
id::UInt64
actual_duration::Union{Dates.CompoundPeriod,Period}
updates::OrderedDict
updates_lock::ReentrantLock
function TimeSlice(start, end_, duration, blocks)
start > end_ && error("out of order")
blocks = isempty(blocks) ? () : Tuple(sort(collect(blocks)))
id = objectid((start, end_, duration, blocks))
actual_duration = canonicalize(end_ - start)
new(Ref(start), Ref(end_), duration, blocks, id, actual_duration, OrderedDict())
new(Ref(start), Ref(end_), duration, blocks, id, actual_duration, OrderedDict(), ReentrantLock())
end
end

Expand Down Expand Up @@ -119,11 +120,21 @@ struct _RelationshipClass
parameter_values::Dict{ObjectTupleLike,Dict{Symbol,ParameterValue}}
parameter_defaults::Dict{Symbol,ParameterValue}
row_map::Dict
row_map_lock::ReentrantLock
_split_kwargs::Ref{Any}
function _RelationshipClass(name, intact_cls_names, object_tuples, vals=Dict(), defaults=Dict())
cls_names = _fix_name_ambiguity(intact_cls_names)
row_map = Dict()
rc = new(name, intact_cls_names, cls_names, [], vals, defaults, row_map, _make_split_kwargs(cls_names))
rc = new(
name,
intact_cls_names,
cls_names,
[],
vals,
defaults,
Dict(),
ReentrantLock(),
_make_split_kwargs(cls_names),
)
rels = [(; zip(cls_names, objects)...) for objects in object_tuples]
_append_relationships!(rc, rels)
rc
Expand Down
16 changes: 13 additions & 3 deletions src/update_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ import .JuMP: MOI, MOIU, MutableArithmetics

_Constant = Union{Number,UniformScaling}

const _si_ext_lock = ReentrantLock()

struct SpineInterfaceExt
lower_bound::Dict{VariableRef,Any}
upper_bound::Dict{VariableRef,Any}
fixer::Dict{VariableRef,Any}
SpineInterfaceExt() = new(Dict(), Dict(), Dict())
end

function _get_si_ext!(m)
lock(_si_ext_lock) do
get!(m.ext, :spineinterface) do
SpineInterfaceExt()
end
end
end

JuMP.copy_extension_data(data::SpineInterfaceExt, new_model::AbstractModel, model::AbstractModel) = nothing

abstract type _CallSet <: MOI.AbstractScalarSet end
Expand Down Expand Up @@ -141,7 +151,7 @@ function _set_lower_bound(var, lb)
if is_fixed(var)
# Save bound
m = owner_model(var)
ext = get!(m.ext, :spineinterface, SpineInterfaceExt())
ext = _get_si_ext!(m)
ext.lower_bound[var] = lb
elseif !isnan(lb)
set_lower_bound(var, lb)
Expand All @@ -164,7 +174,7 @@ function _set_upper_bound(var, ub)
if is_fixed(var)
# Save bound
m = owner_model(var)
ext = get!(m.ext, :spineinterface, SpineInterfaceExt())
ext = _get_si_ext!(m)
ext.upper_bound[var] = ub
elseif !isnan(ub)
set_upper_bound(var, ub)
Expand All @@ -189,7 +199,7 @@ _fix(_upd, ::Nothing) = nothing
function _fix(upd, fix_value)
var = upd.variable
m = owner_model(var)
ext = get!(m.ext, :spineinterface, SpineInterfaceExt())
ext = _get_si_ext!(m)
if !isnan(fix_value)
# Save bounds, remove them and then fix the value
if has_lower_bound(var)
Expand Down
4 changes: 3 additions & 1 deletion src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ function _refresh_metadata!(pval::ParameterValue)
end

function _add_update(t::TimeSlice, timeout, upd)
t.updates[upd] = timeout
lock(t.updates_lock) do
t.updates[upd] = timeout
end
end

function _append_relationships!(rc, rels)
Expand Down

0 comments on commit 48d523d

Please sign in to comment.