xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision d85afc42a62abe4574f9a0a4fad5f51933e98950)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*d85afc42SSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.95 1998/03/12 23:18:23 bsmith Exp balay $";
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) {
96289bc588SBarry Smith       /* allocate a longer ajnew */
972fb3ed76SBarry Smith       int maxadd;
98dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n);
99bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
1002fb3ed76SBarry Smith       jmax += maxadd;
1010452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
102416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
1030452661fSBarry Smith       PetscFree(ajnew);
104289bc588SBarry Smith       ajnew = ajtmp;
1052fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
106289bc588SBarry Smith     }
107dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
108289bc588SBarry Smith     fm    = fill[n];
109289bc588SBarry Smith     nzi   = 0;
1102fb3ed76SBarry Smith     im[i] = nnz;
111289bc588SBarry Smith     while (nnz--) {
112289bc588SBarry Smith       if (fm < i) nzi++;
113dbb450caSBarry Smith       *ajtmp++ = fm - shift;
114289bc588SBarry Smith       fm       = fill[fm];
115289bc588SBarry Smith     }
116289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
117289bc588SBarry Smith   }
118464e8d44SSatish Balay   if (ai[n] != 0) {
119464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
120981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
121f64065bbSBarry Smith              realloc,f,af);
122981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
123981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
124981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
12505bf559cSSatish Balay   } else {
126981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
12705bf559cSSatish Balay   }
1282fb3ed76SBarry Smith 
129898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
130898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1311987afe7SBarry Smith 
1320452661fSBarry Smith   PetscFree(fill);
133289bc588SBarry Smith 
134289bc588SBarry Smith   /* put together the new matrix */
135b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1361987afe7SBarry Smith   PLogObjectParent(*B,isicol);
137416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1380452661fSBarry Smith   PetscFree(b->imax);
139416022c9SBarry Smith   b->singlemalloc = 0;
140e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1410452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
14291bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
143416022c9SBarry Smith   b->j          = ajnew;
144416022c9SBarry Smith   b->i          = ainew;
145416022c9SBarry Smith   b->diag       = idnew;
146416022c9SBarry Smith   b->ilen       = 0;
147416022c9SBarry Smith   b->imax       = 0;
148416022c9SBarry Smith   b->row        = isrow;
149416022c9SBarry Smith   b->col        = iscol;
15082bf6240SBarry Smith   b->icol       = isicol;
15191bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
152416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1537fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
154416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
155416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1567fd98bd6SLois Curfman McInnes 
157ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
158ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
159eea2913aSSatish Balay   if (ai[i] != 0) {
160ae068f58SLois Curfman McInnes     (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
161eea2913aSSatish Balay   } else {
162eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
163eea2913aSSatish Balay   }
164ae068f58SLois Curfman McInnes 
1653a40ed3dSBarry Smith   PetscFunctionReturn(0);
166289bc588SBarry Smith }
1670a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
16841c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
16941c01911SSatish Balay 
1705615d1e5SSatish Balay #undef __FUNC__
1715615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
172416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
173289bc588SBarry Smith {
174416022c9SBarry Smith   Mat        C = *B;
175416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
17682bf6240SBarry Smith   IS         isrow = b->row, isicol = b->icol;
177416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1781987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
179f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
18035aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1813a40ed3dSBarry Smith   register   int    *pj;
1828ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
18335aab85fSBarry Smith   double     ssum;
18435aab85fSBarry Smith   register   Scalar *pv, *rtmps,*u_values;
185289bc588SBarry Smith 
1863a40ed3dSBarry Smith   PetscFunctionBegin;
18782bf6240SBarry Smith 
18878b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
18978b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1900452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
19113ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1920cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
193289bc588SBarry Smith 
19435aab85fSBarry Smith   /* precalcuate row sums */
19535aab85fSBarry Smith   if (preserve_row_sums) {
19635aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
19735aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
19835aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
19935aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
20035aab85fSBarry Smith       sum = 0.0;
20135aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
20235aab85fSBarry Smith       rowsums[i] = sum;
20335aab85fSBarry Smith     }
20435aab85fSBarry Smith   }
20535aab85fSBarry Smith 
206289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
207289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
208dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
20948e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
210289bc588SBarry Smith 
211289bc588SBarry Smith     /* load in initial (unfactored row) */
212416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
213416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
214416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2150cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
216289bc588SBarry Smith 
217dbb450caSBarry Smith     row = *ajtmp++ + shift;
218f2582111SSatish Balay       while  (row < i ) {
2198c37ef55SBarry Smith       pc = rtmp + row;
2208c37ef55SBarry Smith       if (*pc != 0.0) {
2211987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2221987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
22335aab85fSBarry Smith         multiplier = *pc / *pv++;
2248c37ef55SBarry Smith         *pc        = multiplier;
225f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
226f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
227f2582111SSatish Balay         PLogFlops(2*nz);
228289bc588SBarry Smith       }
229dbb450caSBarry Smith       row = *ajtmp++ + shift;
230289bc588SBarry Smith     }
231416022c9SBarry Smith     /* finished row so stick it into b->a */
232416022c9SBarry Smith     pv = b->a + ai[i] + shift;
233416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2348c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
235416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
23635aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
23735aab85fSBarry Smith     /*
23835aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
23935aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
24035aab85fSBarry Smith     */
241d708749eSBarry Smith     if (pv[diag] == 0.0) {
242d708749eSBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot");
243d708749eSBarry Smith     }
24435aab85fSBarry Smith     if (preserve_row_sums) {
24535aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
24635aab85fSBarry Smith       sum = rowsums[i];
24735aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
24835aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
24935aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
25035aab85fSBarry Smith         inner_sum = 0.0;
25135aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
25235aab85fSBarry Smith           inner_sum += u_values[k];
25335aab85fSBarry Smith         }
25435aab85fSBarry Smith         sum -= pv[j]*inner_sum;
25535aab85fSBarry Smith 
25635aab85fSBarry Smith       }
25735aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
25835aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
25935aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
26035aab85fSBarry Smith         sum -= u_values[k];
26135aab85fSBarry Smith       }
26235aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
26335aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
26435aab85fSBarry Smith     }
26535aab85fSBarry Smith     /* check pivot entry for current row */
2668c37ef55SBarry Smith   }
2670f11f9aeSSatish Balay 
26835aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
26935aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
27035aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
27135aab85fSBarry Smith   }
27235aab85fSBarry Smith 
27335aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2740452661fSBarry Smith   PetscFree(rtmp);
27578b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
27678b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
277416022c9SBarry Smith   C->factor = FACTOR_LU;
27841c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
279c456f294SBarry Smith   C->assembled = PETSC_TRUE;
280416022c9SBarry Smith   PLogFlops(b->n);
2813a40ed3dSBarry Smith   PetscFunctionReturn(0);
282289bc588SBarry Smith }
2830a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
2845615d1e5SSatish Balay #undef __FUNC__
2855615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
286416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
287da3a660dSBarry Smith {
288416022c9SBarry Smith   Mat_SeqAIJ     *mat = (Mat_SeqAIJ *) A->data;
2896abc6512SBarry Smith   int            ierr;
290416022c9SBarry Smith   Mat            C;
291f830108cSBarry Smith   PetscOps       *Abops;
292f830108cSBarry Smith   struct _MatOps *Aops;
293416022c9SBarry Smith 
2943a40ed3dSBarry Smith   PetscFunctionBegin;
295f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr);
296f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr);
297da3a660dSBarry Smith 
298da3a660dSBarry Smith   /* free all the data structures from mat */
2990452661fSBarry Smith   PetscFree(mat->a);
3000452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
3010452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
3020452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
3030452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
3040452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
305a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
3060452661fSBarry Smith   PetscFree(mat);
307da3a660dSBarry Smith 
308f830108cSBarry Smith   /*
309f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
310f830108cSBarry Smith     A pointers for the bops and ops but copy everything
311f830108cSBarry Smith     else from C.
312f830108cSBarry Smith   */
313f830108cSBarry Smith   Abops = A->bops;
314f830108cSBarry Smith   Aops  = A->ops;
315f09e8eb9SSatish Balay   PetscMemcpy(A,C,sizeof(struct _p_Mat));
316f830108cSBarry Smith   A->bops = Abops;
317f830108cSBarry Smith   A->ops  = Aops;
318f830108cSBarry Smith 
3190452661fSBarry Smith   PetscHeaderDestroy(C);
3203a40ed3dSBarry Smith   PetscFunctionReturn(0);
321da3a660dSBarry Smith }
3220a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
3235615d1e5SSatish Balay #undef __FUNC__
3245615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
325416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3268c37ef55SBarry Smith {
327416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
328416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
329416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3303b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
331416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3328c37ef55SBarry Smith 
3333a40ed3dSBarry Smith   PetscFunctionBegin;
3343a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
33591bf9adeSBarry Smith 
33690f02eecSBarry Smith   VecGetArray_Fast(bb,b);
33790f02eecSBarry Smith   VecGetArray_Fast(xx,x);
338416022c9SBarry Smith   tmp  = a->solve_work;
3398c37ef55SBarry Smith 
3403b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3413b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3428c37ef55SBarry Smith 
3438c37ef55SBarry Smith   /* forward solve the lower triangular */
3448c37ef55SBarry Smith   tmp[0] = b[*r++];
3450cf60383SBarry Smith   tmps   = tmp + shift;
3468c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
347dbb450caSBarry Smith     v   = aa + ai[i] + shift;
348dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
349416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3508c37ef55SBarry Smith     sum = b[*r++];
3510cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3528c37ef55SBarry Smith     tmp[i] = sum;
3538c37ef55SBarry Smith   }
3548c37ef55SBarry Smith 
3558c37ef55SBarry Smith   /* backward solve the upper triangular */
3568c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
357416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
358416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
359416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3608c37ef55SBarry Smith     sum = tmp[i];
3610cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
362416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3638c37ef55SBarry Smith   }
3648c37ef55SBarry Smith 
3653b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3663b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
367416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3683a40ed3dSBarry Smith   PetscFunctionReturn(0);
3698c37ef55SBarry Smith }
370026e39d0SSatish Balay 
371930ae53cSBarry Smith /* ----------------------------------------------------------- */
372930ae53cSBarry Smith #undef __FUNC__
373930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering"
374930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx)
375930ae53cSBarry Smith {
376930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
377*d85afc42SSatish Balay   int        n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr;
378*d85afc42SSatish Balay   Scalar     *x,*b, *aa = a->a, sum;
379*d85afc42SSatish Balay #if !defined(USE_FORTRAN_KERNELS)
380*d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
381*d85afc42SSatish Balay   Scalar     *v;
382*d85afc42SSatish Balay #endif
383930ae53cSBarry Smith 
3843a40ed3dSBarry Smith   PetscFunctionBegin;
3853a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
386930ae53cSBarry Smith   if (a->indexshift) {
3873a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
3883a40ed3dSBarry Smith      PetscFunctionReturn(0);
389930ae53cSBarry Smith   }
390930ae53cSBarry Smith 
391930ae53cSBarry Smith   VecGetArray_Fast(bb,b);
392930ae53cSBarry Smith   VecGetArray_Fast(xx,x);
393930ae53cSBarry Smith 
394319c5e4dSSatish Balay #if defined(USE_FORTRAN_KERNELS)
3951c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
3961c4feecaSSatish Balay #else
397930ae53cSBarry Smith   /* forward solve the lower triangular */
398930ae53cSBarry Smith   x[0] = b[0];
399930ae53cSBarry Smith   for ( i=1; i<n; i++ ) {
400930ae53cSBarry Smith     ai_i = ai[i];
401930ae53cSBarry Smith     v    = aa + ai_i;
402930ae53cSBarry Smith     vi   = aj + ai_i;
403930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
404930ae53cSBarry Smith     sum  = b[i];
405930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
406930ae53cSBarry Smith     x[i] = sum;
407930ae53cSBarry Smith   }
408930ae53cSBarry Smith 
409930ae53cSBarry Smith   /* backward solve the upper triangular */
410930ae53cSBarry Smith   for ( i=n-1; i>=0; i-- ){
411930ae53cSBarry Smith     adiag_i = adiag[i];
412d708749eSBarry Smith     v       = aa + adiag_i + 1;
413d708749eSBarry Smith     vi      = aj + adiag_i + 1;
414930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
415930ae53cSBarry Smith     sum     = x[i];
416930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
417930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
418930ae53cSBarry Smith   }
4191c4feecaSSatish Balay #endif
420930ae53cSBarry Smith   PLogFlops(2*a->nz - a->n);
4213a40ed3dSBarry Smith   PetscFunctionReturn(0);
422930ae53cSBarry Smith }
423930ae53cSBarry Smith 
4245615d1e5SSatish Balay #undef __FUNC__
4255615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
426416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
427da3a660dSBarry Smith {
428416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
429416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
430416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
4313b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
432416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
433da3a660dSBarry Smith 
4343a40ed3dSBarry Smith   PetscFunctionBegin;
43578b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
436da3a660dSBarry Smith 
43790f02eecSBarry Smith   VecGetArray_Fast(bb,b);
43890f02eecSBarry Smith   VecGetArray_Fast(xx,x);
439416022c9SBarry Smith   tmp  = a->solve_work;
440da3a660dSBarry Smith 
4413b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
4423b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
443da3a660dSBarry Smith 
444da3a660dSBarry Smith   /* forward solve the lower triangular */
445da3a660dSBarry Smith   tmp[0] = b[*r++];
446da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
447dbb450caSBarry Smith     v   = aa + ai[i] + shift;
448dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
449416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
450da3a660dSBarry Smith     sum = b[*r++];
451dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
452da3a660dSBarry Smith     tmp[i] = sum;
453da3a660dSBarry Smith   }
454da3a660dSBarry Smith 
455da3a660dSBarry Smith   /* backward solve the upper triangular */
456da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
457416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
458416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
459416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
460da3a660dSBarry Smith     sum = tmp[i];
461dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
462416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
463da3a660dSBarry Smith     x[*c--] += tmp[i];
464da3a660dSBarry Smith   }
465da3a660dSBarry Smith 
4663b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
4673b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
468416022c9SBarry Smith   PLogFlops(2*a->nz);
469898183ecSLois Curfman McInnes 
4703a40ed3dSBarry Smith   PetscFunctionReturn(0);
471da3a660dSBarry Smith }
472da3a660dSBarry Smith /* -------------------------------------------------------------------*/
4735615d1e5SSatish Balay #undef __FUNC__
4745615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ"
475416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
476da3a660dSBarry Smith {
477416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
478416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
479416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4803b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
481416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
482da3a660dSBarry Smith 
4833a40ed3dSBarry Smith   PetscFunctionBegin;
48490f02eecSBarry Smith   VecGetArray_Fast(bb,b);
48590f02eecSBarry Smith   VecGetArray_Fast(xx,x);
486416022c9SBarry Smith   tmp  = a->solve_work;
487da3a660dSBarry Smith 
488da3a660dSBarry Smith   /* invert the permutations */
48978b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
49078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
491da3a660dSBarry Smith 
4923b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
4933b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
494da3a660dSBarry Smith 
495da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
496da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
497da3a660dSBarry Smith 
498da3a660dSBarry Smith   /* forward solve the U^T */
499da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
500416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
501416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
502416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
503da3a660dSBarry Smith     tmp[i] *= *v++;
504da3a660dSBarry Smith     while (nz--) {
505dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
506da3a660dSBarry Smith     }
507da3a660dSBarry Smith   }
508da3a660dSBarry Smith 
509da3a660dSBarry Smith   /* backward solve the L^T */
510da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
511416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
512416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
513416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
514da3a660dSBarry Smith     while (nz--) {
515dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
516da3a660dSBarry Smith     }
517da3a660dSBarry Smith   }
518da3a660dSBarry Smith 
519da3a660dSBarry Smith   /* copy tmp into x according to permutation */
520da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
521da3a660dSBarry Smith 
5223b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5233b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
524898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
525898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
526da3a660dSBarry Smith 
527416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
5283a40ed3dSBarry Smith   PetscFunctionReturn(0);
529da3a660dSBarry Smith }
530da3a660dSBarry Smith 
5315615d1e5SSatish Balay #undef __FUNC__
5325615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ"
533416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
534da3a660dSBarry Smith {
535416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
536416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
537416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
5383b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
539416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
5406abc6512SBarry Smith 
5413a40ed3dSBarry Smith   PetscFunctionBegin;
5426abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
5436abc6512SBarry Smith 
54490f02eecSBarry Smith   VecGetArray_Fast(bb,b);
54590f02eecSBarry Smith   VecGetArray_Fast(xx,x);
546416022c9SBarry Smith   tmp = a->solve_work;
5476abc6512SBarry Smith 
5486abc6512SBarry Smith   /* invert the permutations */
54978b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
55078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
5513b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
5523b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
5536abc6512SBarry Smith 
5546abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
5556abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
5566abc6512SBarry Smith 
5576abc6512SBarry Smith   /* forward solve the U^T */
5586abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
559416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
560416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
561416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
5626abc6512SBarry Smith     tmp[i] *= *v++;
5636abc6512SBarry Smith     while (nz--) {
564dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
5656abc6512SBarry Smith     }
5666abc6512SBarry Smith   }
5676abc6512SBarry Smith 
5686abc6512SBarry Smith   /* backward solve the L^T */
5696abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
570416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
571416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
572416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
5736abc6512SBarry Smith     while (nz--) {
574dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
5756abc6512SBarry Smith     }
5766abc6512SBarry Smith   }
5776abc6512SBarry Smith 
5786abc6512SBarry Smith   /* copy tmp into x according to permutation */
5796abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
5806abc6512SBarry Smith 
5813b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5823b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
583898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
584898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
5856abc6512SBarry Smith 
586416022c9SBarry Smith   PLogFlops(2*a->nz);
5873a40ed3dSBarry Smith   PetscFunctionReturn(0);
588da3a660dSBarry Smith }
5899e25ed09SBarry Smith /* ----------------------------------------------------------------*/
59008480c60SBarry Smith 
5915615d1e5SSatish Balay #undef __FUNC__
5925615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
59308480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
5949e25ed09SBarry Smith {
595416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
5969e25ed09SBarry Smith   IS         isicol;
597416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
59856beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
59956beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
600416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
60177c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
6029e25ed09SBarry Smith 
6033a40ed3dSBarry Smith   PetscFunctionBegin;
60482bf6240SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
60582bf6240SBarry Smith 
60608480c60SBarry Smith   /* special case that simply copies fill pattern */
60777c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
60877c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
6093d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
61008480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
61108480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
61208480c60SBarry Smith     if (!b->diag) {
61308480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
61408480c60SBarry Smith     }
61508480c60SBarry Smith     b->row             = isrow;
61608480c60SBarry Smith     b->col             = iscol;
61782bf6240SBarry Smith     b->icol            = isicol;
6180452661fSBarry Smith     b->solve_work      = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
619f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
6203a40ed3dSBarry Smith     PetscFunctionReturn(0);
62108480c60SBarry Smith   }
62208480c60SBarry Smith 
623898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
624898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
6259e25ed09SBarry Smith 
6269e25ed09SBarry Smith   /* get new row pointers */
6270452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
628dbb450caSBarry Smith   ainew[0] = -shift;
6299e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
630dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
6310452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
6329e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
6330452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
6349e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
6350452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
63656beaf04SBarry Smith   /* im is level for each filled value */
6370452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
63856beaf04SBarry Smith   /* dloc is location of diagonal in factor */
6390452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
64056beaf04SBarry Smith   dloc[0]  = 0;
64156beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
6429e25ed09SBarry Smith     /* first copy previous fill into linked list */
64356beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
644385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
645dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
6469e25ed09SBarry Smith     fill[n] = n;
6479e25ed09SBarry Smith     while (nz--) {
6489e25ed09SBarry Smith       fm  = n;
649dbb450caSBarry Smith       idx = ic[*xi++ + shift];
6509e25ed09SBarry Smith       do {
6519e25ed09SBarry Smith         m  = fm;
6529e25ed09SBarry Smith         fm = fill[m];
6539e25ed09SBarry Smith       } while (fm < idx);
6549e25ed09SBarry Smith       fill[m]   = idx;
6559e25ed09SBarry Smith       fill[idx] = fm;
65656beaf04SBarry Smith       im[idx]   = 0;
6579e25ed09SBarry Smith     }
65856beaf04SBarry Smith     nzi = 0;
6599e25ed09SBarry Smith     row = fill[n];
66056beaf04SBarry Smith     while ( row < prow ) {
66156beaf04SBarry Smith       incrlev = im[row] + 1;
66256beaf04SBarry Smith       nz      = dloc[row];
663dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
664dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
665416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
666dbb450caSBarry Smith       if (*xi++ + shift != row) {
667965fcd8bSBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot: try running with -pc_ilu_nonzeros_along_diagonal");
66856beaf04SBarry Smith       }
66956beaf04SBarry Smith       fm      = row;
67056beaf04SBarry Smith       while (nnz-- > 0) {
671dbb450caSBarry Smith         idx = *xi++ + shift;
67256beaf04SBarry Smith         if (*flev + incrlev > levels) {
67356beaf04SBarry Smith           flev++;
67456beaf04SBarry Smith           continue;
67556beaf04SBarry Smith         }
67656beaf04SBarry Smith         do {
6779e25ed09SBarry Smith           m  = fm;
6789e25ed09SBarry Smith           fm = fill[m];
67956beaf04SBarry Smith         } while (fm < idx);
6809e25ed09SBarry Smith         if (fm != idx) {
68156beaf04SBarry Smith           im[idx]   = *flev + incrlev;
6829e25ed09SBarry Smith           fill[m]   = idx;
6839e25ed09SBarry Smith           fill[idx] = fm;
6849e25ed09SBarry Smith           fm        = idx;
68556beaf04SBarry Smith           nzf++;
6869e25ed09SBarry Smith         }
68756beaf04SBarry Smith         else {
68856beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
6899e25ed09SBarry Smith         }
69056beaf04SBarry Smith         flev++;
6919e25ed09SBarry Smith       }
6929e25ed09SBarry Smith       row = fill[row];
69356beaf04SBarry Smith       nzi++;
6949e25ed09SBarry Smith     }
6959e25ed09SBarry Smith     /* copy new filled row into permanent storage */
69656beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
697d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
6989e25ed09SBarry Smith       /* allocate a longer ajnew */
699bbb6d6a8SBarry Smith       int maxadd;
700dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
70156beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
702bbb6d6a8SBarry Smith       jmax += maxadd;
7030452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
704416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
7050452661fSBarry Smith       PetscFree(ajnew);
70656beaf04SBarry Smith       ajnew = xi;
7079e25ed09SBarry Smith       /* allocate a longer ajfill */
7080452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
709416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
7100452661fSBarry Smith       PetscFree(ajfill);
71156beaf04SBarry Smith       ajfill = xi;
712bbb6d6a8SBarry Smith       realloc++;
7139e25ed09SBarry Smith     }
714dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
715dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
71656beaf04SBarry Smith     dloc[prow]  = nzi;
7179e25ed09SBarry Smith     fm          = fill[n];
71856beaf04SBarry Smith     while (nzf--) {
719dbb450caSBarry Smith       *xi++   = fm - shift;
72056beaf04SBarry Smith       *flev++ = im[fm];
7219e25ed09SBarry Smith       fm      = fill[fm];
7229e25ed09SBarry Smith     }
7239e25ed09SBarry Smith   }
7240452661fSBarry Smith   PetscFree(ajfill);
725898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
726898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
7270452661fSBarry Smith   PetscFree(fill); PetscFree(im);
7289e25ed09SBarry Smith 
729f64065bbSBarry Smith   {
730464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
731981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
732f64065bbSBarry Smith              realloc,f,af);
733981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
734981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
735981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
736f64065bbSBarry Smith   }
737bbb6d6a8SBarry Smith 
7389e25ed09SBarry Smith   /* put together the new matrix */
739b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
740416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
7410452661fSBarry Smith   PetscFree(b->imax);
742416022c9SBarry Smith   b->singlemalloc = 0;
743dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
7449e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
7450452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
7466b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a);
747416022c9SBarry Smith   b->j          = ajnew;
748416022c9SBarry Smith   b->i          = ainew;
74956beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
750416022c9SBarry Smith   b->diag       = dloc;
751416022c9SBarry Smith   b->ilen       = 0;
752416022c9SBarry Smith   b->imax       = 0;
753416022c9SBarry Smith   b->row        = isrow;
754416022c9SBarry Smith   b->col        = iscol;
75582bf6240SBarry Smith   b->icol       = isicol;
75682bf6240SBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); CHKPTRQ(b->solve_work);
757416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
75856beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
759dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
760416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
7619e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
762ae068f58SLois Curfman McInnes 
763ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
764ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
765ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
766ae068f58SLois Curfman McInnes 
7673a40ed3dSBarry Smith   PetscFunctionReturn(0);
7689e25ed09SBarry Smith }
769d5d45c9bSBarry Smith 
770d5d45c9bSBarry Smith 
771d5d45c9bSBarry Smith 
772d5d45c9bSBarry Smith 
773