xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 3b2fbd549e90270c527c9a23f7b62c8352190f78)
1cb512458SBarry Smith #ifndef lint
2*3b2fbd54SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.66 1996/08/22 20:07:14 curfman Exp bsmith $";
3cb512458SBarry Smith #endif
4289bc588SBarry Smith 
570f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
6*3b2fbd54SBarry Smith 
7*3b2fbd54SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReordering type,IS *irow,IS *icol)
8*3b2fbd54SBarry Smith {
9*3b2fbd54SBarry Smith   SETERRQ(1,"MatOrder_Flow_SeqAIJ:Code not written");
10*3b2fbd54SBarry Smith }
11*3b2fbd54SBarry Smith 
12289bc588SBarry Smith /*
13289bc588SBarry Smith     Factorization code for AIJ format.
14289bc588SBarry Smith */
15416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
16289bc588SBarry Smith {
17416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
18289bc588SBarry Smith   IS         isicol;
19416022c9SBarry Smith   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
20416022c9SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
212fb3ed76SBarry Smith   int        *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im;
22289bc588SBarry Smith 
23d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
24d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
25*3b2fbd54SBarry Smith 
2678b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
27289bc588SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
28289bc588SBarry Smith 
29289bc588SBarry Smith   /* get new row pointers */
300452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
31dbb450caSBarry Smith   ainew[0] = -shift;
32289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
33dbb450caSBarry Smith   jmax = (int) (f*ai[n]+(!shift));
340452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
35289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
360452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
372fb3ed76SBarry Smith   im = fill + n + 1;
38289bc588SBarry Smith   /* idnew is location of diagonal in factor */
390452661fSBarry Smith   idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
40dbb450caSBarry Smith   idnew[0] = -shift;
41289bc588SBarry Smith 
42289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
43289bc588SBarry Smith     /* first copy previous fill into linked list */
44289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
45dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
46289bc588SBarry Smith     fill[n] = n;
47289bc588SBarry Smith     while (nz--) {
48289bc588SBarry Smith       fm  = n;
49dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
50289bc588SBarry Smith       do {
51289bc588SBarry Smith         m  = fm;
52289bc588SBarry Smith         fm = fill[m];
53289bc588SBarry Smith       } while (fm < idx);
54289bc588SBarry Smith       fill[m]   = idx;
55289bc588SBarry Smith       fill[idx] = fm;
56289bc588SBarry Smith     }
57289bc588SBarry Smith     row = fill[n];
58289bc588SBarry Smith     while ( row < i ) {
59dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
602fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
612fb3ed76SBarry Smith       nz    = im[row] - nzbd;
62289bc588SBarry Smith       fm    = row;
632fb3ed76SBarry Smith       while (nz-- > 0) {
64dbb450caSBarry Smith         idx = *ajtmp++ + shift;
652fb3ed76SBarry Smith         nzbd++;
662fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
67289bc588SBarry Smith         do {
68289bc588SBarry Smith           m  = fm;
69289bc588SBarry Smith           fm = fill[m];
70289bc588SBarry Smith         } while (fm < idx);
71289bc588SBarry Smith         if (fm != idx) {
72289bc588SBarry Smith           fill[m]   = idx;
73289bc588SBarry Smith           fill[idx] = fm;
74289bc588SBarry Smith           fm        = idx;
75289bc588SBarry Smith           nnz++;
76289bc588SBarry Smith         }
77289bc588SBarry Smith       }
78289bc588SBarry Smith       row = fill[row];
79289bc588SBarry Smith     }
80289bc588SBarry Smith     /* copy new filled row into permanent storage */
81289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
82496e697eSBarry Smith     if (ainew[i+1] > jmax) {
83289bc588SBarry Smith       /* allocate a longer ajnew */
842fb3ed76SBarry Smith       int maxadd;
85dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n);
86bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
872fb3ed76SBarry Smith       jmax += maxadd;
880452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
89416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
900452661fSBarry Smith       PetscFree(ajnew);
91289bc588SBarry Smith       ajnew = ajtmp;
922fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
93289bc588SBarry Smith     }
94dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
95289bc588SBarry Smith     fm    = fill[n];
96289bc588SBarry Smith     nzi   = 0;
972fb3ed76SBarry Smith     im[i] = nnz;
98289bc588SBarry Smith     while (nnz--) {
99289bc588SBarry Smith       if (fm < i) nzi++;
100dbb450caSBarry Smith       *ajtmp++ = fm - shift;
101289bc588SBarry Smith       fm       = fill[fm];
102289bc588SBarry Smith     }
103289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
104289bc588SBarry Smith   }
105289bc588SBarry Smith 
10694a424c1SBarry Smith   PLogInfo(A,
107ec8511deSBarry Smith     "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
108bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
1092fb3ed76SBarry Smith 
110898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
111898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1121987afe7SBarry Smith 
1130452661fSBarry Smith   PetscFree(fill);
114289bc588SBarry Smith 
115289bc588SBarry Smith   /* put together the new matrix */
116b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1171987afe7SBarry Smith   PLogObjectParent(*B,isicol);
1181987afe7SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
119416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1200452661fSBarry Smith   PetscFree(b->imax);
121416022c9SBarry Smith   b->singlemalloc = 0;
122dbb450caSBarry Smith   len             = (ainew[n] + shift)*sizeof(Scalar);
123e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1240452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
1250452661fSBarry Smith   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
126416022c9SBarry Smith   b->j          = ajnew;
127416022c9SBarry Smith   b->i          = ainew;
128416022c9SBarry Smith   b->diag       = idnew;
129416022c9SBarry Smith   b->ilen       = 0;
130416022c9SBarry Smith   b->imax       = 0;
131416022c9SBarry Smith   b->row        = isrow;
132416022c9SBarry Smith   b->col        = iscol;
1330452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar));
134416022c9SBarry Smith   CHKPTRQ(b->solve_work);
135416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1367fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
137416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
138416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1397fd98bd6SLois Curfman McInnes 
140ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
141ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
142ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
143ae068f58SLois Curfman McInnes 
144289bc588SBarry Smith   return 0;
145289bc588SBarry Smith }
1460a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
14741c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
14841c01911SSatish Balay 
149416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
150289bc588SBarry Smith {
151416022c9SBarry Smith   Mat        C = *B;
152416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
153416022c9SBarry Smith   IS         iscol = b->col, isrow = b->row, isicol;
154416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1551987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
15635aab85fSBarry Smith   int        *diag_offset = b->diag,diag,k;
15735aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1588ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
15935aab85fSBarry Smith   double     ssum;
1601987afe7SBarry Smith   /* These declarations are for optimizations.  They reduce the number of
1611987afe7SBarry Smith      memory references that are made by locally storing information; the
1621987afe7SBarry Smith      word "register" used here with pointers can be viewed as "private" or
1631987afe7SBarry Smith      "known only to me"
1641987afe7SBarry Smith    */
16535aab85fSBarry Smith   register Scalar *pv, *rtmps,*u_values;
1661987afe7SBarry Smith   register int    *pj;
167289bc588SBarry Smith 
16878b31e54SBarry Smith   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
169416022c9SBarry Smith   PLogObjectParent(*B,isicol);
17078b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
17178b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1720452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
17313ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1740cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
175289bc588SBarry Smith 
17635aab85fSBarry Smith   /* precalcuate row sums */
17735aab85fSBarry Smith   if (preserve_row_sums) {
17835aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
17935aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
18035aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
18135aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
18235aab85fSBarry Smith       sum = 0.0;
18335aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
18435aab85fSBarry Smith       rowsums[i] = sum;
18535aab85fSBarry Smith     }
18635aab85fSBarry Smith   }
18735aab85fSBarry Smith 
188289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
189289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
190dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
19148e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
192289bc588SBarry Smith 
193289bc588SBarry Smith     /* load in initial (unfactored row) */
194416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
195416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
196416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
1970cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
198289bc588SBarry Smith 
199dbb450caSBarry Smith     row = *ajtmp++ + shift;
200289bc588SBarry Smith     while (row < i) {
2018c37ef55SBarry Smith       pc = rtmp + row;
2028c37ef55SBarry Smith       if (*pc != 0.0) {
2031987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2041987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
20535aab85fSBarry Smith         multiplier = *pc / *pv++;
2068c37ef55SBarry Smith         *pc        = multiplier;
2071987afe7SBarry Smith         nz         = ai[row+1] - diag_offset[row] - 1;
20835aab85fSBarry Smith         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
209eef2e97bSBarry Smith         PLogFlops(2*nz);
210289bc588SBarry Smith       }
211dbb450caSBarry Smith       row = *ajtmp++ + shift;
212289bc588SBarry Smith     }
213416022c9SBarry Smith     /* finished row so stick it into b->a */
214416022c9SBarry Smith     pv = b->a + ai[i] + shift;
215416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2168c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
217416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
21835aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
21935aab85fSBarry Smith     /*
22035aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
22135aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
22235aab85fSBarry Smith     */
22335aab85fSBarry Smith     if (preserve_row_sums) {
22435aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
22535aab85fSBarry Smith       sum = rowsums[i];
22635aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
22735aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
22835aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
22935aab85fSBarry Smith         inner_sum = 0.0;
23035aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
23135aab85fSBarry Smith           inner_sum += u_values[k];
23235aab85fSBarry Smith         }
23335aab85fSBarry Smith         sum -= pv[j]*inner_sum;
23435aab85fSBarry Smith 
23535aab85fSBarry Smith       }
23635aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
23735aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
23835aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
23935aab85fSBarry Smith         sum -= u_values[k];
24035aab85fSBarry Smith       }
24135aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
24235aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
24335aab85fSBarry Smith     }
24435aab85fSBarry Smith     /* check pivot entry for current row */
24535aab85fSBarry Smith     if (pv[diag] == 0.0) {
24635aab85fSBarry Smith       SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");
24735aab85fSBarry Smith     }
2488c37ef55SBarry Smith   }
2490f11f9aeSSatish Balay 
25035aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
25135aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
25235aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
25335aab85fSBarry Smith   }
25435aab85fSBarry Smith 
25535aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2560452661fSBarry Smith   PetscFree(rtmp);
25778b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
25878b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
25978b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
260416022c9SBarry Smith   C->factor = FACTOR_LU;
26141c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
262c456f294SBarry Smith   C->assembled = PETSC_TRUE;
263416022c9SBarry Smith   PLogFlops(b->n);
264289bc588SBarry Smith   return 0;
265289bc588SBarry Smith }
2660a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
267416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
268da3a660dSBarry Smith {
269416022c9SBarry Smith   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
2706abc6512SBarry Smith   int        ierr;
271416022c9SBarry Smith   Mat        C;
272416022c9SBarry Smith 
273d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(row,IS_COOKIE);
274d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(col,IS_COOKIE);
275416022c9SBarry Smith   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
276416022c9SBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
277da3a660dSBarry Smith 
278da3a660dSBarry Smith   /* free all the data structures from mat */
2790452661fSBarry Smith   PetscFree(mat->a);
2800452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
2810452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
2820452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
2830452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
2840452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
285a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
2860452661fSBarry Smith   PetscFree(mat);
287da3a660dSBarry Smith 
288416022c9SBarry Smith   PetscMemcpy(A,C,sizeof(struct _Mat));
2890452661fSBarry Smith   PetscHeaderDestroy(C);
290da3a660dSBarry Smith   return 0;
291da3a660dSBarry Smith }
2920a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
293416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
2948c37ef55SBarry Smith {
295416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
296416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
297416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
298*3b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
299416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3008c37ef55SBarry Smith 
301416022c9SBarry Smith   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix");
3029e25ed09SBarry Smith 
30378b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
30478b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
305416022c9SBarry Smith   tmp  = a->solve_work;
3068c37ef55SBarry Smith 
307*3b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
308*3b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3098c37ef55SBarry Smith 
3108c37ef55SBarry Smith   /* forward solve the lower triangular */
3118c37ef55SBarry Smith   tmp[0] = b[*r++];
3120cf60383SBarry Smith   tmps   = tmp + shift;
3138c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
314dbb450caSBarry Smith     v   = aa + ai[i] + shift;
315dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
316416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3178c37ef55SBarry Smith     sum = b[*r++];
3180cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3198c37ef55SBarry Smith     tmp[i] = sum;
3208c37ef55SBarry Smith   }
3218c37ef55SBarry Smith 
3228c37ef55SBarry Smith   /* backward solve the upper triangular */
3238c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
324416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
325416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
326416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3278c37ef55SBarry Smith     sum = tmp[i];
3280cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
329416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3308c37ef55SBarry Smith   }
3318c37ef55SBarry Smith 
332*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
333*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
334416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3358c37ef55SBarry Smith   return 0;
3368c37ef55SBarry Smith }
337416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
338da3a660dSBarry Smith {
339416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
340416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
341416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
342*3b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
343416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
344da3a660dSBarry Smith 
345416022c9SBarry Smith   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix");
34678b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
347da3a660dSBarry Smith 
34878b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
34978b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
350416022c9SBarry Smith   tmp  = a->solve_work;
351da3a660dSBarry Smith 
352*3b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
353*3b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
354da3a660dSBarry Smith 
355da3a660dSBarry Smith   /* forward solve the lower triangular */
356da3a660dSBarry Smith   tmp[0] = b[*r++];
357da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
358dbb450caSBarry Smith     v   = aa + ai[i] + shift;
359dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
360416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
361da3a660dSBarry Smith     sum = b[*r++];
362dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
363da3a660dSBarry Smith     tmp[i] = sum;
364da3a660dSBarry Smith   }
365da3a660dSBarry Smith 
366da3a660dSBarry Smith   /* backward solve the upper triangular */
367da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
368416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
369416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
370416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
371da3a660dSBarry Smith     sum = tmp[i];
372dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
373416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
374da3a660dSBarry Smith     x[*c--] += tmp[i];
375da3a660dSBarry Smith   }
376da3a660dSBarry Smith 
377*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
378*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
379416022c9SBarry Smith   PLogFlops(2*a->nz);
380898183ecSLois Curfman McInnes 
381da3a660dSBarry Smith   return 0;
382da3a660dSBarry Smith }
383da3a660dSBarry Smith /* -------------------------------------------------------------------*/
384416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
385da3a660dSBarry Smith {
386416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
387416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
388416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
389*3b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
390416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
391da3a660dSBarry Smith 
392416022c9SBarry Smith   if (A->factor != FACTOR_LU)  SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix");
39378b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
39478b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
395416022c9SBarry Smith   tmp  = a->solve_work;
396da3a660dSBarry Smith 
397da3a660dSBarry Smith   /* invert the permutations */
39878b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
39978b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
400da3a660dSBarry Smith 
401*3b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
402*3b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
403da3a660dSBarry Smith 
404da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
405da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
406da3a660dSBarry Smith 
407da3a660dSBarry Smith   /* forward solve the U^T */
408da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
409416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
410416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
411416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
412da3a660dSBarry Smith     tmp[i] *= *v++;
413da3a660dSBarry Smith     while (nz--) {
414dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
415da3a660dSBarry Smith     }
416da3a660dSBarry Smith   }
417da3a660dSBarry Smith 
418da3a660dSBarry Smith   /* backward solve the L^T */
419da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
420416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
421416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
422416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
423da3a660dSBarry Smith     while (nz--) {
424dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
425da3a660dSBarry Smith     }
426da3a660dSBarry Smith   }
427da3a660dSBarry Smith 
428da3a660dSBarry Smith   /* copy tmp into x according to permutation */
429da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
430da3a660dSBarry Smith 
431*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
432*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
433898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
434898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
435da3a660dSBarry Smith 
436416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
437da3a660dSBarry Smith   return 0;
438da3a660dSBarry Smith }
439da3a660dSBarry Smith 
440416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
441da3a660dSBarry Smith {
442416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
443416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
444416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
445*3b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
446416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
4476abc6512SBarry Smith 
448416022c9SBarry Smith   if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix");
4496abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
4506abc6512SBarry Smith 
45178b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
45278b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
453416022c9SBarry Smith   tmp = a->solve_work;
4546abc6512SBarry Smith 
4556abc6512SBarry Smith   /* invert the permutations */
45678b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
45778b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
458*3b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
459*3b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
4606abc6512SBarry Smith 
4616abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
4626abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
4636abc6512SBarry Smith 
4646abc6512SBarry Smith   /* forward solve the U^T */
4656abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
466416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
467416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
468416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
4696abc6512SBarry Smith     tmp[i] *= *v++;
4706abc6512SBarry Smith     while (nz--) {
471dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
4726abc6512SBarry Smith     }
4736abc6512SBarry Smith   }
4746abc6512SBarry Smith 
4756abc6512SBarry Smith   /* backward solve the L^T */
4766abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
477416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
478416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
479416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
4806abc6512SBarry Smith     while (nz--) {
481dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
4826abc6512SBarry Smith     }
4836abc6512SBarry Smith   }
4846abc6512SBarry Smith 
4856abc6512SBarry Smith   /* copy tmp into x according to permutation */
4866abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4876abc6512SBarry Smith 
488*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
489*3b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
490898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
491898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
4926abc6512SBarry Smith 
493416022c9SBarry Smith   PLogFlops(2*a->nz);
4946abc6512SBarry Smith   return 0;
495da3a660dSBarry Smith }
4969e25ed09SBarry Smith /* ----------------------------------------------------------------*/
49708480c60SBarry Smith 
49808480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
4999e25ed09SBarry Smith {
500416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
5019e25ed09SBarry Smith   IS         isicol;
502416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
50356beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
50456beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
505416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
50677c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
5079e25ed09SBarry Smith 
50808480c60SBarry Smith   /* special case that simply copies fill pattern */
50977c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
51077c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
5113d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
51208480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
51308480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
51408480c60SBarry Smith     if (!b->diag) {
51508480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
51608480c60SBarry Smith     }
51708480c60SBarry Smith     b->row          = isrow;
51808480c60SBarry Smith     b->col          = iscol;
5190452661fSBarry Smith     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
52008480c60SBarry Smith     return 0;
52108480c60SBarry Smith   }
52208480c60SBarry Smith 
52378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
524898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
525898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
5269e25ed09SBarry Smith 
5279e25ed09SBarry Smith   /* get new row pointers */
5280452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
529dbb450caSBarry Smith   ainew[0] = -shift;
5309e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
531dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
5320452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
5339e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
5340452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
5359e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
5360452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
53756beaf04SBarry Smith   /* im is level for each filled value */
5380452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
53956beaf04SBarry Smith   /* dloc is location of diagonal in factor */
5400452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
54156beaf04SBarry Smith   dloc[0]  = 0;
54256beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
5439e25ed09SBarry Smith     /* first copy previous fill into linked list */
54456beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
545dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
5469e25ed09SBarry Smith     fill[n] = n;
5479e25ed09SBarry Smith     while (nz--) {
5489e25ed09SBarry Smith       fm  = n;
549dbb450caSBarry Smith       idx = ic[*xi++ + shift];
5509e25ed09SBarry Smith       do {
5519e25ed09SBarry Smith         m  = fm;
5529e25ed09SBarry Smith         fm = fill[m];
5539e25ed09SBarry Smith       } while (fm < idx);
5549e25ed09SBarry Smith       fill[m]   = idx;
5559e25ed09SBarry Smith       fill[idx] = fm;
55656beaf04SBarry Smith       im[idx]   = 0;
5579e25ed09SBarry Smith     }
55856beaf04SBarry Smith     nzi = 0;
5599e25ed09SBarry Smith     row = fill[n];
56056beaf04SBarry Smith     while ( row < prow ) {
56156beaf04SBarry Smith       incrlev = im[row] + 1;
56256beaf04SBarry Smith       nz      = dloc[row];
563dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
564dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
565416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
566dbb450caSBarry Smith       if (*xi++ + shift != row) {
567ec8511deSBarry Smith         SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot");
56856beaf04SBarry Smith       }
56956beaf04SBarry Smith       fm      = row;
57056beaf04SBarry Smith       while (nnz-- > 0) {
571dbb450caSBarry Smith         idx = *xi++ + shift;
57256beaf04SBarry Smith         if (*flev + incrlev > levels) {
57356beaf04SBarry Smith           flev++;
57456beaf04SBarry Smith           continue;
57556beaf04SBarry Smith         }
57656beaf04SBarry Smith         do {
5779e25ed09SBarry Smith           m  = fm;
5789e25ed09SBarry Smith           fm = fill[m];
57956beaf04SBarry Smith         } while (fm < idx);
5809e25ed09SBarry Smith         if (fm != idx) {
58156beaf04SBarry Smith           im[idx]   = *flev + incrlev;
5829e25ed09SBarry Smith           fill[m]   = idx;
5839e25ed09SBarry Smith           fill[idx] = fm;
5849e25ed09SBarry Smith           fm        = idx;
58556beaf04SBarry Smith           nzf++;
5869e25ed09SBarry Smith         }
58756beaf04SBarry Smith         else {
58856beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
5899e25ed09SBarry Smith         }
59056beaf04SBarry Smith         flev++;
5919e25ed09SBarry Smith       }
5929e25ed09SBarry Smith       row = fill[row];
59356beaf04SBarry Smith       nzi++;
5949e25ed09SBarry Smith     }
5959e25ed09SBarry Smith     /* copy new filled row into permanent storage */
59656beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
597d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
5989e25ed09SBarry Smith       /* allocate a longer ajnew */
599bbb6d6a8SBarry Smith       int maxadd;
600dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
60156beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
602bbb6d6a8SBarry Smith       jmax += maxadd;
6030452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
604416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
6050452661fSBarry Smith       PetscFree(ajnew);
60656beaf04SBarry Smith       ajnew = xi;
6079e25ed09SBarry Smith       /* allocate a longer ajfill */
6080452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
609416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
6100452661fSBarry Smith       PetscFree(ajfill);
61156beaf04SBarry Smith       ajfill = xi;
612bbb6d6a8SBarry Smith       realloc++;
6139e25ed09SBarry Smith     }
614dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
615dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
61656beaf04SBarry Smith     dloc[prow]  = nzi;
6179e25ed09SBarry Smith     fm          = fill[n];
61856beaf04SBarry Smith     while (nzf--) {
619dbb450caSBarry Smith       *xi++   = fm - shift;
62056beaf04SBarry Smith       *flev++ = im[fm];
6219e25ed09SBarry Smith       fm      = fill[fm];
6229e25ed09SBarry Smith     }
6239e25ed09SBarry Smith   }
6240452661fSBarry Smith   PetscFree(ajfill);
625898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
626898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
627898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
6280452661fSBarry Smith   PetscFree(fill); PetscFree(im);
6299e25ed09SBarry Smith 
63094a424c1SBarry Smith   PLogInfo(A,
631ec8511deSBarry Smith     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
63256beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
633bbb6d6a8SBarry Smith 
6349e25ed09SBarry Smith   /* put together the new matrix */
635b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
636416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
6370452661fSBarry Smith   PetscFree(b->imax);
638416022c9SBarry Smith   b->singlemalloc = 0;
639dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
6409e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
6410452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
6420452661fSBarry Smith   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
643416022c9SBarry Smith   b->j          = ajnew;
644416022c9SBarry Smith   b->i          = ainew;
64556beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
646416022c9SBarry Smith   b->diag       = dloc;
647416022c9SBarry Smith   b->ilen       = 0;
648416022c9SBarry Smith   b->imax       = 0;
649416022c9SBarry Smith   b->row        = isrow;
650416022c9SBarry Smith   b->col        = iscol;
6510452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
652416022c9SBarry Smith   CHKPTRQ(b->solve_work);
653416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
65456beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
655dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
656416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
6579e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
658ae068f58SLois Curfman McInnes 
659ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
660ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
661ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
662ae068f58SLois Curfman McInnes 
6639e25ed09SBarry Smith   return 0;
6649e25ed09SBarry Smith }
665d5d45c9bSBarry Smith 
666d5d45c9bSBarry Smith 
667d5d45c9bSBarry Smith 
668d5d45c9bSBarry Smith 
669