Parallel Implementation
In this stage, you are required to write code to evolve Game of Life using multithreading on a single machine.
The following pages are some suggested steps to help you get started, you are not required to follow them.
Your implementation will be marked against the success criteria outlined here.
Step 5
Implement logic to visualise the state of the game using SDL.
You will need to utilise CellFlipped
, CellsFlipped
and TurnComplete
events to achieve this. Check out src/gol/event.rs
and src/sdl/loop.rs
for details.
Send TurnComplete
events at the end of each turn, even if you are about to send FinalTurnComplete
.
Send CellFlipped
events whenever a cell changes from alive to dead or from dead to alive.
Don't forget to send
CellFlipped
events for every initially alive cell before the firstStateChange
is sent.NOTE: You can collect many flipped cells and send a
CellsFlipped
event with all of them instead of sendingCellFlipped
for every flipped cell. You can send more than oneCellsFlipped
event in a turn, i.e., each worker could sendCellsFlipped
. Be careful not to send bothCellFlipped
andCellsFlipped
events for the same cell, or you will flip it twice
Also, implement the following control rules. Note that the running SDL provides you with a channel containing the relevant keypresses.
- If
s
is pressed, save the current state of the board as a PGM image.NOTE: Don't forget to send an
ImageOutputComplete
event after a PGM image is saved. - If
q
is pressed, stop executing Gol computation, save the current state of the board as a PGM image, then terminate the program.NOTE: Your distributor should behave as following after
q
is pressed:Complete the current turn
Send aFinalTurnComplete
event
Save the final state as PGM image and send anImageOutputComplete
event
Send aStateChange
event and terminate - If
p
is pressed, pause the processing and send aStateChange
event.
Ifp
is pressed again, resume the processing and send aStateChange
event.NOTE: It is necessary for
q
ands
to work while the execution is paused.
Select statement
Tokio provides the select! macro that functions similar to golang's select statement in async context.
Flume also provides a Selector type to implements select-like behaviour.
Test
To test the visualisation and control rules, type the following in the terminal.
cargo test --release --test sdl -- --sdl
cargo test --release --test sdl
You can also run the program and test the visualisation and control rules manually by typing the following in the terminal.
cargo run --release
Finally, type the following in the terminal to make sure all tests are passing.
cargo test --release