      PROGRAM BUTEST

      IMPLICIT NONE

      include 'mpif.h'
      intrinsic mod

      INTEGER M, N, NNZ, err, I, NNZ_loc, NNZ_up, OFFSET, RNO, CNO
      INTEGER, DIMENSION(:), ALLOCATABLE :: IRN_loc, JCN_loc
      DOUBLE PRECISION, DIMENSION(:), ALLOCATABLE :: A_loc
      DOUBLE PRECISION, DIMENSION(:), ALLOCATABLE :: ROWSCA
      DOUBLE PRECISION, DIMENSION(:), ALLOCATABLE :: COLSCA
      DOUBLE PRECISION VAL
      INTEGER MYID, NUMPROCS
C     BORA
      DOUBLE PRECISION TIME0, TIME1
      
C     VARS FOR SCALING
      INTEGER LIWK, LWK

      INTEGER, DIMENSION(:), POINTER :: IWK
      DOUBLE PRECISION, DIMENSION(:), POINTER :: WK
C     For the new method
      INTEGER MAXMN, INTSZ, RESZ
      INTEGER REGISTRE(12)

      INTEGER, DIMENSION(:), POINTER:: RPARTVEC
      INTEGER, DIMENSION(:), POINTER:: CPARTVEC
      INTEGER, DIMENSION(:), POINTER:: RSNDRCVSZ
      INTEGER, DIMENSION(:), POINTER:: CSNDRCVSZ
      DOUBLE PRECISION ERR1, ERRINF
      INTEGER ISSYM

      CALL MPI_INIT(err)

      CALL MPI_COMM_RANK(MPI_COMM_WORLD, MYID, err)
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD, NUMPROCS, err)

      open(77,file='boratoymtx_5_5.txt')

      read(77,*) M,N,NNZ

      write(*,*) 'I=', MYID, 'M ', M, ' N ', N, ' NNZ ', NNZ


      NNZ_up = NNZ
      write(*,*) 'I=', MYID, 'nnz up ', NNZ_up
      ALLOCATE(IRN_loc(NNZ_up), stat=err)
      ALLOCATE(JCN_loc(NNZ_up), stat=err)
      ALLOCATE(A_loc(NNZ_up),   stat=err)
      
      offset =0
      DO I=1,NNZ
         READ(77,*) RNO, CNO, VAL
      IF(mod(I-1, NUMPROCS) == MYID) THEN
      OFFSET = OFFSET + 1
      IRN_loc(OFFSET) = RNO
      JCN_loc(OFFSET) = CNO
      A_loc(OFFSET) = VAL
         ENDIF
      ENDDO
      NNZ_loc = OFFSET
      close(77)
      write(6,*) 'I=', MYID, ' N ', N, ' NNZ ',
     *     NNZ, ' NNZ_loc ', NNZ_loc

      
      MAXMN=M
      IF(N > MAXMN) MAXMN = N
      LIWK = 4*MAXMN
      ALLOCATE (IWK(LIWK), stat=err)
      ALLOCATE (RPARTVEC(M), stat=err)
      ALLOCATE (CPARTVEC(N), stat=err)
      ALLOCATE (RSNDRCVSZ(2*NUMPROCS), stat=err)
      ALLOCATE (CSNDRCVSZ(2*NUMPROCS), stat=err)
      ALLOCATE (ROWSCA(M), stat=err)
      ALLOCATE (COLSCA(N), stat=err)
      ISSYM = 0
      CALL DMUMPS_SIMSCALEABS(IRN_loc, JCN_loc, A_loc,NNZ_loc,
     *     M, N, NUMPROCS, MYID, MPI_COMM_WORLD,
     *     RPARTVEC, CPARTVEC,
     *     RSNDRCVSZ, CSNDRCVSZ, REGISTRE,
     *     IWK, LIWK,
     *     INTSZ, RESZ, 1,
     *     ROWSCA, COLSCA, WK, 0,
     *     ISSYM, 3, 3, 3, 1.d-3, ERR1, ERRINF)
      write(6,*) MYID, "first phase is over"
      flush(6)

      IF(LIWK < INTSZ) THEN
         write(6,*) MYID, "Reallocating ", LIWK, INTSZ
         flush(6)
         DEALLOCATE(IWK)
         LIWK = INTSZ
         ALLOCATE(IWK(LIWK), stat=err)
      ENDIF
      LWK = RESZ
      ALLOCATE (WK(LWK), stat=err)
      TIME0 = MPI_WTIME(err)
      CALL  DMUMPS_SIMSCALEABS(IRN_loc, JCN_loc, A_loc, NNZ_loc,
     *     M, N, NUMPROCS, MYID, MPI_COMM_WORLD,
     *     RPARTVEC, CPARTVEC,
     *     RSNDRCVSZ, CSNDRCVSZ, REGISTRE,
     *     IWK, LIWK,
     *     INTSZ, RESZ, 2,
     *     ROWSCA, COLSCA, WK, LWK,
     *     ISSYM, 3, 3, 3, 1.d-3, ERR1, ERRINF) 

      TIME1 = MPI_WTIME(err)
      IF(ISSYM == 0)THEN
C        DO I=1,NNZ_loc
C           IF((IRN_loc(I).GE.1).AND.(IRN_loc(I).LE.M).AND.
C    &          (JCN_loc(I).GE.1).AND.(JCN_loc(I).LE.N)) THEN
C               WRITE(6,*) 'myent ',MYID , ' ',IRN_loc(I), ' ',
C     *              JCN_loc(I),' ',
C     *              A_loc(I)*ROWSCA(IRN_loc(I))*COLSCA(JCN_loc(I)), ' ',
C     *              A_loc(I)
C            ENDIF
C         ENDDO
      ELSE
C         DO I=1,NNZ_loc
C            IF((IRN_loc(I).GE.1).AND.(IRN_loc(I).LE.M).AND.
C     &           (JCN_loc(I).GE.1).AND.(JCN_loc(I).LE.N)) THEN
C               WRITE(6,*) 'myent ',MYID , ' ',IRN_loc(I), ' ',
C     *              JCN_loc(I),' ',
C     *              A_loc(I)*ROWSCA(IRN_loc(I))*ROWSCA(JCN_loc(I)), ' ',
C     *              A_loc(I)
C            ENDIF
C         ENDDO
      ENDIF
      
      write(*,*) 'my ', MYID, ' time ', TIME1-TIME0 
      write(*,*) 'my ', MYID, ' err1 ', ERR1
      write(*,*) 'my ', MYID, ' errinf ', ERRINF
C      IF(MYID.EQ.0) THEN
C         DO I=1,N
C            write(*,*), 'RS(',I,')', ROWSCA(I)
C         ENDDO
C         DO I=1,N
C            write(*,*), 'CS(',I,')', COLSCA(I)
C         ENDDO
C      ENDIF
      DEALLOCATE(IWK)
      DEALLOCATE(WK)
      DEALLOCATE(A_loc)
      DEALLOCATE(IRN_loc)
      DEALLOCATE(JCN_loc)
      DEALLOCATE(ROWSCA)
      DEALLOCATE(COLSCA)

      DEALLOCATE(RPARTVEC)
      DEALLOCATE(CPARTVEC)
      DEALLOCATE(RSNDRCVSZ)
      DEALLOCATE( CSNDRCVSZ)

      CALL MPI_FINALIZE(err)
      END PROGRAM BUTEST
