      program replicate

      logical okay

      call rep1 (okay)
      if (.not. okay) print *, 'ERROR : Replicate 1'
      call rep2 (okay)
      if (.not. okay) print *, 'ERROR : Replicate 2'
      call rep3 (okay)
      if (.not. okay) print *, 'ERROR : Replicate 3'
      call rep4 (okay)
      if (.not. okay) print *, 'ERROR : Replicate 4'

      end

      subroutine rep1 (okay)
      logical okay

      real a(10), ra(5), x
cmf$  layout ra(:serial)

      forall (i=1:10) a(i) = i

      ra = 0
      ra = a(4:8)

      n = 0
      do i = 4, 8
        n = n +i 
      end do

      x = sum (ra)
      okay = (x .eq. n)
      print *, 'Result is ', x, ' and should be ', n
      end

      subroutine rep2 (okay)
      logical okay 

      double precision a(10,10), ra(5,5), x
      integer i, j, n

cmf$  layout ra(:serial,:serial)

      forall (i=1:10,j=1:10) a(i,j) = i + j

      ra = 0
      ra = a(4:8,2:6)

      n = 0
      do i = 4, 8
        do j = 2, 6
           n = n + i + j
        end do
      end do

      x = sum (ra)
      okay = (x == n)
      print *, 'Result is ', x, ' and should be ', n
      end

      subroutine rep3 (okay)
      logical okay

      double precision a(10,10,10), ra(5,5,5), x
      integer i, j, n

cmf$  layout ra(:serial,:serial,:serial)

      forall (i=1:10,j=1:10,k=1:10) a(i,j,k) = i + 2*j - k

      ra = 0
      ra = a(4:8,2:6,5:9)

      n = 0
      do i = 4, 8
        do j = 2, 6
          do k = 5,9
            n = n + i + 2*j - k
          end do
        end do
      end do

      x = sum (ra)
      okay = (x == n)
      print *, 'Result is ', x, ' and should be ', n
      end

      subroutine rep4 (okay)
      logical okay

      integer a(10,10,10,10)
      integer ra(5,5,5,10), x
      integer i, j, k, l, n

cmf$  layout ra(:serial,:serial,:serial,:serial)

      forall (i=1:10,j=1:10,k=1:10,l=1:10)
        a(i,j,k,l) = i + 2*j - k + 3*l
      end forall

      ra = 0
      ra = a(4:8,2:6,5:9,1:10)

      n = 0
      do i = 4, 8
        do j = 2, 6
          do k = 5,9
            do l = 1, 10
               n = n + i + 2*j - k + 3*l
            end do
          end do
        end do
      end do

      x = sum (ra)
      okay = (x .eq. n)
      print *, 'Result is ', x, ' and should be ', n
      end
