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

Fix thread safety of AlternativeForwardPass #808

Merged
merged 4 commits into from
Nov 27, 2024
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
4 changes: 0 additions & 4 deletions src/algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1135,10 +1135,6 @@ function train(
# FIXME(odow): Threaded is broken for objective states
parallel_scheme = Serial()
end
if forward_pass isa AlternativeForwardPass
# FIXME(odow): Threaded is broken for this forward pass
parallel_scheme = Serial()
end
if log_frequency <= 0
msg = "`log_frequency` must be at least `1`. Got $log_frequency."
throw(ArgumentError(msg))
Expand Down
13 changes: 9 additions & 4 deletions src/alternative_forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ Sustainable Energy. 13(1), 196-206.
struct AlternativeForwardPass{T} <: AbstractForwardPass
model::PolicyGraph{T}
forward_pass::AbstractForwardPass
lock::ReentrantLock

function AlternativeForwardPass(
model::PolicyGraph{T};
forward_pass::AbstractForwardPass = DefaultForwardPass(),
) where {T}
return new{T}(model, forward_pass)
return new{T}(model, forward_pass, ReentrantLock())
end
end

Expand All @@ -45,20 +46,24 @@ function forward_pass(
options::Options,
pass::AlternativeForwardPass{T},
) where {T}
# No need for locks here, delegate threadsafety to pass.forward_pass.
return forward_pass(pass.model, options, pass.forward_pass)
end

"""
AlternativePostIterationCallback(forward_model::PolicyGraph)

A post-iteration callback that should be used whenever [`SDDP.AlternativeForwardPass`](@ref)
is used.
A post-iteration callback that should be used whenever
[`SDDP.AlternativeForwardPass`](@ref) is used.
"""
struct AlternativePostIterationCallback{T}
model::PolicyGraph{T}
end

function (callback::AlternativePostIterationCallback)(result::IterationResult)
slave_update(callback.model, result)
# Only one thread is allowed to update the callback model at a time.
callback.lock() do
slave_update(callback.model, result)
odow marked this conversation as resolved.
Show resolved Hide resolved
end
return
end
Loading