Parallel version
A distributor goroutine splits the board into horizontal slices and hands each one to a worker goroutine. Workers read the current world and write to separate rows of the next one, so they never write to the same memory. We used a sync.WaitGroup to wait for each turn to finish, and a sync.Mutex around the turn counter, the pause and quit state, and the world swap.
On a 512x512 board for 1000 turns, on a 6-core, 12-thread machine, the sequential version took 6.18s and the version with 12 workers took 1.50s (4.12x). Scaling was not linear: it flattened beyond the 6 physical cores.
Replacing the modulo operation in the neighbour-counting loop with conditional wrap-around made the single-worker case 38% faster.
Distributed version
The distributed version has three parts: a client that handles input and the SDL display, a broker that runs the game loop, and workers that compute slices of the board. They communicate by RPC. We tested it with 4 workers.
Version 1 sent the whole board to every worker each turn. Version 2 sends each worker only its own rows plus one “halo” row above and below. For a 512x512 board on 4 workers that is 266,240 bytes per turn instead of 1,048,576: 74.6% less. On AWS EC2, 1000 turns took 43.2s with version 1 and 12.5s with version 2.
Version 3 also splits each worker's slice across goroutines inside the worker. On one machine it ran in 1.72s, against 1.50s for the parallel version.
| Run (1000 turns) | Time |
|---|---|
| Sequential, one machine | 6.18s |
| Parallel with 12 workers, one machine | 1.50s |
| Distributed version 1, AWS EC2 | 43.2s |
| Distributed version 2, AWS EC2 | 12.5s |
What went wrong
The Go race detector flagged the ticker goroutine, the keypress handler and the main loop all touching the turn counter and the world. We fixed it with one shared mutex.
The ticker goroutine kept running after the simulation ended and blocked shutdown. We fixed that with a stopTicker channel that is closed at the end.