C     Fx compiler version 2.1
CC    TEMPLATE t(1024)
CC    ALIGN image(I,J) WITH t(I)
CC    DISTRIBUTE t(BLOCK_CYCLIC(16,64))
C     CALL make_image(image)
C     CALL timer_start()
C     DO i = 1, 10, 1
C     CALL hist_forall(image,result)
C     ENDDO
C     forall_clocks = timer_stop()
C     CALL verify(result)
C     CALL make_image(image)
C     CALL timer_start()
C     DO i = 1, 10, 1
C     CALL hist_pdo(image,result)
C     ENDDO
C     pdo_clocks = timer_stop()
C     CALL verify(result)
C     CALL prperf(1024,512,(forall_clocks / 10),(pdo_clocks / 10))
C     END
      PROGRAM main
      INTEGER fxcellid
      EXTERNAL hist_pdo
      INTEGER i
      EXTERNAL fx_procstart
      EXTERNAL fx_deinit
      INTEGER fxdistarg
      INTEGER forall_clocks
      EXTERNAL make_image
      EXTERNAL net_reduce
      EXTERNAL prperf
      INTEGER pdo_clocks
      EXTERNAL verify
      EXTERNAL fxinitdesc
      EXTERNAL timer_stop
      INTEGER timer_stop
      EXTERNAL fx_init
      INTEGER fxidiotic3_0
      EXTERNAL fx_set_module_geometry
      EXTERNAL hist_forall
      EXTERNAL timer_start
      REAL image(16,1024)
      INTEGER result(512)
      INTEGER fximagedesc(71,2)
      INTEGER fxdistarray(6,20)
      COMMON /fxcommon/ fxcellid,fxdistarg
      CALL fx_init(fxcellid,1,1,0,0)
      CALL fx_set_module_geometry(0,0,0,8,8)
      fxdistarg = 0
      fxdistarray(1,1) = 1
      fxdistarray(2,1) = 0
      fxdistarray(3,1) = 16
      fxdistarray(4,1) = 64
      fxdistarray(5,1) = 0
      fxdistarray(6,1) = 0
      fxdistarray(3,2) = 1024
      CALL fxinitdesc(1,2,fxdistarray,fximagedesc)
C     TEMPLATE t(1024)
C     ALIGN image(I,J) WITH t(I)
C     DISTRIBUTE t(BLOCK_CYCLIC(16,64))
      fxdistarg = 1
      CALL make_image(image,fximagedesc)
      CALL timer_start()
      DO i = 1, 10, 1
        fxdistarg = 1
        CALL hist_forall(image,result,fximagedesc,(-1))
      ENDDO
      forall_clocks = timer_stop()
      CALL verify(result)
      fxdistarg = 1
      CALL make_image(image,fximagedesc)
      CALL timer_start()
      DO i = 1, 10, 1
        fxdistarg = 1
        CALL hist_pdo(image,result,fximagedesc,(-1))
      ENDDO
      pdo_clocks = timer_stop()
      CALL verify(result)
      CALL prperf(1024,512,(forall_clocks / 10),(pdo_clocks / 10))
      CALL fx_deinit()
      END
CC    TEMPLATE t(1024)
CC    ALIGN image(I,J) WITH t(I)
CC    DISTRIBUTE t(BLOCK_CYCLIC(16,64))
C     PDO (i = 1:1024)
C     POUT image(i,:)
C     LOCAL
C     fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
C     fxlmidx0 = ifxlm(fximagedesc,0,fxloopstart0)
C     DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
C     DO j = 1, 1024, 1
C     image(i,j) = 32.
C     ENDDO
C     fxlmidx0 = (fxlmidx0 + 1)
C     ENDDO
C     ENDLOCAL
C     ENDPDO
C     END
      SUBROUTINE make_image(image,fximagedesc)
      INTEGER fxlmidx0
      INTEGER fxcellid
      INTEGER fxloopstart0
      EXTERNAL fxinitdescck
      INTEGER i
      INTEGER j
      EXTERNAL fx_procstart
      INTRINSIC float
      REAL float
      EXTERNAL ifxlm
      INTEGER ifxlm
      INTEGER fxdistarg
      EXTERNAL fxdblcheck
      EXTERNAL net_reduce
      EXTERNAL ifxceil
      INTEGER ifxceil
      EXTERNAL fxdistcheck
      REAL image(16,1024)
      INTEGER fximagedesc(71,2)
      INTEGER fxdistarray(6,20)
      COMMON /fxcommon/ fxcellid,fxdistarg
      CALL fxdblcheck(fxdistarg,1,'make_image')
      fxdistarg = 0
      CALL fxdistcheck(fximagedesc,1,1,'make_image')
      fxdistarray(1,1) = 1
      fxdistarray(2,1) = 0
      fxdistarray(3,1) = 16
      fxdistarray(4,1) = 64
      fxdistarray(5,1) = 0
      fxdistarray(6,1) = 0
      fxdistarray(3,2) = 1024
      CALL fxinitdescck(1,2,fxdistarray,fximagedesc,'image',
     $'make_image')
C     TEMPLATE t(1024)
C     ALIGN image(I,J) WITH t(I)
C     DISTRIBUTE t(BLOCK_CYCLIC(16,64))
C     PDO (i = 1:1024)
C     POUT image(i,:)
C     LOCAL
      fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
      fxlmidx0 = ifxlm(fximagedesc,0,fxloopstart0)
      DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
        DO j = 1, 1024, 1
          image(fxlmidx0,j) = 32.
        ENDDO
        fxlmidx0 = (fxlmidx0 + 1)
      ENDDO
C     ENDLOCAL
C     ENDPDO
      END
CC    TEMPLATE t(1024)
CC    ALIGN image(I,J) WITH t(I)
CC    ALIGN local_result(I,J) WITH t(I)
CC    DISTRIBUTE t(BLOCK_CYCLIC(16,64))
C     local_result = 0
C     PDO (i = 1:1024)
C     PIN image(i,:)
C     POUT local_result(i,:)
C     LOCAL
C     fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
C     fxlmidx0 = ifxlm(fxlocal_resultdesc,0,fxloopstart0)
C     DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
C     DO j = 1, 1024, 1
C     pixel = image(i,j)
C     local_result(i,pixel) = (local_result(i,pixel) + 1)
C     ENDDO
C     fxlmidx0 = (fxlmidx0 + 1)
C     ENDDO
C     ENDLOCAL
C     ENDPDO
C     PDO (i = 1:1024)
C     PIN local_result(i,:)
C     PMERGEVARS result
C     LOCAL
C     PINIT
C     result = 0
C     fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
C     fxlmidx0 = ifxlm(fxlocal_resultdesc,0,fxloopstart0)
C     DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
C     PBODY
C     result = (result + local_result(i,:))
C     fxlmidx0 = (fxlmidx0 + 1)
C     ENDDO
C     ENDLOCAL
C     PMERGE
C     DO fxp2_mergedepth = 5, 0, (-1)
C     fxp2_idle = fxmerge_step(result,fxp2_rt_result,512,
C    $fxp2_mergedepth)
C     IF (fxp2_idle.NE.0) THEN
C     result = (result + fxp2_rt_result)
C     ENDIF
C     ENDDO
C     CALL fxmerge_broadcast(result,512)
C     ENDPDO
C     END
      SUBROUTINE hist_forall(image,result,fximagedesc,fxresultdesc)
      INTEGER fxindex1
      INTEGER fxindex2
      INTEGER fxlmidx0
      INTEGER fxfirst0
      INTEGER fxfirst1
      EXTERNAL left
      INTEGER left
      INTEGER fxcellid
      INTEGER fxloopstart0
      INTEGER fxcc0
      INTEGER fxcc1
      EXTERNAL fxinitdescck
      EXTERNAL fxmerge_step
      INTEGER fxmerge_step
      INTEGER fxp2_idle
      INTEGER i
      INTEGER j
      EXTERNAL fx_procstart
      INTEGER fxstride0
      INTEGER fxstride1
      INTEGER fxlmlast0
      INTEGER fxlmlast1
      INTRINSIC float
      REAL float
      EXTERNAL right
      REAL right
      EXTERNAL ifxlm
      INTEGER ifxlm
      INTEGER pixel
      INTEGER fxdistarg
      EXTERNAL fxmerge_broadcast
      EXTERNAL fxdblcheck
      INTEGER fxlmfirst0
      INTEGER fxlmfirst1
      EXTERNAL net_reduce
      EXTERNAL fxcl_inita
      EXTERNAL fxinitdesc
      INTEGER fxlast0
      INTEGER fxlast1
      INTEGER fxlmstride0
      INTEGER fxp2_mergedepth
      INTEGER fxlmstride1
      EXTERNAL ifxceil
      INTEGER ifxceil
      EXTERNAL fxdistcheck
      INTEGER local_result(16,512)
      INTEGER fxresultdesc(71,1)
      REAL image(16,1024)
      INTEGER result(512)
      INTEGER fxp2_rt_result(512)
      INTEGER fxlocal_resultdesc(71,2)
      INTEGER fximagedesc(71,2)
      INTEGER fxdistarray(6,20)
      COMMON /fxcommon/ fxcellid,fxdistarg
      CALL fxdblcheck(fxdistarg,1,'hist_forall')
      fxdistarg = 0
      CALL fxdistcheck(fxresultdesc,0,2,'hist_forall')
      CALL fxdistcheck(fximagedesc,1,1,'hist_forall')
      fxdistarray(1,1) = 1
      fxdistarray(2,1) = 0
      fxdistarray(3,1) = 16
      fxdistarray(4,1) = 64
      fxdistarray(5,1) = 0
      fxdistarray(6,1) = 0
      fxdistarray(3,2) = 512
      CALL fxinitdesc(1,2,fxdistarray,fxlocal_resultdesc)
      fxdistarray(1,1) = 1
      fxdistarray(2,1) = 0
      fxdistarray(3,1) = 16
      fxdistarray(4,1) = 64
      fxdistarray(5,1) = 0
      fxdistarray(6,1) = 0
      fxdistarray(3,2) = 1024
      CALL fxinitdescck(1,2,fxdistarray,fximagedesc,'image',
     $'hist_forall')
C     TEMPLATE t(1024)
C     ALIGN image(I,J) WITH t(I)
C     ALIGN local_result(I,J) WITH t(I)
C     DISTRIBUTE t(BLOCK_CYCLIC(16,64))
      CALL fxcl_inita(0,1,1,fxlast0,fxlmlast0,fxcc0,1,
     $fxlocal_resultdesc((fxcellid + 8),1),1024,1,16,fxfirst0,
     $fxlmfirst0)
      CALL fxcl_inita(0,1,1,fxlast1,fxlmlast1,fxcc1,1,
     $fxlocal_resultdesc((fxcellid + 8),2),512,1,512,fxfirst1,
     $fxlmfirst1)
      DO fxindex1 = fxlmfirst0, fxlmlast0, 1
        DO fxindex2 = fxlmfirst1, fxlmlast1, 1
          local_result(fxindex1,fxindex2) = 0
        ENDDO
      ENDDO
C     PDO (i = 1:1024)
C     PIN image(i,:)
C     POUT local_result(i,:)
C     LOCAL
      fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
      fxlmidx0 = ifxlm(fxlocal_resultdesc,0,fxloopstart0)
      DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
        DO j = 1, 1024, 1
          pixel = image(fxlmidx0,j)
          local_result(fxlmidx0,pixel) = (local_result(fxlmidx0,pixel
     $) + 1)
        ENDDO
        fxlmidx0 = (fxlmidx0 + 1)
      ENDDO
C     ENDLOCAL
C     ENDPDO
C     PDO (i = 1:1024)
C     PIN local_result(i,:)
C     PMERGEVARS result
C     LOCAL
C     PINIT
      DO fxindex1 = 1, 512, 1
        result(fxindex1) = 0
      ENDDO
      fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
      fxlmidx0 = ifxlm(fxlocal_resultdesc,0,fxloopstart0)
      DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
C     PBODY
        DO fxindex1 = 1, 512, 1
          result(fxindex1) = (result(fxindex1) + local_result(
     $fxlmidx0,fxindex1))
        ENDDO
        fxlmidx0 = (fxlmidx0 + 1)
      ENDDO
C     ENDLOCAL
C     PMERGE
      DO fxp2_mergedepth = 5, 0, (-1)
        fxp2_idle = fxmerge_step(result,fxp2_rt_result,512,
     $fxp2_mergedepth)
        IF (fxp2_idle.NE.0) THEN
          DO fxindex1 = 1, 512, 1
            result(fxindex1) = (result(fxindex1) + fxp2_rt_result(
     $fxindex1))
          ENDDO
        ENDIF
      ENDDO
      CALL fxmerge_broadcast(result,512)
C     ENDPDO
      END
CC    TEMPLATE t(1024)
CC    ALIGN image(I,J) WITH t(I)
CC    DISTRIBUTE t(BLOCK_CYCLIC(16,64))
C     PDO (i = 1:1024)
C     PIN image(i,:)
C     PMERGEVARS result
C     LOCAL
C     PINIT
C     result = 0
C     fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
C     fxlmidx0 = ifxlm(fximagedesc,0,fxloopstart0)
C     DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
C     PBODY
C     DO j = 1, 1024, 1
C     pixel = image(i,j)
C     result(pixel) = (result(pixel) + 1)
C     ENDDO
C     fxlmidx0 = (fxlmidx0 + 1)
C     ENDDO
C     ENDLOCAL
C     PMERGE
C     DO fxp3_mergedepth = 5, 0, (-1)
C     fxp3_idle = fxmerge_step(result,fxp3_rt_result,512,
C    $fxp3_mergedepth)
C     IF (fxp3_idle.NE.0) THEN
C     result = (result + fxp3_rt_result)
C     ENDIF
C     ENDDO
C     CALL fxmerge_broadcast(result,512)
C     ENDPDO
C     END
      SUBROUTINE hist_pdo(image,result,fximagedesc,fxresultdesc)
      INTEGER fxindex1
      INTEGER fxlmidx0
      EXTERNAL left
      INTEGER left
      INTEGER fxcellid
      INTEGER fxloopstart0
      EXTERNAL fxinitdescck
      EXTERNAL fxmerge_step
      INTEGER fxmerge_step
      INTEGER fxp3_idle
      INTEGER i
      INTEGER j
      EXTERNAL fx_procstart
      INTRINSIC float
      REAL float
      EXTERNAL right
      REAL right
      EXTERNAL ifxlm
      INTEGER ifxlm
      INTEGER pixel
      INTEGER fxdistarg
      EXTERNAL fxmerge_broadcast
      EXTERNAL fxdblcheck
      EXTERNAL net_reduce
      EXTERNAL timer_stop
      INTEGER timer_stop
      INTEGER fxp3_mergedepth
      EXTERNAL ifxceil
      INTEGER ifxceil
      EXTERNAL fxdistcheck
      INTEGER fxresultdesc(71,1)
      REAL image(16,1024)
      INTEGER result(512)
      INTEGER fxp3_rt_result(512)
      INTEGER fximagedesc(71,2)
      INTEGER fxdistarray(6,20)
      COMMON /fxcommon/ fxcellid,fxdistarg
      CALL fxdblcheck(fxdistarg,1,'hist_pdo')
      fxdistarg = 0
      CALL fxdistcheck(fxresultdesc,0,2,'hist_pdo')
      CALL fxdistcheck(fximagedesc,1,1,'hist_pdo')
      fxdistarray(1,1) = 1
      fxdistarray(2,1) = 0
      fxdistarray(3,1) = 16
      fxdistarray(4,1) = 64
      fxdistarray(5,1) = 0
      fxdistarray(6,1) = 0
      fxdistarray(3,2) = 1024
      CALL fxinitdescck(1,2,fxdistarray,fximagedesc,'image',
     $'hist_pdo')
C     TEMPLATE t(1024)
C     ALIGN image(I,J) WITH t(I)
C     DISTRIBUTE t(BLOCK_CYCLIC(16,64))
C     PDO (i = 1:1024)
C     PIN image(i,:)
C     PMERGEVARS result
C     LOCAL
C     PINIT
      DO fxindex1 = 1, 512, 1
        result(fxindex1) = 0
      ENDDO
      fxloopstart0 = (MAX(((fxcellid * 16) + 1),1))
      fxlmidx0 = ifxlm(fximagedesc,0,fxloopstart0)
      DO i = fxloopstart0, MIN(((fxcellid * 16) + 16),1024), 1
C     PBODY
        DO j = 1, 1024, 1
          pixel = image(fxlmidx0,j)
          result(pixel) = (result(pixel) + 1)
        ENDDO
        fxlmidx0 = (fxlmidx0 + 1)
      ENDDO
C     ENDLOCAL
C     PMERGE
      DO fxp3_mergedepth = 5, 0, (-1)
        fxp3_idle = fxmerge_step(result,fxp3_rt_result,512,
     $fxp3_mergedepth)
        IF (fxp3_idle.NE.0) THEN
          DO fxindex1 = 1, 512, 1
            result(fxindex1) = (result(fxindex1) + fxp3_rt_result(
     $fxindex1))
          ENDDO
        ENDIF
      ENDDO
      CALL fxmerge_broadcast(result,512)
C     ENDPDO
      END
C     errors = 0
C     DO i = 1, 512, 1
C     IF (i.NE.32) THEN
C     IF (result(i).NE.0) THEN
C     errors = (errors + 1)
C     ENDIF
C     ELSE
C     IF (result(i).NE.1048576) THEN
C     errors = (errors + 1)
C     ENDIF
C     ENDIF
C     ENDDO
C     IF (errors.GT.0) WRITE (unit=6,fmt=*) 'Errors =',errors
C     END
      SUBROUTINE verify(result)
      INTEGER fxcellid
      INTEGER i
      EXTERNAL fx_procstart
      INTEGER fxdistarg
      EXTERNAL fxdblcheck
      EXTERNAL net_reduce
      INTEGER errors
      INTEGER result(512)
      COMMON /fxcommon/ fxcellid,fxdistarg
      CALL fxdblcheck(fxdistarg,0,'verify')
      errors = 0
      DO i = 1, 512, 1
        IF (i.NE.32) THEN
          IF (result(i).NE.0) THEN
            errors = (errors + 1)
          ENDIF
        ELSE
          IF (result(i).NE.1048576) THEN
            errors = (errors + 1)
          ENDIF
        ENDIF
      ENDDO
      IF (errors.GT.0) THEN
        IF (fxcellid.EQ.0) THEN
          WRITE (unit=6,fmt=*) 'Errors =',errors
        ENDIF
      ENDIF
      END
C     pdo_msecs = (real(pdo_clocks) / 2e4)
C     forall_msecs = (real(forall_clocks) / 2e4)
C     WRITE (unit=6,fmt=*) n,colors,pdo_clocks,pdo_msecs,
C    $forall_clocks,forall_msecs
C     END
      SUBROUTINE prperf(n,colors,forall_clocks,pdo_clocks)
      INTRINSIC real
      REAL real
      INTEGER fxcellid
      REAL forall_msecs
      EXTERNAL fx_procstart
      INTEGER n
      REAL pdo_msecs
      INTEGER fxdistarg
      INTEGER forall_clocks
      EXTERNAL fxdblcheck
      EXTERNAL net_reduce
      INTEGER pdo_clocks
      INTEGER colors
      COMMON /fxcommon/ fxcellid,fxdistarg
      CALL fxdblcheck(fxdistarg,0,'prperf')
      pdo_msecs = (real(pdo_clocks) / 2e4)
      forall_msecs = (real(forall_clocks) / 2e4)
      IF (fxcellid.EQ.0) THEN
        WRITE (unit=6,fmt=*) n,colors,pdo_clocks,pdo_msecs,
     $forall_clocks,forall_msecs
      ENDIF
      END
