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