next up previous contents
Next: Solution Up: Alignment using Templates Previous: Solution

 

Game o'Life - curtosy of Edinburgh Parallel Computing Centre.

The aim of this exercise is to show how Fortran 90 can be used to program the Game of Life, a simple grid based problem with complex behaviour. It will show how Fortran 90 can be used to produce code in a very neat form and exposes the potential for coding in a data parallel programming style.

The game of life is a simple cellular automata where the world is a 2D grid of cells which have two states: alive or dead. At each iteration the new state of a cell is determined by the state of its neighbours at the previous iteration. This includes both the nearest neighbours and diagonal neighbours.

The rules for the evolution of the system are:

Your code will need to:

  1. initialise the board,
  2. loop,
  3. Print board,
  4. calculate number of neighbours,
  5. if (neighbours = 3) then live elseif (neighbours < 2) or (neighbours > 3) then die,
  6. end loop.

The number of neighbours can be calculated using shifts,

        target = CSHIFT(source, shift, dimension)

sets target to be the same as source but with its elements shifted a distance shift along dimension of the array dimension. For example,

        target = CSHIFT(source, -1, 1)

would set

        target(i) = source(i - 1)

CSHIFT automatically performs periodic boundary conditions. Otherwise references would be made to elements outside the bounds of the array.

The following skeleton program (which is available by clicking here) should be used as a starting point.

      PROGRAM life
      IMPLICIT NONE

! This code performs MAXLOOP iterations of an NxN life board
!
      INTEGER, PARAMETER :: N=8, MAXLOOP=10
      INTEGER            :: loop
      CHARACTER(LEN=10)  :: picfile

! 1) Declare main arrays
!



! 2) Initialise board
!



! Print starting config to file life00.pgm
!
      WRITE(picfile, 20) 0
 20   FORMAT('life', i2.2, '.pgm')

      OPEN(UNIT=10, FILE=picfile)
      WRITE(10, FMT='(''P2'',/,i3,2x,i3,/,i3)') N, N, 1
      WRITE(10,*) board
      CLOSE(UNIT=10)

! 3) Perform MAXLOOP updates
!



! 4) Count number of neighbours
!



! 5) Calculate new generation
!



! Write out new state of board
!
        WRITE(picfile, 20) loop
        OPEN(UNIT=10, FILE=picfile)
        WRITE(10, FMT='(''P2'',/,i3,2x,i3,/,i3)') N, N, 1
        WRITE(10,*) board
        CLOSE(UNIT=10)

      END DO

      END

Now add distribution directives for the two arrays source and target.




next up previous contents
Next: Solution Up: Alignment using Templates Previous: Solution

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 14:10:26 GMT 1996