xref: /petsc/src/mat/impls/sbaij/seq/sro.c (revision b48991e40a97b45b8724565aa79cd4b2b1c79a1c)
1 /*$Id: sro.c,v 1.10 2000/09/11 19:42:15 balay Exp hzhang $*/
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,*ai=a->i,*aj=a->j,*rip,*riip;
41   MatScalar       *aa=a->a;
42   Scalar          ak;
43   int             *nzr,nz,jmin,jmax,j,k,ajk;
44   IS              iperm;  /* inverse of perm */
45 
46   PetscFunctionBegin;
47   if (!mbs) PetscFunctionReturn(0);
48 
49   ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr);
50 
51   ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr);
52 
53   ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr);
54 
55   for (i=0; i<mbs; i++) {
56     if (rip[i] - riip[i] != 0) SETERRQ(1,1,"Non-symm. permutation, use symm. permutation or general matrix format");
57   }
58   ierr = ISRestoreIndices(iperm,&riip);CHKERRA(ierr);
59   ierr = ISDestroy(iperm);CHKERRA(ierr);
60 
61 
62   /* Phase 1: find row in which to store each nonzero (r)
63 	      initialize count of nonzeros to be stored in each row (nzr) */
64 
65   nzr = (int*)PetscMalloc(mbs*sizeof(int));CHKPTRQ(nzr);
66   r   = (int*)PetscMalloc(ai[mbs]*sizeof(int));CHKPTRQ(r);
67   for (i=0; i<mbs; i++) nzr[i] = 0;
68   for (i=0; i<ai[mbs]; i++) r[i] = 0;
69 
70   /*  for each nonzero element */
71   for (i=0; i<mbs; i++){
72     nz = ai[i+1] - ai[i];
73     j = ai[i];
74     /* printf("nz = %d, j=%d\n",nz,j); */
75     while (nz--){
76       /*  --- find row (=r[j]) and column (=aj[j]) in which to store a[j] ...*/
77       k = aj[j];
78       /* printf("nz = %d, k=%d\n", nz,k); */
79 
80       if (rip[k] < rip[i]) aj[j] = i;
81       else k = i;
82 
83       r[j] = k; j++;
84       nzr[k] ++; /* increment count of nonzeros in that row */
85     }
86   }
87 
88   /* Phase 2: find new ai and permutation to apply to (aj,a)
89               determine pointers (r) to delimit rows in permuted (aj,a) */
90     for (i=0; i<mbs; i++){
91       ai[i+1] = ai[i] + nzr[i];
92       nzr[i]    = ai[i+1];
93     }
94 
95   /* determine where each (aj[j], a[j]) is stored in permuted (aj,a)
96      for each nonzero element (in reverse order) */
97   jmin = ai[0]; jmax = ai[mbs];
98   nz = jmax - jmin;
99   j = jmax-1;
100   while (nz--){
101     i = r[j];  /* row value */
102     if (aj[j] == i) r[j] = ai[i]; /* put diagonal nonzero at beginning of row */
103     else { /* put off-diagonal nonzero in last unused location in row */
104       nzr[i]--; r[j] = nzr[i];
105     }
106     j--;
107   }
108 
109   /* Phase 3: permute (aj,a) to upper triangular form (wrt new ordering) */
110   for (j=jmin; j<jmax; j++){
111     while (r[j] != j){
112       k = r[j]; r[j] = r[k]; r[k] = k;
113       ajk = aj[k]; aj[k] = aj[j]; aj[j] = ajk;
114       ak = aa[k]; aa[k] = aa[j]; aa[j] = ak;
115     }
116   }
117 
118   a->row  = perm;
119   a->icol = perm;
120   ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
121   ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
122 
123   ierr= ISRestoreIndices(perm,&rip);CHKERRA(ierr);
124 
125   ierr = PetscFree(nzr);CHKERRA(ierr);
126   ierr = PetscFree(r);CHKERRA(ierr);
127 
128   PetscFunctionReturn(0);
129 }
130 
131