xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision ebcf1981e4b18312f7d47235aee1f2bd3ce7569e)
1 #ifndef lint
2 static char vcid[] = "$Id: aijfact.c,v 1.61 1996/05/03 19:26:39 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   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
166   rtmps = rtmp + shift; ics = ic + shift;
167 
168   /* precalcuate row sums */
169   if (preserve_row_sums) {
170     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
171     for ( i=0; i<n; i++ ) {
172       nz  = a->i[r[i]+1] - a->i[r[i]];
173       v   = a->a + a->i[r[i]] + shift;
174       sum = 0.0;
175       for ( j=0; j<nz; j++ ) sum += v[j];
176       rowsums[i] = sum;
177     }
178   }
179 
180   for ( i=0; i<n; i++ ) {
181     nz    = ai[i+1] - ai[i];
182     ajtmp = aj + ai[i] + shift;
183     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
184 
185     /* load in initial (unfactored row) */
186     nz       = a->i[r[i]+1] - a->i[r[i]];
187     ajtmpold = a->j + a->i[r[i]] + shift;
188     v        = a->a + a->i[r[i]] + shift;
189     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
190 
191     row = *ajtmp++ + shift;
192     while (row < i) {
193       pc = rtmp + row;
194       if (*pc != 0.0) {
195         pv         = b->a + diag_offset[row] + shift;
196         pj         = b->j + diag_offset[row] + (!shift);
197         multiplier = *pc / *pv++;
198         *pc        = multiplier;
199         nz         = ai[row+1] - diag_offset[row] - 1;
200         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
201         PLogFlops(2*nz);
202       }
203       row = *ajtmp++ + shift;
204     }
205     /* finished row so stick it into b->a */
206     pv = b->a + ai[i] + shift;
207     pj = b->j + ai[i] + shift;
208     nz = ai[i+1] - ai[i];
209     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
210     diag = diag_offset[i] - ai[i];
211     /*
212           Possibly adjust diagonal entry on current row to force
213         LU matrix to have same row sum as initial matrix.
214     */
215     if (preserve_row_sums) {
216       pj  = b->j + ai[i] + shift;
217       sum = rowsums[i];
218       for ( j=0; j<diag; j++ ) {
219         u_values  = b->a + diag_offset[pj[j]] + shift;
220         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
221         inner_sum = 0.0;
222         for ( k=0; k<nz; k++ ) {
223           inner_sum += u_values[k];
224         }
225         sum -= pv[j]*inner_sum;
226 
227       }
228       nz       = ai[i+1] - diag_offset[i] - 1;
229       u_values = b->a + diag_offset[i] + 1 + shift;
230       for ( k=0; k<nz; k++ ) {
231         sum -= u_values[k];
232       }
233       ssum = PetscAbsScalar(sum/pv[diag]);
234       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
235     }
236     /* check pivot entry for current row */
237     if (pv[diag] == 0.0) {
238       SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");
239     }
240   }
241 
242   /* invert diagonal entries for simplier triangular solves */
243   for ( i=0; i<n; i++ ) {
244     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
245   }
246 
247   if (preserve_row_sums) PetscFree(rowsums);
248   PetscFree(rtmp);
249   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
250   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
251   ierr = ISDestroy(isicol); CHKERRQ(ierr);
252   C->factor = FACTOR_LU;
253   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
254   C->assembled = PETSC_TRUE;
255   PLogFlops(b->n);
256   return 0;
257 }
258 /* ----------------------------------------------------------- */
259 int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
260 {
261   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
262   int        ierr;
263   Mat        C;
264 
265   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
266   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
267 
268   /* free all the data structures from mat */
269   PetscFree(mat->a);
270   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
271   if (mat->diag) PetscFree(mat->diag);
272   if (mat->ilen) PetscFree(mat->ilen);
273   if (mat->imax) PetscFree(mat->imax);
274   if (mat->solve_work) PetscFree(mat->solve_work);
275   if (mat->inode.size) PetscFree(mat->inode.size);
276   PetscFree(mat);
277 
278   PetscMemcpy(A,C,sizeof(struct _Mat));
279   PetscHeaderDestroy(C);
280   return 0;
281 }
282 /* ----------------------------------------------------------- */
283 int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
284 {
285   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
286   IS         iscol = a->col, isrow = a->row;
287   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
288   int        nz,shift = a->indexshift;
289   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
290 
291   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix");
292 
293   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
294   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
295   tmp  = a->solve_work;
296 
297   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
298   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
299 
300   /* forward solve the lower triangular */
301   tmp[0] = b[*r++];
302   tmps   = tmp + shift;
303   for ( i=1; i<n; i++ ) {
304     v   = aa + ai[i] + shift;
305     vi  = aj + ai[i] + shift;
306     nz  = a->diag[i] - ai[i];
307     sum = b[*r++];
308     while (nz--) sum -= *v++ * tmps[*vi++];
309     tmp[i] = sum;
310   }
311 
312   /* backward solve the upper triangular */
313   for ( i=n-1; i>=0; i-- ){
314     v   = aa + a->diag[i] + (!shift);
315     vi  = aj + a->diag[i] + (!shift);
316     nz  = ai[i+1] - a->diag[i] - 1;
317     sum = tmp[i];
318     while (nz--) sum -= *v++ * tmps[*vi++];
319     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
320   }
321 
322   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
323   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
324   PLogFlops(2*a->nz - a->n);
325   return 0;
326 }
327 int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
328 {
329   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
330   IS         iscol = a->col, isrow = a->row;
331   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
332   int        nz, shift = a->indexshift;
333   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
334 
335   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix");
336   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
337 
338   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
339   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
340   tmp  = a->solve_work;
341 
342   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
343   ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1);
344 
345   /* forward solve the lower triangular */
346   tmp[0] = b[*r++];
347   for ( i=1; i<n; i++ ) {
348     v   = aa + ai[i] + shift;
349     vi  = aj + ai[i] + shift;
350     nz  = a->diag[i] - ai[i];
351     sum = b[*r++];
352     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
353     tmp[i] = sum;
354   }
355 
356   /* backward solve the upper triangular */
357   for ( i=n-1; i>=0; i-- ){
358     v   = aa + a->diag[i] + (!shift);
359     vi  = aj + a->diag[i] + (!shift);
360     nz  = ai[i+1] - a->diag[i] - 1;
361     sum = tmp[i];
362     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
363     tmp[i] = sum*aa[a->diag[i]+shift];
364     x[*c--] += tmp[i];
365   }
366 
367   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
368   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
369   PLogFlops(2*a->nz);
370 
371   return 0;
372 }
373 /* -------------------------------------------------------------------*/
374 int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
375 {
376   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
377   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
378   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
379   int        nz,shift = a->indexshift;
380   Scalar     *x,*b,*tmp, *aa = a->a, *v;
381 
382   if (A->factor != FACTOR_LU)  SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix");
383   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
384   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
385   tmp  = a->solve_work;
386 
387   /* invert the permutations */
388   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
389   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
390 
391   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
392   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
393 
394   /* copy the b into temp work space according to permutation */
395   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
396 
397   /* forward solve the U^T */
398   for ( i=0; i<n; i++ ) {
399     v   = aa + a->diag[i] + shift;
400     vi  = aj + a->diag[i] + (!shift);
401     nz  = ai[i+1] - a->diag[i] - 1;
402     tmp[i] *= *v++;
403     while (nz--) {
404       tmp[*vi++ + shift] -= (*v++)*tmp[i];
405     }
406   }
407 
408   /* backward solve the L^T */
409   for ( i=n-1; i>=0; i-- ){
410     v   = aa + a->diag[i] - 1 + shift;
411     vi  = aj + a->diag[i] - 1 + shift;
412     nz  = a->diag[i] - ai[i];
413     while (nz--) {
414       tmp[*vi-- + shift] -= (*v--)*tmp[i];
415     }
416   }
417 
418   /* copy tmp into x according to permutation */
419   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
420 
421   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
422   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
423   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
424   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
425 
426   PLogFlops(2*a->nz-a->n);
427   return 0;
428 }
429 
430 int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
431 {
432   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
433   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
434   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
435   int        nz,shift = a->indexshift;
436   Scalar     *x,*b,*tmp, *aa = a->a, *v;
437 
438   if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix");
439   if (zz != xx) VecCopy(zz,xx);
440 
441   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
442   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
443   tmp = a->solve_work;
444 
445   /* invert the permutations */
446   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
447   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
448   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
449   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
450 
451   /* copy the b into temp work space according to permutation */
452   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
453 
454   /* forward solve the U^T */
455   for ( i=0; i<n; i++ ) {
456     v   = aa + a->diag[i] + shift;
457     vi  = aj + a->diag[i] + (!shift);
458     nz  = ai[i+1] - a->diag[i] - 1;
459     tmp[i] *= *v++;
460     while (nz--) {
461       tmp[*vi++ + shift] -= (*v++)*tmp[i];
462     }
463   }
464 
465   /* backward solve the L^T */
466   for ( i=n-1; i>=0; i-- ){
467     v   = aa + a->diag[i] - 1 + shift;
468     vi  = aj + a->diag[i] - 1 + shift;
469     nz  = a->diag[i] - ai[i];
470     while (nz--) {
471       tmp[*vi-- + shift] -= (*v--)*tmp[i];
472     }
473   }
474 
475   /* copy tmp into x according to permutation */
476   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
477 
478   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
479   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
480   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
481   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
482 
483   PLogFlops(2*a->nz);
484   return 0;
485 }
486 /* ----------------------------------------------------------------*/
487 
488 int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
489 {
490   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
491   IS         isicol;
492   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
493   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
494   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
495   int        incrlev,nnz,i,shift = a->indexshift;
496   PetscTruth col_identity, row_identity;
497 
498   if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square");
499   if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation");
500   if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation");
501 
502   /* special case that simply copies fill pattern */
503   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
504   if (levels == 0 && row_identity && col_identity) {
505     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
506     (*fact)->factor = FACTOR_LU;
507     b               = (Mat_SeqAIJ *) (*fact)->data;
508     if (!b->diag) {
509       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
510     }
511     b->row          = isrow;
512     b->col          = iscol;
513     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
514     return 0;
515   }
516 
517   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
518   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
519   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
520 
521   /* get new row pointers */
522   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
523   ainew[0] = -shift;
524   /* don't know how many column pointers are needed so estimate */
525   jmax = (int) (f*(ai[n]+!shift));
526   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
527   /* ajfill is level of fill for each fill entry */
528   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
529   /* fill is a linked list of nonzeros in active row */
530   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
531   /* im is level for each filled value */
532   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
533   /* dloc is location of diagonal in factor */
534   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
535   dloc[0]  = 0;
536   for ( prow=0; prow<n; prow++ ) {
537     /* first copy previous fill into linked list */
538     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
539     xi      = aj + ai[r[prow]] + shift;
540     fill[n] = n;
541     while (nz--) {
542       fm  = n;
543       idx = ic[*xi++ + shift];
544       do {
545         m  = fm;
546         fm = fill[m];
547       } while (fm < idx);
548       fill[m]   = idx;
549       fill[idx] = fm;
550       im[idx]   = 0;
551     }
552     nzi = 0;
553     row = fill[n];
554     while ( row < prow ) {
555       incrlev = im[row] + 1;
556       nz      = dloc[row];
557       xi      = ajnew  + ainew[row] + shift + nz;
558       flev    = ajfill + ainew[row] + shift + nz + 1;
559       nnz     = ainew[row+1] - ainew[row] - nz - 1;
560       if (*xi++ + shift != row) {
561         SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot");
562       }
563       fm      = row;
564       while (nnz-- > 0) {
565         idx = *xi++ + shift;
566         if (*flev + incrlev > levels) {
567           flev++;
568           continue;
569         }
570         do {
571           m  = fm;
572           fm = fill[m];
573         } while (fm < idx);
574         if (fm != idx) {
575           im[idx]   = *flev + incrlev;
576           fill[m]   = idx;
577           fill[idx] = fm;
578           fm        = idx;
579           nzf++;
580         }
581         else {
582           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
583         }
584         flev++;
585       }
586       row = fill[row];
587       nzi++;
588     }
589     /* copy new filled row into permanent storage */
590     ainew[prow+1] = ainew[prow] + nzf;
591     if (ainew[prow+1] > jmax-shift) {
592       /* allocate a longer ajnew */
593       int maxadd;
594       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
595       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
596       jmax += maxadd;
597       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
598       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
599       PetscFree(ajnew);
600       ajnew = xi;
601       /* allocate a longer ajfill */
602       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
603       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
604       PetscFree(ajfill);
605       ajfill = xi;
606       realloc++;
607     }
608     xi          = ajnew + ainew[prow] + shift;
609     flev        = ajfill + ainew[prow] + shift;
610     dloc[prow]  = nzi;
611     fm          = fill[n];
612     while (nzf--) {
613       *xi++   = fm - shift;
614       *flev++ = im[fm];
615       fm      = fill[fm];
616     }
617   }
618   PetscFree(ajfill);
619   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
620   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
621   ierr = ISDestroy(isicol); CHKERRQ(ierr);
622   PetscFree(fill); PetscFree(im);
623 
624   PLogInfo(A,
625     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
626                              realloc,f,((double)ainew[n])/((double)ai[prow]));
627 
628   /* put together the new matrix */
629   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
630   b = (Mat_SeqAIJ *) (*fact)->data;
631   PetscFree(b->imax);
632   b->singlemalloc = 0;
633   len = (ainew[n] + shift)*sizeof(Scalar);
634   /* the next line frees the default space generated by the Create() */
635   PetscFree(b->a); PetscFree(b->ilen);
636   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
637   b->j          = ajnew;
638   b->i          = ainew;
639   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
640   b->diag       = dloc;
641   b->ilen       = 0;
642   b->imax       = 0;
643   b->row        = isrow;
644   b->col        = iscol;
645   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
646   CHKPTRQ(b->solve_work);
647   /* In b structure:  Free imax, ilen, old a, old j.
648      Allocate dloc, solve_work, new a, new j */
649   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
650   b->maxnz          = b->nz = ainew[n] + shift;
651   (*fact)->factor   = FACTOR_LU;
652   return 0;
653 }
654 
655 
656 
657 
658