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