Skip to content

Commit

Permalink
Throw warning if fewer nodes than threads
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jan 28, 2025
1 parent 91dceff commit 41f79da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/plugins/parallel_schemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,19 @@ function master_loop(
model::PolicyGraph{T},
options::Options,
) where {T}
max_threads = Threads.nthreads()
num_nodes = length(model.nodes)
if num_nodes < max_threads
@warn(
"There are fewer nodes in the graph ($num_nodes) than there are " *
"threads available ($max_threads). Limiting the number of " *
"threads to $num_nodes."
)
max_threads = num_nodes
end
_initialize_solver(model; throw_error = false)
keep_iterating, status = true, nothing
@sync for _ in 1:Threads.nthreads()
@sync for _ in 1:max_threads
Threads.@spawn begin
try
# This access of `keep_iterating` is not thread-safe, but it
Expand Down
23 changes: 23 additions & 0 deletions test/plugins/threaded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,26 @@ function test_threaded()
end

test_threaded()

function test_threaded_warning()
if Threads.nthreads() == 1
return # Skip this test if running in serial
end
model = SDDP.PolicyGraph(
SDDP.UnicyclicGraph(0.95);
sense = :Min,
lower_bound = 0.0,
optimizer = HiGHS.Optimizer,
) do sp, t
@variable(sp, 0 <= x <= 1, SDDP.State, initial_value = 1)
@stageobjective(sp, x.out)
return
end
@test_logs(
(:warn,),
SDDP.train(model; parallel_scheme = SDDP.Threaded()),
)
return
end

test_threaded_warning()

0 comments on commit 41f79da

Please sign in to comment.