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