Skip to content

Commit

Permalink
Fix Threaded simulate to thread over chunks instead of replications (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Jan 28, 2025
1 parent d71c347 commit a926abe
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/plugins/parallel_schemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,18 @@ function master_loop(
return status
end

function _chunk_split(num_items, num_chunks)
w = div(num_items, num_chunks)
chunks = UnitRange{Int}[]
offset = 0
for i in 1:num_chunks-1
push!(chunks, (offset+1):(offset+w))
offset += w
end
push!(chunks, offset:num_items)
return chunks
end

function _simulate(
model::PolicyGraph,
::Threaded,
Expand All @@ -426,8 +438,13 @@ function _simulate(
)
_initialize_solver(model; throw_error = false)
ret = Vector{Vector{Dict{Symbol,Any}}}(undef, number_replications)
@sync for i in 1:number_replications
Threads.@spawn ret[i] = _simulate(model, variables; kwargs...)
chunks = _chunk_split(number_replications, Threads.nthreads())
# Limit the number of Threads that we `@spawn` to the number of threads.
# Naively spawning `number_replications` threads can slow down the scheduler.
@sync for chunk in chunks
Threads.@spawn for j in chunk
ret[j] = _simulate(model, variables; kwargs...)
end
end
return ret
end

0 comments on commit a926abe

Please sign in to comment.