Approach 3: Map-Reduce over Flipped Cells
Task
Implement the parallel Game of Life using a map-reduce structure:
- Map: each worker examines its strip of the board and, for every cell whose state will change this iteration, pushes that cell's coordinates into a buffered channel.
- Reduce: the coordinates are collected from the channel and used to update the board, so that it holds the next generation.
The board is not copied between iterations; it is updated using only the cells that changed. You may process the channel at the end of each iteration, or concurrently with the workers if you believe that can work.
Pipeline
Requirements
- Exactly
gol.Params.Threadsworkers, each responsible for a horizontal strip. - Workers communicate which cells flip through a buffered channel; they do not return whole strips or boards.
- The board used by the workers for neighbour counting must always be the previous generation, for every cell, in every iteration.
- The same list of flipped cells should be what you use for the
CellFlipped/CellsFlippedevents. - All of the features from Steps 3 to 6 keep working: the ticker, PGM output, keyboard controls and SDL events.
- Passes all tests, including with
-race, and does not deadlock for any buffer size you choose (including very small ones).
Components to build
| Component | Responsibility |
|---|---|
| Distributor | Starts each iteration and ensures the board holds the next generation before the following one begins. |
| Workers (map) | Find the cells that flip in their strip and send their coordinates. |
| Reducer | Receives the coordinates and updates the board. You decide when and in which goroutine. |
| Channel | Buffered; you choose its capacity. |
Suggested steps
- Start with a single worker and the simplest correct reduce step; confirm the board matches your baseline every iteration.
- Add more workers.
- Try several channel capacities, including very small ones, to check the design cannot stall.
- Re-run the full test-suite, including
-race, after each change.
Analysis questions, benchmarks and report guidance for this approach are in Benchmarking and Testing.