xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision ac121b3d91555da2949951aaa7d9245d6aa4de1a)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*ac121b3dSBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.104 1998/07/14 14:48:33 bsmith 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);
39*ac121b3dSBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
40*ac121b3dSBarry Smith   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
41289bc588SBarry Smith 
42289bc588SBarry Smith   /* get new row pointers */
430452661fSBarry Smith   ainew    = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
44dbb450caSBarry Smith   ainew[0] = -shift;
45289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
46dbb450caSBarry Smith   jmax  = (int) (f*ai[n]+(!shift));
470452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
48289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
490452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
502fb3ed76SBarry Smith   im   = fill + n + 1;
51289bc588SBarry Smith   /* idnew is location of diagonal in factor */
520452661fSBarry Smith   idnew    = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
53dbb450caSBarry Smith   idnew[0] = -shift;
54289bc588SBarry Smith 
55289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
56289bc588SBarry Smith     /* first copy previous fill into linked list */
57289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
586b96ef82SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
59dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
60289bc588SBarry Smith     fill[n] = n;
61289bc588SBarry Smith     while (nz--) {
62289bc588SBarry Smith       fm  = n;
63dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
64289bc588SBarry Smith       do {
65289bc588SBarry Smith         m  = fm;
66289bc588SBarry Smith         fm = fill[m];
67289bc588SBarry Smith       } while (fm < idx);
68289bc588SBarry Smith       fill[m]   = idx;
69289bc588SBarry Smith       fill[idx] = fm;
70289bc588SBarry Smith     }
71289bc588SBarry Smith     row = fill[n];
72289bc588SBarry Smith     while ( row < i ) {
73dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
742fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
752fb3ed76SBarry Smith       nz    = im[row] - nzbd;
76289bc588SBarry Smith       fm    = row;
772fb3ed76SBarry Smith       while (nz-- > 0) {
78dbb450caSBarry Smith         idx = *ajtmp++ + shift;
792fb3ed76SBarry Smith         nzbd++;
802fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
81289bc588SBarry Smith         do {
82289bc588SBarry Smith           m  = fm;
83289bc588SBarry Smith           fm = fill[m];
84289bc588SBarry Smith         } while (fm < idx);
85289bc588SBarry Smith         if (fm != idx) {
86289bc588SBarry Smith           fill[m]   = idx;
87289bc588SBarry Smith           fill[idx] = fm;
88289bc588SBarry Smith           fm        = idx;
89289bc588SBarry Smith           nnz++;
90289bc588SBarry Smith         }
91289bc588SBarry Smith       }
92289bc588SBarry Smith       row = fill[row];
93289bc588SBarry Smith     }
94289bc588SBarry Smith     /* copy new filled row into permanent storage */
95289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
96496e697eSBarry Smith     if (ainew[i+1] > jmax) {
97ecf371e4SBarry Smith 
98ecf371e4SBarry Smith       /* estimate how much additional space we will need */
99ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
100ecf371e4SBarry Smith       /* just double the memory each time */
101ecf371e4SBarry Smith       int maxadd = jmax;
102ecf371e4SBarry Smith       /* maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); */
103bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
1042fb3ed76SBarry Smith       jmax += maxadd;
105ecf371e4SBarry Smith 
106ecf371e4SBarry Smith       /* allocate a longer ajnew */
1070452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
108416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
1090452661fSBarry Smith       PetscFree(ajnew);
110289bc588SBarry Smith       ajnew = ajtmp;
1112fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
112289bc588SBarry Smith     }
113dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
114289bc588SBarry Smith     fm    = fill[n];
115289bc588SBarry Smith     nzi   = 0;
1162fb3ed76SBarry Smith     im[i] = nnz;
117289bc588SBarry Smith     while (nnz--) {
118289bc588SBarry Smith       if (fm < i) nzi++;
119dbb450caSBarry Smith       *ajtmp++ = fm - shift;
120289bc588SBarry Smith       fm       = fill[fm];
121289bc588SBarry Smith     }
122289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
123289bc588SBarry Smith   }
124464e8d44SSatish Balay   if (ai[n] != 0) {
125464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
126981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
127f64065bbSBarry Smith              realloc,f,af);
128981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
129981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
130981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
13105bf559cSSatish Balay   } else {
132981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
13305bf559cSSatish Balay   }
1342fb3ed76SBarry Smith 
135898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
136898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1371987afe7SBarry Smith 
1380452661fSBarry Smith   PetscFree(fill);
139289bc588SBarry Smith 
140289bc588SBarry Smith   /* put together the new matrix */
141b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1421987afe7SBarry Smith   PLogObjectParent(*B,isicol);
143416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1440452661fSBarry Smith   PetscFree(b->imax);
145416022c9SBarry Smith   b->singlemalloc = 0;
146e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1470452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
14891bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
149416022c9SBarry Smith   b->j          = ajnew;
150416022c9SBarry Smith   b->i          = ainew;
151416022c9SBarry Smith   b->diag       = idnew;
152416022c9SBarry Smith   b->ilen       = 0;
153416022c9SBarry Smith   b->imax       = 0;
154416022c9SBarry Smith   b->row        = isrow;
155416022c9SBarry Smith   b->col        = iscol;
15682bf6240SBarry Smith   b->icol       = isicol;
15791bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
158416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1597fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
160416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
161416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1627fd98bd6SLois Curfman McInnes 
163ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
164ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
165eea2913aSSatish Balay   if (ai[i] != 0) {
166ae068f58SLois Curfman McInnes     (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]);
167eea2913aSSatish Balay   } else {
168eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
169eea2913aSSatish Balay   }
170ae068f58SLois Curfman McInnes 
1713a40ed3dSBarry Smith   PetscFunctionReturn(0);
172289bc588SBarry Smith }
1730a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
17441c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
17541c01911SSatish Balay 
1765615d1e5SSatish Balay #undef __FUNC__
1775615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
178416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
179289bc588SBarry Smith {
180416022c9SBarry Smith   Mat        C = *B;
181416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
18282bf6240SBarry Smith   IS         isrow = b->row, isicol = b->icol;
183416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1841987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
185f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
18635aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1873a40ed3dSBarry Smith   register   int    *pj;
1888ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
18935aab85fSBarry Smith   double     ssum;
19035aab85fSBarry Smith   register   Scalar *pv, *rtmps,*u_values;
191289bc588SBarry Smith 
1923a40ed3dSBarry Smith   PetscFunctionBegin;
19382bf6240SBarry Smith 
19478b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
19578b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1960452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
19713ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
1980cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
199289bc588SBarry Smith 
20035aab85fSBarry Smith   /* precalcuate row sums */
20135aab85fSBarry Smith   if (preserve_row_sums) {
20235aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
20335aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
20435aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
20535aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
20635aab85fSBarry Smith       sum = 0.0;
20735aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
20835aab85fSBarry Smith       rowsums[i] = sum;
20935aab85fSBarry Smith     }
21035aab85fSBarry Smith   }
21135aab85fSBarry Smith 
212289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
213289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
214dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
21548e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
216289bc588SBarry Smith 
217289bc588SBarry Smith     /* load in initial (unfactored row) */
218416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
219416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
220416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2210cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
222289bc588SBarry Smith 
223dbb450caSBarry Smith     row = *ajtmp++ + shift;
224f2582111SSatish Balay       while  (row < i ) {
2258c37ef55SBarry Smith       pc = rtmp + row;
2268c37ef55SBarry Smith       if (*pc != 0.0) {
2271987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2281987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
22935aab85fSBarry Smith         multiplier = *pc / *pv++;
2308c37ef55SBarry Smith         *pc        = multiplier;
231f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
232f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
233f2582111SSatish Balay         PLogFlops(2*nz);
234289bc588SBarry Smith       }
235dbb450caSBarry Smith       row = *ajtmp++ + shift;
236289bc588SBarry Smith     }
237416022c9SBarry Smith     /* finished row so stick it into b->a */
238416022c9SBarry Smith     pv = b->a + ai[i] + shift;
239416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2408c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
241416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
24235aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
24335aab85fSBarry Smith     /*
24435aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
24535aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
24635aab85fSBarry Smith     */
247d708749eSBarry Smith     if (pv[diag] == 0.0) {
248d708749eSBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot");
249d708749eSBarry Smith     }
25035aab85fSBarry Smith     if (preserve_row_sums) {
25135aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
25235aab85fSBarry Smith       sum = rowsums[i];
25335aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
25435aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
25535aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
25635aab85fSBarry Smith         inner_sum = 0.0;
25735aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
25835aab85fSBarry Smith           inner_sum += u_values[k];
25935aab85fSBarry Smith         }
26035aab85fSBarry Smith         sum -= pv[j]*inner_sum;
26135aab85fSBarry Smith 
26235aab85fSBarry Smith       }
26335aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
26435aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
26535aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
26635aab85fSBarry Smith         sum -= u_values[k];
26735aab85fSBarry Smith       }
26835aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
26935aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
27035aab85fSBarry Smith     }
27135aab85fSBarry Smith     /* check pivot entry for current row */
2728c37ef55SBarry Smith   }
2730f11f9aeSSatish Balay 
27435aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
27535aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
27635aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
27735aab85fSBarry Smith   }
27835aab85fSBarry Smith 
27935aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2800452661fSBarry Smith   PetscFree(rtmp);
28178b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
28278b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
283416022c9SBarry Smith   C->factor = FACTOR_LU;
28441c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
285c456f294SBarry Smith   C->assembled = PETSC_TRUE;
286416022c9SBarry Smith   PLogFlops(b->n);
2873a40ed3dSBarry Smith   PetscFunctionReturn(0);
288289bc588SBarry Smith }
2890a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
2905615d1e5SSatish Balay #undef __FUNC__
2915615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
292416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
293da3a660dSBarry Smith {
294416022c9SBarry Smith   Mat_SeqAIJ     *mat = (Mat_SeqAIJ *) A->data;
2956abc6512SBarry Smith   int            ierr;
296416022c9SBarry Smith   Mat            C;
297f830108cSBarry Smith   PetscOps       *Abops;
2980a6ffc59SBarry Smith   MatOps         Aops;
299416022c9SBarry Smith 
3003a40ed3dSBarry Smith   PetscFunctionBegin;
301f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr);
302f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr);
303da3a660dSBarry Smith 
304da3a660dSBarry Smith   /* free all the data structures from mat */
3050452661fSBarry Smith   PetscFree(mat->a);
3060452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
3070452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
3080452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
3090452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
3100452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
311a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
3120452661fSBarry Smith   PetscFree(mat);
313da3a660dSBarry Smith 
31417642b18SBarry Smith   ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
31517642b18SBarry Smith   ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
31617642b18SBarry Smith 
317f830108cSBarry Smith   /*
318f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
319f830108cSBarry Smith     A pointers for the bops and ops but copy everything
320f830108cSBarry Smith     else from C.
321f830108cSBarry Smith   */
322f830108cSBarry Smith   Abops = A->bops;
323f830108cSBarry Smith   Aops  = A->ops;
324f09e8eb9SSatish Balay   PetscMemcpy(A,C,sizeof(struct _p_Mat));
325f830108cSBarry Smith   A->bops  = Abops;
326f830108cSBarry Smith   A->ops   = Aops;
327bef8e0ddSBarry Smith   A->qlist = 0;
328f830108cSBarry Smith 
3290452661fSBarry Smith   PetscHeaderDestroy(C);
3303a40ed3dSBarry Smith   PetscFunctionReturn(0);
331da3a660dSBarry Smith }
3320a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
3335615d1e5SSatish Balay #undef __FUNC__
3345615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
335416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3368c37ef55SBarry Smith {
337416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
338416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
339416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3403b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
341416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3428c37ef55SBarry Smith 
3433a40ed3dSBarry Smith   PetscFunctionBegin;
3443a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
34591bf9adeSBarry Smith 
346e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
347e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
348416022c9SBarry Smith   tmp  = a->solve_work;
3498c37ef55SBarry Smith 
3503b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3513b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3528c37ef55SBarry Smith 
3538c37ef55SBarry Smith   /* forward solve the lower triangular */
3548c37ef55SBarry Smith   tmp[0] = b[*r++];
3550cf60383SBarry Smith   tmps   = tmp + shift;
3568c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
357dbb450caSBarry Smith     v   = aa + ai[i] + shift;
358dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
359416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3608c37ef55SBarry Smith     sum = b[*r++];
3610cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3628c37ef55SBarry Smith     tmp[i] = sum;
3638c37ef55SBarry Smith   }
3648c37ef55SBarry Smith 
3658c37ef55SBarry Smith   /* backward solve the upper triangular */
3668c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
367416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
368416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
369416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3708c37ef55SBarry Smith     sum = tmp[i];
3710cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
372416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3738c37ef55SBarry Smith   }
3748c37ef55SBarry Smith 
3753b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3763b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
377416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3783a40ed3dSBarry Smith   PetscFunctionReturn(0);
3798c37ef55SBarry Smith }
380026e39d0SSatish Balay 
381930ae53cSBarry Smith /* ----------------------------------------------------------- */
382930ae53cSBarry Smith #undef __FUNC__
383930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering"
384930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx)
385930ae53cSBarry Smith {
386930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
387d85afc42SSatish Balay   int        n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr;
388d85afc42SSatish Balay   Scalar     *x,*b, *aa = a->a, sum;
3892e40c62eSSatish Balay #if !defined(USE_FORTRAN_KERNEL_SOLVEAIJ)
390d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
391d85afc42SSatish Balay   Scalar     *v;
392d85afc42SSatish Balay #endif
393930ae53cSBarry Smith 
3943a40ed3dSBarry Smith   PetscFunctionBegin;
3953a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
396930ae53cSBarry Smith   if (a->indexshift) {
3973a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
3983a40ed3dSBarry Smith      PetscFunctionReturn(0);
399930ae53cSBarry Smith   }
400930ae53cSBarry Smith 
401e1311b90SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
402e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
403930ae53cSBarry Smith 
4042e40c62eSSatish Balay #if defined(USE_FORTRAN_KERNEL_SOLVEAIJ)
4051c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
4061c4feecaSSatish Balay #else
407930ae53cSBarry Smith   /* forward solve the lower triangular */
408930ae53cSBarry Smith   x[0] = b[0];
409930ae53cSBarry Smith   for ( i=1; i<n; i++ ) {
410930ae53cSBarry Smith     ai_i = ai[i];
411930ae53cSBarry Smith     v    = aa + ai_i;
412930ae53cSBarry Smith     vi   = aj + ai_i;
413930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
414930ae53cSBarry Smith     sum  = b[i];
415930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
416930ae53cSBarry Smith     x[i] = sum;
417930ae53cSBarry Smith   }
418930ae53cSBarry Smith 
419930ae53cSBarry Smith   /* backward solve the upper triangular */
420930ae53cSBarry Smith   for ( i=n-1; i>=0; i-- ){
421930ae53cSBarry Smith     adiag_i = adiag[i];
422d708749eSBarry Smith     v       = aa + adiag_i + 1;
423d708749eSBarry Smith     vi      = aj + adiag_i + 1;
424930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
425930ae53cSBarry Smith     sum     = x[i];
426930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
427930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
428930ae53cSBarry Smith   }
4291c4feecaSSatish Balay #endif
430930ae53cSBarry Smith   PLogFlops(2*a->nz - a->n);
4313a40ed3dSBarry Smith   PetscFunctionReturn(0);
432930ae53cSBarry Smith }
433930ae53cSBarry Smith 
4345615d1e5SSatish Balay #undef __FUNC__
4355615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
436416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
437da3a660dSBarry Smith {
438416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
439416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
440416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
4413b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
442416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
443da3a660dSBarry Smith 
4443a40ed3dSBarry Smith   PetscFunctionBegin;
44578b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
446da3a660dSBarry Smith 
447e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
448e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
449416022c9SBarry Smith   tmp  = a->solve_work;
450da3a660dSBarry Smith 
4513b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
4523b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
453da3a660dSBarry Smith 
454da3a660dSBarry Smith   /* forward solve the lower triangular */
455da3a660dSBarry Smith   tmp[0] = b[*r++];
456da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
457dbb450caSBarry Smith     v   = aa + ai[i] + shift;
458dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
459416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
460da3a660dSBarry Smith     sum = b[*r++];
461dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
462da3a660dSBarry Smith     tmp[i] = sum;
463da3a660dSBarry Smith   }
464da3a660dSBarry Smith 
465da3a660dSBarry Smith   /* backward solve the upper triangular */
466da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
467416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
468416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
469416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
470da3a660dSBarry Smith     sum = tmp[i];
471dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
472416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
473da3a660dSBarry Smith     x[*c--] += tmp[i];
474da3a660dSBarry Smith   }
475da3a660dSBarry Smith 
4763b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
4773b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
478416022c9SBarry Smith   PLogFlops(2*a->nz);
479898183ecSLois Curfman McInnes 
4803a40ed3dSBarry Smith   PetscFunctionReturn(0);
481da3a660dSBarry Smith }
482da3a660dSBarry Smith /* -------------------------------------------------------------------*/
4835615d1e5SSatish Balay #undef __FUNC__
4845615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ"
485416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
486da3a660dSBarry Smith {
487416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
488416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
489416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4903b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
491416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
492da3a660dSBarry Smith 
4933a40ed3dSBarry Smith   PetscFunctionBegin;
494e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
495e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
496416022c9SBarry Smith   tmp  = a->solve_work;
497da3a660dSBarry Smith 
498da3a660dSBarry Smith   /* invert the permutations */
49978b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
50078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
501da3a660dSBarry Smith 
5023b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
5033b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
504da3a660dSBarry Smith 
505da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
506da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
507da3a660dSBarry Smith 
508da3a660dSBarry Smith   /* forward solve the U^T */
509da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
510416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
511416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
512416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
513da3a660dSBarry Smith     tmp[i] *= *v++;
514da3a660dSBarry Smith     while (nz--) {
515dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
516da3a660dSBarry Smith     }
517da3a660dSBarry Smith   }
518da3a660dSBarry Smith 
519da3a660dSBarry Smith   /* backward solve the L^T */
520da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
521416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
522416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
523416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
524da3a660dSBarry Smith     while (nz--) {
525dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
526da3a660dSBarry Smith     }
527da3a660dSBarry Smith   }
528da3a660dSBarry Smith 
529da3a660dSBarry Smith   /* copy tmp into x according to permutation */
530da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
531da3a660dSBarry Smith 
5323b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5333b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
534898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
535898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
536da3a660dSBarry Smith 
537416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
5383a40ed3dSBarry Smith   PetscFunctionReturn(0);
539da3a660dSBarry Smith }
540da3a660dSBarry Smith 
5415615d1e5SSatish Balay #undef __FUNC__
5425615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ"
543416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
544da3a660dSBarry Smith {
545416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
546416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
547416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
5483b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
549416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
5506abc6512SBarry Smith 
5513a40ed3dSBarry Smith   PetscFunctionBegin;
5526abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
5536abc6512SBarry Smith 
554e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
555e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
556416022c9SBarry Smith   tmp = a->solve_work;
5576abc6512SBarry Smith 
5586abc6512SBarry Smith   /* invert the permutations */
55978b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
56078b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
5613b2fbd54SBarry Smith   ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout;
5623b2fbd54SBarry Smith   ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout;
5636abc6512SBarry Smith 
5646abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
5656abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
5666abc6512SBarry Smith 
5676abc6512SBarry Smith   /* forward solve the U^T */
5686abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
569416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
570416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
571416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
5726abc6512SBarry Smith     tmp[i] *= *v++;
5736abc6512SBarry Smith     while (nz--) {
574dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
5756abc6512SBarry Smith     }
5766abc6512SBarry Smith   }
5776abc6512SBarry Smith 
5786abc6512SBarry Smith   /* backward solve the L^T */
5796abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
580416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
581416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
582416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
5836abc6512SBarry Smith     while (nz--) {
584dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
5856abc6512SBarry Smith     }
5866abc6512SBarry Smith   }
5876abc6512SBarry Smith 
5886abc6512SBarry Smith   /* copy tmp into x according to permutation */
5896abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
5906abc6512SBarry Smith 
5913b2fbd54SBarry Smith   ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr);
5923b2fbd54SBarry Smith   ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr);
593898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
594898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
5956abc6512SBarry Smith 
596416022c9SBarry Smith   PLogFlops(2*a->nz);
5973a40ed3dSBarry Smith   PetscFunctionReturn(0);
598da3a660dSBarry Smith }
5999e25ed09SBarry Smith /* ----------------------------------------------------------------*/
60008480c60SBarry Smith 
6015615d1e5SSatish Balay #undef __FUNC__
6025615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
60308480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
6049e25ed09SBarry Smith {
605416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
6069e25ed09SBarry Smith   IS         isicol;
607416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
60856beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
60956beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
610416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
61177c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
6129e25ed09SBarry Smith 
6133a40ed3dSBarry Smith   PetscFunctionBegin;
61482bf6240SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
61582bf6240SBarry Smith 
61608480c60SBarry Smith   /* special case that simply copies fill pattern */
61777c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
61877c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
6193d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
62008480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
62108480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
62208480c60SBarry Smith     if (!b->diag) {
62308480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
62408480c60SBarry Smith     }
62508480c60SBarry Smith     b->row             = isrow;
62608480c60SBarry Smith     b->col             = iscol;
62782bf6240SBarry Smith     b->icol            = isicol;
6280452661fSBarry Smith     b->solve_work      = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
629f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
6303a40ed3dSBarry Smith     PetscFunctionReturn(0);
63108480c60SBarry Smith   }
63208480c60SBarry Smith 
633898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
634898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
6359e25ed09SBarry Smith 
6369e25ed09SBarry Smith   /* get new row pointers */
6370452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
638dbb450caSBarry Smith   ainew[0] = -shift;
6399e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
640dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
6410452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
6429e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
6430452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
6449e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
6450452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
64656beaf04SBarry Smith   /* im is level for each filled value */
6470452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
64856beaf04SBarry Smith   /* dloc is location of diagonal in factor */
6490452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
65056beaf04SBarry Smith   dloc[0]  = 0;
65156beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
6529e25ed09SBarry Smith     /* first copy previous fill into linked list */
65356beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
654385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
655dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
6569e25ed09SBarry Smith     fill[n] = n;
6579e25ed09SBarry Smith     while (nz--) {
6589e25ed09SBarry Smith       fm  = n;
659dbb450caSBarry Smith       idx = ic[*xi++ + shift];
6609e25ed09SBarry Smith       do {
6619e25ed09SBarry Smith         m  = fm;
6629e25ed09SBarry Smith         fm = fill[m];
6639e25ed09SBarry Smith       } while (fm < idx);
6649e25ed09SBarry Smith       fill[m]   = idx;
6659e25ed09SBarry Smith       fill[idx] = fm;
66656beaf04SBarry Smith       im[idx]   = 0;
6679e25ed09SBarry Smith     }
66856beaf04SBarry Smith     nzi = 0;
6699e25ed09SBarry Smith     row = fill[n];
67056beaf04SBarry Smith     while ( row < prow ) {
67156beaf04SBarry Smith       incrlev = im[row] + 1;
67256beaf04SBarry Smith       nz      = dloc[row];
673dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
674dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
675416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
676dbb450caSBarry Smith       if (*xi++ + shift != row) {
677965fcd8bSBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot: try running with -pc_ilu_nonzeros_along_diagonal");
67856beaf04SBarry Smith       }
67956beaf04SBarry Smith       fm      = row;
68056beaf04SBarry Smith       while (nnz-- > 0) {
681dbb450caSBarry Smith         idx = *xi++ + shift;
68256beaf04SBarry Smith         if (*flev + incrlev > levels) {
68356beaf04SBarry Smith           flev++;
68456beaf04SBarry Smith           continue;
68556beaf04SBarry Smith         }
68656beaf04SBarry Smith         do {
6879e25ed09SBarry Smith           m  = fm;
6889e25ed09SBarry Smith           fm = fill[m];
68956beaf04SBarry Smith         } while (fm < idx);
6909e25ed09SBarry Smith         if (fm != idx) {
69156beaf04SBarry Smith           im[idx]   = *flev + incrlev;
6929e25ed09SBarry Smith           fill[m]   = idx;
6939e25ed09SBarry Smith           fill[idx] = fm;
6949e25ed09SBarry Smith           fm        = idx;
69556beaf04SBarry Smith           nzf++;
696ecf371e4SBarry Smith         } else {
69756beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
6989e25ed09SBarry Smith         }
69956beaf04SBarry Smith         flev++;
7009e25ed09SBarry Smith       }
7019e25ed09SBarry Smith       row = fill[row];
70256beaf04SBarry Smith       nzi++;
7039e25ed09SBarry Smith     }
7049e25ed09SBarry Smith     /* copy new filled row into permanent storage */
70556beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
706d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
707ecf371e4SBarry Smith 
708ecf371e4SBarry Smith       /* estimate how much additional space we will need */
709ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
710ecf371e4SBarry Smith       /* just double the memory each time */
711ecf371e4SBarry Smith       /*  maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */
712ecf371e4SBarry Smith       int maxadd = jmax;
71356beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
714bbb6d6a8SBarry Smith       jmax += maxadd;
715ecf371e4SBarry Smith 
716ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
7170452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
718416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
7190452661fSBarry Smith       PetscFree(ajnew);
72056beaf04SBarry Smith       ajnew = xi;
7210452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
722416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
7230452661fSBarry Smith       PetscFree(ajfill);
72456beaf04SBarry Smith       ajfill = xi;
725ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
7269e25ed09SBarry Smith     }
727dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
728dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
72956beaf04SBarry Smith     dloc[prow]  = nzi;
7309e25ed09SBarry Smith     fm          = fill[n];
73156beaf04SBarry Smith     while (nzf--) {
732dbb450caSBarry Smith       *xi++   = fm - shift;
73356beaf04SBarry Smith       *flev++ = im[fm];
7349e25ed09SBarry Smith       fm      = fill[fm];
7359e25ed09SBarry Smith     }
7369e25ed09SBarry Smith   }
7370452661fSBarry Smith   PetscFree(ajfill);
738898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
739898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
7400452661fSBarry Smith   PetscFree(fill); PetscFree(im);
7419e25ed09SBarry Smith 
742f64065bbSBarry Smith   {
743464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
744981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
745f64065bbSBarry Smith              realloc,f,af);
746981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
747981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
748981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
749f64065bbSBarry Smith   }
750bbb6d6a8SBarry Smith 
7519e25ed09SBarry Smith   /* put together the new matrix */
752b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
753fa6007c9SSatish Balay   PLogObjectParent(*fact,isicol);
754416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
7550452661fSBarry Smith   PetscFree(b->imax);
756416022c9SBarry Smith   b->singlemalloc = 0;
757dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
7589e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
7590452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
7606b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a);
761416022c9SBarry Smith   b->j          = ajnew;
762416022c9SBarry Smith   b->i          = ainew;
76356beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
764416022c9SBarry Smith   b->diag       = dloc;
765416022c9SBarry Smith   b->ilen       = 0;
766416022c9SBarry Smith   b->imax       = 0;
767416022c9SBarry Smith   b->row        = isrow;
768416022c9SBarry Smith   b->col        = iscol;
76982bf6240SBarry Smith   b->icol       = isicol;
77082bf6240SBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); CHKPTRQ(b->solve_work);
771416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
77256beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
773dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
774416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
7759e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
776ae068f58SLois Curfman McInnes 
777ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
778ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
779ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
780ae068f58SLois Curfman McInnes 
7813a40ed3dSBarry Smith   PetscFunctionReturn(0);
7829e25ed09SBarry Smith }
783d5d45c9bSBarry Smith 
784d5d45c9bSBarry Smith 
785d5d45c9bSBarry Smith 
786d5d45c9bSBarry Smith 
787