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