-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run model(s) in parallel using dask (#119)
* add and document API * implement run batch in parallel (+ tests) * simplify model context manager * fix pickle process classes See cloudpipe/cloudpickle#320 python-attrs/attrs#458 * fix zarr in-memory store and dask processes * disable dask parallel schedulers on CI Is it really supported? * get a dask lock for create / resize zarr datasets * clean-up * add test for DummyLock * run model processes in parallel + more docstrings * update release notes * docstrings tweaks * doc: add run parallel section
- Loading branch information
Showing
20 changed files
with
417 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
.. _run_parallel: | ||
|
||
Run Model(s) in Parallel | ||
======================== | ||
|
||
xarray-simlab allows running one or more models in parallel via the Dask_ | ||
library. There are two parallel modes: | ||
|
||
- run one simulation in parallel (single-model parallelism) | ||
- run a batch of simulations in parallel (multi-model parallelism) | ||
|
||
.. warning:: | ||
|
||
This is an experimental feature. | ||
|
||
.. note:: | ||
|
||
Dask is a versatile library that provides many ways of executing tasks in | ||
parallel (i.e., threads vs. processes, single machine vs. distributed | ||
environments). xarray-simlab lets you choose which alternative best suits | ||
your needs. Beware, however, that not all alternatives are optimal or | ||
supported depending on your case. More details below. | ||
|
||
.. _Dask: https://docs.dask.org/en/latest/ | ||
|
||
.. _run_parallel_single: | ||
|
||
Single-model parallelism | ||
------------------------ | ||
|
||
This mode runs each process in a model in parallel. | ||
|
||
A :class:`~xsimlab.Model` object can be viewed as a Directed Acyclic Graph (DAG) | ||
built from a collection of processes (i.e., process-decorated classes). At each | ||
simulation stage, a task graph is built from this graph, which is then executed | ||
by one of the schedulers available in Dask. | ||
|
||
To activate this parallel mode, simply set ``parallel=True`` when calling | ||
:func:`xarray.Dataset.xsimlab.run`: | ||
|
||
.. code:: python | ||
>>> in_ds.xsimlab.run(model=my_model, parallel=True) | ||
The default Dask scheduler used here is ``"threads"`` (this is the one used by | ||
``dask.delayed``). Other schedulers may be selected via the ``scheduler`` | ||
argument of :func:`~xarray.Dataset.xsimlab.run`. Dask also supports other ways to | ||
select a scheduler, see `here | ||
<https://docs.dask.org/en/latest/setup/single-machine.html>`_. | ||
|
||
Note, however, that multi-processes schedulers are not supported for this mode, | ||
since simulation active data (shared between all model components) is stored | ||
using a simple Python dictionary. | ||
|
||
Note also that the code in the process-decorated classes must be thread-safe | ||
and should release the CPython's Global Interpreter Lock (GIL) as much as | ||
possible in order to see a gain in performance. For example, most Numpy | ||
functions release the GIL. | ||
|
||
The gain in performance compared to sequential execution of the model processes | ||
will also depend on how the DAG is structured, i.e., how many processes can be | ||
executed in parallel. | ||
|
||
.. _run_parallel_multi: | ||
|
||
Multi-models parallelism | ||
------------------------ | ||
|
||
This mode runs multiple simulations in parallel, using the same model but | ||
different input values. | ||
|
||
.. note:: | ||
|
||
This mode should scale well from a few dozen to a few thousand of | ||
simulations but it has not been tested yet beyond that level. | ||
|
||
.. note:: | ||
|
||
It may not work well with dynamic-sized arrays. | ||
|
||
This parallel mode is automatically selected when a batch dimension label is set | ||
while calling :func:`xarray.Dataset.xsimlab.run` (see Section | ||
:ref:`run_batch`). You still need to explicitly set ``Parallel=True``: | ||
|
||
.. code:: python | ||
>>> in_ds.xsimlab.run(model=my_model, batch_dim="batch", parallel=True) | ||
As opposed to single-model parallelism, both multi-threads and multi-processes | ||
Dask schedulers are supported for this embarrassingly parallel problem. | ||
|
||
If you use a multi-threads scheduler, the same precautions apply regarding | ||
thread-safety and CPython's GIL. | ||
|
||
If you use a multi-processes scheduler, beware of the following: | ||
|
||
- The code in the process-decorated classes must be serializable. | ||
- Not all Zarr stores are supported for model outputs, see `Zarr's documentation | ||
<https://zarr.readthedocs.io/en/stable/api/storage.html>`_. For example, the | ||
default in-memory store is not supported. See Section :ref:`io_storage_zarr` | ||
on how to specify an alternative store. | ||
- By default, the chunk size of Zarr datasets along the batch dimension is equal | ||
to 1 in order to prevent race conditions during parallel writes. This might | ||
not be optimal for further post-processing, though. It is possible to override | ||
this default and set larger chunk sizes (via the ``encoding`` parameter of | ||
:func:`~xarray.Dataset.xsimlab.run`), but then you should also use one of the | ||
Zarr's synchronizers (either :class:`zarr.sync.ThreadSynchronizer` or | ||
:class:`zarr.sync.ProcessSynchronizer`) to ensure that all output values will | ||
be properly saved. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.