Actual source code: sro.c
1: /*$Id: sro.c,v 1.26 2001/06/21 21:17:00 bsmith Exp $*/
3: #include src/mat/impls/baij/seq/baij.h
4: #include src/vec/vecimpl.h
5: #include src/inline/spops.h
6: #include src/mat/impls/sbaij/seq/sbaij.h
7: #include petscsys.h
9: /*
10: This function is used before applying a
11: symmetric reordering to matrix A that is
12: in SBAIJ format.
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.
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).
31: -- output: new index set (inew, jnew) for A and a map a2anew that maps
32: values a to anew, such that all
33: nonzero A_(perm(i),iperm(k)) will be stored in the upper triangle.
34: Note: matrix A is not permuted by this function!
35: */
36: int MatReorderingSeqSBAIJ(Mat A,IS perm)
37: {
38: Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ *)A->data;
39: int *r,ierr,i,mbs=a->mbs,*rip,*riip;
40: int *ai,*aj;
41: int *nzr,nz,jmin,jmax,j,k,ajk,len;
42: IS iperm; /* inverse of perm */
45: if (!mbs) return(0);
46: ISGetIndices(perm,&rip);
47: ISInvertPermutation(perm,PETSC_DECIDE,&iperm);
48: ISGetIndices(iperm,&riip);
50: for (i=0; i<mbs; i++) {
51: if (rip[i] != riip[i]) SETERRQ(1,"Non-symm. permutation, use symm. permutation or general matrix format");
52: }
53: ISRestoreIndices(iperm,&riip);
54: ISDestroy(iperm);
55:
56: if (!a->inew){
57: len = (mbs+1 + 2*(a->i[mbs]))*sizeof(int);
58: PetscMalloc(len,&ai);
59: aj = ai + mbs+1;
60: } else {
61: ai = a->inew; aj = a->jnew;
62: }
63: ierr = PetscMemcpy(ai,a->i,(mbs+1)*sizeof(int));
64: ierr = PetscMemcpy(aj,a->j,(a->i[mbs])*sizeof(int));
65:
66: /*
67: Phase 1: Find row index r in which to store each nonzero.
68: Initialize count of nonzeros to be stored in each row (nzr).
69: At the end of this phase, a nonzero a(*,*)=a(r(),aj())
70: s.t. a(perm(r),perm(aj)) will fall into upper triangle part.
71: */
73: PetscMalloc(mbs*sizeof(int),&nzr);
74: PetscMalloc(ai[mbs]*sizeof(int),&r);
75: for (i=0; i<mbs; i++) nzr[i] = 0;
76: for (i=0; i<ai[mbs]; i++) r[i] = 0;
77:
78: /* for each nonzero element */
79: for (i=0; i<mbs; i++){
80: nz = ai[i+1] - ai[i];
81: j = ai[i];
82: /* printf("nz = %d, j=%dn",nz,j); */
83: while (nz--){
84: /* --- find row (=r[j]) and column (=aj[j]) in which to store a[j] ...*/
85: k = aj[j]; /* col. index */
86: /* printf("nz = %d, k=%dn", nz,k); */
87: /* for entry that will be permuted into lower triangle, swap row and col. index */
88: if (rip[k] < rip[i]) aj[j] = i;
89: else k = i;
90:
91: r[j] = k; j++;
92: nzr[k] ++; /* increment count of nonzeros in that row */
93: }
94: }
96: /* Phase 2: Find new ai and permutation to apply to (aj,a).
97: Determine pointers (r) to delimit rows in permuted (aj,a).
98: Note: r is different from r used in phase 1.
99: At the end of this phase, (aj[j],a[j]) will be stored in
100: (aj[r(j)],a[r(j)]).
101: */
102: for (i=0; i<mbs; i++){
103: ai[i+1] = ai[i] + nzr[i];
104: nzr[i] = ai[i+1];
105: }
106:
107: /* determine where each (aj[j], a[j]) is stored in new (aj,a)
108: for each nonzero element (in reverse order) */
109: jmin = ai[0]; jmax = ai[mbs];
110: nz = jmax - jmin;
111: j = jmax-1;
112: while (nz--){
113: i = r[j]; /* row value */
114: if (aj[j] == i) r[j] = ai[i]; /* put diagonal nonzero at beginning of row */
115: else { /* put off-diagonal nonzero in last unused location in row */
116: nzr[i]--; r[j] = nzr[i];
117: }
118: j--;
119: }
120:
121: a->a2anew = aj + ai[mbs];
122: ierr = PetscMemcpy(a->a2anew,r,ai[mbs]*sizeof(int));
123:
124: /* Phase 3: permute (aj,a) to upper triangular form (wrt new ordering) */
125: for (j=jmin; j<jmax; j++){
126: while (r[j] != j){
127: k = r[j]; r[j] = r[k]; r[k] = k;
128: ajk = aj[k]; aj[k] = aj[j]; aj[j] = ajk;
129: /* ak = aa[k]; aa[k] = aa[j]; aa[j] = ak; */
130: }
131: }
132: ierr= ISRestoreIndices(perm,&rip);
134: a->inew = ai;
135: a->jnew = aj;
137: if (a->row) {
138: ISDestroy(a->row);
139: }
140: if (a->icol) {
141: ISDestroy(a->icol);
142: }
143: a->row = perm;
144: a->icol = perm;
145: PetscObjectReference((PetscObject)perm);
146: PetscObjectReference((PetscObject)perm);
148: PetscFree(nzr);
149: PetscFree(r);
150:
151: return(0);
152: }