xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 05bf559cbb56c1096253c7feaa733587ece3e0bd)
1cb512458SBarry Smith #ifndef lint
2*05bf559cSSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.77 1997/02/22 02:25:00 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 
85615d1e5SSatish Balay #undef __FUNC__
95eea60f9SBarry Smith #define __FUNC__ "MatOrder_Flow_SeqAIJ" /* ADIC Ignore */
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]];
506b96ef82SSatish 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   }
111*05bf559cSSatish Balay   if (ai[i] != 0) {
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]));
115*05bf559cSSatish Balay   } else {
116*05bf559cSSatish Balay     PLogInfo(A,
117*05bf559cSSatish Balay              "Info:MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
118*05bf559cSSatish Balay   }
1192fb3ed76SBarry Smith 
120898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
121898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1221987afe7SBarry Smith 
1230452661fSBarry Smith   PetscFree(fill);
124289bc588SBarry Smith 
125289bc588SBarry Smith   /* put together the new matrix */
126b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1271987afe7SBarry Smith   PLogObjectParent(*B,isicol);
1281987afe7SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
129416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1300452661fSBarry Smith   PetscFree(b->imax);
131416022c9SBarry Smith   b->singlemalloc = 0;
132e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1330452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
13491bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
135416022c9SBarry Smith   b->j          = ajnew;
136416022c9SBarry Smith   b->i          = ainew;
137416022c9SBarry Smith   b->diag       = idnew;
138416022c9SBarry Smith   b->ilen       = 0;
139416022c9SBarry Smith   b->imax       = 0;
140416022c9SBarry Smith   b->row        = isrow;
141416022c9SBarry Smith   b->col        = iscol;
14291bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
143416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1447fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
145416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
146416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1477fd98bd6SLois Curfman McInnes 
148ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
149ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
150ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
151ae068f58SLois Curfman McInnes 
152289bc588SBarry Smith   return 0;
153289bc588SBarry Smith }
1540a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
15541c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
15641c01911SSatish Balay 
1575615d1e5SSatish Balay #undef __FUNC__
1585615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
159416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
160289bc588SBarry Smith {
161416022c9SBarry Smith   Mat        C = *B;
162416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
163416022c9SBarry Smith   IS         iscol = b->col, isrow = b->row, isicol;
164416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1651987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
166f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
16735aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1688ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
16935aab85fSBarry Smith   double     ssum;
1701987afe7SBarry Smith   /* These declarations are for optimizations.  They reduce the number of
1711987afe7SBarry Smith      memory references that are made by locally storing information; the
1721987afe7SBarry Smith      word "register" used here with pointers can be viewed as "private" or
1731987afe7SBarry Smith      "known only to me"
1741987afe7SBarry Smith    */
17535aab85fSBarry Smith   register Scalar *pv, *rtmps,*u_values;
1761987afe7SBarry Smith   register int    *pj;
177289bc588SBarry Smith 
17878b31e54SBarry Smith   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
179416022c9SBarry Smith   PLogObjectParent(*B,isicol);
18078b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
18178b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1820452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
18313ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1840cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
185289bc588SBarry Smith 
18635aab85fSBarry Smith   /* precalcuate row sums */
18735aab85fSBarry Smith   if (preserve_row_sums) {
18835aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
18935aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
19035aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
19135aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
19235aab85fSBarry Smith       sum = 0.0;
19335aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
19435aab85fSBarry Smith       rowsums[i] = sum;
19535aab85fSBarry Smith     }
19635aab85fSBarry Smith   }
19735aab85fSBarry Smith 
198289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
199289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
200dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
20148e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
202289bc588SBarry Smith 
203289bc588SBarry Smith     /* load in initial (unfactored row) */
204416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
205416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
206416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2070cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
208289bc588SBarry Smith 
209dbb450caSBarry Smith     row = *ajtmp++ + shift;
210f2582111SSatish Balay       while  (row < i ) {
2118c37ef55SBarry Smith       pc = rtmp + row;
2128c37ef55SBarry Smith       if (*pc != 0.0) {
2131987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2141987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
21535aab85fSBarry Smith         multiplier = *pc / *pv++;
2168c37ef55SBarry Smith         *pc        = multiplier;
217f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
218f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
219f2582111SSatish Balay         PLogFlops(2*nz);
220289bc588SBarry Smith       }
221dbb450caSBarry Smith       row = *ajtmp++ + shift;
222289bc588SBarry Smith     }
223416022c9SBarry Smith     /* finished row so stick it into b->a */
224416022c9SBarry Smith     pv = b->a + ai[i] + shift;
225416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2268c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
227416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
22835aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
22935aab85fSBarry Smith     /*
23035aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
23135aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
23235aab85fSBarry Smith     */
23335aab85fSBarry Smith     if (preserve_row_sums) {
23435aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
23535aab85fSBarry Smith       sum = rowsums[i];
23635aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
23735aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
23835aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
23935aab85fSBarry Smith         inner_sum = 0.0;
24035aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
24135aab85fSBarry Smith           inner_sum += u_values[k];
24235aab85fSBarry Smith         }
24335aab85fSBarry Smith         sum -= pv[j]*inner_sum;
24435aab85fSBarry Smith 
24535aab85fSBarry Smith       }
24635aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
24735aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
24835aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
24935aab85fSBarry Smith         sum -= u_values[k];
25035aab85fSBarry Smith       }
25135aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
25235aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
25335aab85fSBarry Smith     }
25435aab85fSBarry Smith     /* check pivot entry for current row */
25535aab85fSBarry Smith     if (pv[diag] == 0.0) {
256e3372554SBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot");
25735aab85fSBarry Smith     }
2588c37ef55SBarry Smith   }
2590f11f9aeSSatish Balay 
26035aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
26135aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
26235aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
26335aab85fSBarry Smith   }
26435aab85fSBarry Smith 
26535aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2660452661fSBarry Smith   PetscFree(rtmp);
26778b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
26878b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
26978b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
270416022c9SBarry Smith   C->factor = FACTOR_LU;
27141c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
272c456f294SBarry Smith   C->assembled = PETSC_TRUE;
273416022c9SBarry Smith   PLogFlops(b->n);
274289bc588SBarry Smith   return 0;
275289bc588SBarry Smith }
2760a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
2775615d1e5SSatish Balay #undef __FUNC__
2785615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
279416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
280da3a660dSBarry Smith {
281416022c9SBarry Smith   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
2826abc6512SBarry Smith   int        ierr;
283416022c9SBarry Smith   Mat        C;
284416022c9SBarry Smith 
285f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr);
286f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr);
287da3a660dSBarry Smith 
288da3a660dSBarry Smith   /* free all the data structures from mat */
2890452661fSBarry Smith   PetscFree(mat->a);
2900452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
2910452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
2920452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
2930452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
2940452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
295a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
2960452661fSBarry Smith   PetscFree(mat);
297da3a660dSBarry Smith 
298416022c9SBarry Smith   PetscMemcpy(A,C,sizeof(struct _Mat));
2990452661fSBarry Smith   PetscHeaderDestroy(C);
300da3a660dSBarry Smith   return 0;
301da3a660dSBarry Smith }
3020a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
3035615d1e5SSatish Balay #undef __FUNC__
3045615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
305416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3068c37ef55SBarry Smith {
307416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
308416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
309416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3103b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
311416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3128c37ef55SBarry Smith 
31391bf9adeSBarry Smith   if (!n) return 0;
31491bf9adeSBarry Smith 
31590f02eecSBarry Smith   VecGetArray_Fast(bb,b);
31690f02eecSBarry Smith   VecGetArray_Fast(xx,x);
317416022c9SBarry Smith   tmp  = a->solve_work;
3188c37ef55SBarry Smith 
3193b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3203b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3218c37ef55SBarry Smith 
3228c37ef55SBarry Smith   /* forward solve the lower triangular */
3238c37ef55SBarry Smith   tmp[0] = b[*r++];
3240cf60383SBarry Smith   tmps   = tmp + shift;
3258c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
326dbb450caSBarry Smith     v   = aa + ai[i] + shift;
327dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
328416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3298c37ef55SBarry Smith     sum = b[*r++];
3300cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3318c37ef55SBarry Smith     tmp[i] = sum;
3328c37ef55SBarry Smith   }
3338c37ef55SBarry Smith 
3348c37ef55SBarry Smith   /* backward solve the upper triangular */
3358c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
336416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
337416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
338416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3398c37ef55SBarry Smith     sum = tmp[i];
3400cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
341416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3428c37ef55SBarry Smith   }
3438c37ef55SBarry Smith 
3443b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3453b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
346416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3478c37ef55SBarry Smith   return 0;
3488c37ef55SBarry Smith }
349026e39d0SSatish Balay 
3505615d1e5SSatish Balay #undef __FUNC__
3515615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
352416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
353da3a660dSBarry Smith {
354416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
355416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
356416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3573b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
358416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
359da3a660dSBarry Smith 
36078b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
361da3a660dSBarry Smith 
36290f02eecSBarry Smith   VecGetArray_Fast(bb,b);
36390f02eecSBarry Smith   VecGetArray_Fast(xx,x);
364416022c9SBarry Smith   tmp  = a->solve_work;
365da3a660dSBarry Smith 
3663b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
3673b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
368da3a660dSBarry Smith 
369da3a660dSBarry Smith   /* forward solve the lower triangular */
370da3a660dSBarry Smith   tmp[0] = b[*r++];
371da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
372dbb450caSBarry Smith     v   = aa + ai[i] + shift;
373dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
374416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
375da3a660dSBarry Smith     sum = b[*r++];
376dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
377da3a660dSBarry Smith     tmp[i] = sum;
378da3a660dSBarry Smith   }
379da3a660dSBarry Smith 
380da3a660dSBarry Smith   /* backward solve the upper triangular */
381da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
382416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
383416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
384416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
385da3a660dSBarry Smith     sum = tmp[i];
386dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
387416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
388da3a660dSBarry Smith     x[*c--] += tmp[i];
389da3a660dSBarry Smith   }
390da3a660dSBarry Smith 
3913b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3923b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
393416022c9SBarry Smith   PLogFlops(2*a->nz);
394898183ecSLois Curfman McInnes 
395da3a660dSBarry Smith   return 0;
396da3a660dSBarry Smith }
397da3a660dSBarry Smith /* -------------------------------------------------------------------*/
3985615d1e5SSatish Balay #undef __FUNC__
3995615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ"
400416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
401da3a660dSBarry Smith {
402416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
403416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
404416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4053b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
406416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
407da3a660dSBarry Smith 
40890f02eecSBarry Smith   VecGetArray_Fast(bb,b);
40990f02eecSBarry Smith   VecGetArray_Fast(xx,x);
410416022c9SBarry Smith   tmp  = a->solve_work;
411da3a660dSBarry Smith 
412da3a660dSBarry Smith   /* invert the permutations */
41378b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
41478b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
415da3a660dSBarry Smith 
4163b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4173b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
418da3a660dSBarry Smith 
419da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
420da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
421da3a660dSBarry Smith 
422da3a660dSBarry Smith   /* forward solve the U^T */
423da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
424416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
425416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
426416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
427da3a660dSBarry Smith     tmp[i] *= *v++;
428da3a660dSBarry Smith     while (nz--) {
429dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
430da3a660dSBarry Smith     }
431da3a660dSBarry Smith   }
432da3a660dSBarry Smith 
433da3a660dSBarry Smith   /* backward solve the L^T */
434da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
435416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
436416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
437416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
438da3a660dSBarry Smith     while (nz--) {
439dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
440da3a660dSBarry Smith     }
441da3a660dSBarry Smith   }
442da3a660dSBarry Smith 
443da3a660dSBarry Smith   /* copy tmp into x according to permutation */
444da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
445da3a660dSBarry Smith 
4463b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
4473b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
448898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
449898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
450da3a660dSBarry Smith 
451416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
452da3a660dSBarry Smith   return 0;
453da3a660dSBarry Smith }
454da3a660dSBarry Smith 
4555615d1e5SSatish Balay #undef __FUNC__
4565615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ"
457416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
458da3a660dSBarry Smith {
459416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
460416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
461416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4623b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
463416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
4646abc6512SBarry Smith 
4656abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
4666abc6512SBarry Smith 
46790f02eecSBarry Smith   VecGetArray_Fast(bb,b);
46890f02eecSBarry Smith   VecGetArray_Fast(xx,x);
469416022c9SBarry Smith   tmp = a->solve_work;
4706abc6512SBarry Smith 
4716abc6512SBarry Smith   /* invert the permutations */
47278b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
47378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
4743b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4753b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
4766abc6512SBarry Smith 
4776abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
4786abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
4796abc6512SBarry Smith 
4806abc6512SBarry Smith   /* forward solve the U^T */
4816abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
482416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
483416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
484416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
4856abc6512SBarry Smith     tmp[i] *= *v++;
4866abc6512SBarry Smith     while (nz--) {
487dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
4886abc6512SBarry Smith     }
4896abc6512SBarry Smith   }
4906abc6512SBarry Smith 
4916abc6512SBarry Smith   /* backward solve the L^T */
4926abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
493416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
494416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
495416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
4966abc6512SBarry Smith     while (nz--) {
497dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
4986abc6512SBarry Smith     }
4996abc6512SBarry Smith   }
5006abc6512SBarry Smith 
5016abc6512SBarry Smith   /* copy tmp into x according to permutation */
5026abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
5036abc6512SBarry Smith 
5043b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5053b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
506898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
507898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
5086abc6512SBarry Smith 
509416022c9SBarry Smith   PLogFlops(2*a->nz);
5106abc6512SBarry Smith   return 0;
511da3a660dSBarry Smith }
5129e25ed09SBarry Smith /* ----------------------------------------------------------------*/
51308480c60SBarry Smith 
5145615d1e5SSatish Balay #undef __FUNC__
5155615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
51608480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
5179e25ed09SBarry Smith {
518416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
5199e25ed09SBarry Smith   IS         isicol;
520416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
52156beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
52256beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
523416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
52477c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
5259e25ed09SBarry Smith 
52608480c60SBarry Smith   /* special case that simply copies fill pattern */
52777c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
52877c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
5293d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
53008480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
53108480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
53208480c60SBarry Smith     if (!b->diag) {
53308480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
53408480c60SBarry Smith     }
53508480c60SBarry Smith     b->row          = isrow;
53608480c60SBarry Smith     b->col          = iscol;
5370452661fSBarry Smith     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
53808480c60SBarry Smith     return 0;
53908480c60SBarry Smith   }
54008480c60SBarry Smith 
54178b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
542898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
543898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
5449e25ed09SBarry Smith 
5459e25ed09SBarry Smith   /* get new row pointers */
5460452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
547dbb450caSBarry Smith   ainew[0] = -shift;
5489e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
549dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
5500452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
5519e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
5520452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
5539e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
5540452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
55556beaf04SBarry Smith   /* im is level for each filled value */
5560452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
55756beaf04SBarry Smith   /* dloc is location of diagonal in factor */
5580452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
55956beaf04SBarry Smith   dloc[0]  = 0;
56056beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
5619e25ed09SBarry Smith     /* first copy previous fill into linked list */
56256beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
563385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
564dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
5659e25ed09SBarry Smith     fill[n] = n;
5669e25ed09SBarry Smith     while (nz--) {
5679e25ed09SBarry Smith       fm  = n;
568dbb450caSBarry Smith       idx = ic[*xi++ + shift];
5699e25ed09SBarry Smith       do {
5709e25ed09SBarry Smith         m  = fm;
5719e25ed09SBarry Smith         fm = fill[m];
5729e25ed09SBarry Smith       } while (fm < idx);
5739e25ed09SBarry Smith       fill[m]   = idx;
5749e25ed09SBarry Smith       fill[idx] = fm;
57556beaf04SBarry Smith       im[idx]   = 0;
5769e25ed09SBarry Smith     }
57756beaf04SBarry Smith     nzi = 0;
5789e25ed09SBarry Smith     row = fill[n];
57956beaf04SBarry Smith     while ( row < prow ) {
58056beaf04SBarry Smith       incrlev = im[row] + 1;
58156beaf04SBarry Smith       nz      = dloc[row];
582dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
583dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
584416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
585dbb450caSBarry Smith       if (*xi++ + shift != row) {
586e3372554SBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"zero pivot");
58756beaf04SBarry Smith       }
58856beaf04SBarry Smith       fm      = row;
58956beaf04SBarry Smith       while (nnz-- > 0) {
590dbb450caSBarry Smith         idx = *xi++ + shift;
59156beaf04SBarry Smith         if (*flev + incrlev > levels) {
59256beaf04SBarry Smith           flev++;
59356beaf04SBarry Smith           continue;
59456beaf04SBarry Smith         }
59556beaf04SBarry Smith         do {
5969e25ed09SBarry Smith           m  = fm;
5979e25ed09SBarry Smith           fm = fill[m];
59856beaf04SBarry Smith         } while (fm < idx);
5999e25ed09SBarry Smith         if (fm != idx) {
60056beaf04SBarry Smith           im[idx]   = *flev + incrlev;
6019e25ed09SBarry Smith           fill[m]   = idx;
6029e25ed09SBarry Smith           fill[idx] = fm;
6039e25ed09SBarry Smith           fm        = idx;
60456beaf04SBarry Smith           nzf++;
6059e25ed09SBarry Smith         }
60656beaf04SBarry Smith         else {
60756beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
6089e25ed09SBarry Smith         }
60956beaf04SBarry Smith         flev++;
6109e25ed09SBarry Smith       }
6119e25ed09SBarry Smith       row = fill[row];
61256beaf04SBarry Smith       nzi++;
6139e25ed09SBarry Smith     }
6149e25ed09SBarry Smith     /* copy new filled row into permanent storage */
61556beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
616d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
6179e25ed09SBarry Smith       /* allocate a longer ajnew */
618bbb6d6a8SBarry Smith       int maxadd;
619dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
62056beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
621bbb6d6a8SBarry Smith       jmax += maxadd;
6220452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
623416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
6240452661fSBarry Smith       PetscFree(ajnew);
62556beaf04SBarry Smith       ajnew = xi;
6269e25ed09SBarry Smith       /* allocate a longer ajfill */
6270452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
628416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
6290452661fSBarry Smith       PetscFree(ajfill);
63056beaf04SBarry Smith       ajfill = xi;
631bbb6d6a8SBarry Smith       realloc++;
6329e25ed09SBarry Smith     }
633dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
634dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
63556beaf04SBarry Smith     dloc[prow]  = nzi;
6369e25ed09SBarry Smith     fm          = fill[n];
63756beaf04SBarry Smith     while (nzf--) {
638dbb450caSBarry Smith       *xi++   = fm - shift;
63956beaf04SBarry Smith       *flev++ = im[fm];
6409e25ed09SBarry Smith       fm      = fill[fm];
6419e25ed09SBarry Smith     }
6429e25ed09SBarry Smith   }
6430452661fSBarry Smith   PetscFree(ajfill);
644898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
645898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
646898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
6470452661fSBarry Smith   PetscFree(fill); PetscFree(im);
6489e25ed09SBarry Smith 
64994a424c1SBarry Smith   PLogInfo(A,
650ec8511deSBarry Smith     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
65156beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
652bbb6d6a8SBarry Smith 
6539e25ed09SBarry Smith   /* put together the new matrix */
654b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
655416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
6560452661fSBarry Smith   PetscFree(b->imax);
657416022c9SBarry Smith   b->singlemalloc = 0;
658dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
6599e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
6600452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
6616b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a);
662416022c9SBarry Smith   b->j          = ajnew;
663416022c9SBarry Smith   b->i          = ainew;
66456beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
665416022c9SBarry Smith   b->diag       = dloc;
666416022c9SBarry Smith   b->ilen       = 0;
667416022c9SBarry Smith   b->imax       = 0;
668416022c9SBarry Smith   b->row        = isrow;
669416022c9SBarry Smith   b->col        = iscol;
6700452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
671416022c9SBarry Smith   CHKPTRQ(b->solve_work);
672416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
67356beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
674dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
675416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
6769e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
677ae068f58SLois Curfman McInnes 
678ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
679ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
680ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
681ae068f58SLois Curfman McInnes 
6829e25ed09SBarry Smith   return 0;
6839e25ed09SBarry Smith }
684d5d45c9bSBarry Smith 
685d5d45c9bSBarry Smith 
686d5d45c9bSBarry Smith 
687d5d45c9bSBarry Smith 
688