xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 898183eceed0bc4fc6814b4e4a55c88e58af42c8)
1cb512458SBarry Smith #ifndef lint
2*898183ecSLois Curfman McInnes static char vcid[] = "$Id: aijfact.c,v 1.31 1995/08/23 17:14:15 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 
113*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
114*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
115*898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
116*898183ecSLois 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);
21878b31e54SBarry Smith   PETSCFREE(mat);
219da3a660dSBarry Smith 
22078b31e54SBarry Smith   PETSCMEMCPY(matin,fact,sizeof(struct _Mat));
22178b31e54SBarry Smith   PETSCFREE(fact);
222da3a660dSBarry Smith   return 0;
223da3a660dSBarry Smith }
224da3a660dSBarry Smith 
2251fb19edaSLois Curfman McInnes int MatSolve_AIJ(Mat mat,Vec bb, Vec xx)
2268c37ef55SBarry Smith {
2271fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
22886d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row;
2296abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
2308c37ef55SBarry Smith   int     nz;
2318c37ef55SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
2328c37ef55SBarry Smith 
233d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
234bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolve_AIJ:Cannot solve with unfactored matrix");
2359e25ed09SBarry Smith 
23678b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
23778b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
238eef2e97bSBarry Smith   tmp = aij->solve_work;
2398c37ef55SBarry Smith 
24078b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
24178b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
2428c37ef55SBarry Smith 
2438c37ef55SBarry Smith   /* forward solve the lower triangular */
2448c37ef55SBarry Smith   tmp[0] = b[*r++];
2458c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
2468c37ef55SBarry Smith     v   = aa + ai[i] - 1;
2478c37ef55SBarry Smith     vi  = aj + ai[i] - 1;
2488c37ef55SBarry Smith     nz  = aij->diag[i] - ai[i];
2498c37ef55SBarry Smith     sum = b[*r++];
2508c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2518c37ef55SBarry Smith     tmp[i] = sum;
2528c37ef55SBarry Smith   }
2538c37ef55SBarry Smith 
2548c37ef55SBarry Smith   /* backward solve the upper triangular */
2558c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
2568c37ef55SBarry Smith     v   = aa + aij->diag[i];
2578c37ef55SBarry Smith     vi  = aj + aij->diag[i];
2588c37ef55SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
2598c37ef55SBarry Smith     sum = tmp[i];
2608c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2618c37ef55SBarry Smith     x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1];
2628c37ef55SBarry Smith   }
2638c37ef55SBarry Smith 
264*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
265*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
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)
278bbb6d6a8SBarry 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 
310*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
311*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
312eef2e97bSBarry Smith   PLogFlops(2*aij->nz);
313*898183ecSLois Curfman McInnes 
314da3a660dSBarry Smith   return 0;
315da3a660dSBarry Smith }
316da3a660dSBarry Smith /* -------------------------------------------------------------------*/
3171fb19edaSLois Curfman McInnes int MatSolveTrans_AIJ(Mat mat,Vec bb, Vec xx)
318da3a660dSBarry Smith {
3191fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
32086d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row, invisrow,inviscol;
3216abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
322da3a660dSBarry Smith   int     nz;
3236abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
324da3a660dSBarry Smith 
325d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
326bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveTrans_AIJ:Cannot solve with unfactored matrix");
32778b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
32878b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
329eef2e97bSBarry Smith   tmp = aij->solve_work;
330da3a660dSBarry Smith 
331da3a660dSBarry Smith   /* invert the permutations */
33278b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
33378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
334da3a660dSBarry Smith 
33578b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
33678b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
337da3a660dSBarry Smith 
338da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
339da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
340da3a660dSBarry Smith 
341da3a660dSBarry Smith   /* forward solve the U^T */
342da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
343da3a660dSBarry Smith     v   = aa + aij->diag[i] - 1;
344da3a660dSBarry Smith     vi  = aj + aij->diag[i];
345da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
346da3a660dSBarry Smith     tmp[i] *= *v++;
347da3a660dSBarry Smith     while (nz--) {
348da3a660dSBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
349da3a660dSBarry Smith     }
350da3a660dSBarry Smith   }
351da3a660dSBarry Smith 
352da3a660dSBarry Smith   /* backward solve the L^T */
353da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
354da3a660dSBarry Smith     v   = aa + aij->diag[i] - 2;
355da3a660dSBarry Smith     vi  = aj + aij->diag[i] - 2;
356da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
357da3a660dSBarry Smith     while (nz--) {
358da3a660dSBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
359da3a660dSBarry Smith     }
360da3a660dSBarry Smith   }
361da3a660dSBarry Smith 
362da3a660dSBarry Smith   /* copy tmp into x according to permutation */
363da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
364da3a660dSBarry Smith 
365*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
366*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
367*898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
368*898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
369da3a660dSBarry Smith 
370eef2e97bSBarry Smith   PLogFlops(2*aij->nz-aij->n);
371da3a660dSBarry Smith   return 0;
372da3a660dSBarry Smith }
373da3a660dSBarry Smith 
3741fb19edaSLois Curfman McInnes int MatSolveTransAdd_AIJ(Mat mat,Vec bb, Vec zz,Vec xx)
375da3a660dSBarry Smith {
3761fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data;
37786d7a8ceSBarry Smith   IS      iscol = aij->col, isrow = aij->row, invisrow,inviscol;
3786abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
3796abc6512SBarry Smith   int     nz;
3806abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
3816abc6512SBarry Smith 
382d35516d3SLois Curfman McInnes   if (mat->factor != FACTOR_LU)
383bbb6d6a8SBarry Smith     SETERRQ(1,"MatSolveTransAdd_AIJ:Cannot solve with unfactored matrix");
3846abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
3856abc6512SBarry Smith 
38678b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
38778b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
388eef2e97bSBarry Smith   tmp = aij->solve_work;
3896abc6512SBarry Smith 
3906abc6512SBarry Smith   /* invert the permutations */
39178b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
39278b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
39378b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
39478b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
3956abc6512SBarry Smith 
3966abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
3976abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
3986abc6512SBarry Smith 
3996abc6512SBarry Smith   /* forward solve the U^T */
4006abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
4016abc6512SBarry Smith     v   = aa + aij->diag[i] - 1;
4026abc6512SBarry Smith     vi  = aj + aij->diag[i];
4036abc6512SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
4046abc6512SBarry Smith     tmp[i] *= *v++;
4056abc6512SBarry Smith     while (nz--) {
4066abc6512SBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
4076abc6512SBarry Smith     }
4086abc6512SBarry Smith   }
4096abc6512SBarry Smith 
4106abc6512SBarry Smith   /* backward solve the L^T */
4116abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
4126abc6512SBarry Smith     v   = aa + aij->diag[i] - 2;
4136abc6512SBarry Smith     vi  = aj + aij->diag[i] - 2;
4146abc6512SBarry Smith     nz  = aij->diag[i] - ai[i];
4156abc6512SBarry Smith     while (nz--) {
4166abc6512SBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
4176abc6512SBarry Smith     }
4186abc6512SBarry Smith   }
4196abc6512SBarry Smith 
4206abc6512SBarry Smith   /* copy tmp into x according to permutation */
4216abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4226abc6512SBarry Smith 
423*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
424*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
425*898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
426*898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
4276abc6512SBarry Smith 
428eef2e97bSBarry Smith   PLogFlops(2*aij->nz);
4296abc6512SBarry Smith   return 0;
430da3a660dSBarry Smith }
4319e25ed09SBarry Smith /* ----------------------------------------------------------------*/
43249d8b64dSBarry Smith int MatILUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,double f,
43349d8b64dSBarry Smith                              int levels,Mat *fact)
4349e25ed09SBarry Smith {
4351fb19edaSLois Curfman McInnes   Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew;
4369e25ed09SBarry Smith   IS      isicol;
43756beaf04SBarry Smith   int     *r,*ic, ierr, prow, n = aij->m, *ai = aij->i, *aj = aij->j;
43856beaf04SBarry Smith   int     *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
43956beaf04SBarry Smith   int     *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
44056beaf04SBarry Smith   int     incrlev,nnz,i;
4419e25ed09SBarry Smith 
442d35516d3SLois Curfman McInnes   if (n != aij->n)
443bbb6d6a8SBarry Smith     SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must be square");
444d35516d3SLois Curfman McInnes   if (!isrow)
445bbb6d6a8SBarry Smith     SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must have row permutation");
446d35516d3SLois Curfman McInnes   if (!iscol) SETERRQ(1,
447bbb6d6a8SBarry Smith     "MatILUFactorSymbolic_AIJ:Matrix must have column permutation");
4489e25ed09SBarry Smith 
44978b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
450*898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
451*898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
4529e25ed09SBarry Smith 
4539e25ed09SBarry Smith   /* get new row pointers */
45478b31e54SBarry Smith   ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
4559e25ed09SBarry Smith   ainew[0] = 1;
4569e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
457bbb6d6a8SBarry Smith   jmax = (int) (f*ai[n]);
45878b31e54SBarry Smith   ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
4599e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
46078b31e54SBarry Smith   ajfill = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
4619e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
46256beaf04SBarry Smith   fill = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(fill);
46356beaf04SBarry Smith   /* im is level for each filled value */
46456beaf04SBarry Smith   im = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(im);
46556beaf04SBarry Smith   /* dloc is location of diagonal in factor */
46656beaf04SBarry Smith   dloc = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(dloc);
46756beaf04SBarry Smith   dloc[0]  = 0;
4689e25ed09SBarry Smith 
46956beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
4709e25ed09SBarry Smith     /* first copy previous fill into linked list */
47156beaf04SBarry Smith     nzf = nz  = ai[r[prow]+1] - ai[r[prow]];
47256beaf04SBarry Smith     xi  = aj + ai[r[prow]] - 1;
4739e25ed09SBarry Smith     fill[n] = n;
4749e25ed09SBarry Smith     while (nz--) {
4759e25ed09SBarry Smith       fm  = n;
47656beaf04SBarry Smith       idx = ic[*xi++ - 1];
4779e25ed09SBarry Smith       do {
4789e25ed09SBarry Smith         m  = fm;
4799e25ed09SBarry Smith         fm = fill[m];
4809e25ed09SBarry Smith       } while (fm < idx);
4819e25ed09SBarry Smith       fill[m]   = idx;
4829e25ed09SBarry Smith       fill[idx] = fm;
48356beaf04SBarry Smith       im[idx]   = 0;
4849e25ed09SBarry Smith     }
48556beaf04SBarry Smith     nzi = 0;
4869e25ed09SBarry Smith     row = fill[n];
48756beaf04SBarry Smith     while ( row < prow ) {
48856beaf04SBarry Smith       incrlev = im[row] + 1;
48956beaf04SBarry Smith       nz      = dloc[row];
49056beaf04SBarry Smith       xi      = ajnew  + ainew[row] - 1 + nz;
49156beaf04SBarry Smith       flev    = ajfill + ainew[row] - 1 + nz + 1;
49256beaf04SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
49356beaf04SBarry Smith       if (*xi++ - 1 != row) {
49456beaf04SBarry Smith         SETERRQ(1,"MatILUFactorSymbolic_AIJ:zero pivot");
49556beaf04SBarry Smith       }
49656beaf04SBarry Smith       fm      = row;
49756beaf04SBarry Smith       while (nnz-- > 0) {
49856beaf04SBarry Smith         idx = *xi++ - 1;
49956beaf04SBarry Smith         if (*flev + incrlev > levels) {
50056beaf04SBarry Smith           flev++;
50156beaf04SBarry Smith           continue;
50256beaf04SBarry Smith         }
50356beaf04SBarry Smith         do {
5049e25ed09SBarry Smith           m  = fm;
5059e25ed09SBarry Smith           fm = fill[m];
50656beaf04SBarry Smith         } while (fm < idx);
5079e25ed09SBarry Smith         if (fm != idx) {
50856beaf04SBarry Smith           im[idx]  = *flev + incrlev;
5099e25ed09SBarry Smith           fill[m] = idx;
5109e25ed09SBarry Smith           fill[idx] = fm;
5119e25ed09SBarry Smith           fm = idx;
51256beaf04SBarry Smith           nzf++;
5139e25ed09SBarry Smith         }
51456beaf04SBarry Smith         else {
51556beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
5169e25ed09SBarry Smith         }
51756beaf04SBarry Smith         flev++;
5189e25ed09SBarry Smith       }
5199e25ed09SBarry Smith       row = fill[row];
52056beaf04SBarry Smith       nzi++;
5219e25ed09SBarry Smith     }
5229e25ed09SBarry Smith     /* copy new filled row into permanent storage */
52356beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
52456beaf04SBarry Smith     if (ainew[prow+1] > jmax+1) {
5259e25ed09SBarry Smith       /* allocate a longer ajnew */
526bbb6d6a8SBarry Smith       int maxadd;
52756beaf04SBarry Smith       maxadd = (int) ((f*ai[n]*(n-prow+5))/n);
52856beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
529bbb6d6a8SBarry Smith       jmax += maxadd;
53056beaf04SBarry Smith       xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi);
53156beaf04SBarry Smith       PETSCMEMCPY(xi,ajnew,(ainew[prow]-1)*sizeof(int));
53278b31e54SBarry Smith       PETSCFREE(ajnew);
53356beaf04SBarry Smith       ajnew = xi;
5349e25ed09SBarry Smith       /* allocate a longer ajfill */
53556beaf04SBarry Smith       xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi);
53656beaf04SBarry Smith       PETSCMEMCPY(xi,ajfill,(ainew[prow]-1)*sizeof(int));
53778b31e54SBarry Smith       PETSCFREE(ajfill);
53856beaf04SBarry Smith       ajfill = xi;
539bbb6d6a8SBarry Smith       realloc++;
5409e25ed09SBarry Smith     }
54156beaf04SBarry Smith     xi          = ajnew + ainew[prow] - 1;
54256beaf04SBarry Smith     flev        = ajfill + ainew[prow] - 1;
54356beaf04SBarry Smith     dloc[prow]  = nzi;
5449e25ed09SBarry Smith     fm          = fill[n];
54556beaf04SBarry Smith     while (nzf--) {
54656beaf04SBarry Smith       *xi++   = fm + 1;
54756beaf04SBarry Smith       *flev++ = im[fm];
5489e25ed09SBarry Smith       fm      = fill[fm];
5499e25ed09SBarry Smith     }
5509e25ed09SBarry Smith   }
55178b31e54SBarry Smith   PETSCFREE(ajfill);
552*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
553*898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
554*898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
555*898183ecSLois Curfman McInnes   PETSCFREE(fill); PETSCFREE(im);
5569e25ed09SBarry Smith 
557bbb6d6a8SBarry Smith   PLogInfo((PetscObject)mat,
558bbb6d6a8SBarry Smith     "Info:MatILUFactorSymbolic_AIJ:Realloc %d Fill ratio:given %g needed %g\n",
55956beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
560bbb6d6a8SBarry Smith 
5619e25ed09SBarry Smith   /* put together the new matrix */
56278b31e54SBarry Smith   ierr = MatCreateSequentialAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr);
5631fb19edaSLois Curfman McInnes   aijnew = (Mat_AIJ *) (*fact)->data;
56478b31e54SBarry Smith   PETSCFREE(aijnew->imax);
5659e25ed09SBarry Smith   aijnew->singlemalloc = 0;
5669e25ed09SBarry Smith   len = (ainew[n] - 1)*sizeof(Scalar);
5679e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
56878b31e54SBarry Smith   PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen);
56978b31e54SBarry Smith   aijnew->a         = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a);
5709e25ed09SBarry Smith   aijnew->j         = ajnew;
5719e25ed09SBarry Smith   aijnew->i         = ainew;
57256beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
57356beaf04SBarry Smith   aijnew->diag      = dloc;
5749e25ed09SBarry Smith   aijnew->ilen      = 0;
5759e25ed09SBarry Smith   aijnew->imax      = 0;
57686d7a8ceSBarry Smith   aijnew->row       = isrow;
57786d7a8ceSBarry Smith   aijnew->col       = iscol;
5785392566eSBarry Smith   aijnew->solve_work = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar));
57978b31e54SBarry Smith   CHKPTRQ(aijnew->solve_work);
5807fd98bd6SLois Curfman McInnes   /* In aijnew structure:  Free imax, ilen, old a, old j.
58156beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
582033c1e87SLois Curfman McInnes   PLogObjectMemory(*fact,(ainew[n]-1-n) * (sizeof(int)+sizeof(Scalar)));
5837fd98bd6SLois Curfman McInnes   aijnew->maxnz = aijnew->nz = ainew[n] - 1;
5849e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
5859e25ed09SBarry Smith   return 0;
5869e25ed09SBarry Smith }
587