|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0d031ccb-54d9-44a8-8282-63064ec52ba0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Python Multiprocessing\n", |
| 9 | + "\n", |
| 10 | + "## Learning Objectives\n", |
| 11 | + "\n", |
| 12 | + "By the end of this lesson, learners will be able to:\n", |
| 13 | + "\n", |
| 14 | + "- Differentiate between message-passing and multiprocessing approaches in parallel programming.\n", |
| 15 | + "- Implement Python's `multiprocessing` library to parallelize a fractal generation task within a single code instance.\n", |
| 16 | + "- Set up a pool of worker processes using `Pool(processes=n_processes)` and delegate tasks across these processes with the `p.map()` function.\n", |
| 17 | + "- Use `functools.partial` to manage function parameters that remain constant across parallel tasks, optimizing code reuse.\n", |
| 18 | + "- Divide a computational grid into slices and assign each slice to a worker process to handle independently.\n", |
| 19 | + "- Close a pool of processes in Python's multiprocessing model once tasks are completed, resuming the main program.\n", |
| 20 | + "- Evaluate the performance of the multiprocessing approach by timing code execution with varying numbers of slices and processes, and compare results with the serial version in `fractal_complete.py`.\n", |
| 21 | + "\n", |
| 22 | + "\n", |
| 23 | + "## Fractal example with Python multiprocessing\n", |
| 24 | + "\n", |
| 25 | + "In the previous lessons we have seen *message passing* being used to communicate data between multiple running instances of the code.\n", |
| 26 | + "An alternative approach is to use *multi-processing*, where-by we launch one instance of our code which in turn launches new threads with access to the same memory.\n", |
| 27 | + "\n", |
| 28 | + "In `multiprocessing_fractal.py`, the previous fractal example has been implemented using `multiprocessing` from the python standard library.\n", |
| 29 | + "Most of the code follows the same structure as the parallel fractal example.\n", |
| 30 | + "\n", |
| 31 | + "For the multi-processing model, we set up a *pool* of workers, `Pool(processes=n_processes)`, assigned to `p`.\n", |
| 32 | + "The work can then be delegated out to these workers using the [`p.map()`](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool.map) method.\n", |
| 33 | + "This `map` method (equivalent to the builtin [`map`](https://docs.python.org/3/library/functions.html#map)) takes two arguments: a function to run (our fractal function), and a collection of inputs to pass to the function (different regions of the grid to be processed in parallel).\n", |
| 34 | + "\n", |
| 35 | + "```{note}\n", |
| 36 | + "To pass in the parameters that don't change over grid regions, we've used [`functools.partial`](https://docs.python.org/3/library/functools.html#functools.partial):\n", |
| 37 | + "\n", |
| 38 | + "``` python\n", |
| 39 | + "partial_julia_set = partial(julia_set, num_iter=80, c=-0.83 - 0.22 * 1j)\n", |
| 40 | + "```\n", |
| 41 | + "\n", |
| 42 | + "This would be essentially equivalent to defining a new function:\n", |
| 43 | + "\n", |
| 44 | + "``` python\n", |
| 45 | + "def partial_julia_set(grid):\n", |
| 46 | + " return julia_set(grid, num_iter=80, c=-0.83 -0.22 * 1j)\n", |
| 47 | + "```\n", |
| 48 | + "\n", |
| 49 | + "You may be familiar with *lambda* expressions, but these cannot be passed in to the `multiprocessing.Pool.map` function.\n", |
| 50 | + "In this script, we have split up the grid into `n_slices` vertical slices and assigned a pool of of `n_processes` workers.\n", |
| 51 | + "These workers each take a slice, calculate the result saving the output into `fractals`, then work on a new slice.\n", |
| 52 | + "When there are no more slices to work on, the pool is *closed* and the program resumes.\n", |
| 53 | + "We can see how we can speed up the code by timing the full script running with different values of `n_slices` and `n_processes`.\n", |
| 54 | + "Compare these numbers against the previous serial example in `fractal_complete.py`." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "id": "b45c5b07-4d05-4705-93fe-fd841171e4cc", |
| 60 | + "metadata": {}, |
| 61 | + "source": [ |
| 62 | + "# Complete File\n", |
| 63 | + "[Download complete multiprocessing_fractal.py file](complete_files/multiprocessing_fractal.py)" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "id": "b3b04584-c002-47f5-8f1c-66cb00ebe4d3", |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [], |
| 72 | + "source": [] |
| 73 | + } |
| 74 | + ], |
| 75 | + "metadata": { |
| 76 | + "kernelspec": { |
| 77 | + "display_name": "Python 3 (ipykernel)", |
| 78 | + "language": "python", |
| 79 | + "name": "python3" |
| 80 | + }, |
| 81 | + "language_info": { |
| 82 | + "codemirror_mode": { |
| 83 | + "name": "ipython", |
| 84 | + "version": 3 |
| 85 | + }, |
| 86 | + "file_extension": ".py", |
| 87 | + "mimetype": "text/x-python", |
| 88 | + "name": "python", |
| 89 | + "nbconvert_exporter": "python", |
| 90 | + "pygments_lexer": "ipython3", |
| 91 | + "version": "3.9.19" |
| 92 | + } |
| 93 | + }, |
| 94 | + "nbformat": 4, |
| 95 | + "nbformat_minor": 5 |
| 96 | +} |
0 commit comments