xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 026e39d04bcf2a445c0a9332c845c324154b2e8f)
1cb512458SBarry Smith #ifndef lint
2*026e39d0SSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.69 1996/12/08 20:07:38 bsmith Exp balay $";
3cb512458SBarry Smith #endif
4289bc588SBarry Smith 
570f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
690f02eecSBarry Smith #include "src/vec/vecimpl.h"
73b2fbd54SBarry Smith 
8*026e39d0SSatish Balay #undef __FUNCTION__
9*026e39d0SSatish Balay #define __FUNCTION__ "MatOrder_Flow_SeqAIJ"
103b2fbd54SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReordering type,IS *irow,IS *icol)
113b2fbd54SBarry Smith {
1291bf9adeSBarry Smith   SETERRQ(PETSC_ERR_SUP,"MatOrder_Flow_SeqAIJ:Code not written");
133b2fbd54SBarry Smith }
143b2fbd54SBarry Smith 
15289bc588SBarry Smith /*
16289bc588SBarry Smith     Factorization code for AIJ format.
17289bc588SBarry Smith */
18*026e39d0SSatish Balay #undef __FUNCTION__
19*026e39d0SSatish Balay #define __FUNCTION__ "MatLUFactorSymbolic_SeqAIJ"
20416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
21289bc588SBarry Smith {
22416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
23289bc588SBarry Smith   IS         isicol;
24416022c9SBarry Smith   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
25416022c9SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
2691bf9adeSBarry Smith   int        *idnew, idx, row,m,fm, nnz, nzi, realloc = 0,nzbd,*im;
27289bc588SBarry Smith 
28d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
29d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
303b2fbd54SBarry Smith 
3178b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
32289bc588SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
33289bc588SBarry Smith 
34289bc588SBarry Smith   /* get new row pointers */
350452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
36dbb450caSBarry Smith   ainew[0] = -shift;
37289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
38dbb450caSBarry Smith   jmax = (int) (f*ai[n]+(!shift));
390452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
40289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
410452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
422fb3ed76SBarry Smith   im = fill + n + 1;
43289bc588SBarry Smith   /* idnew is location of diagonal in factor */
440452661fSBarry Smith   idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
45dbb450caSBarry Smith   idnew[0] = -shift;
46289bc588SBarry Smith 
47289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
48289bc588SBarry Smith     /* first copy previous fill into linked list */
49289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
50dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
51289bc588SBarry Smith     fill[n] = n;
52289bc588SBarry Smith     while (nz--) {
53289bc588SBarry Smith       fm  = n;
54dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
55289bc588SBarry Smith       do {
56289bc588SBarry Smith         m  = fm;
57289bc588SBarry Smith         fm = fill[m];
58289bc588SBarry Smith       } while (fm < idx);
59289bc588SBarry Smith       fill[m]   = idx;
60289bc588SBarry Smith       fill[idx] = fm;
61289bc588SBarry Smith     }
62289bc588SBarry Smith     row = fill[n];
63289bc588SBarry Smith     while ( row < i ) {
64dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
652fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
662fb3ed76SBarry Smith       nz    = im[row] - nzbd;
67289bc588SBarry Smith       fm    = row;
682fb3ed76SBarry Smith       while (nz-- > 0) {
69dbb450caSBarry Smith         idx = *ajtmp++ + shift;
702fb3ed76SBarry Smith         nzbd++;
712fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
72289bc588SBarry Smith         do {
73289bc588SBarry Smith           m  = fm;
74289bc588SBarry Smith           fm = fill[m];
75289bc588SBarry Smith         } while (fm < idx);
76289bc588SBarry Smith         if (fm != idx) {
77289bc588SBarry Smith           fill[m]   = idx;
78289bc588SBarry Smith           fill[idx] = fm;
79289bc588SBarry Smith           fm        = idx;
80289bc588SBarry Smith           nnz++;
81289bc588SBarry Smith         }
82289bc588SBarry Smith       }
83289bc588SBarry Smith       row = fill[row];
84289bc588SBarry Smith     }
85289bc588SBarry Smith     /* copy new filled row into permanent storage */
86289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
87496e697eSBarry Smith     if (ainew[i+1] > jmax) {
88289bc588SBarry Smith       /* allocate a longer ajnew */
892fb3ed76SBarry Smith       int maxadd;
90dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n);
91bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
922fb3ed76SBarry Smith       jmax += maxadd;
930452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
94416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
950452661fSBarry Smith       PetscFree(ajnew);
96289bc588SBarry Smith       ajnew = ajtmp;
972fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
98289bc588SBarry Smith     }
99dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
100289bc588SBarry Smith     fm    = fill[n];
101289bc588SBarry Smith     nzi   = 0;
1022fb3ed76SBarry Smith     im[i] = nnz;
103289bc588SBarry Smith     while (nnz--) {
104289bc588SBarry Smith       if (fm < i) nzi++;
105dbb450caSBarry Smith       *ajtmp++ = fm - shift;
106289bc588SBarry Smith       fm       = fill[fm];
107289bc588SBarry Smith     }
108289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
109289bc588SBarry Smith   }
110289bc588SBarry Smith 
11194a424c1SBarry Smith   PLogInfo(A,
112ec8511deSBarry Smith     "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
113bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
1142fb3ed76SBarry Smith 
115898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
116898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1171987afe7SBarry Smith 
1180452661fSBarry Smith   PetscFree(fill);
119289bc588SBarry Smith 
120289bc588SBarry Smith   /* put together the new matrix */
121b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1221987afe7SBarry Smith   PLogObjectParent(*B,isicol);
1231987afe7SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
124416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1250452661fSBarry Smith   PetscFree(b->imax);
126416022c9SBarry Smith   b->singlemalloc = 0;
127e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1280452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
12991bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
130416022c9SBarry Smith   b->j          = ajnew;
131416022c9SBarry Smith   b->i          = ainew;
132416022c9SBarry Smith   b->diag       = idnew;
133416022c9SBarry Smith   b->ilen       = 0;
134416022c9SBarry Smith   b->imax       = 0;
135416022c9SBarry Smith   b->row        = isrow;
136416022c9SBarry Smith   b->col        = iscol;
13791bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
138416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1397fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
140416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
141416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1427fd98bd6SLois Curfman McInnes 
143ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
144ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
145ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
146ae068f58SLois Curfman McInnes 
147289bc588SBarry Smith   return 0;
148289bc588SBarry Smith }
1490a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
15041c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
15141c01911SSatish Balay 
152*026e39d0SSatish Balay #undef __FUNCTION__
153*026e39d0SSatish Balay #define __FUNCTION__ "MatLUFactorNumeric_SeqAIJ"
154416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
155289bc588SBarry Smith {
156416022c9SBarry Smith   Mat        C = *B;
157416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
158416022c9SBarry Smith   IS         iscol = b->col, isrow = b->row, isicol;
159416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1601987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
16135aab85fSBarry Smith   int        *diag_offset = b->diag,diag,k;
16235aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1638ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
16435aab85fSBarry Smith   double     ssum;
1651987afe7SBarry Smith   /* These declarations are for optimizations.  They reduce the number of
1661987afe7SBarry Smith      memory references that are made by locally storing information; the
1671987afe7SBarry Smith      word "register" used here with pointers can be viewed as "private" or
1681987afe7SBarry Smith      "known only to me"
1691987afe7SBarry Smith    */
17035aab85fSBarry Smith   register Scalar *pv, *rtmps,*u_values;
1711987afe7SBarry Smith   register int    *pj;
172289bc588SBarry Smith 
17378b31e54SBarry Smith   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
174416022c9SBarry Smith   PLogObjectParent(*B,isicol);
17578b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
17678b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1770452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
17813ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1790cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
180289bc588SBarry Smith 
18135aab85fSBarry Smith   /* precalcuate row sums */
18235aab85fSBarry Smith   if (preserve_row_sums) {
18335aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
18435aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
18535aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
18635aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
18735aab85fSBarry Smith       sum = 0.0;
18835aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
18935aab85fSBarry Smith       rowsums[i] = sum;
19035aab85fSBarry Smith     }
19135aab85fSBarry Smith   }
19235aab85fSBarry Smith 
193289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
194289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
195dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
19648e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
197289bc588SBarry Smith 
198289bc588SBarry Smith     /* load in initial (unfactored row) */
199416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
200416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
201416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2020cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
203289bc588SBarry Smith 
204dbb450caSBarry Smith     row = *ajtmp++ + shift;
205289bc588SBarry Smith     while (row < i) {
2068c37ef55SBarry Smith       pc = rtmp + row;
2078c37ef55SBarry Smith       if (*pc != 0.0) {
2081987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2091987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
21035aab85fSBarry Smith         multiplier = *pc / *pv++;
2118c37ef55SBarry Smith         *pc        = multiplier;
2121987afe7SBarry Smith         nz         = ai[row+1] - diag_offset[row] - 1;
21335aab85fSBarry Smith         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
214eef2e97bSBarry Smith         PLogFlops(2*nz);
215289bc588SBarry Smith       }
216dbb450caSBarry Smith       row = *ajtmp++ + shift;
217289bc588SBarry Smith     }
218416022c9SBarry Smith     /* finished row so stick it into b->a */
219416022c9SBarry Smith     pv = b->a + ai[i] + shift;
220416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2218c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
222416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
22335aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
22435aab85fSBarry Smith     /*
22535aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
22635aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
22735aab85fSBarry Smith     */
22835aab85fSBarry Smith     if (preserve_row_sums) {
22935aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
23035aab85fSBarry Smith       sum = rowsums[i];
23135aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
23235aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
23335aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
23435aab85fSBarry Smith         inner_sum = 0.0;
23535aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
23635aab85fSBarry Smith           inner_sum += u_values[k];
23735aab85fSBarry Smith         }
23835aab85fSBarry Smith         sum -= pv[j]*inner_sum;
23935aab85fSBarry Smith 
24035aab85fSBarry Smith       }
24135aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
24235aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
24335aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
24435aab85fSBarry Smith         sum -= u_values[k];
24535aab85fSBarry Smith       }
24635aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
24735aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
24835aab85fSBarry Smith     }
24935aab85fSBarry Smith     /* check pivot entry for current row */
25035aab85fSBarry Smith     if (pv[diag] == 0.0) {
25191bf9adeSBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"MatLUFactorNumeric_SeqAIJ:Zero pivot");
25235aab85fSBarry Smith     }
2538c37ef55SBarry Smith   }
2540f11f9aeSSatish Balay 
25535aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
25635aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
25735aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
25835aab85fSBarry Smith   }
25935aab85fSBarry Smith 
26035aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2610452661fSBarry Smith   PetscFree(rtmp);
26278b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
26378b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
26478b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
265416022c9SBarry Smith   C->factor = FACTOR_LU;
26641c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
267c456f294SBarry Smith   C->assembled = PETSC_TRUE;
268416022c9SBarry Smith   PLogFlops(b->n);
269289bc588SBarry Smith   return 0;
270289bc588SBarry Smith }
2710a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
272*026e39d0SSatish Balay #undef __FUNCTION__
273*026e39d0SSatish Balay #define __FUNCTION__ "MatLUFactor_SeqAIJ"
274416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
275da3a660dSBarry Smith {
276416022c9SBarry Smith   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
2776abc6512SBarry Smith   int        ierr;
278416022c9SBarry Smith   Mat        C;
279416022c9SBarry Smith 
280416022c9SBarry Smith   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
281416022c9SBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
282da3a660dSBarry Smith 
283da3a660dSBarry Smith   /* free all the data structures from mat */
2840452661fSBarry Smith   PetscFree(mat->a);
2850452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
2860452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
2870452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
2880452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
2890452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
290a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
2910452661fSBarry Smith   PetscFree(mat);
292da3a660dSBarry Smith 
293416022c9SBarry Smith   PetscMemcpy(A,C,sizeof(struct _Mat));
2940452661fSBarry Smith   PetscHeaderDestroy(C);
295da3a660dSBarry Smith   return 0;
296da3a660dSBarry Smith }
2970a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
298*026e39d0SSatish Balay #undef __FUNCTION__
299*026e39d0SSatish Balay #define __FUNCTION__ "MatSolve_SeqAIJ"
300416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3018c37ef55SBarry Smith {
302416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
303416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
304416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3053b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
306416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3078c37ef55SBarry Smith 
30891bf9adeSBarry Smith   if (!n) return 0;
30991bf9adeSBarry Smith 
31090f02eecSBarry Smith   VecGetArray_Fast(bb,b);
31190f02eecSBarry Smith   VecGetArray_Fast(xx,x);
312416022c9SBarry Smith   tmp  = a->solve_work;
3138c37ef55SBarry Smith 
3143b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3153b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3168c37ef55SBarry Smith 
3178c37ef55SBarry Smith   /* forward solve the lower triangular */
3188c37ef55SBarry Smith   tmp[0] = b[*r++];
3190cf60383SBarry Smith   tmps   = tmp + shift;
3208c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
321dbb450caSBarry Smith     v   = aa + ai[i] + shift;
322dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
323416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3248c37ef55SBarry Smith     sum = b[*r++];
3250cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3268c37ef55SBarry Smith     tmp[i] = sum;
3278c37ef55SBarry Smith   }
3288c37ef55SBarry Smith 
3298c37ef55SBarry Smith   /* backward solve the upper triangular */
3308c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
331416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
332416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
333416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3348c37ef55SBarry Smith     sum = tmp[i];
3350cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
336416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3378c37ef55SBarry Smith   }
3388c37ef55SBarry Smith 
3393b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3403b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
341416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3428c37ef55SBarry Smith   return 0;
3438c37ef55SBarry Smith }
344*026e39d0SSatish Balay 
345*026e39d0SSatish Balay #undef __FUNCTION__
346*026e39d0SSatish Balay #define __FUNCTION__ "MatSolveAdd_SeqAIJ"
347416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
348da3a660dSBarry Smith {
349416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
350416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
351416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3523b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
353416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
354da3a660dSBarry Smith 
35578b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
356da3a660dSBarry Smith 
35790f02eecSBarry Smith   VecGetArray_Fast(bb,b);
35890f02eecSBarry Smith   VecGetArray_Fast(xx,x);
359416022c9SBarry Smith   tmp  = a->solve_work;
360da3a660dSBarry Smith 
3613b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
3623b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
363da3a660dSBarry Smith 
364da3a660dSBarry Smith   /* forward solve the lower triangular */
365da3a660dSBarry Smith   tmp[0] = b[*r++];
366da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
367dbb450caSBarry Smith     v   = aa + ai[i] + shift;
368dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
369416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
370da3a660dSBarry Smith     sum = b[*r++];
371dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
372da3a660dSBarry Smith     tmp[i] = sum;
373da3a660dSBarry Smith   }
374da3a660dSBarry Smith 
375da3a660dSBarry Smith   /* backward solve the upper triangular */
376da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
377416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
378416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
379416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
380da3a660dSBarry Smith     sum = tmp[i];
381dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
382416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
383da3a660dSBarry Smith     x[*c--] += tmp[i];
384da3a660dSBarry Smith   }
385da3a660dSBarry Smith 
3863b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3873b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
388416022c9SBarry Smith   PLogFlops(2*a->nz);
389898183ecSLois Curfman McInnes 
390da3a660dSBarry Smith   return 0;
391da3a660dSBarry Smith }
392da3a660dSBarry Smith /* -------------------------------------------------------------------*/
393*026e39d0SSatish Balay #undef __FUNCTION__
394*026e39d0SSatish Balay #define __FUNCTION__ "MatSolveTrans_SeqAIJ"
395416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
396da3a660dSBarry Smith {
397416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
398416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
399416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4003b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
401416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
402da3a660dSBarry Smith 
40390f02eecSBarry Smith   VecGetArray_Fast(bb,b);
40490f02eecSBarry Smith   VecGetArray_Fast(xx,x);
405416022c9SBarry Smith   tmp  = a->solve_work;
406da3a660dSBarry Smith 
407da3a660dSBarry Smith   /* invert the permutations */
40878b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
40978b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
410da3a660dSBarry Smith 
4113b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4123b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
413da3a660dSBarry Smith 
414da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
415da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
416da3a660dSBarry Smith 
417da3a660dSBarry Smith   /* forward solve the U^T */
418da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
419416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
420416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
421416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
422da3a660dSBarry Smith     tmp[i] *= *v++;
423da3a660dSBarry Smith     while (nz--) {
424dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
425da3a660dSBarry Smith     }
426da3a660dSBarry Smith   }
427da3a660dSBarry Smith 
428da3a660dSBarry Smith   /* backward solve the L^T */
429da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
430416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
431416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
432416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
433da3a660dSBarry Smith     while (nz--) {
434dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
435da3a660dSBarry Smith     }
436da3a660dSBarry Smith   }
437da3a660dSBarry Smith 
438da3a660dSBarry Smith   /* copy tmp into x according to permutation */
439da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
440da3a660dSBarry Smith 
4413b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
4423b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
443898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
444898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
445da3a660dSBarry Smith 
446416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
447da3a660dSBarry Smith   return 0;
448da3a660dSBarry Smith }
449da3a660dSBarry Smith 
450*026e39d0SSatish Balay #undef __FUNCTION__
451*026e39d0SSatish Balay #define __FUNCTION__ "MatSolveTransAdd_SeqAIJ"
452416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
453da3a660dSBarry Smith {
454416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
455416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
456416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4573b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
458416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
4596abc6512SBarry Smith 
4606abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
4616abc6512SBarry Smith 
46290f02eecSBarry Smith   VecGetArray_Fast(bb,b);
46390f02eecSBarry Smith   VecGetArray_Fast(xx,x);
464416022c9SBarry Smith   tmp = a->solve_work;
4656abc6512SBarry Smith 
4666abc6512SBarry Smith   /* invert the permutations */
46778b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
46878b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
4693b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4703b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
4716abc6512SBarry Smith 
4726abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
4736abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
4746abc6512SBarry Smith 
4756abc6512SBarry Smith   /* forward solve the U^T */
4766abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
477416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
478416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
479416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
4806abc6512SBarry Smith     tmp[i] *= *v++;
4816abc6512SBarry Smith     while (nz--) {
482dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
4836abc6512SBarry Smith     }
4846abc6512SBarry Smith   }
4856abc6512SBarry Smith 
4866abc6512SBarry Smith   /* backward solve the L^T */
4876abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
488416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
489416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
490416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
4916abc6512SBarry Smith     while (nz--) {
492dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
4936abc6512SBarry Smith     }
4946abc6512SBarry Smith   }
4956abc6512SBarry Smith 
4966abc6512SBarry Smith   /* copy tmp into x according to permutation */
4976abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4986abc6512SBarry Smith 
4993b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5003b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
501898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
502898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
5036abc6512SBarry Smith 
504416022c9SBarry Smith   PLogFlops(2*a->nz);
5056abc6512SBarry Smith   return 0;
506da3a660dSBarry Smith }
5079e25ed09SBarry Smith /* ----------------------------------------------------------------*/
50808480c60SBarry Smith 
509*026e39d0SSatish Balay #undef __FUNCTION__
510*026e39d0SSatish Balay #define __FUNCTION__ "MatILUFactorSymbolic_SeqAIJ"
51108480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
5129e25ed09SBarry Smith {
513416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
5149e25ed09SBarry Smith   IS         isicol;
515416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
51656beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
51756beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
518416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
51977c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
5209e25ed09SBarry Smith 
52108480c60SBarry Smith   /* special case that simply copies fill pattern */
52277c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
52377c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
5243d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
52508480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
52608480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
52708480c60SBarry Smith     if (!b->diag) {
52808480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
52908480c60SBarry Smith     }
53008480c60SBarry Smith     b->row          = isrow;
53108480c60SBarry Smith     b->col          = iscol;
5320452661fSBarry Smith     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
53308480c60SBarry Smith     return 0;
53408480c60SBarry Smith   }
53508480c60SBarry Smith 
53678b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
537898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
538898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
5399e25ed09SBarry Smith 
5409e25ed09SBarry Smith   /* get new row pointers */
5410452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
542dbb450caSBarry Smith   ainew[0] = -shift;
5439e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
544dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
5450452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
5469e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
5470452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
5489e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
5490452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
55056beaf04SBarry Smith   /* im is level for each filled value */
5510452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
55256beaf04SBarry Smith   /* dloc is location of diagonal in factor */
5530452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
55456beaf04SBarry Smith   dloc[0]  = 0;
55556beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
5569e25ed09SBarry Smith     /* first copy previous fill into linked list */
55756beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
558dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
5599e25ed09SBarry Smith     fill[n] = n;
5609e25ed09SBarry Smith     while (nz--) {
5619e25ed09SBarry Smith       fm  = n;
562dbb450caSBarry Smith       idx = ic[*xi++ + shift];
5639e25ed09SBarry Smith       do {
5649e25ed09SBarry Smith         m  = fm;
5659e25ed09SBarry Smith         fm = fill[m];
5669e25ed09SBarry Smith       } while (fm < idx);
5679e25ed09SBarry Smith       fill[m]   = idx;
5689e25ed09SBarry Smith       fill[idx] = fm;
56956beaf04SBarry Smith       im[idx]   = 0;
5709e25ed09SBarry Smith     }
57156beaf04SBarry Smith     nzi = 0;
5729e25ed09SBarry Smith     row = fill[n];
57356beaf04SBarry Smith     while ( row < prow ) {
57456beaf04SBarry Smith       incrlev = im[row] + 1;
57556beaf04SBarry Smith       nz      = dloc[row];
576dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
577dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
578416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
579dbb450caSBarry Smith       if (*xi++ + shift != row) {
58091bf9adeSBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"MatILUFactorSymbolic_SeqAIJ:zero pivot");
58156beaf04SBarry Smith       }
58256beaf04SBarry Smith       fm      = row;
58356beaf04SBarry Smith       while (nnz-- > 0) {
584dbb450caSBarry Smith         idx = *xi++ + shift;
58556beaf04SBarry Smith         if (*flev + incrlev > levels) {
58656beaf04SBarry Smith           flev++;
58756beaf04SBarry Smith           continue;
58856beaf04SBarry Smith         }
58956beaf04SBarry Smith         do {
5909e25ed09SBarry Smith           m  = fm;
5919e25ed09SBarry Smith           fm = fill[m];
59256beaf04SBarry Smith         } while (fm < idx);
5939e25ed09SBarry Smith         if (fm != idx) {
59456beaf04SBarry Smith           im[idx]   = *flev + incrlev;
5959e25ed09SBarry Smith           fill[m]   = idx;
5969e25ed09SBarry Smith           fill[idx] = fm;
5979e25ed09SBarry Smith           fm        = idx;
59856beaf04SBarry Smith           nzf++;
5999e25ed09SBarry Smith         }
60056beaf04SBarry Smith         else {
60156beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
6029e25ed09SBarry Smith         }
60356beaf04SBarry Smith         flev++;
6049e25ed09SBarry Smith       }
6059e25ed09SBarry Smith       row = fill[row];
60656beaf04SBarry Smith       nzi++;
6079e25ed09SBarry Smith     }
6089e25ed09SBarry Smith     /* copy new filled row into permanent storage */
60956beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
610d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
6119e25ed09SBarry Smith       /* allocate a longer ajnew */
612bbb6d6a8SBarry Smith       int maxadd;
613dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
61456beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
615bbb6d6a8SBarry Smith       jmax += maxadd;
6160452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
617416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
6180452661fSBarry Smith       PetscFree(ajnew);
61956beaf04SBarry Smith       ajnew = xi;
6209e25ed09SBarry Smith       /* allocate a longer ajfill */
6210452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
622416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
6230452661fSBarry Smith       PetscFree(ajfill);
62456beaf04SBarry Smith       ajfill = xi;
625bbb6d6a8SBarry Smith       realloc++;
6269e25ed09SBarry Smith     }
627dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
628dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
62956beaf04SBarry Smith     dloc[prow]  = nzi;
6309e25ed09SBarry Smith     fm          = fill[n];
63156beaf04SBarry Smith     while (nzf--) {
632dbb450caSBarry Smith       *xi++   = fm - shift;
63356beaf04SBarry Smith       *flev++ = im[fm];
6349e25ed09SBarry Smith       fm      = fill[fm];
6359e25ed09SBarry Smith     }
6369e25ed09SBarry Smith   }
6370452661fSBarry Smith   PetscFree(ajfill);
638898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
639898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
640898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
6410452661fSBarry Smith   PetscFree(fill); PetscFree(im);
6429e25ed09SBarry Smith 
64394a424c1SBarry Smith   PLogInfo(A,
644ec8511deSBarry Smith     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
64556beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
646bbb6d6a8SBarry Smith 
6479e25ed09SBarry Smith   /* put together the new matrix */
648b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
649416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
6500452661fSBarry Smith   PetscFree(b->imax);
651416022c9SBarry Smith   b->singlemalloc = 0;
652dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
6539e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
6540452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
6550452661fSBarry Smith   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
656416022c9SBarry Smith   b->j          = ajnew;
657416022c9SBarry Smith   b->i          = ainew;
65856beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
659416022c9SBarry Smith   b->diag       = dloc;
660416022c9SBarry Smith   b->ilen       = 0;
661416022c9SBarry Smith   b->imax       = 0;
662416022c9SBarry Smith   b->row        = isrow;
663416022c9SBarry Smith   b->col        = iscol;
6640452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
665416022c9SBarry Smith   CHKPTRQ(b->solve_work);
666416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
66756beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
668dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
669416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
6709e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
671ae068f58SLois Curfman McInnes 
672ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
673ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
674ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
675ae068f58SLois Curfman McInnes 
6769e25ed09SBarry Smith   return 0;
6779e25ed09SBarry Smith }
678d5d45c9bSBarry Smith 
679d5d45c9bSBarry Smith 
680d5d45c9bSBarry Smith 
681d5d45c9bSBarry Smith 
682