Conway's Game of Life using ArrayFire

Shehzan ArrayFire, CUDA, Image Processing, Open Source, OpenGL 4 Comments

Conway's Game of Life is a popular zero player cellular automaton devised by the John Horton Conway in 1970. The game makes for a fun evolution as the player sets the initial condition and then observes the evolution of the game.

Each cell has 2 states: live or dead. There are 4 simple rules that determine this:

  • 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.

From a programmer's perspective, the game is closer to an image processing algorithm. The "world" is a 2D image, where each pixel is a cell.

The state of a cell at any single moment in time is solely determined by the number of neighbors at the previous step. Hence this makes for an embarrassingly parallel image processing algorithm. Ding ding ding! GPUs!!!

The number of neighbors can be computed using a convolution kernel:
kernel_CodeCogsEqn

Since the value of the a cell can be represented as 1 (live) and 0 (dead), using convolution will give us the number of neighbors of any cell. The center value of the kernel is 0 as we do not want to count the cell itself.

Here is a general overview of a convolution operation:
convolve_CodeCogsEqn

Plugging in 1s or 0s into the A matrix, we can see that this convolution operation will give us the neighborhood of each cell.

The next step is to compute the next state of the cell. Lets convert the rules into pseudocode.

But we can optimize these as below:

Removing redundant conditions:

Let's say A is the current state of the cell and neighborhood values are in matrix NHood.
We can compress the conditions into the following code:

But here is the best optimization--only two of these conditions matter: cond1 and cond2.

Using pseudocode:

Let's see how we can compute the next state using just these two conditions.

For cond2 it is straightforward. Any cell that is 1 in cond2 will be 1 in the next state irrespective of its value in A.

For cond1, all alive cells in A with 1 in cond1 will remain 1. All cells become dead if the corresponding value in cond1 is 0. This condition can be satisfied by doing (A * cond1).
That is,

Hence the next state can be computed by:

And this is what ArrayFire is best at. Given its powerful API of element-wise operations and image processing algorithms, implementing the Game of Life is a breeze. Below is the entire ArrayFire code that is required to run Game of Life.

This code is fully platform independent as it uses ArrayFire.

Let us know if you have made any cool applications using ArrayFire.

Facebooktwittergoogle_plusredditlinkedinmail