xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision bbb6d6a8ce168c53119d255f697846c361b726e4)
1cb512458SBarry Smith #ifndef lint
2*bbb6d6a8SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.26 1995/07/10 05:02:25 bsmith Exp bsmith $";
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)
21*bbb6d6a8SBarry Smith     SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must be square");
22d35516d3SLois Curfman McInnes   if (!isrow)
23*bbb6d6a8SBarry Smith     SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must have row permutation");
24d35516d3SLois Curfman McInnes   if (!iscol)
25*bbb6d6a8SBarry 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 */
34*bbb6d6a8SBarry 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;
88*bbb6d6a8SBarry Smith       maxadd = (int) ((f*ai[n]*(n-i+5))/n);
89*bbb6d6a8SBarry 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,
110*bbb6d6a8SBarry Smith     "Info:MatLUFactorSymbolic_AIJ:Reallocs %d Fill ratio:given %g needed %g\n",
111*bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
1122fb3ed76SBarry Smith 
11378b31e54SBarry Smith   ISDestroy(isicol); PETSCFREE(fill);
114289bc588SBarry Smith 
115289bc588SBarry Smith   /* put together the new matrix */
11678b31e54SBarry Smith   ierr = MatCreateSequentialAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr);
1171fb19edaSLois Curfman McInnes   aijnew = (Mat_AIJ *) (*fact)->data;
11878b31e54SBarry Smith   PETSCFREE(aijnew->imax);
119289bc588SBarry Smith   aijnew->singlemalloc = 0;
120f0479e8cSBarry Smith   len = (ainew[n] - 1)*sizeof(Scalar);
121e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
12278b31e54SBarry Smith   PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen);
12378b31e54SBarry Smith   aijnew->a          = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a);
124289bc588SBarry Smith   aijnew->j          = ajnew;
125289bc588SBarry Smith   aijnew->i          = ainew;
1268c37ef55SBarry Smith   aijnew->diag       = idnew;
127e8d4e0b9SBarry Smith   aijnew->ilen       = 0;
12820563c6bSBarry Smith   aijnew->imax       = 0;
12986d7a8ceSBarry Smith   aijnew->row        = isrow;
13086d7a8ceSBarry Smith   aijnew->col        = iscol;
131289bc588SBarry Smith   (*fact)->factor    = FACTOR_LU;
13278b31e54SBarry Smith   aijnew->solve_work = (Scalar *) PETSCMALLOC( n*sizeof(Scalar));
13378b31e54SBarry Smith   CHKPTRQ(aijnew->solve_work);
1347fd98bd6SLois Curfman McInnes   /* In aijnew structure:  Free imax, ilen, old a, old j.
1357fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
1367fd98bd6SLois Curfman McInnes   aijnew->mem += (ainew[n]-1-n)*(sizeof(int) + sizeof(Scalar)) + sizeof(int);
1377fd98bd6SLois Curfman McInnes   aijnew->maxnz = aijnew->nz = ainew[n] - 1;
1387fd98bd6SLois Curfman McInnes 
1395e42470aSBarry Smith   /* Cannot do this here because child is destroyed before parent created
1405e42470aSBarry Smith      PLogObjectParent(*fact,isicol); */
141289bc588SBarry Smith   return 0;
142289bc588SBarry Smith }
143289bc588SBarry Smith 
1441fb19edaSLois Curfman McInnes int MatLUFactorNumeric_AIJ(Mat mat,Mat *infact)
145289bc588SBarry Smith {
14620563c6bSBarry Smith   Mat     fact = *infact;
1471fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew = (Mat_AIJ *)fact->data;
14886d7a8ceSBarry Smith   IS      iscol = aijnew->col, isrow = aijnew->row, isicol;
149289bc588SBarry Smith   int     *r,*ic, ierr, i, j, n = aij->m, *ai = aijnew->i, *aj = aijnew->j;
1506abc6512SBarry Smith   int     *ajtmpold, *ajtmp, nz, row,*pj;
1516abc6512SBarry Smith   Scalar  *rtmp,*v, *pv, *pc, multiplier;
152289bc588SBarry Smith 
15378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
1545e42470aSBarry Smith   PLogObjectParent(*infact,isicol);
15578b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
15678b31e54SBarry Smith   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
15778b31e54SBarry Smith   rtmp = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
158289bc588SBarry Smith 
159289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
160289bc588SBarry Smith     nz = ai[i+1] - ai[i];
161289bc588SBarry Smith     ajtmp = aj + ai[i] - 1;
162289bc588SBarry Smith     for  ( j=0; j<nz; j++ ) rtmp[ajtmp[j]-1] = 0.0;
163289bc588SBarry Smith 
164289bc588SBarry Smith     /* load in initial (unfactored row) */
1658c37ef55SBarry Smith     nz = aij->i[r[i]+1] - aij->i[r[i]];
1668c37ef55SBarry Smith     ajtmpold = aij->j + aij->i[r[i]] - 1;
1678c37ef55SBarry Smith     v  = aij->a + aij->i[r[i]] - 1;
1688c37ef55SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ic[ajtmpold[j]-1]] =  v[j];
169289bc588SBarry Smith 
1708c37ef55SBarry Smith     row = *ajtmp++ - 1;
171289bc588SBarry Smith     while (row < i) {
1728c37ef55SBarry Smith       pc = rtmp + row;
1738c37ef55SBarry Smith       if (*pc != 0.0) {
1748c37ef55SBarry Smith         nz = aijnew->diag[row] - ai[row];
1758c37ef55SBarry Smith         pv = aijnew->a + aijnew->diag[row] - 1;
1768c37ef55SBarry Smith         pj = aijnew->j + aijnew->diag[row];
1778c37ef55SBarry Smith         multiplier = *pc * *pv++;
1788c37ef55SBarry Smith         *pc = multiplier;
1798c37ef55SBarry Smith         nz = ai[row+1] - ai[row] - 1 - nz;
180eef2e97bSBarry Smith         PLogFlops(2*nz);
1818c37ef55SBarry Smith         while (nz-->0) rtmp[*pj++ - 1] -= multiplier* *pv++;
182289bc588SBarry Smith       }
1838c37ef55SBarry Smith       row = *ajtmp++ - 1;
184289bc588SBarry Smith     }
1858c37ef55SBarry Smith     /* finished row so stick it into aijnew->a */
1868c37ef55SBarry Smith     pv = aijnew->a + ai[i] - 1;
1878c37ef55SBarry Smith     pj = aijnew->j + ai[i] - 1;
1888c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
189*bbb6d6a8SBarry Smith     if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_AIJ:Zero pivot");}
1908c37ef55SBarry Smith     rtmp[i] = 1.0/rtmp[i];
1918c37ef55SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmp[pj[j]-1];}
1928c37ef55SBarry Smith   }
19378b31e54SBarry Smith   PETSCFREE(rtmp);
19478b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
19578b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
19678b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
1978c37ef55SBarry Smith   fact->factor      = FACTOR_LU;
1989e25ed09SBarry Smith   aijnew->assembled = 1;
199eef2e97bSBarry Smith   PLogFlops(aijnew->n);
200289bc588SBarry Smith   return 0;
201289bc588SBarry Smith }
20249d8b64dSBarry Smith int MatLUFactor_AIJ(Mat matin,IS row,IS col,double f)
203da3a660dSBarry Smith {
2041fb19edaSLois Curfman McInnes   Mat_AIJ *mat = (Mat_AIJ *) matin->data;
2056abc6512SBarry Smith   int     ierr;
206da3a660dSBarry Smith   Mat     fact;
20749d8b64dSBarry Smith   ierr = MatLUFactorSymbolic_AIJ(matin,row,col,f,&fact); CHKERRQ(ierr);
20878b31e54SBarry Smith   ierr = MatLUFactorNumeric_AIJ(matin,&fact); CHKERRQ(ierr);
209da3a660dSBarry Smith 
210da3a660dSBarry Smith   /* free all the data structures from mat */
21178b31e54SBarry Smith   PETSCFREE(mat->a);
21278b31e54SBarry Smith   if (!mat->singlemalloc) {PETSCFREE(mat->i); PETSCFREE(mat->j);}
21378b31e54SBarry Smith   if (mat->diag) PETSCFREE(mat->diag);
21478b31e54SBarry Smith   if (mat->ilen) PETSCFREE(mat->ilen);
21578b31e54SBarry Smith   if (mat->imax) PETSCFREE(mat->imax);
21686d7a8ceSBarry Smith   if (mat->row && mat->col && mat->row != mat->col) {
21786d7a8ceSBarry Smith     ISDestroy(mat->row);
218da3a660dSBarry Smith   }
21986d7a8ceSBarry Smith   if (mat->col) ISDestroy(mat->col);
22078b31e54SBarry Smith   PETSCFREE(mat);
221da3a660dSBarry Smith 
22278b31e54SBarry Smith   PETSCMEMCPY(matin,fact,sizeof(struct _Mat));
22378b31e54SBarry Smith   PETSCFREE(fact);
224da3a660dSBarry Smith   return 0;
225da3a660dSBarry Smith }
226da3a660dSBarry Smith 
2271fb19edaSLois Curfman McInnes int MatSolve_AIJ(Mat mat,Vec bb, Vec xx)
2288c37ef55SBarry Smith {
2291fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
23086d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row;
2316abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
2328c37ef55SBarry Smith   int     nz;
2338c37ef55SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
2348c37ef55SBarry Smith 
235d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
236*bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolve_AIJ:Cannot solve with unfactored matrix");
2379e25ed09SBarry Smith 
23878b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
23978b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
240eef2e97bSBarry Smith   tmp = aij->solve_work;
2418c37ef55SBarry Smith 
24278b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
24378b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
2448c37ef55SBarry Smith 
2458c37ef55SBarry Smith   /* forward solve the lower triangular */
2468c37ef55SBarry Smith   tmp[0] = b[*r++];
2478c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
2488c37ef55SBarry Smith     v   = aa + ai[i] - 1;
2498c37ef55SBarry Smith     vi  = aj + ai[i] - 1;
2508c37ef55SBarry Smith     nz  = aij->diag[i] - ai[i];
2518c37ef55SBarry Smith     sum = b[*r++];
2528c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2538c37ef55SBarry Smith     tmp[i] = sum;
2548c37ef55SBarry Smith   }
2558c37ef55SBarry Smith 
2568c37ef55SBarry Smith   /* backward solve the upper triangular */
2578c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
2588c37ef55SBarry Smith     v   = aa + aij->diag[i];
2598c37ef55SBarry Smith     vi  = aj + aij->diag[i];
2608c37ef55SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
2618c37ef55SBarry Smith     sum = tmp[i];
2628c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2638c37ef55SBarry Smith     x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1];
2648c37ef55SBarry Smith   }
2658c37ef55SBarry Smith 
266eef2e97bSBarry Smith   PLogFlops(2*aij->nz - aij->n);
2678c37ef55SBarry Smith   return 0;
2688c37ef55SBarry Smith }
2691fb19edaSLois Curfman McInnes int MatSolveAdd_AIJ(Mat mat,Vec bb, Vec yy, Vec xx)
270da3a660dSBarry Smith {
2711fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
27286d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row;
2736abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
274da3a660dSBarry Smith   int     nz;
275da3a660dSBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
276da3a660dSBarry Smith 
277d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
278*bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveAdd_AIJ: Cannot solve with unfactored matrix");
27978b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
280da3a660dSBarry Smith 
28178b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
28278b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
283eef2e97bSBarry Smith   tmp = aij->solve_work;
284da3a660dSBarry Smith 
28578b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
28678b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1);
287da3a660dSBarry Smith 
288da3a660dSBarry Smith   /* forward solve the lower triangular */
289da3a660dSBarry Smith   tmp[0] = b[*r++];
290da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
291da3a660dSBarry Smith     v   = aa + ai[i] - 1;
292da3a660dSBarry Smith     vi  = aj + ai[i] - 1;
293da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
294da3a660dSBarry Smith     sum = b[*r++];
295da3a660dSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
296da3a660dSBarry Smith     tmp[i] = sum;
297da3a660dSBarry Smith   }
298da3a660dSBarry Smith 
299da3a660dSBarry Smith   /* backward solve the upper triangular */
300da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
301da3a660dSBarry Smith     v   = aa + aij->diag[i];
302da3a660dSBarry Smith     vi  = aj + aij->diag[i];
303da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
304da3a660dSBarry Smith     sum = tmp[i];
305da3a660dSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
306da3a660dSBarry Smith     tmp[i] = sum*aa[aij->diag[i]-1];
307da3a660dSBarry Smith     x[*c--] += tmp[i];
308da3a660dSBarry Smith   }
309da3a660dSBarry Smith 
310eef2e97bSBarry Smith   PLogFlops(2*aij->nz);
311da3a660dSBarry Smith   return 0;
312da3a660dSBarry Smith }
313da3a660dSBarry Smith /* -------------------------------------------------------------------*/
3141fb19edaSLois Curfman McInnes int MatSolveTrans_AIJ(Mat mat,Vec bb, Vec xx)
315da3a660dSBarry Smith {
3161fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
31786d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row, invisrow,inviscol;
3186abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
319da3a660dSBarry Smith   int     nz;
3206abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
321da3a660dSBarry Smith 
322d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
323*bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveTrans_AIJ:Cannot solve with unfactored matrix");
32478b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
32578b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
326eef2e97bSBarry Smith   tmp = aij->solve_work;
327da3a660dSBarry Smith 
328da3a660dSBarry Smith   /* invert the permutations */
32978b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
33078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
331da3a660dSBarry Smith 
332da3a660dSBarry Smith 
33378b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
33478b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
335da3a660dSBarry Smith 
336da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
337da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
338da3a660dSBarry Smith 
339da3a660dSBarry Smith   /* forward solve the U^T */
340da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
341da3a660dSBarry Smith     v   = aa + aij->diag[i] - 1;
342da3a660dSBarry Smith     vi  = aj + aij->diag[i];
343da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
344da3a660dSBarry Smith     tmp[i] *= *v++;
345da3a660dSBarry Smith     while (nz--) {
346da3a660dSBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
347da3a660dSBarry Smith     }
348da3a660dSBarry Smith   }
349da3a660dSBarry Smith 
350da3a660dSBarry Smith   /* backward solve the L^T */
351da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
352da3a660dSBarry Smith     v   = aa + aij->diag[i] - 2;
353da3a660dSBarry Smith     vi  = aj + aij->diag[i] - 2;
354da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
355da3a660dSBarry Smith     while (nz--) {
356da3a660dSBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
357da3a660dSBarry Smith     }
358da3a660dSBarry Smith   }
359da3a660dSBarry Smith 
360da3a660dSBarry Smith   /* copy tmp into x according to permutation */
361da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
362da3a660dSBarry Smith 
363da3a660dSBarry Smith   ISDestroy(invisrow); ISDestroy(inviscol);
364da3a660dSBarry Smith 
365eef2e97bSBarry Smith   PLogFlops(2*aij->nz-aij->n);
366da3a660dSBarry Smith   return 0;
367da3a660dSBarry Smith }
368da3a660dSBarry Smith 
3691fb19edaSLois Curfman McInnes int MatSolveTransAdd_AIJ(Mat mat,Vec bb, Vec zz,Vec xx)
370da3a660dSBarry Smith {
3711fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
37286d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row, invisrow,inviscol;
3736abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
3746abc6512SBarry Smith   int     nz;
3756abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
3766abc6512SBarry Smith 
377d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
378*bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveTransAdd_AIJ:Cannot solve with unfactored matrix");
3796abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
3806abc6512SBarry Smith 
38178b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
38278b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
383eef2e97bSBarry Smith   tmp = aij->solve_work;
3846abc6512SBarry Smith 
3856abc6512SBarry Smith   /* invert the permutations */
38678b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
38778b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
3886abc6512SBarry Smith 
3896abc6512SBarry Smith 
39078b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
39178b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
3926abc6512SBarry Smith 
3936abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
3946abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
3956abc6512SBarry Smith 
3966abc6512SBarry Smith   /* forward solve the U^T */
3976abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
3986abc6512SBarry Smith     v   = aa + aij->diag[i] - 1;
3996abc6512SBarry Smith     vi  = aj + aij->diag[i];
4006abc6512SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
4016abc6512SBarry Smith     tmp[i] *= *v++;
4026abc6512SBarry Smith     while (nz--) {
4036abc6512SBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
4046abc6512SBarry Smith     }
4056abc6512SBarry Smith   }
4066abc6512SBarry Smith 
4076abc6512SBarry Smith   /* backward solve the L^T */
4086abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
4096abc6512SBarry Smith     v   = aa + aij->diag[i] - 2;
4106abc6512SBarry Smith     vi  = aj + aij->diag[i] - 2;
4116abc6512SBarry Smith     nz  = aij->diag[i] - ai[i];
4126abc6512SBarry Smith     while (nz--) {
4136abc6512SBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
4146abc6512SBarry Smith     }
4156abc6512SBarry Smith   }
4166abc6512SBarry Smith 
4176abc6512SBarry Smith   /* copy tmp into x according to permutation */
4186abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4196abc6512SBarry Smith 
4206abc6512SBarry Smith   ISDestroy(invisrow); ISDestroy(inviscol);
4216abc6512SBarry Smith 
422eef2e97bSBarry Smith   PLogFlops(2*aij->nz);
4236abc6512SBarry Smith   return 0;
424da3a660dSBarry Smith }
4259e25ed09SBarry Smith /* ----------------------------------------------------------------*/
42649d8b64dSBarry Smith int MatILUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,double f,
42749d8b64dSBarry Smith                              int levels,Mat *fact)
4289e25ed09SBarry Smith {
4291fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew;
4309e25ed09SBarry Smith   IS      isicol;
4319e25ed09SBarry Smith   int     *r,*ic, ierr, i, n = aij->m, *ai = aij->i, *aj = aij->j;
4329e25ed09SBarry Smith   int     *ainew,*ajnew, jmax,*fill, *ajtmp, nz, *lfill,*ajfill,*ajtmpf;
433*bbb6d6a8SBarry Smith   int     *idnew, idx, row,m,fm, nnz, nzi,len, *im, nzbd, realloc = 0;
4349e25ed09SBarry Smith 
435d35516d3SLois Curfman McInnes   if (n != aij->n)
436*bbb6d6a8SBarry Smith     SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must be square");
437d35516d3SLois Curfman McInnes   if (!isrow)
438*bbb6d6a8SBarry Smith     SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must have row permutation");
439d35516d3SLois Curfman McInnes   if (!iscol) SETERRQ(1,
440*bbb6d6a8SBarry Smith     "MatILUFactorSymbolic_AIJ:Matrix must have column permutation");
4419e25ed09SBarry Smith 
44278b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
4439e25ed09SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
4449e25ed09SBarry Smith 
4459e25ed09SBarry Smith   /* get new row pointers */
44678b31e54SBarry Smith   ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
4479e25ed09SBarry Smith   ainew[0] = 1;
4489e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
449*bbb6d6a8SBarry Smith   jmax = (int) (f*ai[n]);
45078b31e54SBarry Smith   ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
4519e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
45278b31e54SBarry Smith   ajfill = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
4539e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
454*bbb6d6a8SBarry Smith   fill = (int *) PETSCMALLOC( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
455*bbb6d6a8SBarry Smith   im   = fill + n + 1;
4569e25ed09SBarry Smith   /* lfill is level for each filled value */
45778b31e54SBarry Smith   lfill = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(lfill);
4589e25ed09SBarry Smith   /* idnew is location of diagonal in factor */
45978b31e54SBarry Smith   idnew = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(idnew);
4609e25ed09SBarry Smith   idnew[0] = 1;
4619e25ed09SBarry Smith 
4629e25ed09SBarry Smith   for ( i=0; i<n; i++ ) {
4639e25ed09SBarry Smith     /* first copy previous fill into linked list */
4649e25ed09SBarry Smith     nnz = nz    = ai[r[i]+1] - ai[r[i]];
4659e25ed09SBarry Smith     ajtmp = aj + ai[r[i]] - 1;
4669e25ed09SBarry Smith     fill[n] = n;
4679e25ed09SBarry Smith     while (nz--) {
4689e25ed09SBarry Smith       fm = n;
4699e25ed09SBarry Smith       idx = ic[*ajtmp++ - 1];
4709e25ed09SBarry Smith       do {
4719e25ed09SBarry Smith         m = fm;
4729e25ed09SBarry Smith         fm = fill[m];
4739e25ed09SBarry Smith       } while (fm < idx);
4749e25ed09SBarry Smith       fill[m] = idx;
4759e25ed09SBarry Smith       fill[idx] = fm;
4769e25ed09SBarry Smith       lfill[idx] = -1;
477*bbb6d6a8SBarry Smith  /* printf("i %d cols %d %d\n",i,nz,idx);  */
4789e25ed09SBarry Smith     }
4799e25ed09SBarry Smith     row = fill[n];
4809e25ed09SBarry Smith     while ( row < i ) {
481*bbb6d6a8SBarry Smith       ajtmp  = ajnew + idnew[row];
482*bbb6d6a8SBarry Smith       ajtmpf = ajfill + idnew[row];
483*bbb6d6a8SBarry Smith 
484*bbb6d6a8SBarry Smith       nzbd = 1 + idnew[row] - ainew[row];
485*bbb6d6a8SBarry Smith       nz = im[row] - nzbd;
486*bbb6d6a8SBarry Smith 
487*bbb6d6a8SBarry Smith       fm = fill[row]; m = row;
488*bbb6d6a8SBarry Smith       while (nz-- > 0) {
4899e25ed09SBarry Smith         idx = *ajtmp++ - 1;
490*bbb6d6a8SBarry Smith         nzbd++;
491*bbb6d6a8SBarry Smith         if (idx == i) im[row] = nzbd;
492*bbb6d6a8SBarry Smith         while (fm < idx) {
4939e25ed09SBarry Smith           m = fm;
4949e25ed09SBarry Smith           fm = fill[m];
495*bbb6d6a8SBarry Smith         }
4969e25ed09SBarry Smith         if (fm != idx) {
4979e25ed09SBarry Smith           lfill[idx] = *ajtmpf + 1;
4989e25ed09SBarry Smith           if (lfill[idx] < levels) {
4999e25ed09SBarry Smith             fill[m] = idx;
5009e25ed09SBarry Smith             fill[idx] = fm;
5019e25ed09SBarry Smith             fm = idx;
5029e25ed09SBarry Smith             nnz++;
5039e25ed09SBarry Smith           }
5049e25ed09SBarry Smith         }
505*bbb6d6a8SBarry Smith  /* printf("i %d row %d nz %d idx %d fm %d level %d nnz %d\n",i,row,nz,idx,fm,*ajtmpf + 1,nnz); */
5069e25ed09SBarry Smith         ajtmpf++;
5079e25ed09SBarry Smith       }
5089e25ed09SBarry Smith       row = fill[row];
5099e25ed09SBarry Smith     }
5109e25ed09SBarry Smith     /* copy new filled row into permanent storage */
5119e25ed09SBarry Smith     ainew[i+1] = ainew[i] + nnz;
5129e25ed09SBarry Smith     if (ainew[i+1] > jmax+1) {
5139e25ed09SBarry Smith       /* allocate a longer ajnew */
514*bbb6d6a8SBarry Smith       int maxadd;
515*bbb6d6a8SBarry Smith       maxadd = (int) ((f*ai[n]*(n-i+5))/n);
516*bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
517*bbb6d6a8SBarry Smith       jmax += maxadd;
51878b31e54SBarry Smith       ajtmp = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(ajtmp);
51978b31e54SBarry Smith       PETSCMEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int));
52078b31e54SBarry Smith       PETSCFREE(ajnew);
5219e25ed09SBarry Smith       ajnew = ajtmp;
5229e25ed09SBarry Smith       /* allocate a longer ajfill */
52378b31e54SBarry Smith       ajtmp = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(ajtmp);
52478b31e54SBarry Smith       PETSCMEMCPY(ajtmp,ajfill,(ainew[i]-1)*sizeof(int));
52578b31e54SBarry Smith       PETSCFREE(ajfill);
5269e25ed09SBarry Smith       ajfill = ajtmp;
527*bbb6d6a8SBarry Smith       realloc++;
5289e25ed09SBarry Smith     }
5299e25ed09SBarry Smith     ajtmp  = ajnew + ainew[i] - 1;
5309e25ed09SBarry Smith     ajtmpf = ajfill + ainew[i] - 1;
5319e25ed09SBarry Smith     fm = fill[n];
532*bbb6d6a8SBarry Smith     im[i] = nnz;
5339e25ed09SBarry Smith     nzi = 0;
5349e25ed09SBarry Smith     while (nnz--) {
5359e25ed09SBarry Smith       if (fm < i) nzi++;
5369e25ed09SBarry Smith       *ajtmp++  = fm + 1;
5379e25ed09SBarry Smith       *ajtmpf++ = lfill[fm];
538*bbb6d6a8SBarry Smith /* printf("i %d col %d level %d\n",i,fm,lfill[fm]); */
5399e25ed09SBarry Smith       fm = fill[fm];
5409e25ed09SBarry Smith     }
5419e25ed09SBarry Smith     idnew[i] = ainew[i] + nzi;
5429e25ed09SBarry Smith   }
54378b31e54SBarry Smith   PETSCFREE(ajfill);
54478b31e54SBarry Smith   ISDestroy(isicol); PETSCFREE(fill); PETSCFREE(lfill);
5459e25ed09SBarry Smith 
546*bbb6d6a8SBarry Smith   PLogInfo((PetscObject)mat,
547*bbb6d6a8SBarry Smith     "Info:MatILUFactorSymbolic_AIJ:Realloc %d Fill ratio:given %g needed %g\n",
548*bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
549*bbb6d6a8SBarry Smith 
5509e25ed09SBarry Smith   /* put together the new matrix */
55178b31e54SBarry Smith   ierr = MatCreateSequentialAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr);
5521fb19edaSLois Curfman McInnes   aijnew = (Mat_AIJ *) (*fact)->data;
55378b31e54SBarry Smith   PETSCFREE(aijnew->imax);
5549e25ed09SBarry Smith   aijnew->singlemalloc = 0;
5559e25ed09SBarry Smith   len = (ainew[n] - 1)*sizeof(Scalar);
5569e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
55778b31e54SBarry Smith   PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen);
55878b31e54SBarry Smith   aijnew->a         = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a);
5599e25ed09SBarry Smith   aijnew->j         = ajnew;
5609e25ed09SBarry Smith   aijnew->i         = ainew;
5619e25ed09SBarry Smith   aijnew->diag      = idnew;
5629e25ed09SBarry Smith   aijnew->ilen      = 0;
5639e25ed09SBarry Smith   aijnew->imax      = 0;
56486d7a8ceSBarry Smith   aijnew->row       = isrow;
56586d7a8ceSBarry Smith   aijnew->col       = iscol;
5665392566eSBarry Smith   aijnew->solve_work = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar));
56778b31e54SBarry Smith   CHKPTRQ(aijnew->solve_work);
5687fd98bd6SLois Curfman McInnes   /* In aijnew structure:  Free imax, ilen, old a, old j.
5697fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
5707fd98bd6SLois Curfman McInnes   aijnew->mem += (ainew[n]-1-n)*(sizeof(int) + sizeof(Scalar)) + sizeof(int);
5717fd98bd6SLois Curfman McInnes   aijnew->maxnz = aijnew->nz = ainew[n] - 1;
5729e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
5739e25ed09SBarry Smith   return 0;
5749e25ed09SBarry Smith }
575