
! This code performs MAXLOOP iterations of an NxN life board
!
      INTEGER, PARAMETER :: N=17, MAXLOOP=10
      INTEGER i, loop

! Declare main arrays
!
!HPF$ PROCESSORS p (2, 3)

      INTEGER, DIMENSION(N,N) :: board, neighbours
!HPF$ DISTRIBUTE board (BLOCK, BLOCK) ONTO p
!HPF$ ALIGN neigbours(:, :) WITH board

! Initialise board
!
      board = 0

      board(N/2 + 1,:) = 1
      board(:,N/2 + 1) = 1

! Print starting config to file life00.pgm
!
      DO i = 1, N
        PRINT *, board(:,i)
      ENDDO
      PRINT *

! Perform MAXLOOP updates
!
      DO loop = 1, MAXLOOP

! Count number of neighbours
!
        neighbours =
     &    CSHIFT(board, SHIFT= 1, DIM=1) +
     &    CSHIFT(board, SHIFT=-1, DIM=1) +
     &    CSHIFT(board, SHIFT= 1, DIM=2) +
     &    CSHIFT(board, SHIFT=-1, DIM=2) +
     &    CSHIFT(CSHIFT(board, SHIFT= 1, DIM=2), SHIFT= 1, DIM=1) +
     &    CSHIFT(CSHIFT(board, SHIFT= 1, DIM=2), SHIFT=-1, DIM=1) +
     &    CSHIFT(CSHIFT(board, SHIFT=-1, DIM=2), SHIFT= 1, DIM=1) +
     &    CSHIFT(CSHIFT(board, SHIFT=-1, DIM=2), SHIFT=-1, DIM=1)

! Calculate new generation
!
        WHERE (neighbours.lt.2 .or. neighbours.gt.3)
          board = 0
        END WHERE
        WHERE (neighbours.eq.3)
          board = 1
        END WHERE

! Write out new state of board
!
        DO i = 1, N
          PRINT *, board(:,i)
        ENDDO
        PRINT *

      END DO

      END


