Trial using threading to run events simultaneously #1327
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proof of Concept
At present, the simulation runs by working through the event queue in serial, and the simulation is deemed complete when the date of the next event in the queue exceeds the end date of the simulation.
We should be able to use threading to run multiple events simultaneously in different threads; so long as the events that run are not going to interfere with each other (target the same patient, advance time before events the previous day finish, etc).
This PR implements
ThreadedSimulation
class. Essentially identical to the existingSimulation
class, but utilises a pool of threads to execute events in the simulation event queue simultaneously when possible. When an event is deemed unsafe to be run in parallel to others, the "threads" are allowed to finish working on the events they have already been given, then the unsafe event is run in the main thread, and then delegation of work is resumed.ThreadController
class. For handling the multiple threads that we'll need when running events simultaneously.Simulation
class into the_BaseSimulation
class, from whichSimulation
now inherits. This is to avoid a large amount of code repetition (as nowSimulation
andThreadedSimulation
can inherit rather than redefine).scale_run
profiling script to allow for the option of using aThreadedSimulation
.TODO
HealthCareSystem
population-level event is really what is slowing the simulation down. This population-level event also has a HSI-event-queue that it works through in serial, so we might be able to apply the same threading logic here to provide further speedup.