xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 94473badd19c10f53c8dd6928da28e8951fa3baf)
1cb512458SBarry Smith #ifndef lint
2*94473badSLois Curfman McInnes static char vcid[] = "$Id: aijfact.c,v 1.32 1995/08/24 21:02:21 curfman Exp curfman $";
3cb512458SBarry Smith #endif
4289bc588SBarry Smith 
5289bc588SBarry Smith 
6289bc588SBarry Smith #include "aij.h"
78c37ef55SBarry Smith #include "inline/spops.h"
8289bc588SBarry Smith /*
9289bc588SBarry Smith     Factorization code for AIJ format.
10289bc588SBarry Smith */
11289bc588SBarry Smith 
1249d8b64dSBarry Smith int MatLUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,double f,Mat *fact)
13289bc588SBarry Smith {
141fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew;
15289bc588SBarry Smith   IS      isicol;
166abc6512SBarry Smith   int     *r,*ic, ierr, i, n = aij->m, *ai = aij->i, *aj = aij->j;
176abc6512SBarry Smith   int     *ainew,*ajnew, jmax,*fill, *ajtmp, nz;
182fb3ed76SBarry Smith   int     *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im;
19289bc588SBarry Smith 
20d35516d3SLois Curfman McInnes   if (n != aij->n)
21bbb6d6a8SBarry Smith     SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must be square");
22d35516d3SLois Curfman McInnes   if (!isrow)
23bbb6d6a8SBarry Smith     SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must have row permutation");
24d35516d3SLois Curfman McInnes   if (!iscol)
25bbb6d6a8SBarry Smith     SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must have column permutation");
26289bc588SBarry Smith 
2778b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
28289bc588SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
29289bc588SBarry Smith 
30289bc588SBarry Smith   /* get new row pointers */
3178b31e54SBarry Smith   ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
32289bc588SBarry Smith   ainew[0] = 1;
33289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
34bbb6d6a8SBarry Smith   jmax = (int) (f*ai[n]);
3578b31e54SBarry Smith   ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
36289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
372fb3ed76SBarry Smith   fill = (int *) PETSCMALLOC( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
382fb3ed76SBarry Smith   im = fill + n + 1;
39289bc588SBarry Smith   /* idnew is location of diagonal in factor */
4078b31e54SBarry Smith   idnew = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(idnew);
41289bc588SBarry Smith   idnew[0] = 1;
42289bc588SBarry Smith 
43289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
44289bc588SBarry Smith     /* first copy previous fill into linked list */
45289bc588SBarry Smith     nnz = nz    = ai[r[i]+1] - ai[r[i]];
46289bc588SBarry Smith     ajtmp = aj + ai[r[i]] - 1;
47289bc588SBarry Smith     fill[n] = n;
48289bc588SBarry Smith     while (nz--) {
49289bc588SBarry Smith       fm = n;
50289bc588SBarry Smith       idx = ic[*ajtmp++ - 1];
51289bc588SBarry Smith       do {
52289bc588SBarry Smith         m = fm;
53289bc588SBarry Smith         fm = fill[m];
54289bc588SBarry Smith       } while (fm < idx);
55289bc588SBarry Smith       fill[m] = idx;
56289bc588SBarry Smith       fill[idx] = fm;
57289bc588SBarry Smith     }
58289bc588SBarry Smith     row = fill[n];
59289bc588SBarry Smith     while ( row < i ) {
602fb3ed76SBarry Smith       ajtmp = ajnew + idnew[row];
612fb3ed76SBarry Smith       nzbd = 1 + idnew[row] - ainew[row];
622fb3ed76SBarry Smith       nz = im[row] - nzbd;
63289bc588SBarry Smith       fm = row;
642fb3ed76SBarry Smith       while (nz-- > 0) {
652fb3ed76SBarry Smith         /* fm = n;  */
66289bc588SBarry Smith         idx = *ajtmp++ - 1;
672fb3ed76SBarry Smith         nzbd++;
682fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
69289bc588SBarry Smith         do {
70289bc588SBarry Smith           m = fm;
71289bc588SBarry Smith           fm = fill[m];
72289bc588SBarry Smith         } while (fm < idx);
73289bc588SBarry Smith         if (fm != idx) {
74289bc588SBarry Smith           fill[m] = idx;
75289bc588SBarry Smith           fill[idx] = fm;
76289bc588SBarry Smith           fm = idx;
77289bc588SBarry Smith           nnz++;
78289bc588SBarry Smith         }
79d5f53d93SBarry Smith /*  printf("i %d row %d nz %d idx %d fm %d\n",i,row,nz,idx,fm);  */
80289bc588SBarry Smith       }
81289bc588SBarry Smith       row = fill[row];
82289bc588SBarry Smith     }
83289bc588SBarry Smith     /* copy new filled row into permanent storage */
84289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
85289bc588SBarry Smith     if (ainew[i+1] > jmax+1) {
86289bc588SBarry Smith       /* allocate a longer ajnew */
872fb3ed76SBarry Smith       int maxadd;
88bbb6d6a8SBarry Smith       maxadd = (int) ((f*ai[n]*(n-i+5))/n);
89bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
902fb3ed76SBarry Smith       jmax += maxadd;
9178b31e54SBarry Smith       ajtmp = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(ajtmp);
9278b31e54SBarry Smith       PETSCMEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int));
9378b31e54SBarry Smith       PETSCFREE(ajnew);
94289bc588SBarry Smith       ajnew = ajtmp;
952fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
96289bc588SBarry Smith     }
97289bc588SBarry Smith     ajtmp = ajnew + ainew[i] - 1;
98289bc588SBarry Smith     fm = fill[n];
99289bc588SBarry Smith     nzi = 0;
1002fb3ed76SBarry Smith     im[i] = nnz;
101289bc588SBarry Smith     while (nnz--) {
102289bc588SBarry Smith       if (fm < i) nzi++;
103289bc588SBarry Smith       *ajtmp++ = fm + 1;
104289bc588SBarry Smith       fm = fill[fm];
105289bc588SBarry Smith     }
106289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
107289bc588SBarry Smith   }
108289bc588SBarry Smith 
1092fb3ed76SBarry Smith   PLogInfo((PetscObject)mat,
110bbb6d6a8SBarry Smith     "Info:MatLUFactorSymbolic_AIJ:Reallocs %d Fill ratio:given %g needed %g\n",
111bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
1122fb3ed76SBarry Smith 
113898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
114898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
115898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
116898183ecSLois Curfman McInnes   PETSCFREE(fill);
117289bc588SBarry Smith 
118289bc588SBarry Smith   /* put together the new matrix */
11978b31e54SBarry Smith   ierr = MatCreateSequentialAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr);
1201fb19edaSLois Curfman McInnes   aijnew = (Mat_AIJ *) (*fact)->data;
12178b31e54SBarry Smith   PETSCFREE(aijnew->imax);
122289bc588SBarry Smith   aijnew->singlemalloc = 0;
123f0479e8cSBarry Smith   len = (ainew[n] - 1)*sizeof(Scalar);
124e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
12578b31e54SBarry Smith   PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen);
12678b31e54SBarry Smith   aijnew->a          = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a);
127289bc588SBarry Smith   aijnew->j          = ajnew;
128289bc588SBarry Smith   aijnew->i          = ainew;
1298c37ef55SBarry Smith   aijnew->diag       = idnew;
130e8d4e0b9SBarry Smith   aijnew->ilen       = 0;
13120563c6bSBarry Smith   aijnew->imax       = 0;
13286d7a8ceSBarry Smith   aijnew->row        = isrow;
13386d7a8ceSBarry Smith   aijnew->col        = iscol;
13478b31e54SBarry Smith   aijnew->solve_work = (Scalar *) PETSCMALLOC( n*sizeof(Scalar));
13578b31e54SBarry Smith   CHKPTRQ(aijnew->solve_work);
1367fd98bd6SLois Curfman McInnes   /* In aijnew structure:  Free imax, ilen, old a, old j.
1377fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
138033c1e87SLois Curfman McInnes   PLogObjectMemory(*fact,(ainew[n]-1-n)*(sizeof(int)+sizeof(Scalar)));
1397fd98bd6SLois Curfman McInnes   aijnew->maxnz = aijnew->nz = ainew[n] - 1;
1407fd98bd6SLois Curfman McInnes 
1415e42470aSBarry Smith   /* Cannot do this here because child is destroyed before parent created
1425e42470aSBarry Smith      PLogObjectParent(*fact,isicol); */
143289bc588SBarry Smith   return 0;
144289bc588SBarry Smith }
145289bc588SBarry Smith 
1461fb19edaSLois Curfman McInnes int MatLUFactorNumeric_AIJ(Mat mat,Mat *infact)
147289bc588SBarry Smith {
14820563c6bSBarry Smith   Mat     fact = *infact;
1491fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew = (Mat_AIJ *)fact->data;
15086d7a8ceSBarry Smith   IS      iscol = aijnew->col, isrow = aijnew->row, isicol;
151289bc588SBarry Smith   int     *r,*ic, ierr, i, j, n = aij->m, *ai = aijnew->i, *aj = aijnew->j;
1526abc6512SBarry Smith   int     *ajtmpold, *ajtmp, nz, row,*pj;
1536abc6512SBarry Smith   Scalar  *rtmp,*v, *pv, *pc, multiplier;
154289bc588SBarry Smith 
15578b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
1565e42470aSBarry Smith   PLogObjectParent(*infact,isicol);
15778b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
15878b31e54SBarry Smith   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
15978b31e54SBarry Smith   rtmp = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
160289bc588SBarry Smith 
161289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
162289bc588SBarry Smith     nz = ai[i+1] - ai[i];
163289bc588SBarry Smith     ajtmp = aj + ai[i] - 1;
164289bc588SBarry Smith     for  ( j=0; j<nz; j++ ) rtmp[ajtmp[j]-1] = 0.0;
165289bc588SBarry Smith 
166289bc588SBarry Smith     /* load in initial (unfactored row) */
1678c37ef55SBarry Smith     nz = aij->i[r[i]+1] - aij->i[r[i]];
1688c37ef55SBarry Smith     ajtmpold = aij->j + aij->i[r[i]] - 1;
1698c37ef55SBarry Smith     v  = aij->a + aij->i[r[i]] - 1;
1708c37ef55SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ic[ajtmpold[j]-1]] =  v[j];
171289bc588SBarry Smith 
1728c37ef55SBarry Smith     row = *ajtmp++ - 1;
173289bc588SBarry Smith     while (row < i) {
1748c37ef55SBarry Smith       pc = rtmp + row;
1758c37ef55SBarry Smith       if (*pc != 0.0) {
1768c37ef55SBarry Smith         nz = aijnew->diag[row] - ai[row];
1778c37ef55SBarry Smith         pv = aijnew->a + aijnew->diag[row] - 1;
1788c37ef55SBarry Smith         pj = aijnew->j + aijnew->diag[row];
1798c37ef55SBarry Smith         multiplier = *pc * *pv++;
1808c37ef55SBarry Smith         *pc = multiplier;
1818c37ef55SBarry Smith         nz = ai[row+1] - ai[row] - 1 - nz;
182eef2e97bSBarry Smith         PLogFlops(2*nz);
1838c37ef55SBarry Smith         while (nz-->0) rtmp[*pj++ - 1] -= multiplier* *pv++;
184289bc588SBarry Smith       }
1858c37ef55SBarry Smith       row = *ajtmp++ - 1;
186289bc588SBarry Smith     }
1878c37ef55SBarry Smith     /* finished row so stick it into aijnew->a */
1888c37ef55SBarry Smith     pv = aijnew->a + ai[i] - 1;
1898c37ef55SBarry Smith     pj = aijnew->j + ai[i] - 1;
1908c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
191bbb6d6a8SBarry Smith     if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_AIJ:Zero pivot");}
1928c37ef55SBarry Smith     rtmp[i] = 1.0/rtmp[i];
1938c37ef55SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmp[pj[j]-1];}
1948c37ef55SBarry Smith   }
19578b31e54SBarry Smith   PETSCFREE(rtmp);
19678b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
19778b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
19878b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
1998c37ef55SBarry Smith   fact->factor      = FACTOR_LU;
2009e25ed09SBarry Smith   aijnew->assembled = 1;
201eef2e97bSBarry Smith   PLogFlops(aijnew->n);
202289bc588SBarry Smith   return 0;
203289bc588SBarry Smith }
20449d8b64dSBarry Smith int MatLUFactor_AIJ(Mat matin,IS row,IS col,double f)
205da3a660dSBarry Smith {
2061fb19edaSLois Curfman McInnes   Mat_AIJ *mat = (Mat_AIJ *) matin->data;
2076abc6512SBarry Smith   int     ierr;
208da3a660dSBarry Smith   Mat     fact;
20949d8b64dSBarry Smith   ierr = MatLUFactorSymbolic_AIJ(matin,row,col,f,&fact); CHKERRQ(ierr);
21078b31e54SBarry Smith   ierr = MatLUFactorNumeric_AIJ(matin,&fact); CHKERRQ(ierr);
211da3a660dSBarry Smith 
212da3a660dSBarry Smith   /* free all the data structures from mat */
21378b31e54SBarry Smith   PETSCFREE(mat->a);
21478b31e54SBarry Smith   if (!mat->singlemalloc) {PETSCFREE(mat->i); PETSCFREE(mat->j);}
21578b31e54SBarry Smith   if (mat->diag) PETSCFREE(mat->diag);
21678b31e54SBarry Smith   if (mat->ilen) PETSCFREE(mat->ilen);
21778b31e54SBarry Smith   if (mat->imax) PETSCFREE(mat->imax);
218*94473badSLois Curfman McInnes   if (mat->solve_work) PETSCFREE(mat->solve_work);
21978b31e54SBarry Smith   PETSCFREE(mat);
220da3a660dSBarry Smith 
22178b31e54SBarry Smith   PETSCMEMCPY(matin,fact,sizeof(struct _Mat));
22278b31e54SBarry Smith   PETSCFREE(fact);
223da3a660dSBarry Smith   return 0;
224da3a660dSBarry Smith }
225da3a660dSBarry Smith 
2261fb19edaSLois Curfman McInnes int MatSolve_AIJ(Mat mat,Vec bb, Vec xx)
2278c37ef55SBarry Smith {
2281fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
22986d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row;
2306abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
2318c37ef55SBarry Smith   int     nz;
2328c37ef55SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
2338c37ef55SBarry Smith 
234d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
235bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolve_AIJ:Cannot solve with unfactored matrix");
2369e25ed09SBarry Smith 
23778b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
23878b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
239eef2e97bSBarry Smith   tmp = aij->solve_work;
2408c37ef55SBarry Smith 
24178b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
24278b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
2438c37ef55SBarry Smith 
2448c37ef55SBarry Smith   /* forward solve the lower triangular */
2458c37ef55SBarry Smith   tmp[0] = b[*r++];
2468c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
2478c37ef55SBarry Smith     v   = aa + ai[i] - 1;
2488c37ef55SBarry Smith     vi  = aj + ai[i] - 1;
2498c37ef55SBarry Smith     nz  = aij->diag[i] - ai[i];
2508c37ef55SBarry Smith     sum = b[*r++];
2518c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2528c37ef55SBarry Smith     tmp[i] = sum;
2538c37ef55SBarry Smith   }
2548c37ef55SBarry Smith 
2558c37ef55SBarry Smith   /* backward solve the upper triangular */
2568c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
2578c37ef55SBarry Smith     v   = aa + aij->diag[i];
2588c37ef55SBarry Smith     vi  = aj + aij->diag[i];
2598c37ef55SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
2608c37ef55SBarry Smith     sum = tmp[i];
2618c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2628c37ef55SBarry Smith     x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1];
2638c37ef55SBarry Smith   }
2648c37ef55SBarry Smith 
265898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
266898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
267eef2e97bSBarry Smith   PLogFlops(2*aij->nz - aij->n);
2688c37ef55SBarry Smith   return 0;
2698c37ef55SBarry Smith }
2701fb19edaSLois Curfman McInnes int MatSolveAdd_AIJ(Mat mat,Vec bb, Vec yy, Vec xx)
271da3a660dSBarry Smith {
2721fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
27386d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row;
2746abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
275da3a660dSBarry Smith   int     nz;
276da3a660dSBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
277da3a660dSBarry Smith 
278d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
279bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveAdd_AIJ: Cannot solve with unfactored matrix");
28078b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
281da3a660dSBarry Smith 
28278b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
28378b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
284eef2e97bSBarry Smith   tmp = aij->solve_work;
285da3a660dSBarry Smith 
28678b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
28778b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1);
288da3a660dSBarry Smith 
289da3a660dSBarry Smith   /* forward solve the lower triangular */
290da3a660dSBarry Smith   tmp[0] = b[*r++];
291da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
292da3a660dSBarry Smith     v   = aa + ai[i] - 1;
293da3a660dSBarry Smith     vi  = aj + ai[i] - 1;
294da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
295da3a660dSBarry Smith     sum = b[*r++];
296da3a660dSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
297da3a660dSBarry Smith     tmp[i] = sum;
298da3a660dSBarry Smith   }
299da3a660dSBarry Smith 
300da3a660dSBarry Smith   /* backward solve the upper triangular */
301da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
302da3a660dSBarry Smith     v   = aa + aij->diag[i];
303da3a660dSBarry Smith     vi  = aj + aij->diag[i];
304da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
305da3a660dSBarry Smith     sum = tmp[i];
306da3a660dSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
307da3a660dSBarry Smith     tmp[i] = sum*aa[aij->diag[i]-1];
308da3a660dSBarry Smith     x[*c--] += tmp[i];
309da3a660dSBarry Smith   }
310da3a660dSBarry Smith 
311898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
312898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
313eef2e97bSBarry Smith   PLogFlops(2*aij->nz);
314898183ecSLois Curfman McInnes 
315da3a660dSBarry Smith   return 0;
316da3a660dSBarry Smith }
317da3a660dSBarry Smith /* -------------------------------------------------------------------*/
3181fb19edaSLois Curfman McInnes int MatSolveTrans_AIJ(Mat mat,Vec bb, Vec xx)
319da3a660dSBarry Smith {
3201fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
32186d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row, invisrow,inviscol;
3226abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
323da3a660dSBarry Smith   int     nz;
3246abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
325da3a660dSBarry Smith 
326d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
327bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveTrans_AIJ:Cannot solve with unfactored matrix");
32878b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
32978b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
330eef2e97bSBarry Smith   tmp = aij->solve_work;
331da3a660dSBarry Smith 
332da3a660dSBarry Smith   /* invert the permutations */
33378b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
33478b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
335da3a660dSBarry Smith 
33678b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
33778b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
338da3a660dSBarry Smith 
339da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
340da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
341da3a660dSBarry Smith 
342da3a660dSBarry Smith   /* forward solve the U^T */
343da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
344da3a660dSBarry Smith     v   = aa + aij->diag[i] - 1;
345da3a660dSBarry Smith     vi  = aj + aij->diag[i];
346da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
347da3a660dSBarry Smith     tmp[i] *= *v++;
348da3a660dSBarry Smith     while (nz--) {
349da3a660dSBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
350da3a660dSBarry Smith     }
351da3a660dSBarry Smith   }
352da3a660dSBarry Smith 
353da3a660dSBarry Smith   /* backward solve the L^T */
354da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
355da3a660dSBarry Smith     v   = aa + aij->diag[i] - 2;
356da3a660dSBarry Smith     vi  = aj + aij->diag[i] - 2;
357da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
358da3a660dSBarry Smith     while (nz--) {
359da3a660dSBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
360da3a660dSBarry Smith     }
361da3a660dSBarry Smith   }
362da3a660dSBarry Smith 
363da3a660dSBarry Smith   /* copy tmp into x according to permutation */
364da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
365da3a660dSBarry Smith 
366898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
367898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
368898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
369898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
370da3a660dSBarry Smith 
371eef2e97bSBarry Smith   PLogFlops(2*aij->nz-aij->n);
372da3a660dSBarry Smith   return 0;
373da3a660dSBarry Smith }
374da3a660dSBarry Smith 
3751fb19edaSLois Curfman McInnes int MatSolveTransAdd_AIJ(Mat mat,Vec bb, Vec zz,Vec xx)
376da3a660dSBarry Smith {
3771fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
37886d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row, invisrow,inviscol;
3796abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
3806abc6512SBarry Smith   int     nz;
3816abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
3826abc6512SBarry Smith 
383d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
384bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveTransAdd_AIJ:Cannot solve with unfactored matrix");
3856abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
3866abc6512SBarry Smith 
38778b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
38878b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
389eef2e97bSBarry Smith   tmp = aij->solve_work;
3906abc6512SBarry Smith 
3916abc6512SBarry Smith   /* invert the permutations */
39278b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
39378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
39478b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
39578b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
3966abc6512SBarry Smith 
3976abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
3986abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
3996abc6512SBarry Smith 
4006abc6512SBarry Smith   /* forward solve the U^T */
4016abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
4026abc6512SBarry Smith     v   = aa + aij->diag[i] - 1;
4036abc6512SBarry Smith     vi  = aj + aij->diag[i];
4046abc6512SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
4056abc6512SBarry Smith     tmp[i] *= *v++;
4066abc6512SBarry Smith     while (nz--) {
4076abc6512SBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
4086abc6512SBarry Smith     }
4096abc6512SBarry Smith   }
4106abc6512SBarry Smith 
4116abc6512SBarry Smith   /* backward solve the L^T */
4126abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
4136abc6512SBarry Smith     v   = aa + aij->diag[i] - 2;
4146abc6512SBarry Smith     vi  = aj + aij->diag[i] - 2;
4156abc6512SBarry Smith     nz  = aij->diag[i] - ai[i];
4166abc6512SBarry Smith     while (nz--) {
4176abc6512SBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
4186abc6512SBarry Smith     }
4196abc6512SBarry Smith   }
4206abc6512SBarry Smith 
4216abc6512SBarry Smith   /* copy tmp into x according to permutation */
4226abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4236abc6512SBarry Smith 
424898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
425898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
426898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
427898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
4286abc6512SBarry Smith 
429eef2e97bSBarry Smith   PLogFlops(2*aij->nz);
4306abc6512SBarry Smith   return 0;
431da3a660dSBarry Smith }
4329e25ed09SBarry Smith /* ----------------------------------------------------------------*/
43349d8b64dSBarry Smith int MatILUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,double f,
43449d8b64dSBarry Smith                              int levels,Mat *fact)
4359e25ed09SBarry Smith {
4361fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew;
4379e25ed09SBarry Smith   IS      isicol;
43856beaf04SBarry Smith   int     *r,*ic, ierr, prow, n = aij->m, *ai = aij->i, *aj = aij->j;
43956beaf04SBarry Smith   int     *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
44056beaf04SBarry Smith   int     *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
44156beaf04SBarry Smith   int     incrlev,nnz,i;
4429e25ed09SBarry Smith 
443d35516d3SLois Curfman McInnes   if (n != aij->n)
444bbb6d6a8SBarry Smith     SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must be square");
445d35516d3SLois Curfman McInnes   if (!isrow)
446bbb6d6a8SBarry Smith     SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must have row permutation");
447d35516d3SLois Curfman McInnes   if (!iscol) SETERRQ(1,
448bbb6d6a8SBarry Smith     "MatILUFactorSymbolic_AIJ:Matrix must have column permutation");
4499e25ed09SBarry Smith 
45078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
451898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
452898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
4539e25ed09SBarry Smith 
4549e25ed09SBarry Smith   /* get new row pointers */
45578b31e54SBarry Smith   ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
4569e25ed09SBarry Smith   ainew[0] = 1;
4579e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
458bbb6d6a8SBarry Smith   jmax = (int) (f*ai[n]);
45978b31e54SBarry Smith   ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
4609e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
46178b31e54SBarry Smith   ajfill = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
4629e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
46356beaf04SBarry Smith   fill = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(fill);
46456beaf04SBarry Smith   /* im is level for each filled value */
46556beaf04SBarry Smith   im = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(im);
46656beaf04SBarry Smith   /* dloc is location of diagonal in factor */
46756beaf04SBarry Smith   dloc = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(dloc);
46856beaf04SBarry Smith   dloc[0]  = 0;
4699e25ed09SBarry Smith 
47056beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
4719e25ed09SBarry Smith     /* first copy previous fill into linked list */
47256beaf04SBarry Smith     nzf = nz  = ai[r[prow]+1] - ai[r[prow]];
47356beaf04SBarry Smith     xi  = aj + ai[r[prow]] - 1;
4749e25ed09SBarry Smith     fill[n] = n;
4759e25ed09SBarry Smith     while (nz--) {
4769e25ed09SBarry Smith       fm  = n;
47756beaf04SBarry Smith       idx = ic[*xi++ - 1];
4789e25ed09SBarry Smith       do {
4799e25ed09SBarry Smith         m  = fm;
4809e25ed09SBarry Smith         fm = fill[m];
4819e25ed09SBarry Smith       } while (fm < idx);
4829e25ed09SBarry Smith       fill[m]   = idx;
4839e25ed09SBarry Smith       fill[idx] = fm;
48456beaf04SBarry Smith       im[idx]   = 0;
4859e25ed09SBarry Smith     }
48656beaf04SBarry Smith     nzi = 0;
4879e25ed09SBarry Smith     row = fill[n];
48856beaf04SBarry Smith     while ( row < prow ) {
48956beaf04SBarry Smith       incrlev = im[row] + 1;
49056beaf04SBarry Smith       nz      = dloc[row];
49156beaf04SBarry Smith       xi      = ajnew  + ainew[row] - 1 + nz;
49256beaf04SBarry Smith       flev    = ajfill + ainew[row] - 1 + nz + 1;
49356beaf04SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
49456beaf04SBarry Smith       if (*xi++ - 1 != row) {
49556beaf04SBarry Smith         SETERRQ(1,"MatILUFactorSymbolic_AIJ:zero pivot");
49656beaf04SBarry Smith       }
49756beaf04SBarry Smith       fm      = row;
49856beaf04SBarry Smith       while (nnz-- > 0) {
49956beaf04SBarry Smith         idx = *xi++ - 1;
50056beaf04SBarry Smith         if (*flev + incrlev > levels) {
50156beaf04SBarry Smith           flev++;
50256beaf04SBarry Smith           continue;
50356beaf04SBarry Smith         }
50456beaf04SBarry Smith         do {
5059e25ed09SBarry Smith           m  = fm;
5069e25ed09SBarry Smith           fm = fill[m];
50756beaf04SBarry Smith         } while (fm < idx);
5089e25ed09SBarry Smith         if (fm != idx) {
50956beaf04SBarry Smith           im[idx]  = *flev + incrlev;
5109e25ed09SBarry Smith           fill[m] = idx;
5119e25ed09SBarry Smith           fill[idx] = fm;
5129e25ed09SBarry Smith           fm = idx;
51356beaf04SBarry Smith           nzf++;
5149e25ed09SBarry Smith         }
51556beaf04SBarry Smith         else {
51656beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
5179e25ed09SBarry Smith         }
51856beaf04SBarry Smith         flev++;
5199e25ed09SBarry Smith       }
5209e25ed09SBarry Smith       row = fill[row];
52156beaf04SBarry Smith       nzi++;
5229e25ed09SBarry Smith     }
5239e25ed09SBarry Smith     /* copy new filled row into permanent storage */
52456beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
52556beaf04SBarry Smith     if (ainew[prow+1] > jmax+1) {
5269e25ed09SBarry Smith       /* allocate a longer ajnew */
527bbb6d6a8SBarry Smith       int maxadd;
52856beaf04SBarry Smith       maxadd = (int) ((f*ai[n]*(n-prow+5))/n);
52956beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
530bbb6d6a8SBarry Smith       jmax += maxadd;
53156beaf04SBarry Smith       xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi);
53256beaf04SBarry Smith       PETSCMEMCPY(xi,ajnew,(ainew[prow]-1)*sizeof(int));
53378b31e54SBarry Smith       PETSCFREE(ajnew);
53456beaf04SBarry Smith       ajnew = xi;
5359e25ed09SBarry Smith       /* allocate a longer ajfill */
53656beaf04SBarry Smith       xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi);
53756beaf04SBarry Smith       PETSCMEMCPY(xi,ajfill,(ainew[prow]-1)*sizeof(int));
53878b31e54SBarry Smith       PETSCFREE(ajfill);
53956beaf04SBarry Smith       ajfill = xi;
540bbb6d6a8SBarry Smith       realloc++;
5419e25ed09SBarry Smith     }
54256beaf04SBarry Smith     xi          = ajnew + ainew[prow] - 1;
54356beaf04SBarry Smith     flev        = ajfill + ainew[prow] - 1;
54456beaf04SBarry Smith     dloc[prow]  = nzi;
5459e25ed09SBarry Smith     fm          = fill[n];
54656beaf04SBarry Smith     while (nzf--) {
54756beaf04SBarry Smith       *xi++   = fm + 1;
54856beaf04SBarry Smith       *flev++ = im[fm];
5499e25ed09SBarry Smith       fm      = fill[fm];
5509e25ed09SBarry Smith     }
5519e25ed09SBarry Smith   }
55278b31e54SBarry Smith   PETSCFREE(ajfill);
553898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
554898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
555898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
556898183ecSLois Curfman McInnes   PETSCFREE(fill); PETSCFREE(im);
5579e25ed09SBarry Smith 
558bbb6d6a8SBarry Smith   PLogInfo((PetscObject)mat,
559bbb6d6a8SBarry Smith     "Info:MatILUFactorSymbolic_AIJ:Realloc %d Fill ratio:given %g needed %g\n",
56056beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
561bbb6d6a8SBarry Smith 
5629e25ed09SBarry Smith   /* put together the new matrix */
56378b31e54SBarry Smith   ierr = MatCreateSequentialAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr);
5641fb19edaSLois Curfman McInnes   aijnew = (Mat_AIJ *) (*fact)->data;
56578b31e54SBarry Smith   PETSCFREE(aijnew->imax);
5669e25ed09SBarry Smith   aijnew->singlemalloc = 0;
5679e25ed09SBarry Smith   len = (ainew[n] - 1)*sizeof(Scalar);
5689e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
56978b31e54SBarry Smith   PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen);
57078b31e54SBarry Smith   aijnew->a         = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a);
5719e25ed09SBarry Smith   aijnew->j         = ajnew;
5729e25ed09SBarry Smith   aijnew->i         = ainew;
57356beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
57456beaf04SBarry Smith   aijnew->diag      = dloc;
5759e25ed09SBarry Smith   aijnew->ilen      = 0;
5769e25ed09SBarry Smith   aijnew->imax      = 0;
57786d7a8ceSBarry Smith   aijnew->row       = isrow;
57886d7a8ceSBarry Smith   aijnew->col       = iscol;
5795392566eSBarry Smith   aijnew->solve_work = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar));
58078b31e54SBarry Smith   CHKPTRQ(aijnew->solve_work);
5817fd98bd6SLois Curfman McInnes   /* In aijnew structure:  Free imax, ilen, old a, old j.
58256beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
583033c1e87SLois Curfman McInnes   PLogObjectMemory(*fact,(ainew[n]-1-n) * (sizeof(int)+sizeof(Scalar)));
5847fd98bd6SLois Curfman McInnes   aijnew->maxnz = aijnew->nz = ainew[n] - 1;
5859e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
5869e25ed09SBarry Smith   return 0;
5879e25ed09SBarry Smith }
588