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

Trial using threading to run events simultaneously #1327

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Progress bar fixed for threaded simulations
willGraham01 committed Apr 30, 2024
commit f63536ccc6168c2c7af8183c882440338bc20238
14 changes: 6 additions & 8 deletions src/tlo/threaded_simulation.py
Original file line number Diff line number Diff line change
@@ -129,9 +129,6 @@ def __init__(self, n_threads: int = 1, **kwargs) -> None:
# Initialise as you would for any other simulation
super().__init__(**kwargs)

# Progress bar currently not supported
self.show_progress_bar = False

# Setup the thread controller
self.thread_controller = ThreadController(n_threads=n_threads, name = "EventWorker-")

@@ -191,23 +188,23 @@ def step_through_events(self) -> None:

# Whilst the event queue is not empty
while self.event_queue:
event, date = self.event_queue.next_event()
event, date_of_next_event = self.event_queue.next_event()
self.update_progress_bar(self.date)

# If the simulation should end, escape
if date >= self.end_date:
if date_of_next_event >= self.end_date:
break
# If we want to advance time, we need to ensure that
# the worker queue. Otherwise, a worker might be running an
# event from the previous date but may still call sim.date
# to get the "current" time, which would then be out-of-sync.
elif date > self.date:
elif date_of_next_event > self.date:
# This event moves time forward, wait until all jobs
# from the current date have finished before advancing time
self.wait_for_workers()
# All jobs from the previous day have ended.
# Advance time and continue.
self.date = date
self.update_progress_bar(self.date)
self.date = date_of_next_event

# Next, determine if the event to be run can be delegated to the
# worker pool.
@@ -223,6 +220,7 @@ def step_through_events(self) -> None:
# We may have exhausted all the events in the queue, but the workers will
# still need time to process them all!
self.wait_for_workers()
self.update_progress_bar(date_of_next_event)

def wait_for_workers(self) -> None:
"""