Skip to content

Commit

Permalink
Remove multi CPU from batch_run intro tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed May 1, 2022
1 parent 6f3948e commit 604bd0f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 64 deletions.
33 changes: 3 additions & 30 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@
"\n",
"* `number_processes`\n",
"<br/><br/>\n",
"Number of processors used to run the sweep in parallel. Optional. If not specified, defaults to use all the available processors.\n",
"If not specified, defaults to 1. Set it to `None` to use all the available processors.\n",
"<br/><br/>\n",
"Note: Multiprocessing does make debugging challenging. If your parameter sweeps are resulting in unexpected errors set `number_processes = 1`.\n",
"<br/><br/>\n",
Expand Down Expand Up @@ -892,7 +892,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note for Windows OS users:** If you are running this tutorial in Jupyter, we recommend changing the the parameter that controls the number of processors you are using from `number_processes = None` (this uses all available processors) to `number_processes = 1.` More information on leveraging `batch_run`'s multiprocessing capability is below. "
"**Note for Windows OS users:** If you are running this tutorial in Jupyter, make sure that you set `number_processes = 1` (single process). If `number_processes` is greater than 1, it is less straightforward to set up. You can read [Mesa's collection of useful snippets](https://github.com/projectmesa/mesa/blob/main/docs/useful-snippets/snippets.rst), in 'Using multi-process `batch_run` on Windows' section for how to do it."
]
},
{
Expand All @@ -908,39 +908,12 @@
" parameters=params,\n",
" iterations=5,\n",
" max_steps=100,\n",
" number_processes=None,\n",
" number_processes=1,\n",
" data_collection_period=1,\n",
" display_progress=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you would still like to run your code in Jupyter your will still need to adjust the cell as noted above. Then you can**Note for Windows OS users:** You will have an issue with `batch_run` and `number_processes = None`. Your cell will show no progress, and in your terminal you will receive *AttributeError: Can't get attribute 'MoneyModel' on <module '__main__' (built-in)>*. One way to overcome this is to take your code outside of Jupyter and adjust the above code as follows. \n",
"\n",
"```\n",
"from multiprocessing import freeze_support\n",
"\n",
"params = {\"width\": 10, \"height\": 10, \"N\": range(10, 500, 10)}\n",
"\n",
"if __name__ == '__main__':\n",
" freeze_support()\n",
" results = batch_run(\n",
" MoneyModel,\n",
" parameters=params,\n",
" iterations=5,\n",
" max_steps=100,\n",
" number_processes=None,\n",
" data_collection_period=1,\n",
" display_progress=True,\n",
" )\n",
"```\n",
"\n",
"If you would still like to run your code in Jupyter you will need to adjust the cell as noted above. Then you can you can add the [nbmultitask library](https://nbviewer.org/github/micahscopes/nbmultitask/blob/39b6f31b047e8a51a0fcb5c93ae4572684f877ce/examples.ipynb) or look at this [stackoverflow](https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter). "
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
38 changes: 4 additions & 34 deletions docs/tutorials/intro_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ We call ``batch_run`` with the following arguments:
* ``number_processes``

Number of processors used to run the sweep in parallel. Optional.
If not specified, defaults to use all the available processors.
If not specified, defaults to 1. Set it to `None` to use all the available
processors.

Note: Multiprocessing does make debugging challenging. If your
parameter sweeps are resulting in unexpected errors set ``number_processes = 1``.
Expand Down Expand Up @@ -984,9 +985,7 @@ iteration).
.. code:: python
from mesa.batchrunner import batch_run
**Note for Windows OS users:** If you are running this tutorial in Jupyter, we recommend changing the the parameter
that controls the number of processors you are using from `number_processes = None` (this uses all available processors)
to `number_processes = 1`. More information on leveraging batch_run's multiprocessing capability is below.
**Note for Windows OS users:** If you are running this tutorial in Jupyter, make sure that you set `number_processes = 1` (single process). If `number_processes` is greater than 1, it is less straightforward to set up. You can read `Mesa's collection of useful snippets <https://github.com/projectmesa/mesa/blob/main/docs/useful-snippets/snippets.rst>`__, in "Using multi-process ```batch_run``` on Windows" section for how to do it.

.. code:: python
Expand All @@ -997,7 +996,7 @@ to `number_processes = 1`. More information on leveraging batch_run's multiproce
parameters=params,
iterations=5,
max_steps=100,
number_processes=None,
number_processes=1,
data_collection_period=1,
display_progress=True,
)
Expand All @@ -1007,35 +1006,6 @@ to `number_processes = 1`. More information on leveraging batch_run's multiproce
245it [00:25, 9.75it/s]
**Note for Windows OS users:** You will have an issue with `batch_run` and `number_processes = None`. Your cell will
show no progress, and in your terminal you will receive *AttributeError: Can't get attribute 'MoneyModel' on
<module '__main__' (built-in)>*. One way to overcome this is to take your code outside of Jupyter and adjust the above
code as follows.


.. code:: python
from multiprocessing import freeze_support
params = {"width": 10, "height": 10, "N": range(10, 500, 10)}
if __name__ == '__main__':
freeze_support()
results = batch_run(
MoneyModel,
parameters=params,
iterations=5,
max_steps=100,
number_processes=None,
data_collection_period=1,
display_progress=True,
)
If you would still like to run your code in Jupyter you will need to adjust the cell as noted above. Then you can
you can add the `nbmultitask library <(https://nbviewer.org/github/micahscopes/nbmultitask/blob/39b6f31b047e8a51a0fcb5c93ae4572684f877ce/examples.ipynb)>`__
or look at this `stackoverflow <https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter>`__.

To further analyze the return of the ``batch_run`` function, we convert
the list of dictionaries to a Pandas DataFrame and print its keys.

Expand Down
32 changes: 32 additions & 0 deletions docs/useful-snippets/snippets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,35 @@ Sometimes you need to use ``numpy``'s ``random`` library, for example to get a P
self.random = np.random.default_rng(seed)
And just use `numpy`'s random as usual, e.g. ``self.random.poisson()``.

Using multi-process ```batch_run``` on Windows
-------------

You will have an issue with `batch_run` and `number_processes = None`. Your cell will
show no progress, and in your terminal you will receive *AttributeError: Can't get attribute 'MoneyModel' on
<module '__main__' (built-in)>*. One way to overcome this is to take your code outside of Jupyter and adjust the above
code as follows.


.. code:: python
from multiprocessing import freeze_support
params = {"width": 10, "height": 10, "N": range(10, 500, 10)}
if __name__ == '__main__':
freeze_support()
results = batch_run(
MoneyModel,
parameters=params,
iterations=5,
max_steps=100,
number_processes=None,
data_collection_period=1,
display_progress=True,
)
If you would still like to run your code in Jupyter you will need to adjust the cell as noted above. Then you can
you can add the `nbmultitask library <(https://nbviewer.org/github/micahscopes/nbmultitask/blob/39b6f31b047e8a51a0fcb5c93ae4572684f877ce/examples.ipynb)>`__
or look at this `stackoverflow <https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter>`__.

0 comments on commit 604bd0f

Please sign in to comment.