Skip to content

Write guide for parallel processing trials. #209

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ jobs:
- name: Run docts
run: |
python doctests.py

- name: Test parallel processing script
run: |
python docs/_static/script_for_parallel_processing/main.py
3 changes: 2 additions & 1 deletion docs/Guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Contents:
deadlock.rst
process_based.rst
from_file.rst
behaviour/index.rst
behaviour/index.rst
parallel_process.rst
59 changes: 59 additions & 0 deletions docs/Guides/parallel_process.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.. _parallel_process:

=========================
How to Parallelise Trials
=========================

It is possible to repeat a simulation in parallel using the cores available on a
given computer. This can lead to decreases in computational time as instead of
running each successive simulation :ref:`one after the other <tutorial-iv>` they
can be run at the same time.

As an example consider the following simulation network::

>>> import ciw
>>> N = ciw.create_network(arrival_distributions=[ciw.dists.Exponential(rate=0.2)],
... service_distributions=[ciw.dists.Exponential(rate=0.1)],
... number_of_servers=[3])

The following function will return the mean wait time::

>>> def get_mean_wait(network, seed=0, max_time=10000):
... """Return the mean waiting time for a given network"""
... ciw.seed(seed)
... Q = ciw.Simulation(N)
... Q.simulate_until_max_time(max_simulation_time=max_time)
... recs = Q.get_all_records()
... waits = [r.waiting_time for r in recs]
... mean_wait = sum(waits) / len(waits)
... return mean_wait
>>> get_mean_wait(network=N)
3.386690...

To be able to better approximate the average wait, the above function will be
repeated and the average taken::

>>> import numpy as np
>>> max_time = 500
>>> repetitions = 200
>>> np.mean([get_mean_wait(network=N, max_time=max_time, seed=seed) for seed in range(repetitions)])
3.762233...

To obtain the above by running 2 simulations at the same time (assuming that 2
cores are available), the :code:`multiprocessing` library can be used. In which
case the following :download:`main.py
<../_static/script_for_parallel_processing/main.py>` script gives a working
example:

.. literalinclude:: ../_static/script_for_parallel_processing/main.py

It is possible to use :code:`multiprocessing.cpu_count()` to obtain the number
of available cores.

Note that the conditional :code:`if __name__ == '__main__':` is needed to ensure
that :code:`get_mean_wait` can be pickled. This is necessary to ensure that it
can be used by the parallel processing pool.

The :code:`multiprocessing` library is part of the python standard library so no
further dependencies are required. However other options are available, one
example of which is `dask <https://www.dask.org>`_.
5 changes: 5 additions & 0 deletions docs/_static/script_for_parallel_processing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This directory contains a script for carrying out parallel processing with ciw.

This is referred to in `./docs/Guides/parallel_process.rst`.

This script is shown in the docs but is also tested as part of the CI.
30 changes: 30 additions & 0 deletions docs/_static/script_for_parallel_processing/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ciw
import multiprocessing
import numpy as np

N = ciw.create_network(
arrival_distributions=[ciw.dists.Exponential(rate=0.2)],
service_distributions=[ciw.dists.Exponential(rate=0.1)],
number_of_servers=[3],
)

max_time = 500
repetitions = 200


def get_mean_wait(network, seed=0, max_time=10000):
"""Return the mean waiting time for a given network"""
ciw.seed(seed)
Q = ciw.Simulation(N)
Q.simulate_until_max_time(max_simulation_time=max_time)
recs = Q.get_all_records()
waits = [r.waiting_time for r in recs]
mean_wait = sum(waits) / len(waits)
return mean_wait


if __name__ == "__main__":
pool = multiprocessing.Pool(processes=2)
args = [(N, seed, max_time) for seed in range(repetitions)]
waits = pool.starmap(get_mean_wait, args)
print(np.mean(waits))