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