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