Mid I: Multi-threaded Game of Life
- Strictly no access to Internet is allowed.
- No communication devices are allowed. Phones/tablets/second laptops should be stored away from you.
- No sharing is allowed at all. Not even pens, paper, notes, etc.
- No talking at all with other students.
- There can be an intermediate evaluation during the midterm.
- Do not leave the room without asking the TAs.
- There will be a penalty if you need help on basic things like creating a repository etc.
Game Of Life
The following description is taken from Wikipedia. The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed–births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.
In principle, the Life field is infinite, but computers have finite memory. This leads to problems when the active area encroaches on the border of the array. Programmers have used several strategies to address these problems. The simplest strategy is simply to assume that every cell outside the array is dead. This is the strategy we will used in this exam.
Here is a running example:
You are to write a Java program
GameOfLife that is executed like this:
java GameOfLife N G F
N defines the size of NxN game area. G is the number of generations to simulate. F is the name of a file containing the initial seed configuration. The file should contain N lines with N characters each. Valid characters are a dot “.” (representing dead cells) and character “x” (representing alive cells). Don’t forget the rules of defensive programming.
The only output of the program is the game configuration after all G generations have been calculated in the same format as seed configuration in the given file.
Here are some sample input data sets to try. This initial population dies out.
..... .x... ..x.. ...x. .....
This initial population dies more slowly.
..... .x... .x... ..x.. .....
This initial population reaches a stable state.
..... .xx.. .x... ..... .....
This initial population oscillates.
..... ..x.. ..x.. ..x.. .....
Your work should be in a new local git repository (no
git push) from the start with commits when you reach a working milestone. There should be a
serial branch and a
master branch where the serial branch has code that solves the problem without multi-threading and the master branch uses threads as described below. When and where to branch the code is your choice. If both branches are working correctly, then merge both and add an extra command line argument to choose between the serial version and the parallel version.
Each cell should be updated by a separate thread. The cell value is shared between the threads corresponding to this cell and neighbors’ cells. There are two issues to handle:
- A cell cannot calculate its value for generation G+1 until all its neighbors have updated their cell values for generation G (because the cell needs neighbors’ generation G values).
- A cell cannot update its cell value with its calculated value for generation G+1 until all its neighbors have also calculated values for their generation G+1 (because neighbors need the generation G value of this cell).
The above two should be the only two dependencies. In particular, you should not have a global dependency that generation G+1 cannot be calculated until all cells have been updated with generation G values. Consider, for example, a cell in a corner. It can be at generation G+2, its three neighbors at generation G+1 with calculated but not updated values for generation G+2 and their five other neighbors at generation G with calculated but not updated values for generation G+1.
Design and code quality
Multithreading, correct synchronization,
git usage, top-down design, and coding quality (as discussed in Code Complete 2) are the prime focus. Performance is not a focus of this assignment except that you should not overly synchronize as discussed above. I recommend that you give a thought to your overall design for 20-30 minutes with a pen and paper before actually starting to code.