Skip to content

Commit

Permalink
MNT: improve process ordering for spawned workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
phmbressan committed Aug 23, 2024
1 parent 1e24643 commit d22c957
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions rocketpy/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ def __run_in_parallel(self, n_workers=None):
None
"""
if n_workers is None or n_workers > os.cpu_count():
# For Windows, the number of workers must be at most os.cpu_count() - 1
n_workers = os.cpu_count() - 1
n_workers = os.cpu_count()

Check warning on line 301 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L300-L301

Added lines #L300 - L301 were not covered by tests

if n_workers < 2:
raise ValueError("Number of workers must be at least 2 for parallel mode.")

Check warning on line 304 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L303-L304

Added lines #L303 - L304 were not covered by tests
Expand All @@ -321,6 +320,13 @@ def __run_in_parallel(self, n_workers=None):
processes = []
seeds = np.random.SeedSequence().spawn(n_workers - 1)

Check warning on line 321 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L320-L321

Added lines #L320 - L321 were not covered by tests

sim_consumer = multiprocess.Process(

Check warning on line 323 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L323

Added line #L323 was not covered by tests
target=self.__sim_consumer,
args=(export_queue, mutex, consumer_stop_event, simulation_error_event),
)

sim_consumer.start()

Check warning on line 328 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L328

Added line #L328 was not covered by tests

for seed in seeds:
sim_producer = multiprocess.Process(

Check warning on line 331 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L330-L331

Added lines #L330 - L331 were not covered by tests
target=self.__sim_producer,
Expand All @@ -337,13 +343,6 @@ def __run_in_parallel(self, n_workers=None):
for sim_producer in processes:
sim_producer.start()

Check warning on line 344 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L343-L344

Added lines #L343 - L344 were not covered by tests

sim_consumer = multiprocess.Process(
target=self.__sim_consumer,
args=(export_queue, mutex, consumer_stop_event, simulation_error_event),
)

sim_consumer.start()

try:
for sim_producer in processes:
sim_producer.join()

Check warning on line 348 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L346-L348

Added lines #L346 - L348 were not covered by tests
Expand Down Expand Up @@ -455,14 +454,18 @@ def __sim_consumer(
The event indicating that an error occurred during the simulation.
"""
trials = 0

Check warning on line 456 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L456

Added line #L456 was not covered by tests
while not stop_event.is_set() and not error_event.is_set():

while not error_event.is_set():
try:
mutex.acquire()
inputs_dict, outputs_dict = export_queue.get(timeout=3)

Check warning on line 461 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L458-L461

Added lines #L458 - L461 were not covered by tests

self.__export_flight_data(inputs_dict, outputs_dict)

Check warning on line 463 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L463

Added line #L463 was not covered by tests

except queue.Empty as exc:
if stop_event.is_set():
break

Check warning on line 467 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L465-L467

Added lines #L465 - L467 were not covered by tests

trials += 1

Check warning on line 469 in rocketpy/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/simulation/monte_carlo.py#L469

Added line #L469 was not covered by tests

if trials > 10:
Expand Down

0 comments on commit d22c957

Please sign in to comment.