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