From 604bd0fb3901466da63b8498104c138e6b603515 Mon Sep 17 00:00:00 2001 From: rht Date: Sat, 30 Apr 2022 22:41:48 -0400 Subject: [PATCH] Remove multi CPU from batch_run intro tutorial --- docs/tutorials/intro_tutorial.ipynb | 33 +++---------------------- docs/tutorials/intro_tutorial.rst | 38 +++-------------------------- docs/useful-snippets/snippets.rst | 32 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 64 deletions(-) diff --git a/docs/tutorials/intro_tutorial.ipynb b/docs/tutorials/intro_tutorial.ipynb index 32f2e82efd1..4837e885456 100644 --- a/docs/tutorials/intro_tutorial.ipynb +++ b/docs/tutorials/intro_tutorial.ipynb @@ -840,7 +840,7 @@ "\n", "* `number_processes`\n", "

\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", "

\n", "Note: Multiprocessing does make debugging challenging. If your parameter sweeps are resulting in unexpected errors set `number_processes = 1`.\n", "

\n", @@ -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." ] }, { @@ -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 *. 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": {}, diff --git a/docs/tutorials/intro_tutorial.rst b/docs/tutorials/intro_tutorial.rst index b7512659a87..ac2c4d0ae54 100644 --- a/docs/tutorials/intro_tutorial.rst +++ b/docs/tutorials/intro_tutorial.rst @@ -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``. @@ -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 `__, in "Using multi-process ```batch_run``` on Windows" section for how to do it. .. code:: python @@ -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, ) @@ -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 -*. 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 `__. - To further analyze the return of the ``batch_run`` function, we convert the list of dictionaries to a Pandas DataFrame and print its keys. diff --git a/docs/useful-snippets/snippets.rst b/docs/useful-snippets/snippets.rst index 4b5cf76713d..75751c74a75 100644 --- a/docs/useful-snippets/snippets.rst +++ b/docs/useful-snippets/snippets.rst @@ -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 +*. 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 `__.