xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision ecf371e4f11dc92d760a8a34d4f20e65e993c904)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*ecf371e4SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.96 1998/03/16 18:51:28 balay Exp bsmith $";
3cb512458SBarry Smith #endif
4289bc588SBarry Smith 
570f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
690f02eecSBarry Smith #include "src/vec/vecimpl.h"
71c4feecaSSatish Balay #include "src/inline/dot.h"
83b2fbd54SBarry Smith 
95615d1e5SSatish Balay #undef __FUNC__
10d4bb536fSBarry Smith #define __FUNC__ "MatOrder_Flow_SeqAIJ"
11ca161407SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReorderingType type,IS *irow,IS *icol)
123b2fbd54SBarry Smith {
133a40ed3dSBarry Smith   PetscFunctionBegin;
143a40ed3dSBarry Smith 
15e3372554SBarry Smith   SETERRQ(PETSC_ERR_SUP,0,"Code not written");
16d64ed03dSBarry Smith #if !defined(USE_PETSC_DEBUG)
17d64ed03dSBarry Smith   PetscFunctionReturn(0);
18d64ed03dSBarry Smith #endif
193b2fbd54SBarry Smith }
203b2fbd54SBarry Smith 
21289bc588SBarry Smith /*
22289bc588SBarry Smith     Factorization code for AIJ format.
23289bc588SBarry Smith */
245615d1e5SSatish Balay #undef __FUNC__
255615d1e5SSatish Balay #define __FUNC__ "MatLUFactorSymbolic_SeqAIJ"
26416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
27289bc588SBarry Smith {
28416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
29289bc588SBarry Smith   IS         isicol;
30416022c9SBarry Smith   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
31416022c9SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
3291bf9adeSBarry Smith   int        *idnew, idx, row,m,fm, nnz, nzi, realloc = 0,nzbd,*im;
33289bc588SBarry Smith 
343a40ed3dSBarry Smith   PetscFunctionBegin;
35d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
36d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
373b2fbd54SBarry Smith 
3878b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
39289bc588SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
40289bc588SBarry Smith 
41289bc588SBarry Smith   /* get new row pointers */
420452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
43dbb450caSBarry Smith   ainew[0] = -shift;
44289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
45dbb450caSBarry Smith   jmax = (int) (f*ai[n]+(!shift));
460452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
47289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
480452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
492fb3ed76SBarry Smith   im = fill + n + 1;
50289bc588SBarry Smith   /* idnew is location of diagonal in factor */
510452661fSBarry Smith   idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
52dbb450caSBarry Smith   idnew[0] = -shift;
53289bc588SBarry Smith 
54289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
55289bc588SBarry Smith     /* first copy previous fill into linked list */
56289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
576b96ef82SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
58dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
59289bc588SBarry Smith     fill[n] = n;
60289bc588SBarry Smith     while (nz--) {
61289bc588SBarry Smith       fm  = n;
62dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
63289bc588SBarry Smith       do {
64289bc588SBarry Smith         m  = fm;
65289bc588SBarry Smith         fm = fill[m];
66289bc588SBarry Smith       } while (fm < idx);
67289bc588SBarry Smith       fill[m]   = idx;
68289bc588SBarry Smith       fill[idx] = fm;
69289bc588SBarry Smith     }
70289bc588SBarry Smith     row = fill[n];
71289bc588SBarry Smith     while ( row < i ) {
72dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
732fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
742fb3ed76SBarry Smith       nz    = im[row] - nzbd;
75289bc588SBarry Smith       fm    = row;
762fb3ed76SBarry Smith       while (nz-- > 0) {
77dbb450caSBarry Smith         idx = *ajtmp++ + shift;
782fb3ed76SBarry Smith         nzbd++;
792fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
80289bc588SBarry Smith         do {
81289bc588SBarry Smith           m  = fm;
82289bc588SBarry Smith           fm = fill[m];
83289bc588SBarry Smith         } while (fm < idx);
84289bc588SBarry Smith         if (fm != idx) {
85289bc588SBarry Smith           fill[m]   = idx;
86289bc588SBarry Smith           fill[idx] = fm;
87289bc588SBarry Smith           fm        = idx;
88289bc588SBarry Smith           nnz++;
89289bc588SBarry Smith         }
90289bc588SBarry Smith       }
91289bc588SBarry Smith       row = fill[row];
92289bc588SBarry Smith     }
93289bc588SBarry Smith     /* copy new filled row into permanent storage */
94289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
95496e697eSBarry Smith     if (ainew[i+1] > jmax) {
96*ecf371e4SBarry Smith 
97*ecf371e4SBarry Smith       /* estimate how much additional space we will need */
98*ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
99*ecf371e4SBarry Smith       /* just double the memory each time */
100*ecf371e4SBarry Smith       int maxadd = jmax;
101*ecf371e4SBarry Smith       /* maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); */
102bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
1032fb3ed76SBarry Smith       jmax += maxadd;
104*ecf371e4SBarry Smith 
105*ecf371e4SBarry Smith       /* allocate a longer ajnew */
1060452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
107416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
1080452661fSBarry Smith       PetscFree(ajnew);
109289bc588SBarry Smith       ajnew = ajtmp;
1102fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
111289bc588SBarry Smith     }
112dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
113289bc588SBarry Smith     fm    = fill[n];
114289bc588SBarry Smith     nzi   = 0;
1152fb3ed76SBarry Smith     im[i] = nnz;
116289bc588SBarry Smith     while (nnz--) {
117289bc588SBarry Smith       if (fm < i) nzi++;
118dbb450caSBarry Smith       *ajtmp++ = fm - shift;
119289bc588SBarry Smith       fm       = fill[fm];
120289bc588SBarry Smith     }
121289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
122289bc588SBarry Smith   }
123464e8d44SSatish Balay   if (ai[n] != 0) {
124464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
125981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
126f64065bbSBarry Smith              realloc,f,af);
127981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
128981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
129981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
13005bf559cSSatish Balay   } else {
131981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
13205bf559cSSatish Balay   }
1332fb3ed76SBarry Smith 
134898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
135898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1361987afe7SBarry Smith 
1370452661fSBarry Smith   PetscFree(fill);
138289bc588SBarry Smith 
139289bc588SBarry Smith   /* put together the new matrix */
140b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1411987afe7SBarry Smith   PLogObjectParent(*B,isicol);
142416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1430452661fSBarry Smith   PetscFree(b->imax);
144416022c9SBarry Smith   b->singlemalloc = 0;
145e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1460452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
14791bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
148416022c9SBarry Smith   b->j          = ajnew;
149416022c9SBarry Smith   b->i          = ainew;
150416022c9SBarry Smith   b->diag       = idnew;
151416022c9SBarry Smith   b->ilen       = 0;
152416022c9SBarry Smith   b->imax       = 0;
153416022c9SBarry Smith   b->row        = isrow;
154416022c9SBarry Smith   b->col        = iscol;
15582bf6240SBarry Smith   b->icol       = isicol;
15691bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
157416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1587fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
159416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
160416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1617fd98bd6SLois Curfman McInnes 
162ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
163ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
164eea2913aSSatish Balay   if (ai[i] != 0) {
165ae068f58SLois Curfman McInnes     (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
166eea2913aSSatish Balay   } else {
167eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
168eea2913aSSatish Balay   }
169ae068f58SLois Curfman McInnes 
1703a40ed3dSBarry Smith   PetscFunctionReturn(0);
171289bc588SBarry Smith }
1720a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
17341c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
17441c01911SSatish Balay 
1755615d1e5SSatish Balay #undef __FUNC__
1765615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
177416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
178289bc588SBarry Smith {
179416022c9SBarry Smith   Mat        C = *B;
180416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
18182bf6240SBarry Smith   IS         isrow = b->row, isicol = b->icol;
182416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1831987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
184f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
18535aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1863a40ed3dSBarry Smith   register   int    *pj;
1878ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
18835aab85fSBarry Smith   double     ssum;
18935aab85fSBarry Smith   register   Scalar *pv, *rtmps,*u_values;
190289bc588SBarry Smith 
1913a40ed3dSBarry Smith   PetscFunctionBegin;
19282bf6240SBarry Smith 
19378b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
19478b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1950452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
19613ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1970cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
198289bc588SBarry Smith 
19935aab85fSBarry Smith   /* precalcuate row sums */
20035aab85fSBarry Smith   if (preserve_row_sums) {
20135aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
20235aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
20335aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
20435aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
20535aab85fSBarry Smith       sum = 0.0;
20635aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
20735aab85fSBarry Smith       rowsums[i] = sum;
20835aab85fSBarry Smith     }
20935aab85fSBarry Smith   }
21035aab85fSBarry Smith 
211289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
212289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
213dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
21448e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
215289bc588SBarry Smith 
216289bc588SBarry Smith     /* load in initial (unfactored row) */
217416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
218416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
219416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2200cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
221289bc588SBarry Smith 
222dbb450caSBarry Smith     row = *ajtmp++ + shift;
223f2582111SSatish Balay       while  (row < i ) {
2248c37ef55SBarry Smith       pc = rtmp + row;
2258c37ef55SBarry Smith       if (*pc != 0.0) {
2261987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2271987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
22835aab85fSBarry Smith         multiplier = *pc / *pv++;
2298c37ef55SBarry Smith         *pc        = multiplier;
230f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
231f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
232f2582111SSatish Balay         PLogFlops(2*nz);
233289bc588SBarry Smith       }
234dbb450caSBarry Smith       row = *ajtmp++ + shift;
235289bc588SBarry Smith     }
236416022c9SBarry Smith     /* finished row so stick it into b->a */
237416022c9SBarry Smith     pv = b->a + ai[i] + shift;
238416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2398c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
240416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
24135aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
24235aab85fSBarry Smith     /*
24335aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
24435aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
24535aab85fSBarry Smith     */
246d708749eSBarry Smith     if (pv[diag] == 0.0) {
247d708749eSBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot");
248d708749eSBarry Smith     }
24935aab85fSBarry Smith     if (preserve_row_sums) {
25035aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
25135aab85fSBarry Smith       sum = rowsums[i];
25235aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
25335aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
25435aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
25535aab85fSBarry Smith         inner_sum = 0.0;
25635aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
25735aab85fSBarry Smith           inner_sum += u_values[k];
25835aab85fSBarry Smith         }
25935aab85fSBarry Smith         sum -= pv[j]*inner_sum;
26035aab85fSBarry Smith 
26135aab85fSBarry Smith       }
26235aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
26335aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
26435aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
26535aab85fSBarry Smith         sum -= u_values[k];
26635aab85fSBarry Smith       }
26735aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
26835aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
26935aab85fSBarry Smith     }
27035aab85fSBarry Smith     /* check pivot entry for current row */
2718c37ef55SBarry Smith   }
2720f11f9aeSSatish Balay 
27335aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
27435aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
27535aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
27635aab85fSBarry Smith   }
27735aab85fSBarry Smith 
27835aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2790452661fSBarry Smith   PetscFree(rtmp);
28078b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
28178b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
282416022c9SBarry Smith   C->factor = FACTOR_LU;
28341c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
284c456f294SBarry Smith   C->assembled = PETSC_TRUE;
285416022c9SBarry Smith   PLogFlops(b->n);
2863a40ed3dSBarry Smith   PetscFunctionReturn(0);
287289bc588SBarry Smith }
2880a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
2895615d1e5SSatish Balay #undef __FUNC__
2905615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
291416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
292da3a660dSBarry Smith {
293416022c9SBarry Smith   Mat_SeqAIJ     *mat = (Mat_SeqAIJ *) A->data;
2946abc6512SBarry Smith   int            ierr;
295416022c9SBarry Smith   Mat            C;
296f830108cSBarry Smith   PetscOps       *Abops;
297f830108cSBarry Smith   struct _MatOps *Aops;
298416022c9SBarry Smith 
2993a40ed3dSBarry Smith   PetscFunctionBegin;
300f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr);
301f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr);
302da3a660dSBarry Smith 
303da3a660dSBarry Smith   /* free all the data structures from mat */
3040452661fSBarry Smith   PetscFree(mat->a);
3050452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
3060452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
3070452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
3080452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
3090452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
310a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
3110452661fSBarry Smith   PetscFree(mat);
312da3a660dSBarry Smith 
313f830108cSBarry Smith   /*
314f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
315f830108cSBarry Smith     A pointers for the bops and ops but copy everything
316f830108cSBarry Smith     else from C.
317f830108cSBarry Smith   */
318f830108cSBarry Smith   Abops = A->bops;
319f830108cSBarry Smith   Aops  = A->ops;
320f09e8eb9SSatish Balay   PetscMemcpy(A,C,sizeof(struct _p_Mat));
321f830108cSBarry Smith   A->bops = Abops;
322f830108cSBarry Smith   A->ops  = Aops;
323f830108cSBarry Smith 
3240452661fSBarry Smith   PetscHeaderDestroy(C);
3253a40ed3dSBarry Smith   PetscFunctionReturn(0);
326da3a660dSBarry Smith }
3270a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
3285615d1e5SSatish Balay #undef __FUNC__
3295615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
330416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3318c37ef55SBarry Smith {
332416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
333416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
334416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3353b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
336416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3378c37ef55SBarry Smith 
3383a40ed3dSBarry Smith   PetscFunctionBegin;
3393a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
34091bf9adeSBarry Smith 
34190f02eecSBarry Smith   VecGetArray_Fast(bb,b);
34290f02eecSBarry Smith   VecGetArray_Fast(xx,x);
343416022c9SBarry Smith   tmp  = a->solve_work;
3448c37ef55SBarry Smith 
3453b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3463b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3478c37ef55SBarry Smith 
3488c37ef55SBarry Smith   /* forward solve the lower triangular */
3498c37ef55SBarry Smith   tmp[0] = b[*r++];
3500cf60383SBarry Smith   tmps   = tmp + shift;
3518c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
352dbb450caSBarry Smith     v   = aa + ai[i] + shift;
353dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
354416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3558c37ef55SBarry Smith     sum = b[*r++];
3560cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3578c37ef55SBarry Smith     tmp[i] = sum;
3588c37ef55SBarry Smith   }
3598c37ef55SBarry Smith 
3608c37ef55SBarry Smith   /* backward solve the upper triangular */
3618c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
362416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
363416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
364416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3658c37ef55SBarry Smith     sum = tmp[i];
3660cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
367416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3688c37ef55SBarry Smith   }
3698c37ef55SBarry Smith 
3703b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3713b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
372416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3733a40ed3dSBarry Smith   PetscFunctionReturn(0);
3748c37ef55SBarry Smith }
375026e39d0SSatish Balay 
376930ae53cSBarry Smith /* ----------------------------------------------------------- */
377930ae53cSBarry Smith #undef __FUNC__
378930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering"
379930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx)
380930ae53cSBarry Smith {
381930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
382d85afc42SSatish Balay   int        n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr;
383d85afc42SSatish Balay   Scalar     *x,*b, *aa = a->a, sum;
384d85afc42SSatish Balay #if !defined(USE_FORTRAN_KERNELS)
385d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
386d85afc42SSatish Balay   Scalar     *v;
387d85afc42SSatish Balay #endif
388930ae53cSBarry Smith 
3893a40ed3dSBarry Smith   PetscFunctionBegin;
3903a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
391930ae53cSBarry Smith   if (a->indexshift) {
3923a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
3933a40ed3dSBarry Smith      PetscFunctionReturn(0);
394930ae53cSBarry Smith   }
395930ae53cSBarry Smith 
396930ae53cSBarry Smith   VecGetArray_Fast(bb,b);
397930ae53cSBarry Smith   VecGetArray_Fast(xx,x);
398930ae53cSBarry Smith 
399319c5e4dSSatish Balay #if defined(USE_FORTRAN_KERNELS)
4001c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
4011c4feecaSSatish Balay #else
402930ae53cSBarry Smith   /* forward solve the lower triangular */
403930ae53cSBarry Smith   x[0] = b[0];
404930ae53cSBarry Smith   for ( i=1; i<n; i++ ) {
405930ae53cSBarry Smith     ai_i = ai[i];
406930ae53cSBarry Smith     v    = aa + ai_i;
407930ae53cSBarry Smith     vi   = aj + ai_i;
408930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
409930ae53cSBarry Smith     sum  = b[i];
410930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
411930ae53cSBarry Smith     x[i] = sum;
412930ae53cSBarry Smith   }
413930ae53cSBarry Smith 
414930ae53cSBarry Smith   /* backward solve the upper triangular */
415930ae53cSBarry Smith   for ( i=n-1; i>=0; i-- ){
416930ae53cSBarry Smith     adiag_i = adiag[i];
417d708749eSBarry Smith     v       = aa + adiag_i + 1;
418d708749eSBarry Smith     vi      = aj + adiag_i + 1;
419930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
420930ae53cSBarry Smith     sum     = x[i];
421930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
422930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
423930ae53cSBarry Smith   }
4241c4feecaSSatish Balay #endif
425930ae53cSBarry Smith   PLogFlops(2*a->nz - a->n);
4263a40ed3dSBarry Smith   PetscFunctionReturn(0);
427930ae53cSBarry Smith }
428930ae53cSBarry Smith 
4295615d1e5SSatish Balay #undef __FUNC__
4305615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
431416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
432da3a660dSBarry Smith {
433416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
434416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
435416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
4363b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
437416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
438da3a660dSBarry Smith 
4393a40ed3dSBarry Smith   PetscFunctionBegin;
44078b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
441da3a660dSBarry Smith 
44290f02eecSBarry Smith   VecGetArray_Fast(bb,b);
44390f02eecSBarry Smith   VecGetArray_Fast(xx,x);
444416022c9SBarry Smith   tmp  = a->solve_work;
445da3a660dSBarry Smith 
4463b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
4473b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
448da3a660dSBarry Smith 
449da3a660dSBarry Smith   /* forward solve the lower triangular */
450da3a660dSBarry Smith   tmp[0] = b[*r++];
451da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
452dbb450caSBarry Smith     v   = aa + ai[i] + shift;
453dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
454416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
455da3a660dSBarry Smith     sum = b[*r++];
456dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
457da3a660dSBarry Smith     tmp[i] = sum;
458da3a660dSBarry Smith   }
459da3a660dSBarry Smith 
460da3a660dSBarry Smith   /* backward solve the upper triangular */
461da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
462416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
463416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
464416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
465da3a660dSBarry Smith     sum = tmp[i];
466dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
467416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
468da3a660dSBarry Smith     x[*c--] += tmp[i];
469da3a660dSBarry Smith   }
470da3a660dSBarry Smith 
4713b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
4723b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
473416022c9SBarry Smith   PLogFlops(2*a->nz);
474898183ecSLois Curfman McInnes 
4753a40ed3dSBarry Smith   PetscFunctionReturn(0);
476da3a660dSBarry Smith }
477da3a660dSBarry Smith /* -------------------------------------------------------------------*/
4785615d1e5SSatish Balay #undef __FUNC__
4795615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ"
480416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
481da3a660dSBarry Smith {
482416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
483416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
484416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4853b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
486416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
487da3a660dSBarry Smith 
4883a40ed3dSBarry Smith   PetscFunctionBegin;
48990f02eecSBarry Smith   VecGetArray_Fast(bb,b);
49090f02eecSBarry Smith   VecGetArray_Fast(xx,x);
491416022c9SBarry Smith   tmp  = a->solve_work;
492da3a660dSBarry Smith 
493da3a660dSBarry Smith   /* invert the permutations */
49478b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
49578b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
496da3a660dSBarry Smith 
4973b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4983b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
499da3a660dSBarry Smith 
500da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
501da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
502da3a660dSBarry Smith 
503da3a660dSBarry Smith   /* forward solve the U^T */
504da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
505416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
506416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
507416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
508da3a660dSBarry Smith     tmp[i] *= *v++;
509da3a660dSBarry Smith     while (nz--) {
510dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
511da3a660dSBarry Smith     }
512da3a660dSBarry Smith   }
513da3a660dSBarry Smith 
514da3a660dSBarry Smith   /* backward solve the L^T */
515da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
516416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
517416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
518416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
519da3a660dSBarry Smith     while (nz--) {
520dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
521da3a660dSBarry Smith     }
522da3a660dSBarry Smith   }
523da3a660dSBarry Smith 
524da3a660dSBarry Smith   /* copy tmp into x according to permutation */
525da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
526da3a660dSBarry Smith 
5273b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5283b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
529898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
530898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
531da3a660dSBarry Smith 
532416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
5333a40ed3dSBarry Smith   PetscFunctionReturn(0);
534da3a660dSBarry Smith }
535da3a660dSBarry Smith 
5365615d1e5SSatish Balay #undef __FUNC__
5375615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ"
538416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
539da3a660dSBarry Smith {
540416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
541416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
542416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
5433b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
544416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
5456abc6512SBarry Smith 
5463a40ed3dSBarry Smith   PetscFunctionBegin;
5476abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
5486abc6512SBarry Smith 
54990f02eecSBarry Smith   VecGetArray_Fast(bb,b);
55090f02eecSBarry Smith   VecGetArray_Fast(xx,x);
551416022c9SBarry Smith   tmp = a->solve_work;
5526abc6512SBarry Smith 
5536abc6512SBarry Smith   /* invert the permutations */
55478b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
55578b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
5563b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
5573b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
5586abc6512SBarry Smith 
5596abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
5606abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
5616abc6512SBarry Smith 
5626abc6512SBarry Smith   /* forward solve the U^T */
5636abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
564416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
565416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
566416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
5676abc6512SBarry Smith     tmp[i] *= *v++;
5686abc6512SBarry Smith     while (nz--) {
569dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
5706abc6512SBarry Smith     }
5716abc6512SBarry Smith   }
5726abc6512SBarry Smith 
5736abc6512SBarry Smith   /* backward solve the L^T */
5746abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
575416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
576416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
577416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
5786abc6512SBarry Smith     while (nz--) {
579dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
5806abc6512SBarry Smith     }
5816abc6512SBarry Smith   }
5826abc6512SBarry Smith 
5836abc6512SBarry Smith   /* copy tmp into x according to permutation */
5846abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
5856abc6512SBarry Smith 
5863b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5873b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
588898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
589898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
5906abc6512SBarry Smith 
591416022c9SBarry Smith   PLogFlops(2*a->nz);
5923a40ed3dSBarry Smith   PetscFunctionReturn(0);
593da3a660dSBarry Smith }
5949e25ed09SBarry Smith /* ----------------------------------------------------------------*/
59508480c60SBarry Smith 
5965615d1e5SSatish Balay #undef __FUNC__
5975615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
59808480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
5999e25ed09SBarry Smith {
600416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
6019e25ed09SBarry Smith   IS         isicol;
602416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
60356beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
60456beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
605416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
60677c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
6079e25ed09SBarry Smith 
6083a40ed3dSBarry Smith   PetscFunctionBegin;
60982bf6240SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
61082bf6240SBarry Smith 
61108480c60SBarry Smith   /* special case that simply copies fill pattern */
61277c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
61377c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
6143d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
61508480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
61608480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
61708480c60SBarry Smith     if (!b->diag) {
61808480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
61908480c60SBarry Smith     }
62008480c60SBarry Smith     b->row             = isrow;
62108480c60SBarry Smith     b->col             = iscol;
62282bf6240SBarry Smith     b->icol            = isicol;
6230452661fSBarry Smith     b->solve_work      = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
624f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
6253a40ed3dSBarry Smith     PetscFunctionReturn(0);
62608480c60SBarry Smith   }
62708480c60SBarry Smith 
628898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
629898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
6309e25ed09SBarry Smith 
6319e25ed09SBarry Smith   /* get new row pointers */
6320452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
633dbb450caSBarry Smith   ainew[0] = -shift;
6349e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
635dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
6360452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
6379e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
6380452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
6399e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
6400452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
64156beaf04SBarry Smith   /* im is level for each filled value */
6420452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
64356beaf04SBarry Smith   /* dloc is location of diagonal in factor */
6440452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
64556beaf04SBarry Smith   dloc[0]  = 0;
64656beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
6479e25ed09SBarry Smith     /* first copy previous fill into linked list */
64856beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
649385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
650dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
6519e25ed09SBarry Smith     fill[n] = n;
6529e25ed09SBarry Smith     while (nz--) {
6539e25ed09SBarry Smith       fm  = n;
654dbb450caSBarry Smith       idx = ic[*xi++ + shift];
6559e25ed09SBarry Smith       do {
6569e25ed09SBarry Smith         m  = fm;
6579e25ed09SBarry Smith         fm = fill[m];
6589e25ed09SBarry Smith       } while (fm < idx);
6599e25ed09SBarry Smith       fill[m]   = idx;
6609e25ed09SBarry Smith       fill[idx] = fm;
66156beaf04SBarry Smith       im[idx]   = 0;
6629e25ed09SBarry Smith     }
66356beaf04SBarry Smith     nzi = 0;
6649e25ed09SBarry Smith     row = fill[n];
66556beaf04SBarry Smith     while ( row < prow ) {
66656beaf04SBarry Smith       incrlev = im[row] + 1;
66756beaf04SBarry Smith       nz      = dloc[row];
668dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
669dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
670416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
671dbb450caSBarry Smith       if (*xi++ + shift != row) {
672965fcd8bSBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot: try running with -pc_ilu_nonzeros_along_diagonal");
67356beaf04SBarry Smith       }
67456beaf04SBarry Smith       fm      = row;
67556beaf04SBarry Smith       while (nnz-- > 0) {
676dbb450caSBarry Smith         idx = *xi++ + shift;
67756beaf04SBarry Smith         if (*flev + incrlev > levels) {
67856beaf04SBarry Smith           flev++;
67956beaf04SBarry Smith           continue;
68056beaf04SBarry Smith         }
68156beaf04SBarry Smith         do {
6829e25ed09SBarry Smith           m  = fm;
6839e25ed09SBarry Smith           fm = fill[m];
68456beaf04SBarry Smith         } while (fm < idx);
6859e25ed09SBarry Smith         if (fm != idx) {
68656beaf04SBarry Smith           im[idx]   = *flev + incrlev;
6879e25ed09SBarry Smith           fill[m]   = idx;
6889e25ed09SBarry Smith           fill[idx] = fm;
6899e25ed09SBarry Smith           fm        = idx;
69056beaf04SBarry Smith           nzf++;
691*ecf371e4SBarry Smith         } else {
69256beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
6939e25ed09SBarry Smith         }
69456beaf04SBarry Smith         flev++;
6959e25ed09SBarry Smith       }
6969e25ed09SBarry Smith       row = fill[row];
69756beaf04SBarry Smith       nzi++;
6989e25ed09SBarry Smith     }
6999e25ed09SBarry Smith     /* copy new filled row into permanent storage */
70056beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
701d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
702*ecf371e4SBarry Smith 
703*ecf371e4SBarry Smith       /* estimate how much additional space we will need */
704*ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
705*ecf371e4SBarry Smith       /* just double the memory each time */
706*ecf371e4SBarry Smith       /*  maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */
707*ecf371e4SBarry Smith       int maxadd = jmax;
70856beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
709bbb6d6a8SBarry Smith       jmax += maxadd;
710*ecf371e4SBarry Smith 
711*ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
7120452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
713416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
7140452661fSBarry Smith       PetscFree(ajnew);
71556beaf04SBarry Smith       ajnew = xi;
7160452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
717416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
7180452661fSBarry Smith       PetscFree(ajfill);
71956beaf04SBarry Smith       ajfill = xi;
720*ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
7219e25ed09SBarry Smith     }
722dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
723dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
72456beaf04SBarry Smith     dloc[prow]  = nzi;
7259e25ed09SBarry Smith     fm          = fill[n];
72656beaf04SBarry Smith     while (nzf--) {
727dbb450caSBarry Smith       *xi++   = fm - shift;
72856beaf04SBarry Smith       *flev++ = im[fm];
7299e25ed09SBarry Smith       fm      = fill[fm];
7309e25ed09SBarry Smith     }
7319e25ed09SBarry Smith   }
7320452661fSBarry Smith   PetscFree(ajfill);
733898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
734898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
7350452661fSBarry Smith   PetscFree(fill); PetscFree(im);
7369e25ed09SBarry Smith 
737f64065bbSBarry Smith   {
738464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
739981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
740f64065bbSBarry Smith              realloc,f,af);
741981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
742981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
743981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
744f64065bbSBarry Smith   }
745bbb6d6a8SBarry Smith 
7469e25ed09SBarry Smith   /* put together the new matrix */
747b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
748416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
7490452661fSBarry Smith   PetscFree(b->imax);
750416022c9SBarry Smith   b->singlemalloc = 0;
751dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
7529e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
7530452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
7546b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a);
755416022c9SBarry Smith   b->j          = ajnew;
756416022c9SBarry Smith   b->i          = ainew;
75756beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
758416022c9SBarry Smith   b->diag       = dloc;
759416022c9SBarry Smith   b->ilen       = 0;
760416022c9SBarry Smith   b->imax       = 0;
761416022c9SBarry Smith   b->row        = isrow;
762416022c9SBarry Smith   b->col        = iscol;
76382bf6240SBarry Smith   b->icol       = isicol;
76482bf6240SBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); CHKPTRQ(b->solve_work);
765416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
76656beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
767dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
768416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
7699e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
770ae068f58SLois Curfman McInnes 
771ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
772ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
773ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
774ae068f58SLois Curfman McInnes 
7753a40ed3dSBarry Smith   PetscFunctionReturn(0);
7769e25ed09SBarry Smith }
777d5d45c9bSBarry Smith 
778d5d45c9bSBarry Smith 
779d5d45c9bSBarry Smith 
780d5d45c9bSBarry Smith 
781