xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 6b96ef82c8a99dbc18298b3061995642a6da030c)
1cb512458SBarry Smith #ifndef lint
2*6b96ef82SSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.73 1997/01/06 20:24:23 balay 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 
85615d1e5SSatish Balay #undef __FUNC__
95615d1e5SSatish Balay #define __FUNC__ "MatOrder_Flow_SeqAIJ"
103b2fbd54SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReordering type,IS *irow,IS *icol)
113b2fbd54SBarry Smith {
12e3372554SBarry Smith   SETERRQ(PETSC_ERR_SUP,0,"Code not written");
133b2fbd54SBarry Smith }
143b2fbd54SBarry Smith 
15289bc588SBarry Smith /*
16289bc588SBarry Smith     Factorization code for AIJ format.
17289bc588SBarry Smith */
185615d1e5SSatish Balay #undef __FUNC__
195615d1e5SSatish Balay #define __FUNC__ "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]];
50*6b96ef82SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
51dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
52289bc588SBarry Smith     fill[n] = n;
53289bc588SBarry Smith     while (nz--) {
54289bc588SBarry Smith       fm  = n;
55dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
56289bc588SBarry Smith       do {
57289bc588SBarry Smith         m  = fm;
58289bc588SBarry Smith         fm = fill[m];
59289bc588SBarry Smith       } while (fm < idx);
60289bc588SBarry Smith       fill[m]   = idx;
61289bc588SBarry Smith       fill[idx] = fm;
62289bc588SBarry Smith     }
63289bc588SBarry Smith     row = fill[n];
64289bc588SBarry Smith     while ( row < i ) {
65dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
662fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
672fb3ed76SBarry Smith       nz    = im[row] - nzbd;
68289bc588SBarry Smith       fm    = row;
692fb3ed76SBarry Smith       while (nz-- > 0) {
70dbb450caSBarry Smith         idx = *ajtmp++ + shift;
712fb3ed76SBarry Smith         nzbd++;
722fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
73289bc588SBarry Smith         do {
74289bc588SBarry Smith           m  = fm;
75289bc588SBarry Smith           fm = fill[m];
76289bc588SBarry Smith         } while (fm < idx);
77289bc588SBarry Smith         if (fm != idx) {
78289bc588SBarry Smith           fill[m]   = idx;
79289bc588SBarry Smith           fill[idx] = fm;
80289bc588SBarry Smith           fm        = idx;
81289bc588SBarry Smith           nnz++;
82289bc588SBarry Smith         }
83289bc588SBarry Smith       }
84289bc588SBarry Smith       row = fill[row];
85289bc588SBarry Smith     }
86289bc588SBarry Smith     /* copy new filled row into permanent storage */
87289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
88496e697eSBarry Smith     if (ainew[i+1] > jmax) {
89289bc588SBarry Smith       /* allocate a longer ajnew */
902fb3ed76SBarry Smith       int maxadd;
91dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n);
92bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
932fb3ed76SBarry Smith       jmax += maxadd;
940452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
95416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
960452661fSBarry Smith       PetscFree(ajnew);
97289bc588SBarry Smith       ajnew = ajtmp;
982fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
99289bc588SBarry Smith     }
100dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
101289bc588SBarry Smith     fm    = fill[n];
102289bc588SBarry Smith     nzi   = 0;
1032fb3ed76SBarry Smith     im[i] = nnz;
104289bc588SBarry Smith     while (nnz--) {
105289bc588SBarry Smith       if (fm < i) nzi++;
106dbb450caSBarry Smith       *ajtmp++ = fm - shift;
107289bc588SBarry Smith       fm       = fill[fm];
108289bc588SBarry Smith     }
109289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
110289bc588SBarry Smith   }
111289bc588SBarry Smith 
11294a424c1SBarry Smith   PLogInfo(A,
113ec8511deSBarry Smith     "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
114bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
1152fb3ed76SBarry Smith 
116898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
117898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1181987afe7SBarry Smith 
1190452661fSBarry Smith   PetscFree(fill);
120289bc588SBarry Smith 
121289bc588SBarry Smith   /* put together the new matrix */
122b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1231987afe7SBarry Smith   PLogObjectParent(*B,isicol);
1241987afe7SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
125416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1260452661fSBarry Smith   PetscFree(b->imax);
127416022c9SBarry Smith   b->singlemalloc = 0;
128e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1290452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
13091bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
131416022c9SBarry Smith   b->j          = ajnew;
132416022c9SBarry Smith   b->i          = ainew;
133416022c9SBarry Smith   b->diag       = idnew;
134416022c9SBarry Smith   b->ilen       = 0;
135416022c9SBarry Smith   b->imax       = 0;
136416022c9SBarry Smith   b->row        = isrow;
137416022c9SBarry Smith   b->col        = iscol;
13891bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
139416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1407fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
141416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
142416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1437fd98bd6SLois Curfman McInnes 
144ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
145ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
146ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
147ae068f58SLois Curfman McInnes 
148289bc588SBarry Smith   return 0;
149289bc588SBarry Smith }
1500a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
15141c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
15241c01911SSatish Balay 
1535615d1e5SSatish Balay #undef __FUNC__
1545615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
155416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
156289bc588SBarry Smith {
157416022c9SBarry Smith   Mat        C = *B;
158416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
159416022c9SBarry Smith   IS         iscol = b->col, isrow = b->row, isicol;
160416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1611987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
162*6b96ef82SSatish Balay   int        *diag_offset = b->diag,diag,k,nzo;
16335aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1648ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
16535aab85fSBarry Smith   double     ssum;
1661987afe7SBarry Smith   /* These declarations are for optimizations.  They reduce the number of
1671987afe7SBarry Smith      memory references that are made by locally storing information; the
1681987afe7SBarry Smith      word "register" used here with pointers can be viewed as "private" or
1691987afe7SBarry Smith      "known only to me"
1701987afe7SBarry Smith    */
17135aab85fSBarry Smith   register Scalar *pv, *rtmps,*u_values;
1721987afe7SBarry Smith   register int    *pj;
173289bc588SBarry Smith 
17478b31e54SBarry Smith   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
175416022c9SBarry Smith   PLogObjectParent(*B,isicol);
17678b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
17778b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1780452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
17913ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1800cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
181289bc588SBarry Smith 
18235aab85fSBarry Smith   /* precalcuate row sums */
18335aab85fSBarry Smith   if (preserve_row_sums) {
18435aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
18535aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
18635aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
18735aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
18835aab85fSBarry Smith       sum = 0.0;
18935aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
19035aab85fSBarry Smith       rowsums[i] = sum;
19135aab85fSBarry Smith     }
19235aab85fSBarry Smith   }
19335aab85fSBarry Smith 
194289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
195289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
196dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
19748e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
198289bc588SBarry Smith 
199289bc588SBarry Smith     /* load in initial (unfactored row) */
200416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
201416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
202416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2030cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
204289bc588SBarry Smith 
205dbb450caSBarry Smith     row = *ajtmp++ + shift;
206*6b96ef82SSatish Balay 
207*6b96ef82SSatish Balay     /* Handle case when nz=0 and the upperdiag = 0 */
208*6b96ef82SSatish Balay     while ((row < i) && nz > 0) {
2098c37ef55SBarry Smith       pc = rtmp + row;
2108c37ef55SBarry Smith       if (*pc != 0.0) {
2111987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2121987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
21335aab85fSBarry Smith         multiplier = *pc / *pv++;
2148c37ef55SBarry Smith         *pc        = multiplier;
215*6b96ef82SSatish Balay         nzo         = ai[row+1] - diag_offset[row] - 1;
216*6b96ef82SSatish Balay         for (j=0; j<nzo; j++) rtmps[pj[j]] -= multiplier * pv[j];
217*6b96ef82SSatish Balay         PLogFlops(2*nzo);
218289bc588SBarry Smith       }
219dbb450caSBarry Smith       row = *ajtmp++ + shift;
220*6b96ef82SSatish Balay       nz --;
221289bc588SBarry Smith     }
222416022c9SBarry Smith     /* finished row so stick it into b->a */
223416022c9SBarry Smith     pv = b->a + ai[i] + shift;
224416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2258c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
226416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
22735aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
22835aab85fSBarry Smith     /*
22935aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
23035aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
23135aab85fSBarry Smith     */
23235aab85fSBarry Smith     if (preserve_row_sums) {
23335aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
23435aab85fSBarry Smith       sum = rowsums[i];
23535aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
23635aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
23735aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
23835aab85fSBarry Smith         inner_sum = 0.0;
23935aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
24035aab85fSBarry Smith           inner_sum += u_values[k];
24135aab85fSBarry Smith         }
24235aab85fSBarry Smith         sum -= pv[j]*inner_sum;
24335aab85fSBarry Smith 
24435aab85fSBarry Smith       }
24535aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
24635aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
24735aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
24835aab85fSBarry Smith         sum -= u_values[k];
24935aab85fSBarry Smith       }
25035aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
25135aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
25235aab85fSBarry Smith     }
25335aab85fSBarry Smith     /* check pivot entry for current row */
25435aab85fSBarry Smith     if (pv[diag] == 0.0) {
255e3372554SBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot");
25635aab85fSBarry Smith     }
2578c37ef55SBarry Smith   }
2580f11f9aeSSatish Balay 
25935aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
26035aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
26135aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
26235aab85fSBarry Smith   }
26335aab85fSBarry Smith 
26435aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2650452661fSBarry Smith   PetscFree(rtmp);
26678b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
26778b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
26878b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
269416022c9SBarry Smith   C->factor = FACTOR_LU;
27041c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
271c456f294SBarry Smith   C->assembled = PETSC_TRUE;
272416022c9SBarry Smith   PLogFlops(b->n);
273289bc588SBarry Smith   return 0;
274289bc588SBarry Smith }
2750a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
2765615d1e5SSatish Balay #undef __FUNC__
2775615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
278416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
279da3a660dSBarry Smith {
280416022c9SBarry Smith   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
2816abc6512SBarry Smith   int        ierr;
282416022c9SBarry Smith   Mat        C;
283416022c9SBarry Smith 
284416022c9SBarry Smith   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
285416022c9SBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
286da3a660dSBarry Smith 
287da3a660dSBarry Smith   /* free all the data structures from mat */
2880452661fSBarry Smith   PetscFree(mat->a);
2890452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
2900452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
2910452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
2920452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
2930452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
294a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
2950452661fSBarry Smith   PetscFree(mat);
296da3a660dSBarry Smith 
297416022c9SBarry Smith   PetscMemcpy(A,C,sizeof(struct _Mat));
2980452661fSBarry Smith   PetscHeaderDestroy(C);
299da3a660dSBarry Smith   return 0;
300da3a660dSBarry Smith }
3010a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
3025615d1e5SSatish Balay #undef __FUNC__
3035615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
304416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3058c37ef55SBarry Smith {
306416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
307416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
308416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3093b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
310416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3118c37ef55SBarry Smith 
31291bf9adeSBarry Smith   if (!n) return 0;
31391bf9adeSBarry Smith 
31490f02eecSBarry Smith   VecGetArray_Fast(bb,b);
31590f02eecSBarry Smith   VecGetArray_Fast(xx,x);
316416022c9SBarry Smith   tmp  = a->solve_work;
3178c37ef55SBarry Smith 
3183b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3193b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3208c37ef55SBarry Smith 
3218c37ef55SBarry Smith   /* forward solve the lower triangular */
3228c37ef55SBarry Smith   tmp[0] = b[*r++];
3230cf60383SBarry Smith   tmps   = tmp + shift;
3248c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
325dbb450caSBarry Smith     v   = aa + ai[i] + shift;
326dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
327416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3288c37ef55SBarry Smith     sum = b[*r++];
3290cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3308c37ef55SBarry Smith     tmp[i] = sum;
3318c37ef55SBarry Smith   }
3328c37ef55SBarry Smith 
3338c37ef55SBarry Smith   /* backward solve the upper triangular */
3348c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
335416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
336416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
337416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3388c37ef55SBarry Smith     sum = tmp[i];
3390cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
340416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3418c37ef55SBarry Smith   }
3428c37ef55SBarry Smith 
3433b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3443b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
345416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3468c37ef55SBarry Smith   return 0;
3478c37ef55SBarry Smith }
348026e39d0SSatish Balay 
3495615d1e5SSatish Balay #undef __FUNC__
3505615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
351416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
352da3a660dSBarry Smith {
353416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
354416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
355416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3563b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
357416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
358da3a660dSBarry Smith 
35978b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
360da3a660dSBarry Smith 
36190f02eecSBarry Smith   VecGetArray_Fast(bb,b);
36290f02eecSBarry Smith   VecGetArray_Fast(xx,x);
363416022c9SBarry Smith   tmp  = a->solve_work;
364da3a660dSBarry Smith 
3653b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
3663b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
367da3a660dSBarry Smith 
368da3a660dSBarry Smith   /* forward solve the lower triangular */
369da3a660dSBarry Smith   tmp[0] = b[*r++];
370da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
371dbb450caSBarry Smith     v   = aa + ai[i] + shift;
372dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
373416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
374da3a660dSBarry Smith     sum = b[*r++];
375dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
376da3a660dSBarry Smith     tmp[i] = sum;
377da3a660dSBarry Smith   }
378da3a660dSBarry Smith 
379da3a660dSBarry Smith   /* backward solve the upper triangular */
380da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
381416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
382416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
383416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
384da3a660dSBarry Smith     sum = tmp[i];
385dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
386416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
387da3a660dSBarry Smith     x[*c--] += tmp[i];
388da3a660dSBarry Smith   }
389da3a660dSBarry Smith 
3903b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3913b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
392416022c9SBarry Smith   PLogFlops(2*a->nz);
393898183ecSLois Curfman McInnes 
394da3a660dSBarry Smith   return 0;
395da3a660dSBarry Smith }
396da3a660dSBarry Smith /* -------------------------------------------------------------------*/
3975615d1e5SSatish Balay #undef __FUNC__
3985615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ"
399416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
400da3a660dSBarry Smith {
401416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
402416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
403416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4043b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
405416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
406da3a660dSBarry Smith 
40790f02eecSBarry Smith   VecGetArray_Fast(bb,b);
40890f02eecSBarry Smith   VecGetArray_Fast(xx,x);
409416022c9SBarry Smith   tmp  = a->solve_work;
410da3a660dSBarry Smith 
411da3a660dSBarry Smith   /* invert the permutations */
41278b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
41378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
414da3a660dSBarry Smith 
4153b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4163b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
417da3a660dSBarry Smith 
418da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
419da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
420da3a660dSBarry Smith 
421da3a660dSBarry Smith   /* forward solve the U^T */
422da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
423416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
424416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
425416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
426da3a660dSBarry Smith     tmp[i] *= *v++;
427da3a660dSBarry Smith     while (nz--) {
428dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
429da3a660dSBarry Smith     }
430da3a660dSBarry Smith   }
431da3a660dSBarry Smith 
432da3a660dSBarry Smith   /* backward solve the L^T */
433da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
434416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
435416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
436416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
437da3a660dSBarry Smith     while (nz--) {
438dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
439da3a660dSBarry Smith     }
440da3a660dSBarry Smith   }
441da3a660dSBarry Smith 
442da3a660dSBarry Smith   /* copy tmp into x according to permutation */
443da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
444da3a660dSBarry Smith 
4453b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
4463b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
447898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
448898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
449da3a660dSBarry Smith 
450416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
451da3a660dSBarry Smith   return 0;
452da3a660dSBarry Smith }
453da3a660dSBarry Smith 
4545615d1e5SSatish Balay #undef __FUNC__
4555615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ"
456416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
457da3a660dSBarry Smith {
458416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
459416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
460416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4613b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
462416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
4636abc6512SBarry Smith 
4646abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
4656abc6512SBarry Smith 
46690f02eecSBarry Smith   VecGetArray_Fast(bb,b);
46790f02eecSBarry Smith   VecGetArray_Fast(xx,x);
468416022c9SBarry Smith   tmp = a->solve_work;
4696abc6512SBarry Smith 
4706abc6512SBarry Smith   /* invert the permutations */
47178b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
47278b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
4733b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4743b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
4756abc6512SBarry Smith 
4766abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
4776abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
4786abc6512SBarry Smith 
4796abc6512SBarry Smith   /* forward solve the U^T */
4806abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
481416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
482416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
483416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
4846abc6512SBarry Smith     tmp[i] *= *v++;
4856abc6512SBarry Smith     while (nz--) {
486dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
4876abc6512SBarry Smith     }
4886abc6512SBarry Smith   }
4896abc6512SBarry Smith 
4906abc6512SBarry Smith   /* backward solve the L^T */
4916abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
492416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
493416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
494416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
4956abc6512SBarry Smith     while (nz--) {
496dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
4976abc6512SBarry Smith     }
4986abc6512SBarry Smith   }
4996abc6512SBarry Smith 
5006abc6512SBarry Smith   /* copy tmp into x according to permutation */
5016abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
5026abc6512SBarry Smith 
5033b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5043b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
505898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
506898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
5076abc6512SBarry Smith 
508416022c9SBarry Smith   PLogFlops(2*a->nz);
5096abc6512SBarry Smith   return 0;
510da3a660dSBarry Smith }
5119e25ed09SBarry Smith /* ----------------------------------------------------------------*/
51208480c60SBarry Smith 
5135615d1e5SSatish Balay #undef __FUNC__
5145615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
51508480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
5169e25ed09SBarry Smith {
517416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
5189e25ed09SBarry Smith   IS         isicol;
519416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
52056beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
52156beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
522416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
52377c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
5249e25ed09SBarry Smith 
52508480c60SBarry Smith   /* special case that simply copies fill pattern */
52677c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
52777c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
5283d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
52908480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
53008480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
53108480c60SBarry Smith     if (!b->diag) {
53208480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
53308480c60SBarry Smith     }
53408480c60SBarry Smith     b->row          = isrow;
53508480c60SBarry Smith     b->col          = iscol;
5360452661fSBarry Smith     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
53708480c60SBarry Smith     return 0;
53808480c60SBarry Smith   }
53908480c60SBarry Smith 
54078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
541898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
542898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
5439e25ed09SBarry Smith 
5449e25ed09SBarry Smith   /* get new row pointers */
5450452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
546dbb450caSBarry Smith   ainew[0] = -shift;
5479e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
548dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
5490452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
5509e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
5510452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
5529e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
5530452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
55456beaf04SBarry Smith   /* im is level for each filled value */
5550452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
55656beaf04SBarry Smith   /* dloc is location of diagonal in factor */
5570452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
55856beaf04SBarry Smith   dloc[0]  = 0;
55956beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
5609e25ed09SBarry Smith     /* first copy previous fill into linked list */
56156beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
562dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
5639e25ed09SBarry Smith     fill[n] = n;
5649e25ed09SBarry Smith     while (nz--) {
5659e25ed09SBarry Smith       fm  = n;
566dbb450caSBarry Smith       idx = ic[*xi++ + shift];
5679e25ed09SBarry Smith       do {
5689e25ed09SBarry Smith         m  = fm;
5699e25ed09SBarry Smith         fm = fill[m];
5709e25ed09SBarry Smith       } while (fm < idx);
5719e25ed09SBarry Smith       fill[m]   = idx;
5729e25ed09SBarry Smith       fill[idx] = fm;
57356beaf04SBarry Smith       im[idx]   = 0;
5749e25ed09SBarry Smith     }
57556beaf04SBarry Smith     nzi = 0;
5769e25ed09SBarry Smith     row = fill[n];
57756beaf04SBarry Smith     while ( row < prow ) {
57856beaf04SBarry Smith       incrlev = im[row] + 1;
57956beaf04SBarry Smith       nz      = dloc[row];
580dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
581dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
582416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
583dbb450caSBarry Smith       if (*xi++ + shift != row) {
584e3372554SBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"zero pivot");
58556beaf04SBarry Smith       }
58656beaf04SBarry Smith       fm      = row;
58756beaf04SBarry Smith       while (nnz-- > 0) {
588dbb450caSBarry Smith         idx = *xi++ + shift;
58956beaf04SBarry Smith         if (*flev + incrlev > levels) {
59056beaf04SBarry Smith           flev++;
59156beaf04SBarry Smith           continue;
59256beaf04SBarry Smith         }
59356beaf04SBarry Smith         do {
5949e25ed09SBarry Smith           m  = fm;
5959e25ed09SBarry Smith           fm = fill[m];
59656beaf04SBarry Smith         } while (fm < idx);
5979e25ed09SBarry Smith         if (fm != idx) {
59856beaf04SBarry Smith           im[idx]   = *flev + incrlev;
5999e25ed09SBarry Smith           fill[m]   = idx;
6009e25ed09SBarry Smith           fill[idx] = fm;
6019e25ed09SBarry Smith           fm        = idx;
60256beaf04SBarry Smith           nzf++;
6039e25ed09SBarry Smith         }
60456beaf04SBarry Smith         else {
60556beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
6069e25ed09SBarry Smith         }
60756beaf04SBarry Smith         flev++;
6089e25ed09SBarry Smith       }
6099e25ed09SBarry Smith       row = fill[row];
61056beaf04SBarry Smith       nzi++;
6119e25ed09SBarry Smith     }
6129e25ed09SBarry Smith     /* copy new filled row into permanent storage */
61356beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
614d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
6159e25ed09SBarry Smith       /* allocate a longer ajnew */
616bbb6d6a8SBarry Smith       int maxadd;
617dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
61856beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
619bbb6d6a8SBarry Smith       jmax += maxadd;
6200452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
621416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
6220452661fSBarry Smith       PetscFree(ajnew);
62356beaf04SBarry Smith       ajnew = xi;
6249e25ed09SBarry Smith       /* allocate a longer ajfill */
6250452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
626416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
6270452661fSBarry Smith       PetscFree(ajfill);
62856beaf04SBarry Smith       ajfill = xi;
629bbb6d6a8SBarry Smith       realloc++;
6309e25ed09SBarry Smith     }
631dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
632dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
63356beaf04SBarry Smith     dloc[prow]  = nzi;
6349e25ed09SBarry Smith     fm          = fill[n];
63556beaf04SBarry Smith     while (nzf--) {
636dbb450caSBarry Smith       *xi++   = fm - shift;
63756beaf04SBarry Smith       *flev++ = im[fm];
6389e25ed09SBarry Smith       fm      = fill[fm];
6399e25ed09SBarry Smith     }
6409e25ed09SBarry Smith   }
6410452661fSBarry Smith   PetscFree(ajfill);
642898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
643898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
644898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
6450452661fSBarry Smith   PetscFree(fill); PetscFree(im);
6469e25ed09SBarry Smith 
64794a424c1SBarry Smith   PLogInfo(A,
648ec8511deSBarry Smith     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
64956beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
650bbb6d6a8SBarry Smith 
6519e25ed09SBarry Smith   /* put together the new matrix */
652b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
653416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
6540452661fSBarry Smith   PetscFree(b->imax);
655416022c9SBarry Smith   b->singlemalloc = 0;
656dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
6579e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
6580452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
659*6b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a);
660416022c9SBarry Smith   b->j          = ajnew;
661416022c9SBarry Smith   b->i          = ainew;
66256beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
663416022c9SBarry Smith   b->diag       = dloc;
664416022c9SBarry Smith   b->ilen       = 0;
665416022c9SBarry Smith   b->imax       = 0;
666416022c9SBarry Smith   b->row        = isrow;
667416022c9SBarry Smith   b->col        = iscol;
6680452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
669416022c9SBarry Smith   CHKPTRQ(b->solve_work);
670416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
67156beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
672dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
673416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
6749e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
675ae068f58SLois Curfman McInnes 
676ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
677ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
678ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
679ae068f58SLois Curfman McInnes 
6809e25ed09SBarry Smith   return 0;
6819e25ed09SBarry Smith }
682d5d45c9bSBarry Smith 
683d5d45c9bSBarry Smith 
684d5d45c9bSBarry Smith 
685d5d45c9bSBarry Smith 
686