C{{{  master.u
C{{{  SUBROUTINE display
      SUBROUTINE display (source, rows, columns)
C  Read a stream of 'rows' * 'columns' real numbers from 'source',
C  and print as a table.
      IMPLICIT NONE
      INTEGER source, rows, columns
      INTEGER i, j, tag, data(0:40)
C     C{{{  tags
      INTEGER tend, tint, tr32, tstr, tuntagged
      PARAMETER (tuntagged = -1)
      PARAMETER (tend = 0)
      PARAMETER (tint = 1)
      PARAMETER (tr32 = 2)
      PARAMETER (tstr = 3)
C     C}}}
      DO 10, i = 0, rows-1
        DO 20, j = 0, columns-1
           call INTAG(source, tag)
           IF (tag .EQ. tr32) THEN
             call INPUT(source, data(j), 1, tuntagged)
           ELSE
             STOP "Unexpected Tag value"
           ENDIF
20      CONTINUE
        print '(50(i3, ''  ''))', (data(j), j = 0, columns-1)
10    CONTINUE
      END
C}}}
C
C{{{  SUBROUTINE transpose
      SUBROUTINE transpose (chan, size)
      IMPLICIT NONE
      INTEGER chan, size, i, j
C     C{{{  tags
      INTEGER tend, tint, tr32, tstr, tuntagged
      PARAMETER (tuntagged = -1)
      PARAMETER (tend = 0)
      PARAMETER (tint = 1)
      PARAMETER (tr32 = 2)
      PARAMETER (tstr = 3)
C     C}}}
      DO 30, i = 0, size-1
        DO 40, j = 0, size-1
          call OUTAG(chan, tr32)
          call OUTPUT(chan, ((size * i) + j), 1, tuntagged)
40      CONTINUE
30    CONTINUE
      print *, 'matrix'
      call display(chan, size, size)
      print *, 'transpose'
      call display (chan, size, size)
      END
C}}}
C
      SUBROUTINE master (nchans, chan)
      IMPLICIT NONE
      INTEGER nchans
      INTEGER chan
C     C{{{  tags
      INTEGER tend, tint, tr32, tstr, tuntagged
      PARAMETER (tuntagged = -1)
      PARAMETER (tend = 0)
      PARAMETER (tint = 1)
      PARAMETER (tr32 = 2)
      PARAMETER (tstr = 3)
C     C}}}
      INTEGER size, tag
50    CONTINUE
      call INTAG(chan, tag)
      IF (tag .EQ. tint) THEN
        call INPUT(chan, size, 1, tuntagged)
        print *, 'Calling tranpose'
        call transpose(chan, size)
        GOTO 50
      ENDIF
      END
C}}}
C
C{{{  worker.u
      SUBROUTINE worker (nchans, chan, dim, rank)
      IMPLICIT NONE
      INTEGER dim, nchans
      INTEGER chan
      INTEGER rank (0 : dim - 1)
      INTEGER x, y
      INTEGER Isize, Icoord
C     C{{{  tags
      INTEGER tend, tint, tr32, tstr, tuntagged
      PARAMETER (tuntagged = -1)
      PARAMETER (tend = 0)
      PARAMETER (tint = 1)
      PARAMETER (tr32 = 2)
      PARAMETER (tstr = 3)
C     C}}}
      INTEGER L, A
      x = rank(0)
      y = rank(1)
C  5 * 5 problem (one element per process).
      L = Isize (x)
C     C{{{  Tell master problem size
      call OUTPUT (chan, L, 1, tint)
C     C}}}
C     C{{{  Get matrix from master
      call IN2D (chan, x, y, A, 1, tr32)
C     C}}}
C     C{{{  Output original matrix
      call OU2D (chan, x, y, A, 1, tr32)
C     C}}}
      call PSHFTI (x, A, 1, Icoord (y))
      call PREFLI (y, A, 1, Icoord (x))
      call PSHFTI (x, A, 1,-Icoord (y))
C     C{{{  Output transposed matrix
      call OU2D (chan, x, y, A, 1, tr32)
C     C}}}
C     C{{{  Tell master we've finished
      call OUTAG (chan, tend)
C     C}}}
      END
C}}}
C
C{{{  configuration
      IMPLICIT NONE
      EXTERNAL master, worker
      INTEGER xid, yid
      INTEGER xsize, ysize
      INTEGER chan, null
      INTEGER rank (2)
      PARAMETER (xid   = 0, yid   = 1)
      PARAMETER (xsize = 3, ysize = 3)
      PARAMETER (chan = 0)
      DATA rank /xid, yid/
      CALL acbegn ()
      CALL acindx (xid, xsize)
      CALL acindx (yid, ysize)
      CALL acchan (chan, 0)
      CALL acarry (master, 1, chan, 0, null)
      CALL acarry (worker, 1, chan, 2, rank)
      CALL acdone ()
      END
C}}}
