Skip to content

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 ​

world tread-only this phaseMAPworker 0worker 1worker 2chan util.Cellbuffered, cap BREDUCEcollectorstore / processapplyupdate theboard fromthe flipsWhich parts of the reduce step may overlap with the workers is for you to decide and justify.

Requirements ​

  • Exactly gol.Params.Threads workers, 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 / CellsFlipped events.
  • 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 ​

ComponentResponsibility
DistributorStarts 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.
ReducerReceives the coordinates and updates the board. You decide when and in which goroutine.
ChannelBuffered; you choose its capacity.

Suggested steps ​

  1. Start with a single worker and the simplest correct reduce step; confirm the board matches your baseline every iteration.
  2. Add more workers.
  3. Try several channel capacities, including very small ones, to check the design cannot stall.
  4. 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.