      SUBROUTINE PZLUR( M, N, A, LDA, IPIV, ICURROW, 
     $     WORK, LDWORK, NB, INFO )
*
*     .. Scalar Arguments ..
      INTEGER            INFO, LDA, M, N
*     ..
*     .. Array Arguments ..
      INTEGER            IPIV( * )
      COMPLEX*16         A( LDA, * )
*
*     icur               index of row that holds current diagonal 
*                        block
*     nb                 block size of wrapping
*
      COMPLEX*16         WORK(LDWORK, *)
      INTEGER            ICURROW, NB, LDWORK
*     ..
*
*  Purpose
*  =======
*
*  PZLUR computes an LU factorization of a general m-by-n matrix A
*  using partial pivoting with row interchanges.
*
*  The factorization has the form
*     A = P * L * U
*  where P is a permutation matrix, L is lower triangular with unit
*  diagonal elements (lower trapezoidal if m > n), and U is upper
*  triangular (upper trapezoidal if m < n).
*
*  This is the right-looking Level 2 BLAS version of the algorithm.
*
*  Arguments
*  =========
*
*  M       (input) INTEGER
*          The number of rows of the matrix A.  M >= 0.
*
*  N       (input) INTEGER
*          The number of columns of the matrix A.  N >= 0.
*
*  A       (input/output) COMPLEX*16 array, dimension (LDA,N)
*          On entry, the m by n matrix to be factored.
*          On exit, the factors L and U; the unit diagonal elements of L
*          are not stored.
*
*  LDA     (input) INTEGER
*          The leading dimension of the array A.  LDA >= max(1,M).
*
*  IPIV    (output) INTEGER array, dimension (min(M,N))
*          The pivot indices.  Row i of the matrix was interchanged with
*          row IPIV(i).
*
*  INFO    (output) INTEGER
*          = 0: successful exit
*          < 0: if INFO = -k, the k-th argument had an illegal value
*          > 0: if INFO = k, U(k,k) is exactly zero. The factorization
*               has been completed, but the factor U is exactly
*               singular, and division by zero will occur if it is used
*               to solve a system of equations or to compute the inverse
*               of A.
*
*  =====================================================================
*
*     VERSION 2.0 DATED Aug. 1992
*     R. VAN DE GEIJN
*
*        This code resulted from a joint collaboration between  
*              Robert van de Geijn 
*     and the group for 'Research in Parallel Computational
*     Electromagnetics' at the Jet Propulsion Laboratory.  Futher 
*     information can be found in the paper,
*
*         T. Cwik, R. van de Geijn, and J. Patterson, 
*         "Application of Massively Parallel Computation to 
*          Integral Equation Models of Electromagnetic Scattering 
*          (Invited Paper)," J. Opt. Soc. Am. A, Vol. 11, No. 4, 
*          April 1994.
*
*     Please email your comments to the authors at
*
*        rvdg@cs.utexas.edu (Robert van de Geijn)
*        cwik@yosemite.jpl.nasa.gov (Tom Cwik)
*        jep@yosemite.jpl.nasa.gov  (Jean Patterson)
*
*     Use of this code must properly reference that paper.
*
*     The code is free, and comes with no implicit or explicit 
*     guarantees.  
*
*
*     All rights reserved
*
*     .. Parameters ..
      COMPLEX*16         ONE, ZERO
      PARAMETER          ( ONE = (1.0D+0,0.0D0), 
     $                     ZERO = (0.0D+0,0.0D0) )
*     ..
*     .. Local Scalars ..
      INTEGER            J, JP
*     ..
*     .. local scalars ..
*
*     nprow          row dimension of node grid
*     npcol          column dimension of node grid
*
*     IMPORTANT:  this code assumes nprow < npcol
*
*     myrow          my row index
*     mycol          my column index
*
      INTEGER           NPROW, NPCOL, MYROW, MYCOL, MYRROW
      INTEGER           MYMROW, IPIVNODE
      INTEGER           IDUMMY
      DOUBLE PRECISION  DDUMMY, DTEMP
*     ..
*     .. External Functions ..
      INTEGER           IZAMAX
      EXTERNAL          IZAMAX
*     ..
*     .. external functions ..
      INTEGER           ITYPE_TO, ITYPE_FROM, ITYPE_COL
      DOUBLE PRECISION  DCABS1
*     ..
*     .. External Subroutines ..
      EXTERNAL           DGER, DSCAL, DSWAP, XERBLA, PLAMCH2
*     ..
*     .. Intrinsic Functions ..
      INTRINSIC          MAX, MIN
*     ..
*     .. Executable Statements ..
*
*     Test the input parameters.
*
      INFO = 0
      IF( M.LT.0 ) THEN
         INFO = -1
      ELSE IF( N.LT.0 ) THEN
         INFO = -2
*      ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
*         INFO = -4
      END IF
      IF( INFO.NE.0 ) THEN
         CALL XERBLA( 'PDLUR  ', -INFO )
         RETURN
      END IF
*
*     get machine parameters
*
      CALL PLAMCH2( NPROW, NPCOL, MYROW, MYCOL )

*
*     myrrow =    row index of node relative to node with row index
*                 icurrow
*                 
      MYRROW = MOD( MYROW - ICURROW + NPROW, NPROW )

*
*     mymrow = number of rows assigned to this node
*
      CALL IMYPART( 1, M, NB, IDUMMY, MYMROW, MYRROW, NPROW )

*
      DO 10 J = 1, MIN( NB, N )
*
*           Find pivot and test for singularity.
*
         IF ( MYROW .EQ. ICURROW ) THEN
            JP = J - 1 + IZAMAX( MYMROW-J+1, A( J, J ), 1 )
            CALL ZCOPY( N, A( JP, 1 ), LDA, WORK( J, 1 ), 
     $           LDWORK )
         ELSE
            IF (MYMROW .GE. 1) THEN
               JP = IZAMAX( MYMROW, A( 1, J ), 1 )
               CALL ZCOPY( N, A( JP, 1 ), LDA, WORK( J, 1 ), 
     $              LDWORK )
            ELSE
               JP = 0
               WORK(J,J) = 0.0D0
            ENDIF
         ENDIF
c
c        determine pivot row
c
         DTEMP = DCABS1( WORK( J, J ) )
         CALL DGAMAX2D( 'COLUMN', 1, 1, DTEMP, 1, DDUMMY, 1, 
     $        IPIVNODE, 1, IDUMMY, 1, -1, MYCOL )

         IF ( DDUMMY .EQ. 0.0D00 ) IPIVNODE = ICURROW
c
c        send pivot row to all nodes in this column
c
         CALL ZGEBC2D( 'CTREE', 'COLUMN', 1, N, WORK( J, 1 ), LDWORK,
     $        IPIVNODE, MYCOL, ITYPE_COL() )

         IF (MYROW .EQ. ICURROW) THEN
            IF ( IPIVNODE .EQ. MYROW ) THEN
               IF ( J .NE. JP ) 
     $              CALL ZSWAP( N, A( J, 1 ), LDA, A( JP, 1 ), 
     $              LDA )
               IPIV( J ) = JP
            ELSE
               CALL ZGESD2D( 1, N, A( J, 1 ), LDA, IPIVNODE, 
     $              MYCOL, ITYPE_TO( IPIVNODE, MYCOL ) )
               CALL ZCOPY( N, WORK( J, 1 ), LDWORK, A( J, 1 ), LDA )
               IPIV( J ) = - IPIVNODE
            ENDIF
         ELSEIF ( MYROW .EQ. IPIVNODE ) THEN
            CALL ZGERV2D( 1, N, A( JP, 1 ), LDA, 
     $           ITYPE_FROM( ICURROW, MYCOL) )
            IPIV( J ) = JP
         ELSE
            IPIV( J ) = - IPIVNODE
         ENDIF

         IF (MYROW .EQ. 0 .AND. MYCOL .EQ. 0) THEN
*            call prtmat( 1, nb, work( j, 1 ), ldwork )
*            call prtmat( 1, nb, a( j, 1 ), lda )
         ENDIF
*
*           Compute elements J+1:M of J-th column.
*
         IF (WORK ( J, J ) .EQ. ZERO ) THEN
            PRINT *, "EXACT SINGULARITY DETECTED:TROUBLE !!"
         ELSE   
            IF (MYROW .EQ. ICURROW) THEN
               IF( J.LT.MYMROW )
     $              CALL ZSCAL( MYMROW-J, ONE/WORK( J, J ), 
     $              A( J+1, J ), 1 )
            ELSE
               IF ( MYMROW .GT. 0 )
     $              CALL ZSCAL( MYMROW, ONE/WORK( J, J ), A( 1, J ),
     $              1 )
            ENDIF
         ENDIF
*
*

         IF( J+1.LE.N .AND. WORK (J, J) .NE. ZERO) THEN
*
*           Update trailing submatrix.
*
            IF ( MYROW .EQ. ICURROW ) THEN
               IF (MYMROW .GT. J) 
     $              CALL ZGERU( MYMROW-J, N-J, -ONE, A( J+1, J ), 
     $              1, 
     $              WORK ( J, J+1 ), LDWORK, A( J+1, J+1 ), LDA )
            ELSEIF ( MYMROW .GT. 0 ) THEN
               CALL ZGERU( MYMROW, N-J, -ONE, A( 1, J ), 1, 
     $              WORK ( J, J+1 ), LDWORK, A( 1, J+1 ), LDA )
            ENDIF
         END IF

 10   CONTINUE

      RETURN
*
*     End of PZLUR
*
      END
 

