From d326cb150a222e48beb0dea1d63f79130a5382e7 Mon Sep 17 00:00:00 2001 From: Repo Updater <noreply@mpcdf.mpg.de> Date: Mon, 14 Nov 2022 13:44:31 +0100 Subject: [PATCH] fb3c2462 Remove Process and Queue exercise --- exercises/Parallel_Exercise.ipynb | 52 +-------- .../Parallel_Exercise_withsolution.ipynb | 106 +----------------- 2 files changed, 4 insertions(+), 154 deletions(-) diff --git a/exercises/Parallel_Exercise.ipynb b/exercises/Parallel_Exercise.ipynb index 214e4b8..df02eaf 100644 --- a/exercises/Parallel_Exercise.ipynb +++ b/exercises/Parallel_Exercise.ipynb @@ -94,54 +94,6 @@ "* Compare timings to ensure you benefit from the parallelization" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Processes and queues\n", - "* Goal: Implement a work queueing system for computing the squares of numbers\n", - "* Create one `Queue` for the input data and one `Queue` for the output data (i.e. the results)\n", - "* Make these the arguments of the function that is the target for the `Process` objects\n", - "* Fill input `Queue` on the root process\n", - "* Get the results from the output `Queue` and print them\n", - "* Compare timings to check that you benefit from the parallelization\n", - "* Extension: add index to parameter of worker function and put that on the return queue to sort the results\n", - "* Extension: use this scheme to implement another parallelization of the Monte-Carlo computation of $\\pi$" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from multiprocessing import Queue, Process\n", - "import time\n", - "\n", - "def worker_function(in_queue, out_queue):\n", - " # loop over in_queue and put function results into out_queue\n", - " time.sleep(0.1)\n", - " \n", - "\n", - "# create queues\n", - "# create worker processes\n", - "number_processes = 3\n", - "ps = [Process(target=worker_function, args=('fill out here', 'bla'))\n", - " for _ in range(number_processes)]\n", - "# start processes\n", - "for p in ps:\n", - " p.start()\n", - "# add parameters to queue\n", - "# now get the results \n", - "# send stop signal to stop iteration\n", - "# stop processes\n", - "for p in ps:\n", - " p.join()\n", - "\n", - "\n", - "# compare timings for different numbers of processes" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -158,7 +110,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -172,7 +124,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.9.7" } }, "nbformat": 4, diff --git a/exercises/_solutions/Parallel_Exercise_withsolution.ipynb b/exercises/_solutions/Parallel_Exercise_withsolution.ipynb index 47a6fdf..6e4293e 100644 --- a/exercises/_solutions/Parallel_Exercise_withsolution.ipynb +++ b/exercises/_solutions/Parallel_Exercise_withsolution.ipynb @@ -165,108 +165,6 @@ "%timeit list(map(get_pi_montecarlo, chunks))" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Processes and queues\n", - "* Goal: Implement a work queueing system for computing the squares of numbers\n", - "* Create one `Queue` for the input data and one `Queue` for the output data (i.e. the results)\n", - "* Make these the arguments of the function that is the target for the `Process` objects\n", - "* Fill input `Queue` on the root process\n", - "* Get the results from the output `Queue` and print them\n", - "* Compare timings to check that you benefit from the parallelization\n", - "* Extension: use this scheme to implement another parallelization of the Monte-Carlo computation of $\\pi$" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n", - "1.04 s ± 5.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", - "310 ms ± 5.43 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", - "338 ms ± 32.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "from multiprocessing import Queue, Process\n", - "import time\n", - "\n", - "def worker_function(function, in_queue, out_queue):\n", - " for index, parameter in iter(in_queue.get, 'STOP'):\n", - " result = function(parameter)\n", - " out_queue.put((index, result))\n", - " \n", - "\n", - "def get_results(function, parameters, number_processes):\n", - " # create queues\n", - " in_queue = Queue()\n", - " out_queue = Queue()\n", - "\n", - " # create worker processes\n", - " ps = [Process(target=worker_function, args=(function, in_queue, out_queue))\n", - " for _ in range(number_processes)]\n", - " # start processes\n", - " for p in ps:\n", - " p.start()\n", - " # add parameters to queue\n", - " for i, x in enumerate(parameters):\n", - " in_queue.put((i, x))\n", - " # now get the results with indices\n", - " indexed_results = [out_queue.get() for _ in range(len(parameters))]\n", - " # send stop signal to stop iteration\n", - " for _ in range(number_processes):\n", - " in_queue.put('STOP')\n", - " # sort and remove indices\n", - " results = [x[1] for x in sorted(indexed_results)]\n", - " # stop processes\n", - " for p in ps:\n", - " p.join()\n", - " return results\n", - "\n", - "def square(x):\n", - " time.sleep(0.1)\n", - " return x**2\n", - "\n", - "print(get_results(square, range(10**1), 5))\n", - "\n", - "%timeit get_results(square, range(10), 1)\n", - "%timeit get_results(square, range(10), 5)\n", - "%timeit get_results(square, range(10), 10)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Approximation for pi: 3.143794\n", - "1.16 s ± 59.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", - "568 ms ± 61.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", - "571 ms ± 39.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" - ] - } - ], - "source": [ - "# now use this to compute pi\n", - "pi = np.mean(get_results(get_pi_montecarlo, [10**5]*100, 10))\n", - "print(\"Approximation for pi: {:f}\".format(pi))\n", - "\n", - "for number_processes in [1, 5, 10]:\n", - " %timeit np.mean(get_results(get_pi_montecarlo, [10**5]*100, number_processes))" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -282,7 +180,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +194,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.9.7" } }, "nbformat": 4, -- GitLab