C RPERM     SOURCE    CHAT      05/01/13    03:07:15     5004
      SUBROUTINE RPERM (NROW,A,JA,IA,AO,JAO,IAO,PERM,JOB)
      IMPLICIT INTEGER(I-N)
      IMPLICIT REAL*8 (A-H,O-Z)
C***********************************************************************
C NOM         : RPERM
C DESCRIPTION : Permutation des lignes d'une matrice Morse.
C
C
C LANGAGE     : FORTRAN 77
C ADAPTATION  :  Stéphane GOUNAND (CEA/DRN/DMT/SEMT/LTMF)
C                mél : gounand@semt2.smts.cea.fr
C AUTEUR      :
C  Sparskit : a basic tool kit for sparse matrix computations
C  Version 2 (Youcef Saad)
C  -> URL : http://www.cs.umn.edu/Research/arpa/SPARSKIT/sparskit.html
C
C***********************************************************************
      INTEGER NROW,JA(*),IA(NROW+1),JAO(*),IAO(NROW+1),PERM(NROW),JOB
      REAL*8 A(*),AO(*)
c-----------------------------------------------------------------------
c this subroutine permutes the rows of a matrix in CSR format.
c rperm  computes B = P A  where P is a permutation matrix.
c the permutation P is defined through the array perm: for each j,
c perm(j) represents the destination row number of row number j.
c Youcef Saad -- recoded Jan 28, 1991.
c-----------------------------------------------------------------------
c on entry:
c----------
c n     = dimension of the matrix
c a, ja, ia = input matrix in csr format
c perm  = integer array of length nrow containing the permutation arrays
c         for the rows: perm(i) is the destination of row i in the
c         permuted matrix.
c         ---> a(i,j) in the original matrix becomes a(perm(i),j)
c         in the output  matrix.
c
c job   = integer indicating the work to be done:
c               job = 1 permute a, ja, ia into ao, jao, iao
c                       (including the copying of real values ao and
c                       the array iao).
c               job .ne. 1 :  ignore real values.
c                     (in which case arrays a and ao are not needed nor
c                      used).
c
c------------
c on return:
c------------
c ao, jao, iao = input matrix in a, ja, ia format
c note :
c        if (job.ne.1)  then the arrays a and ao are not used.
c----------------------------------------------------------------------c
c           Y. Saad, May  2, 1990                                      c
c----------------------------------------------------------------------c
C Local variables
      INTEGER I,II,J,K,KO
      LOGICAL VALUES
      VALUES = (JOB .EQ. 1)
c
c     determine pointers for output matix.
c
      DO 50 J=1,NROW
         I = PERM(J)
         IAO(I+1) = IA(J+1) - IA(J)
 50   CONTINUE
c
c get pointers from lengths
c
      IAO(1) = 1
      DO 51 J=1,NROW
         IAO(J+1)=IAO(J+1)+IAO(J)
 51   CONTINUE
c
c copying
c
      DO 100 II=1,NROW
c
c old row = ii  -- new row = iperm(ii) -- ko = new pointer
c
         KO = IAO(PERM(II))
         DO 60 K=IA(II), IA(II+1)-1
            JAO(KO) = JA(K)
            IF (VALUES) AO(KO) = A(K)
            KO = KO+1
 60      CONTINUE
 100  CONTINUE
c
      RETURN
c---------end-of-rperm -------------------------------------------------
c-----------------------------------------------------------------------
      END


