xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 273d9f13de75c4ed17021f7f2c11eebb99d26f0d)
1*273d9f13SBarry Smith /*$Id: aijfact.c,v 1.156 2000/09/28 21:11:00 bsmith Exp bsmith $*/
2289bc588SBarry Smith 
370f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
490f02eecSBarry Smith #include "src/vec/vecimpl.h"
51c4feecaSSatish Balay #include "src/inline/dot.h"
63b2fbd54SBarry Smith 
75615d1e5SSatish Balay #undef __FUNC__
8b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatOrdering_Flow_SeqAIJ"
991e9ee9fSBarry Smith int MatOrdering_Flow_SeqAIJ(Mat mat,MatOrderingType type,IS *irow,IS *icol)
103b2fbd54SBarry Smith {
113a40ed3dSBarry Smith   PetscFunctionBegin;
123a40ed3dSBarry Smith 
1329bbc08cSBarry Smith   SETERRQ(PETSC_ERR_SUP,"Code not written");
14aa482453SBarry Smith #if !defined(PETSC_USE_DEBUG)
15d64ed03dSBarry Smith   PetscFunctionReturn(0);
16d64ed03dSBarry Smith #endif
173b2fbd54SBarry Smith }
183b2fbd54SBarry Smith 
1986bacbd2SBarry Smith 
20ca44d042SBarry Smith EXTERN int MatMarkDiagonal_SeqAIJ(Mat);
21ca44d042SBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat);
2286bacbd2SBarry Smith 
23ca44d042SBarry Smith EXTERN int SPARSEKIT2dperm(int*,Scalar*,int*,int*,Scalar*,int*,int*,int*,int*,int*);
24ca44d042SBarry Smith EXTERN int SPARSEKIT2ilutp(int*,Scalar*,int*,int*,int*,PetscReal*,PetscReal*,int*,Scalar*,int*,int*,int*,Scalar*,int*,int*,int*);
25ca44d042SBarry Smith EXTERN int SPARSEKIT2msrcsr(int*,Scalar*,int*,Scalar*,int*,int*,Scalar*,int*);
2686bacbd2SBarry Smith 
2786bacbd2SBarry Smith #undef __FUNC__
28b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatILUDTFactor_SeqAIJ"
2986bacbd2SBarry Smith   /* ------------------------------------------------------------
3086bacbd2SBarry Smith 
3186bacbd2SBarry Smith           This interface was contribed by Tony Caola
3286bacbd2SBarry Smith 
3386bacbd2SBarry Smith      This routine is an interface to the pivoting drop-tolerance
345bd2ddc7SBarry Smith      ILU routine written by Yousef Saad (saad@cs.umn.edu) as part of
355bd2ddc7SBarry Smith      SPARSEKIT2.
365bd2ddc7SBarry Smith 
375bd2ddc7SBarry Smith      The SPARSEKIT2 routines used here are covered by the GNU
385bd2ddc7SBarry Smith      copyright; see the file gnu in this directory.
3986bacbd2SBarry Smith 
4086bacbd2SBarry Smith      Thanks to Prof. Saad, Dr. Hysom, and Dr. Smith for their
4186bacbd2SBarry Smith      help in getting this routine ironed out.
4286bacbd2SBarry Smith 
435bd2ddc7SBarry Smith      The major drawback to this routine is that if info->fill is
445bd2ddc7SBarry Smith      not large enough it fails rather than allocating more space;
455bd2ddc7SBarry Smith      this can be fixed by hacking/improving the f2c version of
465bd2ddc7SBarry Smith      Yousef Saad's code.
4786bacbd2SBarry Smith 
485bd2ddc7SBarry Smith      ishift = 0, for indices start at 1
495bd2ddc7SBarry Smith      ishift = 1, for indices starting at 0
5086bacbd2SBarry Smith      ------------------------------------------------------------
5186bacbd2SBarry Smith   */
5286bacbd2SBarry Smith 
5386bacbd2SBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,MatILUInfo *info,IS isrow,IS iscol,Mat *fact)
5486bacbd2SBarry Smith {
5586bacbd2SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b;
5607d2056aSBarry Smith   IS         iscolf,isicol,isirow;
5786bacbd2SBarry Smith   PetscTruth reorder;
58*273d9f13SBarry Smith   int        *c,*r,*ic,ierr,i,n = A->m;
59329f5518SBarry Smith   int        *old_i = a->i,*old_j = a->j,*new_i,*old_i2 = 0,*old_j2 = 0,*new_j;
60313ae024SBarry Smith   int        *ordcol,*iwk,*iperm,*jw;
615bd2ddc7SBarry Smith   int        ishift = !a->indexshift;
62b19713cbSBarry Smith   int        jmax,lfill,job,*o_i,*o_j;
63329f5518SBarry Smith   Scalar     *old_a = a->a,*w,*new_a,*old_a2 = 0,*wk,*o_a;
64329f5518SBarry Smith   PetscReal  permtol,af;
6586bacbd2SBarry Smith 
6686bacbd2SBarry Smith   PetscFunctionBegin;
6786bacbd2SBarry Smith 
6886bacbd2SBarry Smith   if (info->dt == PETSC_DEFAULT)      info->dt      = .005;
6986bacbd2SBarry Smith   if (info->dtcount == PETSC_DEFAULT) info->dtcount = (int)(1.5*a->rmax);
7086bacbd2SBarry Smith   if (info->dtcol == PETSC_DEFAULT)   info->dtcol   = .01;
7133259db3SBarry Smith   if (info->fill == PETSC_DEFAULT)    info->fill    = ((double)(n*(info->dtcount+1)))/a->nz;
726faa4630SBarry Smith   lfill   = (int)(info->dtcount/2.0);
736faa4630SBarry Smith   jmax    = (int)(info->fill*a->nz);
7486bacbd2SBarry Smith   permtol = info->dtcol;
7586bacbd2SBarry Smith 
7686bacbd2SBarry Smith 
7786bacbd2SBarry Smith   /* ------------------------------------------------------------
7886bacbd2SBarry Smith      If reorder=.TRUE., then the original matrix has to be
7986bacbd2SBarry Smith      reordered to reflect the user selected ordering scheme, and
8086bacbd2SBarry Smith      then de-reordered so it is in it's original format.
8186bacbd2SBarry Smith      Because Saad's dperm() is NOT in place, we have to copy
8286bacbd2SBarry Smith      the original matrix and allocate more storage. . .
8386bacbd2SBarry Smith      ------------------------------------------------------------
8486bacbd2SBarry Smith   */
8586bacbd2SBarry Smith 
8686bacbd2SBarry Smith   /* set reorder to true if either isrow or iscol is not identity */
8786bacbd2SBarry Smith   ierr = ISIdentity(isrow,&reorder);CHKERRQ(ierr);
8886bacbd2SBarry Smith   if (reorder) {ierr = ISIdentity(iscol,&reorder);CHKERRQ(ierr);}
8986bacbd2SBarry Smith   reorder = PetscNot(reorder);
9086bacbd2SBarry Smith 
9186bacbd2SBarry Smith 
9286bacbd2SBarry Smith   /* storage for ilu factor */
9386bacbd2SBarry Smith   new_i = (int*)   PetscMalloc((n+1)*sizeof(int));CHKPTRQ(new_i);
9486bacbd2SBarry Smith   new_j = (int*)   PetscMalloc(jmax*sizeof(int));CHKPTRQ(new_j);
9586bacbd2SBarry Smith   new_a = (Scalar*)PetscMalloc(jmax*sizeof(Scalar));CHKPTRQ(new_a);
9686bacbd2SBarry Smith 
9786bacbd2SBarry Smith   ordcol = (int*)PetscMalloc(n*sizeof(int));CHKPTRQ(ordcol);
9886bacbd2SBarry Smith 
9986bacbd2SBarry Smith   /* ------------------------------------------------------------
10086bacbd2SBarry Smith      Make sure that everything is Fortran formatted (1-Based)
10186bacbd2SBarry Smith      ------------------------------------------------------------
10286bacbd2SBarry Smith   */
103b19713cbSBarry Smith   if (ishift) {
104b19713cbSBarry Smith     for (i=old_i[0];i<old_i[n];i++) {
105b19713cbSBarry Smith       old_j[i]++;
10686bacbd2SBarry Smith     }
107b19713cbSBarry Smith     for(i=0;i<n+1;i++) {
108b19713cbSBarry Smith       old_i[i]++;
109b19713cbSBarry Smith     };
11086bacbd2SBarry Smith   };
11186bacbd2SBarry Smith 
11286bacbd2SBarry Smith   if (reorder) {
113c0c2c81eSBarry Smith     ierr = ISGetIndices(iscol,&c);           CHKERRQ(ierr);
114c0c2c81eSBarry Smith     ierr = ISGetIndices(isrow,&r);           CHKERRQ(ierr);
115c0c2c81eSBarry Smith     for(i=0;i<n;i++) {
116c0c2c81eSBarry Smith       r[i]  = r[i]+1;
117c0c2c81eSBarry Smith       c[i]  = c[i]+1;
118c0c2c81eSBarry Smith     }
119313ae024SBarry Smith     old_i2 = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(old_i2);
120313ae024SBarry Smith     old_j2 = (int*)PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(int));CHKPTRQ(old_j2);
121313ae024SBarry Smith     old_a2 = (Scalar*)PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(Scalar));CHKPTRQ(old_a2);
1225bd2ddc7SBarry Smith     job = 3; SPARSEKIT2dperm(&n,old_a,old_j,old_i,old_a2,old_j2,old_i2,r,c,&job);
123c0c2c81eSBarry Smith     for (i=0;i<n;i++) {
124c0c2c81eSBarry Smith       r[i]  = r[i]-1;
125c0c2c81eSBarry Smith       c[i]  = c[i]-1;
126c0c2c81eSBarry Smith     }
127c0c2c81eSBarry Smith     ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr);
128c0c2c81eSBarry Smith     ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
129b19713cbSBarry Smith     o_a = old_a2;
130b19713cbSBarry Smith     o_j = old_j2;
131b19713cbSBarry Smith     o_i = old_i2;
132b19713cbSBarry Smith   } else {
133b19713cbSBarry Smith     o_a = old_a;
134b19713cbSBarry Smith     o_j = old_j;
135b19713cbSBarry Smith     o_i = old_i;
13686bacbd2SBarry Smith   }
13786bacbd2SBarry Smith 
13886bacbd2SBarry Smith   /* ------------------------------------------------------------
13986bacbd2SBarry Smith      Call Saad's ilutp() routine to generate the factorization
14086bacbd2SBarry Smith      ------------------------------------------------------------
14186bacbd2SBarry Smith   */
14286bacbd2SBarry Smith 
14386bacbd2SBarry Smith   iperm   = (int*)   PetscMalloc(2*n*sizeof(int));CHKPTRQ(iperm);
14486bacbd2SBarry Smith   jw      = (int*)   PetscMalloc(2*n*sizeof(int));CHKPTRQ(jw);
14586bacbd2SBarry Smith   w       = (Scalar*)PetscMalloc(n*sizeof(Scalar));CHKPTRQ(w);
14686bacbd2SBarry Smith 
1475bd2ddc7SBarry Smith   SPARSEKIT2ilutp(&n,o_a,o_j,o_i,&lfill,&info->dt,&permtol,&n,new_a,new_j,new_i,&jmax,w,jw,iperm,&ierr);
14886bacbd2SBarry Smith   if (ierr) {
14986bacbd2SBarry Smith     switch (ierr) {
15029bbc08cSBarry Smith       case -3: SETERRQ2(1,"ilutp(), matrix U overflows, need larger info->fill current fill %g space allocated %d",info->fill,jmax);
15129bbc08cSBarry Smith       case -2: SETERRQ2(1,"ilutp(), matrix L overflows, need larger info->fill current fill %g space allocated %d",info->fill,jmax);
15229bbc08cSBarry Smith       case -5: SETERRQ(1,"ilutp(), zero row encountered");
15329bbc08cSBarry Smith       case -1: SETERRQ(1,"ilutp(), input matrix may be wrong");
15429bbc08cSBarry Smith       case -4: SETERRQ1(1,"ilutp(), illegal info->fill value %d",jmax);
15529bbc08cSBarry Smith       default: SETERRQ1(1,"ilutp(), zero pivot detected on row %d",ierr);
15686bacbd2SBarry Smith     }
15786bacbd2SBarry Smith   }
15886bacbd2SBarry Smith 
15986bacbd2SBarry Smith   ierr = PetscFree(w);CHKERRQ(ierr);
16086bacbd2SBarry Smith   ierr = PetscFree(jw);CHKERRQ(ierr);
16186bacbd2SBarry Smith 
16286bacbd2SBarry Smith   /* ------------------------------------------------------------
16386bacbd2SBarry Smith      Saad's routine gives the result in Modified Sparse Row (msr)
16486bacbd2SBarry Smith      Convert to Compressed Sparse Row format (csr)
16586bacbd2SBarry Smith      ------------------------------------------------------------
16686bacbd2SBarry Smith   */
16786bacbd2SBarry Smith 
16886bacbd2SBarry Smith   wk  = (Scalar*)PetscMalloc(n*sizeof(Scalar));CHKPTRQ(wk);
16986bacbd2SBarry Smith   iwk = (int*)   PetscMalloc((n+1)*sizeof(int));CHKPTRQ(iwk);
17086bacbd2SBarry Smith 
1715bd2ddc7SBarry Smith   SPARSEKIT2msrcsr(&n,new_a,new_j,new_a,new_j,new_i,wk,iwk);
17286bacbd2SBarry Smith 
17386bacbd2SBarry Smith   ierr = PetscFree(iwk);CHKERRQ(ierr);
17486bacbd2SBarry Smith   ierr = PetscFree(wk);CHKERRQ(ierr);
17586bacbd2SBarry Smith 
17686bacbd2SBarry Smith   if (reorder) {
17786bacbd2SBarry Smith     ierr = PetscFree(old_a2);CHKERRQ(ierr);
17886bacbd2SBarry Smith     ierr = PetscFree(old_j2);CHKERRQ(ierr);
17986bacbd2SBarry Smith     ierr = PetscFree(old_i2);CHKERRQ(ierr);
180313ae024SBarry Smith   } else {
181b19713cbSBarry Smith     /* fix permutation of old_j that the factorization introduced */
182f170005cSBarry Smith     for (i=old_i[0]; i<old_i[n]; i++) {
183b19713cbSBarry Smith       old_j[i-1] = iperm[old_j[i-1]-1];
184b19713cbSBarry Smith     }
185b19713cbSBarry Smith   }
186b19713cbSBarry Smith 
187b801d0f9SBarry Smith   /* get rid of the shift to indices starting at 1 */
188b19713cbSBarry Smith   if (ishift) {
18986bacbd2SBarry Smith     for (i=0; i<n+1; i++) {
190b19713cbSBarry Smith       old_i[i]--;
191b19713cbSBarry Smith     }
192b19713cbSBarry Smith     for (i=old_i[0];i<old_i[n];i++) {
193b19713cbSBarry Smith       old_j[i]--;
194b19713cbSBarry Smith     }
19586bacbd2SBarry Smith   }
19686bacbd2SBarry Smith 
197b19713cbSBarry Smith   /* Make the factored matrix 0-based */
198b19713cbSBarry Smith   if (ishift) {
19986bacbd2SBarry Smith     for (i=0; i<n+1; i++) {
200b19713cbSBarry Smith       new_i[i]--;
201b19713cbSBarry Smith     }
202b19713cbSBarry Smith     for (i=new_i[0];i<new_i[n];i++) {
203b19713cbSBarry Smith       new_j[i]--;
204b19713cbSBarry Smith     }
20586bacbd2SBarry Smith   }
20686bacbd2SBarry Smith 
20786bacbd2SBarry Smith   /*-- due to the pivoting, we need to reorder iscol to correctly --*/
20886bacbd2SBarry Smith   /*-- permute the right-hand-side and solution vectors           --*/
2094c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
2104c49b128SBarry Smith   ierr = ISInvertPermutation(isrow,PETSC_DECIDE,&isirow);CHKERRQ(ierr);
211c0c2c81eSBarry Smith   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
21286bacbd2SBarry Smith   for(i=0; i<n; i++) {
21386bacbd2SBarry Smith     ordcol[i] = ic[iperm[i]-1];
21486bacbd2SBarry Smith   };
21586bacbd2SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
21607d2056aSBarry Smith   ierr = ISDestroy(isicol);CHKERRQ(ierr);
21786bacbd2SBarry Smith 
218c0c2c81eSBarry Smith   ierr = PetscFree(iperm);CHKERRQ(ierr);
219c0c2c81eSBarry Smith 
220329f5518SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,ordcol,&iscolf);CHKERRQ(ierr);
22107d2056aSBarry Smith   ierr = PetscFree(ordcol);CHKERRQ(ierr);
22286bacbd2SBarry Smith 
22386bacbd2SBarry Smith   /*----- put together the new matrix -----*/
22486bacbd2SBarry Smith 
22586bacbd2SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr);
22686bacbd2SBarry Smith   (*fact)->factor    = FACTOR_LU;
22786bacbd2SBarry Smith   (*fact)->assembled = PETSC_TRUE;
22886bacbd2SBarry Smith 
22986bacbd2SBarry Smith   b = (Mat_SeqAIJ*)(*fact)->data;
23086bacbd2SBarry Smith   ierr = PetscFree(b->imax);CHKERRQ(ierr);
23186bacbd2SBarry Smith   b->sorted        = PETSC_FALSE;
23207d2056aSBarry Smith   b->singlemalloc  = PETSC_FALSE;
233b19713cbSBarry Smith   /* the next line frees the default space generated by the MatCreate() */
23486bacbd2SBarry Smith   ierr             = PetscFree(b->a);CHKERRQ(ierr);
23586bacbd2SBarry Smith   ierr             = PetscFree(b->ilen);CHKERRQ(ierr);
23686bacbd2SBarry Smith   b->a             = new_a;
23786bacbd2SBarry Smith   b->j             = new_j;
23886bacbd2SBarry Smith   b->i             = new_i;
23986bacbd2SBarry Smith   b->ilen          = 0;
24086bacbd2SBarry Smith   b->imax          = 0;
2411f9e874aSBarry Smith   /*  I am not sure why these are the inverses of the row and column permutations; but the other way is NO GOOD */
242313ae024SBarry Smith   b->row           = isirow;
24386bacbd2SBarry Smith   b->col           = iscolf;
24486bacbd2SBarry Smith   b->solve_work    =  (Scalar*)PetscMalloc((n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
24586bacbd2SBarry Smith   b->maxnz = b->nz = new_i[n];
24686bacbd2SBarry Smith   b->indexshift    = a->indexshift;
24786bacbd2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
24886bacbd2SBarry Smith   (*fact)->info.factor_mallocs = 0;
24986bacbd2SBarry Smith 
25086bacbd2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
25186bacbd2SBarry Smith 
25286bacbd2SBarry Smith   /* check out for identical nodes. If found, use inode functions */
25386bacbd2SBarry Smith   ierr = Mat_AIJ_CheckInode(*fact);CHKERRQ(ierr);
25486bacbd2SBarry Smith 
255431e710aSBarry Smith   af = ((double)b->nz)/((double)a->nz) + .001;
256431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:Fill ratio:given %g needed %g\n",info->fill,af);
257431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
258431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
259431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:for best performance.\n");
260431e710aSBarry Smith 
26186bacbd2SBarry Smith   PetscFunctionReturn(0);
26286bacbd2SBarry Smith }
26386bacbd2SBarry Smith 
264289bc588SBarry Smith /*
265289bc588SBarry Smith     Factorization code for AIJ format.
266289bc588SBarry Smith */
2675615d1e5SSatish Balay #undef __FUNC__
2689e046878SBarry Smith #define __FUNC__ /*<a name="MatLUFactorSymbolic_SeqAIJ""></a>*/"MatLUFactorSymbolic_SeqAIJ"
2699e046878SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatLUInfo *info,Mat *B)
270289bc588SBarry Smith {
271416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b;
272289bc588SBarry Smith   IS         isicol;
273*273d9f13SBarry Smith   int        *r,*ic,ierr,i,n = A->m,*ai = a->i,*aj = a->j;
274416022c9SBarry Smith   int        *ainew,*ajnew,jmax,*fill,*ajtmp,nz,shift = a->indexshift;
27591bf9adeSBarry Smith   int        *idnew,idx,row,m,fm,nnz,nzi,realloc = 0,nzbd,*im;
2769e046878SBarry Smith   PetscReal  f;
277289bc588SBarry Smith 
2783a40ed3dSBarry Smith   PetscFunctionBegin;
279d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
280d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
28129bbc08cSBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
2823b2fbd54SBarry Smith 
2839863cb4fSBarry Smith   if (!isrow) {
2849863cb4fSBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,A->M,0,1,&isrow);CHKERRQ(ierr);
2859863cb4fSBarry Smith   }
2869863cb4fSBarry Smith   if (!iscol) {
2879863cb4fSBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,A->M,0,1,&iscol);CHKERRQ(ierr);
2889863cb4fSBarry Smith   }
2899863cb4fSBarry Smith 
2904c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
291ac121b3dSBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
292ac121b3dSBarry Smith   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
293289bc588SBarry Smith 
294289bc588SBarry Smith   /* get new row pointers */
2950452661fSBarry Smith   ainew    = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(ainew);
296dbb450caSBarry Smith   ainew[0] = -shift;
297289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
2989e046878SBarry Smith   if (info) f = info->fill; else f = 1.0;
299dbb450caSBarry Smith   jmax  = (int)(f*ai[n]+(!shift));
3000452661fSBarry Smith   ajnew = (int*)PetscMalloc((jmax)*sizeof(int));CHKPTRQ(ajnew);
301289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
3020452661fSBarry Smith   fill = (int*)PetscMalloc((2*n+1)*sizeof(int));CHKPTRQ(fill);
3032fb3ed76SBarry Smith   im   = fill + n + 1;
304289bc588SBarry Smith   /* idnew is location of diagonal in factor */
3050452661fSBarry Smith   idnew    = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idnew);
306dbb450caSBarry Smith   idnew[0] = -shift;
307289bc588SBarry Smith 
308289bc588SBarry Smith   for (i=0; i<n; i++) {
309289bc588SBarry Smith     /* first copy previous fill into linked list */
310289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
31129bbc08cSBarry Smith     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix");
312dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
313289bc588SBarry Smith     fill[n] = n;
314289bc588SBarry Smith     while (nz--) {
315289bc588SBarry Smith       fm  = n;
316dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
317289bc588SBarry Smith       do {
318289bc588SBarry Smith         m  = fm;
319289bc588SBarry Smith         fm = fill[m];
320289bc588SBarry Smith       } while (fm < idx);
321289bc588SBarry Smith       fill[m]   = idx;
322289bc588SBarry Smith       fill[idx] = fm;
323289bc588SBarry Smith     }
324289bc588SBarry Smith     row = fill[n];
325289bc588SBarry Smith     while (row < i) {
326dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
3272fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
3282fb3ed76SBarry Smith       nz    = im[row] - nzbd;
329289bc588SBarry Smith       fm    = row;
3302fb3ed76SBarry Smith       while (nz-- > 0) {
331dbb450caSBarry Smith         idx = *ajtmp++ + shift;
3322fb3ed76SBarry Smith         nzbd++;
3332fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
334289bc588SBarry Smith         do {
335289bc588SBarry Smith           m  = fm;
336289bc588SBarry Smith           fm = fill[m];
337289bc588SBarry Smith         } while (fm < idx);
338289bc588SBarry Smith         if (fm != idx) {
339289bc588SBarry Smith           fill[m]   = idx;
340289bc588SBarry Smith           fill[idx] = fm;
341289bc588SBarry Smith           fm        = idx;
342289bc588SBarry Smith           nnz++;
343289bc588SBarry Smith         }
344289bc588SBarry Smith       }
345289bc588SBarry Smith       row = fill[row];
346289bc588SBarry Smith     }
347289bc588SBarry Smith     /* copy new filled row into permanent storage */
348289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
349496e697eSBarry Smith     if (ainew[i+1] > jmax) {
350ecf371e4SBarry Smith 
351ecf371e4SBarry Smith       /* estimate how much additional space we will need */
352ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
353ecf371e4SBarry Smith       /* just double the memory each time */
354ecf371e4SBarry Smith       int maxadd = jmax;
355ecf371e4SBarry Smith       /* maxadd = (int)((f*(ai[n]+(!shift))*(n-i+5))/n); */
356bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
3572fb3ed76SBarry Smith       jmax += maxadd;
358ecf371e4SBarry Smith 
359ecf371e4SBarry Smith       /* allocate a longer ajnew */
3600452661fSBarry Smith       ajtmp = (int*)PetscMalloc(jmax*sizeof(int));CHKPTRQ(ajtmp);
361549d3d68SSatish Balay       ierr  = PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));CHKERRQ(ierr);
362606d414cSSatish Balay       ierr  = PetscFree(ajnew);CHKERRQ(ierr);
363289bc588SBarry Smith       ajnew = ajtmp;
3642fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
365289bc588SBarry Smith     }
366dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
367289bc588SBarry Smith     fm    = fill[n];
368289bc588SBarry Smith     nzi   = 0;
3692fb3ed76SBarry Smith     im[i] = nnz;
370289bc588SBarry Smith     while (nnz--) {
371289bc588SBarry Smith       if (fm < i) nzi++;
372dbb450caSBarry Smith       *ajtmp++ = fm - shift;
373289bc588SBarry Smith       fm       = fill[fm];
374289bc588SBarry Smith     }
375289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
376289bc588SBarry Smith   }
377464e8d44SSatish Balay   if (ai[n] != 0) {
378329f5518SBarry Smith     PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
379c38d4ed2SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
380981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
381981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
382981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
38305bf559cSSatish Balay   } else {
384981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
38505bf559cSSatish Balay   }
3862fb3ed76SBarry Smith 
387898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
388898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
3891987afe7SBarry Smith 
390606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
391289bc588SBarry Smith 
392289bc588SBarry Smith   /* put together the new matrix */
393b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B);CHKERRQ(ierr);
3941987afe7SBarry Smith   PLogObjectParent(*B,isicol);
395416022c9SBarry Smith   b = (Mat_SeqAIJ*)(*B)->data;
396606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
3977c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
398e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
399606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
400606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
40191bf9adeSBarry Smith   b->a          = (Scalar*)PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
402416022c9SBarry Smith   b->j          = ajnew;
403416022c9SBarry Smith   b->i          = ainew;
404416022c9SBarry Smith   b->diag       = idnew;
405416022c9SBarry Smith   b->ilen       = 0;
406416022c9SBarry Smith   b->imax       = 0;
407416022c9SBarry Smith   b->row        = isrow;
408416022c9SBarry Smith   b->col        = iscol;
409085256b3SBarry Smith   if (info) {
4101d1367b7SBarry Smith     b->lu_damp    = (PetscTruth) ((int)info->damp);
411085256b3SBarry Smith     b->lu_damping = info->damping;
412085256b3SBarry Smith   } else {
413085256b3SBarry Smith     b->lu_damp    = PETSC_FALSE;
414085256b3SBarry Smith     b->lu_damping = 0.0;
415085256b3SBarry Smith   }
416c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
417c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
41882bf6240SBarry Smith   b->icol       = isicol;
41991bf9adeSBarry Smith   b->solve_work = (Scalar*)PetscMalloc((n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
420416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
4217fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
422416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
423416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
4247fd98bd6SLois Curfman McInnes 
425329f5518SBarry Smith   (*B)->factor                 =  FACTOR_LU;
426ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
427ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
4287dda7e10SBarry Smith   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */
429703deb49SSatish Balay 
430e93ccc4dSBarry Smith   if (ai[n] != 0) {
431329f5518SBarry Smith     (*B)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
432eea2913aSSatish Balay   } else {
433eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
434eea2913aSSatish Balay   }
4353a40ed3dSBarry Smith   PetscFunctionReturn(0);
436289bc588SBarry Smith }
4370a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
438ca44d042SBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat);
43941c01911SSatish Balay 
4405615d1e5SSatish Balay #undef __FUNC__
4419e046878SBarry Smith #define __FUNC__ /*<a name="MatLUFactorNumeric_SeqAIJ"></a>*/"MatLUFactorNumeric_SeqAIJ"
442416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
443289bc588SBarry Smith {
444416022c9SBarry Smith   Mat        C = *B;
445416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ *)C->data;
44682bf6240SBarry Smith   IS         isrow = b->row,isicol = b->icol;
447*273d9f13SBarry Smith   int        *r,*ic,ierr,i,j,n = A->m,*ai = b->i,*aj = b->j;
4481987afe7SBarry Smith   int        *ajtmpold,*ajtmp,nz,row,*ics,shift = a->indexshift;
449085256b3SBarry Smith   int        *diag_offset = b->diag,diag;
450085256b3SBarry Smith   int        *pj,ndamp = 0;
451085256b3SBarry Smith   Scalar     *rtmp,*v,*pc,multiplier,*pv,*rtmps;
452085256b3SBarry Smith   PetscReal  damping = b->lu_damping;
453085256b3SBarry Smith   PetscTruth damp;
454289bc588SBarry Smith 
4553a40ed3dSBarry Smith   PetscFunctionBegin;
45682bf6240SBarry Smith 
45778b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r);CHKERRQ(ierr);
45878b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
4590452661fSBarry Smith   rtmp  = (Scalar*)PetscMalloc((n+1)*sizeof(Scalar));CHKPTRQ(rtmp);
460549d3d68SSatish Balay   ierr  = PetscMemzero(rtmp,(n+1)*sizeof(Scalar));CHKERRQ(ierr);
4610cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
462289bc588SBarry Smith 
463085256b3SBarry Smith   do {
464085256b3SBarry Smith     damp = PETSC_FALSE;
465289bc588SBarry Smith     for (i=0; i<n; i++) {
466289bc588SBarry Smith       nz    = ai[i+1] - ai[i];
467dbb450caSBarry Smith       ajtmp = aj + ai[i] + shift;
46848e94559SBarry Smith       for  (j=0; j<nz; j++) rtmps[ajtmp[j]] = 0.0;
469289bc588SBarry Smith 
470289bc588SBarry Smith       /* load in initial (unfactored row) */
471416022c9SBarry Smith       nz       = a->i[r[i]+1] - a->i[r[i]];
472416022c9SBarry Smith       ajtmpold = a->j + a->i[r[i]] + shift;
473416022c9SBarry Smith       v        = a->a + a->i[r[i]] + shift;
474085256b3SBarry Smith       for (j=0; j<nz; j++) {
475085256b3SBarry Smith         rtmp[ics[ajtmpold[j]]] = v[j];
476085256b3SBarry Smith         if (ajtmpold[j] == r[i]) { /* damp the diagonal of the matrix */
477085256b3SBarry Smith           rtmp[ics[ajtmpold[j]]] += damping;
478085256b3SBarry Smith         }
479085256b3SBarry Smith       }
480289bc588SBarry Smith 
481dbb450caSBarry Smith       row = *ajtmp++ + shift;
482f2582111SSatish Balay       while  (row < i) {
4838c37ef55SBarry Smith         pc = rtmp + row;
4848c37ef55SBarry Smith         if (*pc != 0.0) {
4851987afe7SBarry Smith           pv         = b->a + diag_offset[row] + shift;
4861987afe7SBarry Smith           pj         = b->j + diag_offset[row] + (!shift);
48735aab85fSBarry Smith           multiplier = *pc / *pv++;
4888c37ef55SBarry Smith           *pc        = multiplier;
489f2582111SSatish Balay           nz         = ai[row+1] - diag_offset[row] - 1;
490f2582111SSatish Balay           for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
491f2582111SSatish Balay           PLogFlops(2*nz);
492289bc588SBarry Smith         }
493dbb450caSBarry Smith         row = *ajtmp++ + shift;
494289bc588SBarry Smith       }
495416022c9SBarry Smith       /* finished row so stick it into b->a */
496416022c9SBarry Smith       pv = b->a + ai[i] + shift;
497416022c9SBarry Smith       pj = b->j + ai[i] + shift;
4988c37ef55SBarry Smith       nz = ai[i+1] - ai[i];
499416022c9SBarry Smith       for (j=0; j<nz; j++) {pv[j] = rtmps[pj[j]];}
50035aab85fSBarry Smith       diag = diag_offset[i] - ai[i];
501085256b3SBarry Smith       if (PetscAbsScalar(pv[diag]) < 1.e-12) {
502085256b3SBarry Smith         if (b->lu_damp) {
503085256b3SBarry Smith           damp = PETSC_TRUE;
504085256b3SBarry Smith           if (damping) damping *= 2.0;
505085256b3SBarry Smith           else damping = 1.e-12;
506085256b3SBarry Smith           ndamp++;
507085256b3SBarry Smith           break;
508085256b3SBarry Smith         } else {
50929bbc08cSBarry Smith           SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot row %d",i);
510d708749eSBarry Smith         }
51135aab85fSBarry Smith       }
51235aab85fSBarry Smith     }
513085256b3SBarry Smith   } while (damp);
5140f11f9aeSSatish Balay 
51535aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
51635aab85fSBarry Smith   for (i=0; i<n; i++) {
51735aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
51835aab85fSBarry Smith   }
51935aab85fSBarry Smith 
520606d414cSSatish Balay   ierr = PetscFree(rtmp);CHKERRQ(ierr);
52178b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
52278b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
523416022c9SBarry Smith   C->factor = FACTOR_LU;
52441c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C);CHKERRQ(ierr);
5257dda7e10SBarry Smith   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */
526c456f294SBarry Smith   C->assembled = PETSC_TRUE;
527*273d9f13SBarry Smith   PLogFlops(C->n);
528a2e34c3dSBarry Smith   if (ndamp || b->lu_damping) {
529085256b3SBarry Smith     PLogInfo(0,"MatLUFactorNumerical_SeqAIJ: number of damping tries %d damping value %g\n",ndamp,damping);
530085256b3SBarry Smith   }
5313a40ed3dSBarry Smith   PetscFunctionReturn(0);
532289bc588SBarry Smith }
5330a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5345615d1e5SSatish Balay #undef __FUNC__
5359e046878SBarry Smith #define __FUNC__ /*<a name="MatLUFactor_SeqAIJ"></a>*/"MatLUFactor_SeqAIJ"
5369e046878SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,MatLUInfo *info)
537da3a660dSBarry Smith {
538*273d9f13SBarry Smith   int ierr;
539416022c9SBarry Smith   Mat C;
540416022c9SBarry Smith 
5413a40ed3dSBarry Smith   PetscFunctionBegin;
5429e046878SBarry Smith   ierr = MatLUFactorSymbolic(A,row,col,info,&C);CHKERRQ(ierr);
543f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C);CHKERRQ(ierr);
544*273d9f13SBarry Smith   ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
5453a40ed3dSBarry Smith   PetscFunctionReturn(0);
546da3a660dSBarry Smith }
5470a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5485615d1e5SSatish Balay #undef __FUNC__
549b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSolve_SeqAIJ"
550416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx)
5518c37ef55SBarry Smith {
552416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
553416022c9SBarry Smith   IS         iscol = a->col,isrow = a->row;
554*273d9f13SBarry Smith   int        *r,*c,ierr,i, n = A->m,*vi,*ai = a->i,*aj = a->j;
5553b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
556416022c9SBarry Smith   Scalar     *x,*b,*tmp,*tmps,*aa = a->a,sum,*v;
5578c37ef55SBarry Smith 
5583a40ed3dSBarry Smith   PetscFunctionBegin;
5593a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
56091bf9adeSBarry Smith 
561e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
562e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
563416022c9SBarry Smith   tmp  = a->solve_work;
5648c37ef55SBarry Smith 
5653b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
5663b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
5678c37ef55SBarry Smith 
5688c37ef55SBarry Smith   /* forward solve the lower triangular */
5698c37ef55SBarry Smith   tmp[0] = b[*r++];
5700cf60383SBarry Smith   tmps   = tmp + shift;
5718c37ef55SBarry Smith   for (i=1; i<n; i++) {
572dbb450caSBarry Smith     v   = aa + ai[i] + shift;
573dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
574416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
5758c37ef55SBarry Smith     sum = b[*r++];
5760cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
5778c37ef55SBarry Smith     tmp[i] = sum;
5788c37ef55SBarry Smith   }
5798c37ef55SBarry Smith 
5808c37ef55SBarry Smith   /* backward solve the upper triangular */
5818c37ef55SBarry Smith   for (i=n-1; i>=0; i--){
582416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
583416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
584416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
5858c37ef55SBarry Smith     sum = tmp[i];
5860cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
587416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
5888c37ef55SBarry Smith   }
5898c37ef55SBarry Smith 
5903b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
5913b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
592cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
593cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
594*273d9f13SBarry Smith   PLogFlops(2*a->nz - A->n);
5953a40ed3dSBarry Smith   PetscFunctionReturn(0);
5968c37ef55SBarry Smith }
597026e39d0SSatish Balay 
598930ae53cSBarry Smith /* ----------------------------------------------------------- */
599930ae53cSBarry Smith #undef __FUNC__
600b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSolve_SeqAIJ_NaturalOrdering"
601930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx)
602930ae53cSBarry Smith {
603930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
604*273d9f13SBarry Smith   int        n = A->m,*ai = a->i,*aj = a->j,*adiag = a->diag,ierr;
605d85afc42SSatish Balay   Scalar     *x,*b,*aa = a->a,sum;
606aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
607d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
608d85afc42SSatish Balay   Scalar     *v;
609d85afc42SSatish Balay #endif
610930ae53cSBarry Smith 
6113a40ed3dSBarry Smith   PetscFunctionBegin;
6123a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
613930ae53cSBarry Smith   if (a->indexshift) {
6143a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
6153a40ed3dSBarry Smith      PetscFunctionReturn(0);
616930ae53cSBarry Smith   }
617930ae53cSBarry Smith 
618e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
619e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
620930ae53cSBarry Smith 
621aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
6221c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
6231c4feecaSSatish Balay #else
624930ae53cSBarry Smith   /* forward solve the lower triangular */
625930ae53cSBarry Smith   x[0] = b[0];
626930ae53cSBarry Smith   for (i=1; i<n; i++) {
627930ae53cSBarry Smith     ai_i = ai[i];
628930ae53cSBarry Smith     v    = aa + ai_i;
629930ae53cSBarry Smith     vi   = aj + ai_i;
630930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
631930ae53cSBarry Smith     sum  = b[i];
632930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
633930ae53cSBarry Smith     x[i] = sum;
634930ae53cSBarry Smith   }
635930ae53cSBarry Smith 
636930ae53cSBarry Smith   /* backward solve the upper triangular */
637930ae53cSBarry Smith   for (i=n-1; i>=0; i--){
638930ae53cSBarry Smith     adiag_i = adiag[i];
639d708749eSBarry Smith     v       = aa + adiag_i + 1;
640d708749eSBarry Smith     vi      = aj + adiag_i + 1;
641930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
642930ae53cSBarry Smith     sum     = x[i];
643930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
644930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
645930ae53cSBarry Smith   }
6461c4feecaSSatish Balay #endif
647*273d9f13SBarry Smith   PLogFlops(2*a->nz - A->n);
648cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
649cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
6503a40ed3dSBarry Smith   PetscFunctionReturn(0);
651930ae53cSBarry Smith }
652930ae53cSBarry Smith 
6535615d1e5SSatish Balay #undef __FUNC__
654b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSolveAdd_SeqAIJ"
655416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx)
656da3a660dSBarry Smith {
657416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
658416022c9SBarry Smith   IS         iscol = a->col,isrow = a->row;
659*273d9f13SBarry Smith   int        *r,*c,ierr,i, n = A->m,*vi,*ai = a->i,*aj = a->j;
6603b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
661416022c9SBarry Smith   Scalar     *x,*b,*tmp,*aa = a->a,sum,*v;
662da3a660dSBarry Smith 
6633a40ed3dSBarry Smith   PetscFunctionBegin;
66478b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);}
665da3a660dSBarry Smith 
666e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
667e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
668416022c9SBarry Smith   tmp  = a->solve_work;
669da3a660dSBarry Smith 
6703b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
6713b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
672da3a660dSBarry Smith 
673da3a660dSBarry Smith   /* forward solve the lower triangular */
674da3a660dSBarry Smith   tmp[0] = b[*r++];
675da3a660dSBarry Smith   for (i=1; i<n; i++) {
676dbb450caSBarry Smith     v   = aa + ai[i] + shift;
677dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
678416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
679da3a660dSBarry Smith     sum = b[*r++];
680dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
681da3a660dSBarry Smith     tmp[i] = sum;
682da3a660dSBarry Smith   }
683da3a660dSBarry Smith 
684da3a660dSBarry Smith   /* backward solve the upper triangular */
685da3a660dSBarry Smith   for (i=n-1; i>=0; i--){
686416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
687416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
688416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
689da3a660dSBarry Smith     sum = tmp[i];
690dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
691416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
692da3a660dSBarry Smith     x[*c--] += tmp[i];
693da3a660dSBarry Smith   }
694da3a660dSBarry Smith 
6953b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
6963b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
697cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
698cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
699416022c9SBarry Smith   PLogFlops(2*a->nz);
700898183ecSLois Curfman McInnes 
7013a40ed3dSBarry Smith   PetscFunctionReturn(0);
702da3a660dSBarry Smith }
703da3a660dSBarry Smith /* -------------------------------------------------------------------*/
7045615d1e5SSatish Balay #undef __FUNC__
705b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSolveTranspose_SeqAIJ"
7067c922b88SBarry Smith int MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx)
707da3a660dSBarry Smith {
708416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7092235e667SBarry Smith   IS         iscol = a->col,isrow = a->row;
710*273d9f13SBarry Smith   int        *r,*c,ierr,i,n = A->m,*vi,*ai = a->i,*aj = a->j;
711f1af5d2fSBarry Smith   int        nz,shift = a->indexshift,*rout,*cout,*diag = a->diag;
712f1af5d2fSBarry Smith   Scalar     *x,*b,*tmp,*aa = a->a,*v,s1;
713da3a660dSBarry Smith 
7143a40ed3dSBarry Smith   PetscFunctionBegin;
715e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
716e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
717416022c9SBarry Smith   tmp  = a->solve_work;
718da3a660dSBarry Smith 
7192235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7202235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
721da3a660dSBarry Smith 
722da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
7232235e667SBarry Smith   for (i=0; i<n; i++) tmp[i] = b[c[i]];
724da3a660dSBarry Smith 
725da3a660dSBarry Smith   /* forward solve the U^T */
726da3a660dSBarry Smith   for (i=0; i<n; i++) {
727f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
728f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
729f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
730c38d4ed2SBarry Smith     s1  = tmp[i];
731c38d4ed2SBarry Smith     s1 *= *(v++);  /* multiply by inverse of diagonal entry */
732da3a660dSBarry Smith     while (nz--) {
733f1af5d2fSBarry Smith       tmp[*vi++ + shift] -= (*v++)*s1;
734da3a660dSBarry Smith     }
735c38d4ed2SBarry Smith     tmp[i] = s1;
736da3a660dSBarry Smith   }
737da3a660dSBarry Smith 
738da3a660dSBarry Smith   /* backward solve the L^T */
739da3a660dSBarry Smith   for (i=n-1; i>=0; i--){
740f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
741f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
742f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
743f1af5d2fSBarry Smith     s1  = tmp[i];
744da3a660dSBarry Smith     while (nz--) {
745f1af5d2fSBarry Smith       tmp[*vi-- + shift] -= (*v--)*s1;
746da3a660dSBarry Smith     }
747da3a660dSBarry Smith   }
748da3a660dSBarry Smith 
749da3a660dSBarry Smith   /* copy tmp into x according to permutation */
750da3a660dSBarry Smith   for (i=0; i<n; i++) x[r[i]] = tmp[i];
751da3a660dSBarry Smith 
7522235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
7532235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
754cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
755cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
756da3a660dSBarry Smith 
757*273d9f13SBarry Smith   PLogFlops(2*a->nz-A->n);
7583a40ed3dSBarry Smith   PetscFunctionReturn(0);
759da3a660dSBarry Smith }
760da3a660dSBarry Smith 
7615615d1e5SSatish Balay #undef __FUNC__
762b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSolveTransposeAdd_SeqAIJ"
7637c922b88SBarry Smith int MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx)
764da3a660dSBarry Smith {
765416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7662235e667SBarry Smith   IS         iscol = a->col,isrow = a->row;
767*273d9f13SBarry Smith   int        *r,*c,ierr,i,n = A->m,*vi,*ai = a->i,*aj = a->j;
768f1af5d2fSBarry Smith   int        nz,shift = a->indexshift,*rout,*cout,*diag = a->diag;
769416022c9SBarry Smith   Scalar     *x,*b,*tmp,*aa = a->a,*v;
7706abc6512SBarry Smith 
7713a40ed3dSBarry Smith   PetscFunctionBegin;
7726abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
7736abc6512SBarry Smith 
774e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
775e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
776416022c9SBarry Smith   tmp = a->solve_work;
7776abc6512SBarry Smith 
7782235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7792235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
7806abc6512SBarry Smith 
7816abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
7822235e667SBarry Smith   for (i=0; i<n; i++) tmp[i] = b[c[i]];
7836abc6512SBarry Smith 
7846abc6512SBarry Smith   /* forward solve the U^T */
7856abc6512SBarry Smith   for (i=0; i<n; i++) {
786f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
787f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
788f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
7896abc6512SBarry Smith     tmp[i] *= *v++;
7906abc6512SBarry Smith     while (nz--) {
791dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
7926abc6512SBarry Smith     }
7936abc6512SBarry Smith   }
7946abc6512SBarry Smith 
7956abc6512SBarry Smith   /* backward solve the L^T */
7966abc6512SBarry Smith   for (i=n-1; i>=0; i--){
797f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
798f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
799f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
8006abc6512SBarry Smith     while (nz--) {
801dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
8026abc6512SBarry Smith     }
8036abc6512SBarry Smith   }
8046abc6512SBarry Smith 
8056abc6512SBarry Smith   /* copy tmp into x according to permutation */
8066abc6512SBarry Smith   for (i=0; i<n; i++) x[r[i]] += tmp[i];
8076abc6512SBarry Smith 
8082235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
8092235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
810cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
811cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8126abc6512SBarry Smith 
813416022c9SBarry Smith   PLogFlops(2*a->nz);
8143a40ed3dSBarry Smith   PetscFunctionReturn(0);
815da3a660dSBarry Smith }
8169e25ed09SBarry Smith /* ----------------------------------------------------------------*/
817ca44d042SBarry Smith EXTERN int MatMissingDiagonal_SeqAIJ(Mat);
81808480c60SBarry Smith 
8195615d1e5SSatish Balay #undef __FUNC__
820b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatILUFactorSymbolic_SeqAIJ"
8216cf997b0SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatILUInfo *info,Mat *fact)
8229e25ed09SBarry Smith {
823416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b;
8249e25ed09SBarry Smith   IS         isicol;
825*273d9f13SBarry Smith   int        *r,*ic,ierr,prow,n = A->m,*ai = a->i,*aj = a->j;
82656beaf04SBarry Smith   int        *ainew,*ajnew,jmax,*fill,*xi,nz,*im,*ajfill,*flev;
827335d9088SBarry Smith   int        *dloc,idx,row,m,fm,nzf,nzi,len, realloc = 0,dcount = 0;
8286cf997b0SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift,levels,diagonal_fill;
82977c4ece6SBarry Smith   PetscTruth col_identity,row_identity;
830329f5518SBarry Smith   PetscReal  f;
8319e25ed09SBarry Smith 
8323a40ed3dSBarry Smith   PetscFunctionBegin;
8336cf997b0SBarry Smith   if (info) {
8346cf997b0SBarry Smith     f             = info->fill;
8350cd17293SBarry Smith     levels        = (int)info->levels;
8360cd17293SBarry Smith     diagonal_fill = (int)info->diagonal_fill;
8376cf997b0SBarry Smith   } else {
8386cf997b0SBarry Smith     f             = 1.0;
8396cf997b0SBarry Smith     levels        = 0;
8406cf997b0SBarry Smith     diagonal_fill = 0;
8416cf997b0SBarry Smith   }
8424da77479SBarry Smith   if (!isrow) {
8434da77479SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,A->M,0,1,&isrow);CHKERRQ(ierr);
8444da77479SBarry Smith   }
8454da77479SBarry Smith   if (!iscol) {
8464da77479SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,A->M,0,1,&iscol);CHKERRQ(ierr);
8474da77479SBarry Smith   }
8484da77479SBarry Smith 
8494c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
85082bf6240SBarry Smith 
85108480c60SBarry Smith   /* special case that simply copies fill pattern */
852be0abb6dSBarry Smith   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
853be0abb6dSBarry Smith   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
85486bacbd2SBarry Smith   if (!levels && row_identity && col_identity) {
8552e8a6d31SBarry Smith     ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact);CHKERRQ(ierr);
85608480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
85708480c60SBarry Smith     b               = (Mat_SeqAIJ*)(*fact)->data;
85808480c60SBarry Smith     if (!b->diag) {
8597c922b88SBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
86008480c60SBarry Smith     }
8617c922b88SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
86208480c60SBarry Smith     b->row              = isrow;
86308480c60SBarry Smith     b->col              = iscol;
86482bf6240SBarry Smith     b->icol             = isicol;
865a2e34c3dSBarry Smith     if (info) {
8661d1367b7SBarry Smith       b->lu_damp    = (PetscTruth)((int)info->damp);
867a2e34c3dSBarry Smith       b->lu_damping = info->damping;
868a2e34c3dSBarry Smith     } else {
869a2e34c3dSBarry Smith       b->lu_damp    = PETSC_FALSE;
870a2e34c3dSBarry Smith       b->lu_damping = 0.0;
871a2e34c3dSBarry Smith     }
872*273d9f13SBarry Smith     b->solve_work       = (Scalar*)PetscMalloc(((*fact)->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
873f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
874c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
875c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
8763a40ed3dSBarry Smith     PetscFunctionReturn(0);
87708480c60SBarry Smith   }
87808480c60SBarry Smith 
879898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
880898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
8819e25ed09SBarry Smith 
8829e25ed09SBarry Smith   /* get new row pointers */
8830452661fSBarry Smith   ainew = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(ainew);
884dbb450caSBarry Smith   ainew[0] = -shift;
8859e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
886dbb450caSBarry Smith   jmax = (int)(f*(ai[n]+!shift));
8870452661fSBarry Smith   ajnew = (int*)PetscMalloc((jmax)*sizeof(int));CHKPTRQ(ajnew);
8889e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
8890452661fSBarry Smith   ajfill = (int*)PetscMalloc((jmax)*sizeof(int));CHKPTRQ(ajfill);
8909e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
8910452661fSBarry Smith   fill = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(fill);
89256beaf04SBarry Smith   /* im is level for each filled value */
8930452661fSBarry Smith   im = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(im);
89456beaf04SBarry Smith   /* dloc is location of diagonal in factor */
8950452661fSBarry Smith   dloc = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(dloc);
89656beaf04SBarry Smith   dloc[0]  = 0;
89756beaf04SBarry Smith   for (prow=0; prow<n; prow++) {
8986cf997b0SBarry Smith 
8996cf997b0SBarry Smith     /* copy current row into linked list */
90056beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
90129bbc08cSBarry Smith     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix");
902dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
9039e25ed09SBarry Smith     fill[n]    = n;
904435faa5fSBarry Smith     fill[prow] = -1; /* marker to indicate if diagonal exists */
9059e25ed09SBarry Smith     while (nz--) {
9069e25ed09SBarry Smith       fm  = n;
907dbb450caSBarry Smith       idx = ic[*xi++ + shift];
9089e25ed09SBarry Smith       do {
9099e25ed09SBarry Smith         m  = fm;
9109e25ed09SBarry Smith         fm = fill[m];
9119e25ed09SBarry Smith       } while (fm < idx);
9129e25ed09SBarry Smith       fill[m]   = idx;
9139e25ed09SBarry Smith       fill[idx] = fm;
91456beaf04SBarry Smith       im[idx]   = 0;
9159e25ed09SBarry Smith     }
9166cf997b0SBarry Smith 
9176cf997b0SBarry Smith     /* make sure diagonal entry is included */
918435faa5fSBarry Smith     if (diagonal_fill && fill[prow] == -1) {
9196cf997b0SBarry Smith       fm = n;
920435faa5fSBarry Smith       while (fill[fm] < prow) fm = fill[fm];
921435faa5fSBarry Smith       fill[prow] = fill[fm]; /* insert diagonal into linked list */
922435faa5fSBarry Smith       fill[fm]   = prow;
9236cf997b0SBarry Smith       im[prow]   = 0;
9246cf997b0SBarry Smith       nzf++;
925335d9088SBarry Smith       dcount++;
9266cf997b0SBarry Smith     }
9276cf997b0SBarry Smith 
92856beaf04SBarry Smith     nzi = 0;
9299e25ed09SBarry Smith     row = fill[n];
93056beaf04SBarry Smith     while (row < prow) {
93156beaf04SBarry Smith       incrlev = im[row] + 1;
93256beaf04SBarry Smith       nz      = dloc[row];
9336cf997b0SBarry Smith       xi      = ajnew  + ainew[row] + shift + nz + 1;
934dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
935416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
93656beaf04SBarry Smith       fm      = row;
93756beaf04SBarry Smith       while (nnz-- > 0) {
938dbb450caSBarry Smith         idx = *xi++ + shift;
93956beaf04SBarry Smith         if (*flev + incrlev > levels) {
94056beaf04SBarry Smith           flev++;
94156beaf04SBarry Smith           continue;
94256beaf04SBarry Smith         }
94356beaf04SBarry Smith         do {
9449e25ed09SBarry Smith           m  = fm;
9459e25ed09SBarry Smith           fm = fill[m];
94656beaf04SBarry Smith         } while (fm < idx);
9479e25ed09SBarry Smith         if (fm != idx) {
94856beaf04SBarry Smith           im[idx]   = *flev + incrlev;
9499e25ed09SBarry Smith           fill[m]   = idx;
9509e25ed09SBarry Smith           fill[idx] = fm;
9519e25ed09SBarry Smith           fm        = idx;
95256beaf04SBarry Smith           nzf++;
953ecf371e4SBarry Smith         } else {
95456beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
9559e25ed09SBarry Smith         }
95656beaf04SBarry Smith         flev++;
9579e25ed09SBarry Smith       }
9589e25ed09SBarry Smith       row = fill[row];
95956beaf04SBarry Smith       nzi++;
9609e25ed09SBarry Smith     }
9619e25ed09SBarry Smith     /* copy new filled row into permanent storage */
96256beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
963d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
964ecf371e4SBarry Smith 
965ecf371e4SBarry Smith       /* estimate how much additional space we will need */
966ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
967ecf371e4SBarry Smith       /* just double the memory each time */
968ecf371e4SBarry Smith       /*  maxadd = (int)((f*(ai[n]+!shift)*(n-prow+5))/n); */
969ecf371e4SBarry Smith       int maxadd = jmax;
97056beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
971bbb6d6a8SBarry Smith       jmax += maxadd;
972ecf371e4SBarry Smith 
973ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
9740452661fSBarry Smith       xi     = (int*)PetscMalloc(jmax*sizeof(int));CHKPTRQ(xi);
975549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
976606d414cSSatish Balay       ierr = PetscFree(ajnew);CHKERRQ(ierr);
97756beaf04SBarry Smith       ajnew  = xi;
9780452661fSBarry Smith       xi     = (int*)PetscMalloc(jmax*sizeof(int));CHKPTRQ(xi);
979549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
980606d414cSSatish Balay       ierr = PetscFree(ajfill);CHKERRQ(ierr);
98156beaf04SBarry Smith       ajfill = xi;
982ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
9839e25ed09SBarry Smith     }
984dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
985dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
98656beaf04SBarry Smith     dloc[prow]  = nzi;
9879e25ed09SBarry Smith     fm          = fill[n];
98856beaf04SBarry Smith     while (nzf--) {
989dbb450caSBarry Smith       *xi++   = fm - shift;
99056beaf04SBarry Smith       *flev++ = im[fm];
9919e25ed09SBarry Smith       fm      = fill[fm];
9929e25ed09SBarry Smith     }
9936cf997b0SBarry Smith     /* make sure row has diagonal entry */
9946cf997b0SBarry Smith     if (ajnew[ainew[prow]+shift+dloc[prow]]+shift != prow) {
99529bbc08cSBarry Smith       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %d has missing diagonal in factored matrix\n\
9966cf997b0SBarry Smith     try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow);
9976cf997b0SBarry Smith     }
9989e25ed09SBarry Smith   }
999606d414cSSatish Balay   ierr = PetscFree(ajfill);CHKERRQ(ierr);
1000898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
1001898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
1002606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
1003606d414cSSatish Balay   ierr = PetscFree(im);CHKERRQ(ierr);
10049e25ed09SBarry Smith 
1005f64065bbSBarry Smith   {
1006329f5518SBarry Smith     PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
1007c38d4ed2SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
1008981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
1009981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
1010981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
1011335d9088SBarry Smith     if (diagonal_fill) {
1012335d9088SBarry Smith       PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replace %d missing diagonals",dcount);
1013335d9088SBarry Smith     }
1014f64065bbSBarry Smith   }
1015bbb6d6a8SBarry Smith 
10169e25ed09SBarry Smith   /* put together the new matrix */
1017b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr);
1018fa6007c9SSatish Balay   PLogObjectParent(*fact,isicol);
1019416022c9SBarry Smith   b = (Mat_SeqAIJ*)(*fact)->data;
1020606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
10217c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
1022dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
10239e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
1024606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
1025606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
10266b96ef82SSatish Balay   b->a          = (Scalar*)PetscMalloc(len+1);CHKPTRQ(b->a);
1027416022c9SBarry Smith   b->j          = ajnew;
1028416022c9SBarry Smith   b->i          = ainew;
102956beaf04SBarry Smith   for (i=0; i<n; i++) dloc[i] += ainew[i];
1030416022c9SBarry Smith   b->diag       = dloc;
1031416022c9SBarry Smith   b->ilen       = 0;
1032416022c9SBarry Smith   b->imax       = 0;
1033416022c9SBarry Smith   b->row        = isrow;
1034416022c9SBarry Smith   b->col        = iscol;
1035c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1036c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
103782bf6240SBarry Smith   b->icol       = isicol;
103882bf6240SBarry Smith   b->solve_work = (Scalar*)PetscMalloc((n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
1039416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
104056beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
1041dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
1042416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
1043a2e34c3dSBarry Smith   if (info) {
10441d1367b7SBarry Smith     b->lu_damp    = (PetscTruth)((int)info->damp);
1045a2e34c3dSBarry Smith     b->lu_damping = info->damping;
1046a2e34c3dSBarry Smith   } else {
1047a2e34c3dSBarry Smith     b->lu_damp    = PETSC_FALSE;
1048a2e34c3dSBarry Smith     b->lu_damping = 0.0;
1049a2e34c3dSBarry Smith   }
10509e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
1051ae068f58SLois Curfman McInnes 
1052ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
1053ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
1054329f5518SBarry Smith   (*fact)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[prow]);
1055329f5518SBarry Smith   (*fact)->factor                 =  FACTOR_LU;
1056ae068f58SLois Curfman McInnes 
10573a40ed3dSBarry Smith   PetscFunctionReturn(0);
10589e25ed09SBarry Smith }
1059d5d45c9bSBarry Smith 
1060d5d45c9bSBarry Smith 
1061d5d45c9bSBarry Smith 
1062d5d45c9bSBarry Smith 
1063