      SUBROUTINE SGETF2( M, N, A, LDA, IPIV, INFO )
      IMPLICIT NONE
*
*     Noblock routine for LU Factorization
*
*  -- NPAC, Syracuse University --
*     modified by Manish Parashar
*     April 1992
*
*  -- LAPACK routine (preliminary version) --
*     Univ. of Tennessee, Oak Ridge National Lab, Argonne National Lab,
*     Courant Institute, NAG Ltd., and Rice University
*     March 26, 1990

*
*     .. Scalar Arguments ..
      INTEGER            INFO, LDA, M, N
*     ..
*     .. Array Arguments ..
      integer IPIV(:)
      real A(:,:)
*     ..
*
*  Purpose
*  =======
*
*  SGETF2 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 Level 2 BLAS version of the Crout 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) REAL array, dimension (LDA,N)
*          On entry, the m x 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.
*
*  =====================================================================
*
*     .. Parameters ..
      REAL               ONE, ZERO
      PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
*     ..
*     .. Local Scalars ..
      INTEGER            J, JP
      integer i,ii, mp
*     ..
*     .. Intrinsic Functions ..
       INTRINSIC          MAX, MIN, matmul
*     ..
*     .. Local Arrays ..
      integer            pivot_index(1)
      real               temp(n)

*     .. Executable Statements ..
*
      a = a
      ipiv = ipiv
      temp = temp
      pivot_index = pivot_index
*
*     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( 'SGETF2', -INFO )
         RETURN
      END IF
*
*     Quick return if possible.
*
      IF( M.EQ.0 .OR. N.EQ.0 ) RETURN
*
      DO 10 J = 1, MIN( M, N )
*
*        Update diagonal and subdiagonal elements in column J.
*
         if( J .gt. 1 )then
            A(J:M,J)= A(J:M,J) - matmul(A(J:M,1:J-1),A(1:J-1,J))
         endif


*
*        Find pivot and test for singularity.
*
c         JP = J - 1 + ISAMAX( M-J+1, A( J, J ), 1 )

         pivot_index= maxloc(abs(A(J:M,J)))
                JP = J - 1 + pivot_index(1)

c        pivot_index = firstmaxoffset(abs(A(J:M,J)))
c        JP = J + pivot_index(1)

         IPIV( J ) = JP
         IF( A( JP, J ).NE.ZERO ) THEN
*
*           Apply interchange to columns 1:J.
*
            IF( JP.NE.J ) then
c     $         CALL SSWAP( N, A( J, 1 ), LDA, A( JP, 1 ), LDA )
               temp= A(J,:)
               A(J,:)= A(JP,:)
               A(JP,:)= temp
            endif


*
*           Compute elements J+1:M of J-th column.
*
            IF( J.LT.M )then
c     $         CALL SSCAL( M-J, ONE/A( J, J ), A( J+1, J ), 1 )
               A(J+1:M,J)= A(J+1:M,J) / A(J,J)
            endif

         ELSE
*
*           If A( JP, J ) is zero, set INFO to indicate that a zero
*           pivot has been found.  INFO returns the index of the
*           last zero pivot to the calling routine if there is more
*           than one.
*
            INFO = J
         END IF
*
         IF( J+1.LE.N ) THEN
*
*           Compute block row of U.
*
c            CALL SGEMV( 'Transpose', J-1, N-J, -ONE, A( 1, J+1 ), LDA,
c     $                  A( J, 1 ), LDA, ONE, A( J, J+1 ), LDA )
            if( J .gt. 1 )then

c              tmpo(j,j+1:n) =  matmul(A(J,1:J-1),A(1:J-1,J+1:N))

               A(J,J+1:N)= A(J,J+1:N) 
     &              - matmul(A(J,1:J-1),A(1:J-1,J+1:N))

            endif

         END IF
 10   end do ! CONTINUE

      RETURN
*
*     End of SGETF2
*
      END

