1 /*$Id: sro.c,v 1.15 2000/09/20 14:46:08 bsmith Exp bsmith $*/ 2 3 #include "petscsys.h" 4 #include "src/mat/impls/baij/seq/baij.h" 5 #include "src/vec/vecimpl.h" 6 #include "src/inline/spops.h" 7 #include "sbaij.h" 8 9 /* 10 This function is used before applying a 11 symmetric reordering to matrix A that is 12 in SBAIJ format. 13 14 The permutation is assumed to be symmetric, i.e., 15 P = P^T (= inv(P)), 16 so the permuted matrix P*A*inv(P)=P*A*P^T is ensured to be symmetric. 17 18 The function is modified from sro.f of YSMP. The description from YSMP: 19 C THE NONZERO ENTRIES OF THE MATRIX M ARE ASSUMED TO BE STORED 20 C SYMMETRICALLY IN (IA,JA,A) FORMAT (I.E., NOT BOTH M(I,J) AND M(J,I) 21 C ARE STORED IF I NE J). 22 C 23 C SRO DOES NOT REARRANGE THE ORDER OF THE ROWS, BUT DOES MOVE 24 C NONZEROES FROM ONE ROW TO ANOTHER TO ENSURE THAT IF M(I,J) WILL BE 25 C IN THE UPPER TRIANGLE OF M WITH RESPECT TO THE NEW ORDERING, THEN 26 C M(I,J) IS STORED IN ROW I (AND THUS M(J,I) IS NOT STORED); WHEREAS 27 C IF M(I,J) WILL BE IN THE STRICT LOWER TRIANGLE OF M, THEN M(J,I) IS 28 C STORED IN ROW J (AND THUS M(I,J) IS NOT STORED). 29 30 31 -- output: new index set (ai, aj, a) for A such that all 32 nonzero A_(p(i),isp(k)) will be stored in the upper triangle. 33 Note: matrix A is not permuted by this function! 34 */ 35 #undef __FUNC__ 36 #define __FUNC__ "MatReorderingSeqSBAIJ" 37 int MatReorderingSeqSBAIJ(Mat A,IS perm) 38 { 39 Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ *)A->data; 40 int *r,ierr,i,mbs=a->mbs,*rip,*riip; 41 int *ai,*aj; 42 int *nzr,nz,jmin,jmax,j,k,ajk,len; 43 IS iperm; /* inverse of perm */ 44 45 PetscFunctionBegin; 46 if (!mbs) PetscFunctionReturn(0); 47 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 48 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 49 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 50 51 for (i=0; i<mbs; i++) { 52 if (rip[i] - riip[i] != 0) SETERRQ(1,1,"Non-symm. permutation, use symm. permutation or general matrix format"); 53 } 54 ierr = ISRestoreIndices(iperm,&riip);CHKERRA(ierr); 55 ierr = ISDestroy(iperm);CHKERRA(ierr); 56 57 len = (mbs+1 + 2*(a->i[mbs]))*sizeof(int); 58 ai = (int*)PetscMalloc(len); CHKPTRQ(ai); 59 aj = ai + mbs+1; 60 61 /* 62 ai = (int*)PetscMalloc((mbs+1)*sizeof(int));CHKPTRQ(ai); 63 aj = (int*)PetscMalloc((a->i[mbs])*sizeof(int));CHKPTRQ(aj); 64 */ 65 ierr = PetscMemcpy(ai,a->i,(mbs+1)*sizeof(int));CHKERRQ(ierr); 66 ierr = PetscMemcpy(aj,a->j,(a->i[mbs])*sizeof(int));CHKERRQ(ierr); 67 /* 68 printf("ainew= %d %d %d\n",ai[0],ai[mbs-1],ai[mbs]); 69 printf("ajnew=%d %d\n",aj[0],aj[a->i[mbs]-1]); 70 */ 71 /* 72 Phase 1: Find row index r in which to store each nonzero. 73 Initialize count of nonzeros to be stored in each row (nzr). 74 At the end of this phase, a nonzero a(*,*)=a(r(),aj()) 75 s.t. a(perm(r),perm(aj)) will fall into upper triangle part. 76 */ 77 78 nzr = (int*)PetscMalloc(mbs*sizeof(int));CHKPTRQ(nzr); 79 r = (int*)PetscMalloc(ai[mbs]*sizeof(int));CHKPTRQ(r); 80 for (i=0; i<mbs; i++) nzr[i] = 0; 81 for (i=0; i<ai[mbs]; i++) r[i] = 0; 82 83 /* for each nonzero element */ 84 for (i=0; i<mbs; i++){ 85 nz = ai[i+1] - ai[i]; 86 j = ai[i]; 87 /* printf("nz = %d, j=%d\n",nz,j); */ 88 while (nz--){ 89 /* --- find row (=r[j]) and column (=aj[j]) in which to store a[j] ...*/ 90 k = aj[j]; /* col. index */ 91 /* printf("nz = %d, k=%d\n", nz,k); */ 92 /* for entry that will be permuted into lower triangle, swap row and col. index */ 93 if (rip[k] < rip[i]) aj[j] = i; 94 else k = i; 95 96 r[j] = k; j++; 97 nzr[k] ++; /* increment count of nonzeros in that row */ 98 } 99 } 100 101 /* Phase 2: Find new ai and permutation to apply to (aj,a). 102 Determine pointers (r) to delimit rows in permuted (aj,a). 103 Note: r is different from r used in phase 1. 104 At the end of this phase, (aj[j],a[j]) will be stored in 105 (aj[r(j)],a[r(j)]). 106 */ 107 for (i=0; i<mbs; i++){ 108 ai[i+1] = ai[i] + nzr[i]; 109 nzr[i] = ai[i+1]; 110 } 111 112 /* determine where each (aj[j], a[j]) is stored in new (aj,a) 113 for each nonzero element (in reverse order) */ 114 jmin = ai[0]; jmax = ai[mbs]; 115 nz = jmax - jmin; 116 j = jmax-1; 117 while (nz--){ 118 i = r[j]; /* row value */ 119 if (aj[j] == i) r[j] = ai[i]; /* put diagonal nonzero at beginning of row */ 120 else { /* put off-diagonal nonzero in last unused location in row */ 121 nzr[i]--; r[j] = nzr[i]; 122 } 123 j--; 124 } 125 /* a->a2anew = (int*)PetscMalloc(ai[mbs]*sizeof(int));CHKPTRQ(a->a2anew); */ 126 a->a2anew = aj + ai[mbs]; 127 ierr = PetscMemcpy(a->a2anew,r,ai[mbs]*sizeof(int));CHKERRQ(ierr); 128 129 /* Phase 3: permute (aj,a) to upper triangular form (wrt new ordering) */ 130 for (j=jmin; j<jmax; j++){ 131 while (r[j] != j){ 132 k = r[j]; r[j] = r[k]; r[k] = k; 133 ajk = aj[k]; aj[k] = aj[j]; aj[j] = ajk; 134 /* ak = aa[k]; aa[k] = aa[j]; aa[j] = ak; */ 135 } 136 } 137 ierr= ISRestoreIndices(perm,&rip);CHKERRA(ierr); 138 139 a->inew = ai; 140 a->jnew = aj; 141 142 if (a->row) { 143 ierr = ISDestroy(a->row);CHKERRQ(ierr); 144 } 145 if (a->icol) { 146 ierr = ISDestroy(a->icol);CHKERRQ(ierr); 147 } 148 a->row = perm; 149 a->icol = perm; 150 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 151 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 152 153 154 ierr = PetscFree(nzr);CHKERRA(ierr); 155 ierr = PetscFree(r);CHKERRA(ierr); 156 157 PetscFunctionReturn(0); 158 } 159 160 161