Module 2: Parallel and Distributed Computing
Overview
Big data frameworks such as Apache Spark are built on functional programming, because pure functions on immutable data are easy to run in parallel. This module covers the functional programming you need in Python (first-class functions, lambda, and higher-order functions such as map, filter and reduce), then speeds up real programs on a single machine by optimising pandas and using the built-in multiprocessing library.
Estimated time: about 3 hours (1 hour lecture, 2 hours hands-on)
Learning objectives
By the end of this module, you should be able to:
- Explain what functional programming is, and why pure functions and immutable data make code easy to parallelise.
- Use Python functions as first-class objects: assign them to variables, pass them as arguments and return them from other functions.
- Write anonymous functions with
lambda, and usemap,filterandreduceto transform and aggregate data. - Load large files efficiently with pandas by reading selected columns, a subset of rows, or chunks.
- Speed up CPU-bound and I/O-bound tasks with
multiprocessing, choose a sensible number of processes for your machine, and measure the speed-up.
Before class
Prerequisites: Module 0 or equivalent Python (functions, lists, dictionaries, basic pandas), and the concepts from Module 1, especially CPU-bound vs I/O-bound programs.
Install the course environment by following the Python environment setup.
Lecture slides
Hands-on
Functional Programming
Functions as first-class citizens, anonymous functions, and map, filter and reduce.
Parallel Processing in Python
Reading large files in chunks, and parallelising work with multiprocessing.
Scripts (run from a terminal):
intro-to-parallelism-in-python-cpu.py: Sequential vs. parallel timing for a CPU-bound task.intro-to-parallelism-in-python-io.py: Speeding up an I/O-bound task.
Exercises
To be added.
Further reading
- Functional Programming HOWTO (Python documentation)
functoolsdocumentation, includingreduceandpartial- Functional programming and lambda calculus on Wikipedia
- pandas: scaling to large datasets
- Dask: a parallel computing library with pandas-like APIs, worth exploring on your own