Actual source code: sro.c


  2: #include <../src/mat/impls/baij/seq/baij.h>
  3: #include <../src/mat/impls/sbaij/seq/sbaij.h>

  5: /*
  6: This function is used before applying a
  7: symmetric reordering to matrix A that is
  8: in SBAIJ format.

 10: The permutation is assumed to be symmetric, i.e.,
 11: P = P^T (= inv(P)),
 12: so the permuted matrix P*A*inv(P)=P*A*P^T is ensured to be symmetric.
 13:  - a wrong assumption! This code needs rework!  -- Hong

 15: The function is modified from sro.f of YSMP. The description from YSMP:
 16: C    THE NONZERO ENTRIES OF THE MATRIX M ARE ASSUMED TO BE STORED
 17: C    SYMMETRICALLY IN (IA,JA,A) FORMAT (I.E., NOT BOTH M(I,J) AND M(J,I)
 18: C    ARE STORED IF I NE J).
 19: C
 20: C    SRO DOES NOT REARRANGE THE ORDER OF THE ROWS, BUT DOES MOVE
 21: C    NONZEROES FROM ONE ROW TO ANOTHER TO ENSURE THAT IF M(I,J) WILL BE
 22: C    IN THE UPPER TRIANGLE OF M WITH RESPECT TO THE NEW ORDERING, THEN
 23: C    M(I,J) IS STORED IN ROW I (AND THUS M(J,I) IS NOT STORED);  WHEREAS
 24: C    IF M(I,J) WILL BE IN THE STRICT LOWER TRIANGLE OF M, THEN M(J,I) IS
 25: C    STORED IN ROW J (AND THUS M(I,J) IS NOT STORED).

 27:   -- output: new index set (inew, jnew) for A and a map a2anew that maps
 28:              values a to anew, such that all
 29:              nonzero A_(perm(i),iperm(k)) will be stored in the upper triangle.
 30:              Note: matrix A is not permuted by this function!
 31: */
 32: PetscErrorCode MatReorderingSeqSBAIJ(Mat A, IS perm)
 33: {
 34:   Mat_SeqSBAIJ  *a   = (Mat_SeqSBAIJ *)A->data;
 35:   const PetscInt mbs = a->mbs;

 37:   PetscFunctionBegin;
 38:   if (!mbs) PetscFunctionReturn(PETSC_SUCCESS);
 39:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Matrix reordering is not supported for sbaij matrix. Use aij format");
 40: #if 0
 41:   const PetscInt *rip,*riip;
 42:   PetscInt       *ai,*aj,*r;
 43:   PetscInt       *nzr,nz,jmin,jmax,j,k,ajk,i;
 44:   IS             iperm;  /* inverse of perm */
 45:   PetscCall(ISGetIndices(perm,&rip));

 47:   PetscCall(ISInvertPermutation(perm,PETSC_DECIDE,&iperm));
 48:   PetscCall(ISGetIndices(iperm,&riip));

 50:   for (i=0; i<mbs; i++) {
 51:     PetscCheck(rip[i] == riip[i],PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Non-symmetric permutation, use symmetric permutation for symmetric matrices");
 52:   }
 53:   PetscCall(ISRestoreIndices(iperm,&riip));
 54:   PetscCall(ISDestroy(&iperm));

 56:   if (!a->inew) {
 57:     PetscCall(PetscMalloc2(mbs+1,&ai, 2*a->i[mbs],&aj));
 58:   } else {
 59:     ai = a->inew; aj = a->jnew;
 60:   }
 61:   PetscCall(PetscArraycpy(ai,a->i,mbs+1));
 62:   PetscCall(PetscArraycpy(aj,a->j,a->i[mbs]));

 64:   /*
 65:      Phase 1: Find row index r in which to store each nonzero.
 66:               Initialize count of nonzeros to be stored in each row (nzr).
 67:               At the end of this phase, a nonzero a(*,*)=a(r(),aj())
 68:               s.t. a(perm(r),perm(aj)) will fall into upper triangle part.
 69:   */

 71:   PetscCall(PetscMalloc1(mbs,&nzr));
 72:   PetscCall(PetscMalloc1(ai[mbs],&r));
 73:   for (i=0; i<mbs; i++) nzr[i] = 0;
 74:   for (i=0; i<ai[mbs]; i++) r[i] = 0;

 76:   /*  for each nonzero element */
 77:   for (i=0; i<mbs; i++) {
 78:     nz = ai[i+1] - ai[i];
 79:     j  = ai[i];
 80:     /* printf("nz = %d, j=%d\n",nz,j); */
 81:     while (nz--) {
 82:       /*  --- find row (=r[j]) and column (=aj[j]) in which to store a[j] ...*/
 83:       k = aj[j];                          /* col. index */
 84:       /* printf("nz = %d, k=%d\n", nz,k); */
 85:       /* for entry that will be permuted into lower triangle, swap row and col. index */
 86:       if (rip[k] < rip[i]) aj[j] = i;
 87:       else k = i;

 89:       r[j] = k; j++;
 90:       nzr[k]++;  /* increment count of nonzeros in that row */
 91:     }
 92:   }

 94:   /* Phase 2: Find new ai and permutation to apply to (aj,a).
 95:               Determine pointers (r) to delimit rows in permuted (aj,a).
 96:               Note: r is different from r used in phase 1.
 97:               At the end of this phase, (aj[j],a[j]) will be stored in
 98:               (aj[r(j)],a[r(j)]).
 99:   */
100:   for (i=0; i<mbs; i++) {
101:     ai[i+1] = ai[i] + nzr[i];
102:     nzr[i]  = ai[i+1];
103:   }

105:   /* determine where each (aj[j], a[j]) is stored in new (aj,a)
106:      for each nonzero element (in reverse order) */
107:   jmin = ai[0]; jmax = ai[mbs];
108:   nz   = jmax - jmin;
109:   j    = jmax-1;
110:   while (nz--) {
111:     i = r[j];  /* row value */
112:     if (aj[j] == i) r[j] = ai[i]; /* put diagonal nonzero at beginning of row */
113:     else { /* put off-diagonal nonzero in last unused location in row */
114:       nzr[i]--; r[j] = nzr[i];
115:     }
116:     j--;
117:   }

119:   a->a2anew = aj + ai[mbs];
120:   PetscCall(PetscArraycpy(a->a2anew,r,ai[mbs]));

122:   /* Phase 3: permute (aj,a) to upper triangular form (wrt new ordering) */
123:   for (j=jmin; j<jmax; j++) {
124:     while (r[j] != j) {
125:       k   = r[j]; r[j] = r[k]; r[k] = k;
126:       ajk = aj[k]; aj[k] = aj[j]; aj[j] = ajk;
127:       /* ak = aa[k]; aa[k] = aa[j]; aa[j] = ak; */
128:     }
129:   }
130:   PetscCall(ISRestoreIndices(perm,&rip));

132:   a->inew = ai;
133:   a->jnew = aj;

135:   PetscCall(ISDestroy(&a->row));
136:   PetscCall(ISDestroy(&a->icol));
137:   PetscCall(PetscObjectReference((PetscObject)perm));
138:   PetscCall(ISDestroy(&a->row));
139:   a->row  = perm;
140:   PetscCall(PetscObjectReference((PetscObject)perm));
141:   PetscCall(ISDestroy(&a->icol));
142:   a->icol = perm;

144:   PetscCall(PetscFree(nzr));
145:   PetscCall(PetscFree(r));
146:   PetscFunctionReturn(PETSC_SUCCESS);
147: #endif
148: }