Game of Life CSP is a Java implementation by @ebarlas on GitHub, of Conway's Game of Life using communicating sequential processes (CSP).
Each grid cell is an independent process and all cell communication occurs via channels.
It's built atop virtual threads, defined in JDK Enhancement Proposal (JEP) 425.
The virtual threads feature is part of Project Loom.
Prior to Project Loom and virtual threads, CSP style programming in this manner simply wasn't available in Java.
GraalVM 22.2 or later is required.
Build JAR with mvn
:
mvn package
Build native image with GraalVM (adjust parallelism to your machine's number of hardware threads or cores):
native-image -cp target/csp-game-of-life-1.0.0-SNAPSHOT.jar -H:+SupportContinuations -Djdk.virtualThreadScheduler.parallelism=16 --add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.thread=ALL-UNNAMED gameoflife.Main
Run:
./gameoflife.main
Command line arguments are optional.
./gameoflife.main patterns/spaceship.txt 1800 1200 20 50 50 5 5 false true
- Pattern text file, ex.
patterns/spaceship.txt
- Maximum window width, ex.
1800
- Maximum window height, ex.
1200
- Game of Life simulation period milliseconds, ex.
25
- Left padding columns, ex.
50
- Top padding rows, ex.
50
- Right padding columns, ex.
5
- Bottom padding rows, ex.
5
- Rotate boolean flag, ex.
false
- Log rate boolean flag, ex.
true
The patterns directory contains text-encoded patters taken from Life Lexicon located at: https://people.sc.fsu.edu/~jburkardt/m_src/exm/lexicon.txt
The lexicon is copyright (C) Stephen Silver, 1997-2005.
The full list of contributors can be found under the credits section of the website.
Every cell runs in its own process, defined in Cell.java. Cell processes communicate with each other via channels.
The simulation runs in its own process, defined in GameOfLife.java.
Finally, the viewer runs in its own process, defined in Main.java.
- Cell processes:
R * C
- Simulation processes:
1
- Viewer processes:
1
- Total processes:
R * C + 2
A pair of channels, one in each direction, exists for every pair of neighbor cells.
- Vertical segments:
(C - 1) * R
- Horizontal segments:
(R - 1) * C
- Interior vertices:
(R - 1) * (C - 1)
- Total cell-to-cell channels:
[2 * (C - 1) * R] + [2 * (R - 1) * C] + [4 * (R - 1) * (C - 1)]
Additionally, each cell has a channel for receiving a tick event and and a channel for emitting results after each simulation tick.
- Tick channels:
R * C
- Result channels:
R * C
Finally, a channel is used to communicate a full liveness matrix to the main application consumer.
- Total channels:
2 * (C - 1) * R + 2 * (R - 1) * C + 4 * (R - 1) * (C - 1) + R * C * 2 + 1
The following command results in a grid of 50,000 cells (250 x 200):
That results in 50,002
virtual threads and 497,305
channels.
./gameoflife.main patterns/puffer_train.txt 1600 800 0 235 91 10 91 true true
It's a demonstration of the viability of virtual threads in a highly concurrent, computationally intensive application.