xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 608f96eb4f80b150d49ed2fa0fafd4acdd2a6f3a)
1 #ifndef lint
2 static char vcid[] = "$Id: aijfact.c,v 1.60 1996/03/23 20:42:31 bsmith Exp bsmith $";
3 #endif
4 
5 #include "aij.h"
6 /*
7     Factorization code for AIJ format.
8 */
9 
10 int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
11 {
12   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
13   IS         isicol;
14   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
15   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
16   int        *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im;
17 
18   if (n != a->n) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must be square");
19   if (!isrow) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have row permutation");
20   if (!iscol) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have col. permutation");
21 
22   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
23   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
24 
25   /* get new row pointers */
26   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
27   ainew[0] = -shift;
28   /* don't know how many column pointers are needed so estimate */
29   jmax = (int) (f*ai[n]+(!shift));
30   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
31   /* fill is a linked list of nonzeros in active row */
32   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
33   im = fill + n + 1;
34   /* idnew is location of diagonal in factor */
35   idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
36   idnew[0] = -shift;
37 
38   for ( i=0; i<n; i++ ) {
39     /* first copy previous fill into linked list */
40     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
41     ajtmp   = aj + ai[r[i]] + shift;
42     fill[n] = n;
43     while (nz--) {
44       fm  = n;
45       idx = ic[*ajtmp++ + shift];
46       do {
47         m  = fm;
48         fm = fill[m];
49       } while (fm < idx);
50       fill[m]   = idx;
51       fill[idx] = fm;
52     }
53     row = fill[n];
54     while ( row < i ) {
55       ajtmp = ajnew + idnew[row] + (!shift);
56       nzbd  = 1 + idnew[row] - ainew[row];
57       nz    = im[row] - nzbd;
58       fm    = row;
59       while (nz-- > 0) {
60         idx = *ajtmp++ + shift;
61         nzbd++;
62         if (idx == i) im[row] = nzbd;
63         do {
64           m  = fm;
65           fm = fill[m];
66         } while (fm < idx);
67         if (fm != idx) {
68           fill[m]   = idx;
69           fill[idx] = fm;
70           fm        = idx;
71           nnz++;
72         }
73       }
74       row = fill[row];
75     }
76     /* copy new filled row into permanent storage */
77     ainew[i+1] = ainew[i] + nnz;
78     if (ainew[i+1] > jmax) {
79       /* allocate a longer ajnew */
80       int maxadd;
81       maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n);
82       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
83       jmax += maxadd;
84       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
85       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
86       PetscFree(ajnew);
87       ajnew = ajtmp;
88       realloc++; /* count how many times we realloc */
89     }
90     ajtmp = ajnew + ainew[i] + shift;
91     fm    = fill[n];
92     nzi   = 0;
93     im[i] = nnz;
94     while (nnz--) {
95       if (fm < i) nzi++;
96       *ajtmp++ = fm - shift;
97       fm       = fill[fm];
98     }
99     idnew[i] = ainew[i] + nzi;
100   }
101 
102   PLogInfo(A,
103     "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
104                              realloc,f,((double)ainew[n])/((double)ai[i]));
105 
106   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
107   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
108 
109   PetscFree(fill);
110 
111   /* put together the new matrix */
112   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
113   PLogObjectParent(*B,isicol);
114   ierr = ISDestroy(isicol); CHKERRQ(ierr);
115   b = (Mat_SeqAIJ *) (*B)->data;
116   PetscFree(b->imax);
117   b->singlemalloc = 0;
118   len             = (ainew[n] + shift)*sizeof(Scalar);
119   /* the next line frees the default space generated by the Create() */
120   PetscFree(b->a); PetscFree(b->ilen);
121   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
122   b->j          = ajnew;
123   b->i          = ainew;
124   b->diag       = idnew;
125   b->ilen       = 0;
126   b->imax       = 0;
127   b->row        = isrow;
128   b->col        = iscol;
129   b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar));
130   CHKPTRQ(b->solve_work);
131   /* In b structure:  Free imax, ilen, old a, old j.
132      Allocate idnew, solve_work, new a, new j */
133   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
134   b->maxnz = b->nz = ainew[n] + shift;
135 
136   return 0;
137 }
138 /* ----------------------------------------------------------- */
139 int Mat_AIJ_CheckInode(Mat);
140 
141 int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
142 {
143   Mat        C = *B;
144   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
145   IS         iscol = b->col, isrow = b->row, isicol;
146   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
147   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
148   int        *diag_offset = b->diag,diag,k;
149   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
150   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
151   double     ssum;
152   /* These declarations are for optimizations.  They reduce the number of
153      memory references that are made by locally storing information; the
154      word "register" used here with pointers can be viewed as "private" or
155      "known only to me"
156    */
157   register Scalar *pv, *rtmps,*u_values;
158   register int    *pj;
159 
160   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
161   PLogObjectParent(*B,isicol);
162   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
163   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
164   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
165   rtmps = rtmp + shift; ics = ic + shift;
166 
167   /* precalcuate row sums */
168   if (preserve_row_sums) {
169     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
170     for ( i=0; i<n; i++ ) {
171       nz  = a->i[r[i]+1] - a->i[r[i]];
172       v   = a->a + a->i[r[i]] + shift;
173       sum = 0.0;
174       for ( j=0; j<nz; j++ ) sum += v[j];
175       rowsums[i] = sum;
176     }
177   }
178 
179   for ( i=0; i<n; i++ ) {
180     nz    = ai[i+1] - ai[i];
181     ajtmp = aj + ai[i] + shift;
182     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
183 
184     /* load in initial (unfactored row) */
185     nz       = a->i[r[i]+1] - a->i[r[i]];
186     ajtmpold = a->j + a->i[r[i]] + shift;
187     v        = a->a + a->i[r[i]] + shift;
188     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
189 
190     row = *ajtmp++ + shift;
191     while (row < i) {
192       pc = rtmp + row;
193       if (*pc != 0.0) {
194         pv         = b->a + diag_offset[row] + shift;
195         pj         = b->j + diag_offset[row] + (!shift);
196         multiplier = *pc / *pv++;
197         *pc        = multiplier;
198         nz         = ai[row+1] - diag_offset[row] - 1;
199         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
200         PLogFlops(2*nz);
201       }
202       row = *ajtmp++ + shift;
203     }
204     /* finished row so stick it into b->a */
205     pv = b->a + ai[i] + shift;
206     pj = b->j + ai[i] + shift;
207     nz = ai[i+1] - ai[i];
208     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
209     diag = diag_offset[i] - ai[i];
210     /*
211           Possibly adjust diagonal entry on current row to force
212         LU matrix to have same row sum as initial matrix.
213     */
214     if (preserve_row_sums) {
215       pj  = b->j + ai[i] + shift;
216       sum = rowsums[i];
217       for ( j=0; j<diag; j++ ) {
218         u_values  = b->a + diag_offset[pj[j]] + shift;
219         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
220         inner_sum = 0.0;
221         for ( k=0; k<nz; k++ ) {
222           inner_sum += u_values[k];
223         }
224         sum -= pv[j]*inner_sum;
225 
226       }
227       nz       = ai[i+1] - diag_offset[i] - 1;
228       u_values = b->a + diag_offset[i] + 1 + shift;
229       for ( k=0; k<nz; k++ ) {
230         sum -= u_values[k];
231       }
232       ssum = PetscAbsScalar(sum/pv[diag]);
233       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
234     }
235     /* check pivot entry for current row */
236     if (pv[diag] == 0.0) {
237       SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");
238     }
239   }
240 
241   /* invert diagonal entries for simplier triangular solves */
242   for ( i=0; i<n; i++ ) {
243     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
244   }
245 
246   if (preserve_row_sums) PetscFree(rowsums);
247   PetscFree(rtmp);
248   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
249   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
250   ierr = ISDestroy(isicol); CHKERRQ(ierr);
251   C->factor = FACTOR_LU;
252   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
253   C->assembled = PETSC_TRUE;
254   PLogFlops(b->n);
255   return 0;
256 }
257 /* ----------------------------------------------------------- */
258 int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
259 {
260   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
261   int        ierr;
262   Mat        C;
263 
264   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
265   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
266 
267   /* free all the data structures from mat */
268   PetscFree(mat->a);
269   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
270   if (mat->diag) PetscFree(mat->diag);
271   if (mat->ilen) PetscFree(mat->ilen);
272   if (mat->imax) PetscFree(mat->imax);
273   if (mat->solve_work) PetscFree(mat->solve_work);
274   if (mat->inode.size) PetscFree(mat->inode.size);
275   PetscFree(mat);
276 
277   PetscMemcpy(A,C,sizeof(struct _Mat));
278   PetscHeaderDestroy(C);
279   return 0;
280 }
281 /* ----------------------------------------------------------- */
282 int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
283 {
284   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
285   IS         iscol = a->col, isrow = a->row;
286   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
287   int        nz,shift = a->indexshift;
288   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
289 
290   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix");
291 
292   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
293   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
294   tmp  = a->solve_work;
295 
296   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
297   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
298 
299   /* forward solve the lower triangular */
300   tmp[0] = b[*r++];
301   tmps   = tmp + shift;
302   for ( i=1; i<n; i++ ) {
303     v   = aa + ai[i] + shift;
304     vi  = aj + ai[i] + shift;
305     nz  = a->diag[i] - ai[i];
306     sum = b[*r++];
307     while (nz--) sum -= *v++ * tmps[*vi++];
308     tmp[i] = sum;
309   }
310 
311   /* backward solve the upper triangular */
312   for ( i=n-1; i>=0; i-- ){
313     v   = aa + a->diag[i] + (!shift);
314     vi  = aj + a->diag[i] + (!shift);
315     nz  = ai[i+1] - a->diag[i] - 1;
316     sum = tmp[i];
317     while (nz--) sum -= *v++ * tmps[*vi++];
318     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
319   }
320 
321   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
322   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
323   PLogFlops(2*a->nz - a->n);
324   return 0;
325 }
326 int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
327 {
328   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
329   IS         iscol = a->col, isrow = a->row;
330   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
331   int        nz, shift = a->indexshift;
332   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
333 
334   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix");
335   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
336 
337   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
338   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
339   tmp  = a->solve_work;
340 
341   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
342   ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1);
343 
344   /* forward solve the lower triangular */
345   tmp[0] = b[*r++];
346   for ( i=1; i<n; i++ ) {
347     v   = aa + ai[i] + shift;
348     vi  = aj + ai[i] + shift;
349     nz  = a->diag[i] - ai[i];
350     sum = b[*r++];
351     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
352     tmp[i] = sum;
353   }
354 
355   /* backward solve the upper triangular */
356   for ( i=n-1; i>=0; i-- ){
357     v   = aa + a->diag[i] + (!shift);
358     vi  = aj + a->diag[i] + (!shift);
359     nz  = ai[i+1] - a->diag[i] - 1;
360     sum = tmp[i];
361     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
362     tmp[i] = sum*aa[a->diag[i]+shift];
363     x[*c--] += tmp[i];
364   }
365 
366   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
367   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
368   PLogFlops(2*a->nz);
369 
370   return 0;
371 }
372 /* -------------------------------------------------------------------*/
373 int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
374 {
375   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
376   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
377   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
378   int        nz,shift = a->indexshift;
379   Scalar     *x,*b,*tmp, *aa = a->a, *v;
380 
381   if (A->factor != FACTOR_LU)  SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix");
382   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
383   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
384   tmp  = a->solve_work;
385 
386   /* invert the permutations */
387   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
388   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
389 
390   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
391   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
392 
393   /* copy the b into temp work space according to permutation */
394   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
395 
396   /* forward solve the U^T */
397   for ( i=0; i<n; i++ ) {
398     v   = aa + a->diag[i] + shift;
399     vi  = aj + a->diag[i] + (!shift);
400     nz  = ai[i+1] - a->diag[i] - 1;
401     tmp[i] *= *v++;
402     while (nz--) {
403       tmp[*vi++ + shift] -= (*v++)*tmp[i];
404     }
405   }
406 
407   /* backward solve the L^T */
408   for ( i=n-1; i>=0; i-- ){
409     v   = aa + a->diag[i] - 1 + shift;
410     vi  = aj + a->diag[i] - 1 + shift;
411     nz  = a->diag[i] - ai[i];
412     while (nz--) {
413       tmp[*vi-- + shift] -= (*v--)*tmp[i];
414     }
415   }
416 
417   /* copy tmp into x according to permutation */
418   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
419 
420   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
421   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
422   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
423   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
424 
425   PLogFlops(2*a->nz-a->n);
426   return 0;
427 }
428 
429 int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
430 {
431   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
432   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
433   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
434   int        nz,shift = a->indexshift;
435   Scalar     *x,*b,*tmp, *aa = a->a, *v;
436 
437   if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix");
438   if (zz != xx) VecCopy(zz,xx);
439 
440   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
441   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
442   tmp = a->solve_work;
443 
444   /* invert the permutations */
445   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
446   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
447   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
448   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
449 
450   /* copy the b into temp work space according to permutation */
451   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
452 
453   /* forward solve the U^T */
454   for ( i=0; i<n; i++ ) {
455     v   = aa + a->diag[i] + shift;
456     vi  = aj + a->diag[i] + (!shift);
457     nz  = ai[i+1] - a->diag[i] - 1;
458     tmp[i] *= *v++;
459     while (nz--) {
460       tmp[*vi++ + shift] -= (*v++)*tmp[i];
461     }
462   }
463 
464   /* backward solve the L^T */
465   for ( i=n-1; i>=0; i-- ){
466     v   = aa + a->diag[i] - 1 + shift;
467     vi  = aj + a->diag[i] - 1 + shift;
468     nz  = a->diag[i] - ai[i];
469     while (nz--) {
470       tmp[*vi-- + shift] -= (*v--)*tmp[i];
471     }
472   }
473 
474   /* copy tmp into x according to permutation */
475   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
476 
477   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
478   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
479   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
480   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
481 
482   PLogFlops(2*a->nz);
483   return 0;
484 }
485 /* ----------------------------------------------------------------*/
486 
487 int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
488 {
489   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
490   IS         isicol;
491   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
492   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
493   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
494   int        incrlev,nnz,i,shift = a->indexshift;
495   PetscTruth col_identity, row_identity;
496 
497   if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square");
498   if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation");
499   if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation");
500 
501   /* special case that simply copies fill pattern */
502   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
503   if (levels == 0 && row_identity && col_identity) {
504     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
505     (*fact)->factor = FACTOR_LU;
506     b               = (Mat_SeqAIJ *) (*fact)->data;
507     if (!b->diag) {
508       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
509     }
510     b->row          = isrow;
511     b->col          = iscol;
512     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
513     return 0;
514   }
515 
516   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
517   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
518   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
519 
520   /* get new row pointers */
521   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
522   ainew[0] = -shift;
523   /* don't know how many column pointers are needed so estimate */
524   jmax = (int) (f*(ai[n]+!shift));
525   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
526   /* ajfill is level of fill for each fill entry */
527   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
528   /* fill is a linked list of nonzeros in active row */
529   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
530   /* im is level for each filled value */
531   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
532   /* dloc is location of diagonal in factor */
533   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
534   dloc[0]  = 0;
535   for ( prow=0; prow<n; prow++ ) {
536     /* first copy previous fill into linked list */
537     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
538     xi      = aj + ai[r[prow]] + shift;
539     fill[n] = n;
540     while (nz--) {
541       fm  = n;
542       idx = ic[*xi++ + shift];
543       do {
544         m  = fm;
545         fm = fill[m];
546       } while (fm < idx);
547       fill[m]   = idx;
548       fill[idx] = fm;
549       im[idx]   = 0;
550     }
551     nzi = 0;
552     row = fill[n];
553     while ( row < prow ) {
554       incrlev = im[row] + 1;
555       nz      = dloc[row];
556       xi      = ajnew  + ainew[row] + shift + nz;
557       flev    = ajfill + ainew[row] + shift + nz + 1;
558       nnz     = ainew[row+1] - ainew[row] - nz - 1;
559       if (*xi++ + shift != row) {
560         SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot");
561       }
562       fm      = row;
563       while (nnz-- > 0) {
564         idx = *xi++ + shift;
565         if (*flev + incrlev > levels) {
566           flev++;
567           continue;
568         }
569         do {
570           m  = fm;
571           fm = fill[m];
572         } while (fm < idx);
573         if (fm != idx) {
574           im[idx]   = *flev + incrlev;
575           fill[m]   = idx;
576           fill[idx] = fm;
577           fm        = idx;
578           nzf++;
579         }
580         else {
581           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
582         }
583         flev++;
584       }
585       row = fill[row];
586       nzi++;
587     }
588     /* copy new filled row into permanent storage */
589     ainew[prow+1] = ainew[prow] + nzf;
590     if (ainew[prow+1] > jmax-shift) {
591       /* allocate a longer ajnew */
592       int maxadd;
593       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
594       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
595       jmax += maxadd;
596       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
597       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
598       PetscFree(ajnew);
599       ajnew = xi;
600       /* allocate a longer ajfill */
601       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
602       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
603       PetscFree(ajfill);
604       ajfill = xi;
605       realloc++;
606     }
607     xi          = ajnew + ainew[prow] + shift;
608     flev        = ajfill + ainew[prow] + shift;
609     dloc[prow]  = nzi;
610     fm          = fill[n];
611     while (nzf--) {
612       *xi++   = fm - shift;
613       *flev++ = im[fm];
614       fm      = fill[fm];
615     }
616   }
617   PetscFree(ajfill);
618   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
619   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
620   ierr = ISDestroy(isicol); CHKERRQ(ierr);
621   PetscFree(fill); PetscFree(im);
622 
623   PLogInfo(A,
624     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
625                              realloc,f,((double)ainew[n])/((double)ai[prow]));
626 
627   /* put together the new matrix */
628   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
629   b = (Mat_SeqAIJ *) (*fact)->data;
630   PetscFree(b->imax);
631   b->singlemalloc = 0;
632   len = (ainew[n] + shift)*sizeof(Scalar);
633   /* the next line frees the default space generated by the Create() */
634   PetscFree(b->a); PetscFree(b->ilen);
635   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
636   b->j          = ajnew;
637   b->i          = ainew;
638   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
639   b->diag       = dloc;
640   b->ilen       = 0;
641   b->imax       = 0;
642   b->row        = isrow;
643   b->col        = iscol;
644   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
645   CHKPTRQ(b->solve_work);
646   /* In b structure:  Free imax, ilen, old a, old j.
647      Allocate dloc, solve_work, new a, new j */
648   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
649   b->maxnz          = b->nz = ainew[n] + shift;
650   (*fact)->factor   = FACTOR_LU;
651   return 0;
652 }
653 
654 
655 
656 
657