Actual source code: sro.c

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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