Hopalong Map – fast, parallel, low-memory – two-pass checkpointed orbit decomposition

Draws the Hopalong map (Barry Martin's map) from hundreds of millions or even billions of points, using all CPU cores, with memory that does not grow with the number of points.
- Exact: the picture is bit-for-bit identical to a plain, single-threaded implementation, and identical for any number of threads (checked with 1 and 8).
- Fast: all cores are used, and each core is kept busier than in a naive loop.
- Small: memory stays fixed however many points you compute (about 76 MB on an 8-thread machine, on top of Python and its libraries).
- Two output flavours: counts (hits per pixel) or a proper probability density on the colour bar.
- Three colour scales: linear, square root, logarithmic.
- Optional FMA variant (
hopalong_fast_fma.py): same results, slightly faster pass 1 – see FMA variant.
- Interactive window (matplotlib) plus PNG export.
Contents
Quick start
Requirements: Python 3 (developed with 3.15), numpy, numba, matplotlib. Works on macOS and Linux (the memory read-out uses the Unix-only resource module; on Windows remove display_resource_usage).
pip install numpy numba matplotlib
python hopalong_fast.py
The script asks for six things:
| Prompt |
Meaning |
a, b, c |
The three parameters of the map (any real numbers; a = 0 together with c = 0 is rejected because the orbit would never leave the origin). |
n |
Number of points to compute, at least 1000. Scientific notation such as 1e8 is accepted. |
| Colour scale |
linear (default), sqrt or log – see Colour scales. |
| Colour bar shows |
counts (default) or density – see Counts vs. density. |
Example session (times and memory will differ on your machine; this one was run on an Apple MacMini M2):
Enter a float value for "a": -3.14
Enter a float value for "b": 0.3
Enter a float value for "c": 0.3
Enter a positive integer value > 1000 for "n": 1e8
Colour scale (linear / sqrt / log) [linear]: sqrt
Colour bar shows (counts / density) [counts]: density
Pass 1 (extents + checkpoints, serial): 0.81 s
Pass 2 (parallel plot): 0.08 s
Wall-clock time (compute): 0.89 s
Memory (RAM): 221.25 MByte used (peak)
Check: sum(density) * cell area = 1.000000
Image saved to: <OUTPUT_DIR>/py_num=100000000_a=-3.14_b=0.3_c=0.3_sqrt_density.png
Then a window opens with the picture, and a PNG is written to OUTPUT_DIR. Change OUTPUT_DIR at the top of the script to a folder that exists on your machine. The PNG is the raw 1000 × 1000 picture without axes; the file name encodes the parameters, the colour scale (_sqrt, _log) and the mode (_density).
For the FMA variant run python hopalong_fast_fma.py instead (same prompts, same output; see FMA variant).
The first start takes a few seconds because numba compiles the kernels; they are cached on disk afterwards.
The "Memory" line is the peak of the whole Python process, including Python, numba and matplotlib themselves (roughly 150 MB before anything is computed).
The Hopalong map
Every point is computed from the previous one. Starting at the origin, (x, y) = (0, 0):
$$
x_{k+1} = y_k - \mathrm{sign}(x_k),\sqrt{|,b,x_k - c,|}
$$
$$
y_{k+1} = a - x_k
$$
where sign(x) is +1 for positive and -1 for negative x. The three numbers a, b, c decide what the picture looks like; for example a = -3.14, b = 0.3, c = 0.3 gives a ring-and-petal pattern.
Two facts about this map drive the whole design:
- It is strictly serial. Point number 1,000,000 cannot be computed without first computing points 1 to 999,999. You cannot simply hand different parts of the orbit to different CPU cores.
- It is deterministic. The same
(x, y) always leads to the same next point. There is no randomness. If you know where you are, you know everything that follows.

A note on "map" versus "attractor". The Hopalong map is often called an attractor, but it does not pull orbits in from elsewhere. Away from the lines x = 0 and b * x = c, its Jacobian determinant (the factor by which the map stretches areas) is exactly 1, so the map preserves area. You can check this by hand: the new x changes with y at slope 1, and the new y changes with x at slope -1 and does not depend on y, so the determinant is 1. Orbits therefore stay in regions that are determined by where they start, and different start points generally give different pictures. The picture is the orbit from (0, 0), not a property of a, b and c alone. That is why the exact orbit is replayed from checkpoints, instead of starting the segments at independent random points.
The general approach (checkpoints and segments)
The problem
To draw the picture you lay a grid over the plane (here 1000 × 1000 cells) and, for every point of the orbit, add one tally mark to the cell it lands in. Cells that were visited often end up bright. Such a grid of tally counts is called a histogram.
Two things make this harder than it sounds:
- You must know the size of the picture before you can start tallying. Where is the leftmost point, the topmost? That is only known after the whole orbit has been walked. This is why every implementation needs two passes: one to measure, one to draw.
- Storing the orbit is out of the question. One point is two decimal numbers, 16 bytes. A billion points would be 16 GB.
A plain implementation therefore walks the orbit twice, one after the other, on a single core.
The idea in one picture
Think of the orbit as a very long hiking trail where each step depends on the previous one, so one hiker has to walk it in order. The trick:
- Pass 1: one hiker walks the whole trail once. On the way they note the extreme positions (how big must the map be?) and plant a flag every so often. A flag records just the exact position at that spot.
- Pass 2: now many hikers start at the same time, each at a different flag, and each walks only the stretch up to the next flag. Because the trail is deterministic, they all take exactly the steps the single hiker would have taken. Each hiker makes tally marks on their own private tally sheet, so nobody gets in anybody's way. At the end, the tally sheets are added up.

Checkpoints (flags) and segments
The orbit is cut into consecutive pieces called segments. A checkpoint is the exact position (x, y) at the start of a segment.

- A worker that starts at flag k just keeps computing until it reaches the position stored in flag k+1, adding a tally mark for every point on the way.
- If the number of points does not divide evenly into segments, the leftover points are simply added to the last segment, so every point is visited exactly once.
Why the result is exactly the same
Starting from an exact checkpoint, a worker repeats exactly the same arithmetic as the single hiker did, so it produces exactly the same numbers. The set of tally marks is the same as in a single sequential run, only the order in which they are made differs, and adding up whole numbers does not depend on the order. So the picture is identical, no matter how many cores are used.
This matters more than it may seem: the map is chaotic, so even a difference in the 16th decimal place would grow until the replayed orbit looked completely different. Both passes therefore use the very same step function and plain IEEE floating-point arithmetic (do not switch on numba's fastmath).
What is stored, and how much
| What |
Size |
Depends on n? |
| The orbit itself |
not stored at all |
– |
| Checkpoints |
16 bytes per segment, about 1 KB in total |
no |
| One tally sheet per task |
4 MB each (1000 × 1000 counters of 4 bytes) |
no |
| Final picture |
8 MB |
no |
With T CPU threads the script uses 2 × T tasks and 4 segments per task, so 8 × T segments. On an 8-thread machine that is 64 segments, i.e. 1 KB of checkpoints, instead of 16 GB for a billion stored points. The only memory that grows is the number of tally sheets (one per task), and it grows with the number of cores and with the picture resolution, never with n.
The counters of a tally sheet are 4-byte numbers, which overflow at about 4.3 billion. To be safe, no single task is allowed to plot more than 2 billion points (MAX_POINTS_PER_TASK); for very large n the script simply uses more tasks. The sheets are then added up in 8-byte numbers.
Lanes: keeping each core busy
Each step needs the result of the previous step, including a square root, which takes the CPU a moment. A core working on one chain of steps spends part of its time waiting. So each task works on four consecutive segments in lock-step ("lanes"): while the CPU waits for the square root of lane 1, it already works on lanes 2, 3 and 4. This uses no extra cores, it just makes each core busier. In the benchmark below this makes pass 2 about 3.7 times faster per core (0.82 s down to 0.22 s on an Apple M2; the gain depends on the CPU).

Where the time goes
- Pass 1 is serial: it costs roughly as much as the walk itself.
- Pass 2 is parallel: its time shrinks with the number of cores.
So the total time is roughly pass 1 + pass 2 / number of cores. Beyond a handful of cores, pass 1 dominates, and extra cores no longer help much. The script prints both times so you can see this. On an 8-thread Apple M2, pass 1 takes 0.81 s of the 0.87 s total (see the benchmark below).
Counts vs. density
Both options run the same two passes and show practically the same picture. They differ in how points are assigned to cells and in what the colour bar says.
Counts (default)
The colour bar shows how many points landed in each pixel.
A point is assigned to the nearest grid point, where the grid points lie exactly on the minimum and maximum of the orbit. With W columns: