xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision d3d3201969bfb173a03363e1420c4b2880581fed)
173f4d377SMatthew Knepley /*$Id: aijfact.c,v 1.167 2001/09/11 16:32:26 bsmith Exp $*/
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 
74a2ae208SSatish Balay #undef __FUNCT__
84a2ae208SSatish Balay #define __FUNCT__ "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);
213a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
2286bacbd2SBarry Smith 
2387828ca2SBarry Smith EXTERN int SPARSEKIT2dperm(int*,PetscScalar*,int*,int*,PetscScalar*,int*,int*,int*,int*,int*);
2487828ca2SBarry Smith EXTERN int SPARSEKIT2ilutp(int*,PetscScalar*,int*,int*,int*,PetscReal,PetscReal*,int*,PetscScalar*,int*,int*,int*,PetscScalar*,int*,int*,int*);
2587828ca2SBarry Smith EXTERN int SPARSEKIT2msrcsr(int*,PetscScalar*,int*,PetscScalar*,int*,int*,PetscScalar*,int*);
2686bacbd2SBarry Smith 
274a2ae208SSatish Balay #undef __FUNCT__
284a2ae208SSatish Balay #define __FUNCT__ "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;
58273d9f13SBarry 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;
6387828ca2SBarry Smith   PetscScalar  *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 */
93b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&new_i);CHKERRQ(ierr);
94b0a32e0cSBarry Smith   ierr = PetscMalloc(jmax*sizeof(int),&new_j);CHKERRQ(ierr);
9587828ca2SBarry Smith   ierr = PetscMalloc(jmax*sizeof(PetscScalar),&new_a);CHKERRQ(ierr);
96b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&ordcol);CHKERRQ(ierr);
9786bacbd2SBarry Smith 
9886bacbd2SBarry Smith   /* ------------------------------------------------------------
9986bacbd2SBarry Smith      Make sure that everything is Fortran formatted (1-Based)
10086bacbd2SBarry Smith      ------------------------------------------------------------
10186bacbd2SBarry Smith   */
102b19713cbSBarry Smith   if (ishift) {
103b19713cbSBarry Smith     for (i=old_i[0];i<old_i[n];i++) {
104b19713cbSBarry Smith       old_j[i]++;
10586bacbd2SBarry Smith     }
106b19713cbSBarry Smith     for(i=0;i<n+1;i++) {
107b19713cbSBarry Smith       old_i[i]++;
108b19713cbSBarry Smith     };
10986bacbd2SBarry Smith   };
11086bacbd2SBarry Smith 
11186bacbd2SBarry Smith   if (reorder) {
112c0c2c81eSBarry Smith     ierr = ISGetIndices(iscol,&c);           CHKERRQ(ierr);
113c0c2c81eSBarry Smith     ierr = ISGetIndices(isrow,&r);           CHKERRQ(ierr);
114c0c2c81eSBarry Smith     for(i=0;i<n;i++) {
115c0c2c81eSBarry Smith       r[i]  = r[i]+1;
116c0c2c81eSBarry Smith       c[i]  = c[i]+1;
117c0c2c81eSBarry Smith     }
118b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&old_i2);CHKERRQ(ierr);
119b0a32e0cSBarry Smith     ierr = PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(int),&old_j2);CHKERRQ(ierr);
12087828ca2SBarry Smith     ierr = PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(PetscScalar),&old_a2);CHKERRQ(ierr);
1215bd2ddc7SBarry Smith     job  = 3; SPARSEKIT2dperm(&n,old_a,old_j,old_i,old_a2,old_j2,old_i2,r,c,&job);
122c0c2c81eSBarry Smith     for (i=0;i<n;i++) {
123c0c2c81eSBarry Smith       r[i]  = r[i]-1;
124c0c2c81eSBarry Smith       c[i]  = c[i]-1;
125c0c2c81eSBarry Smith     }
126c0c2c81eSBarry Smith     ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr);
127c0c2c81eSBarry Smith     ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
128b19713cbSBarry Smith     o_a = old_a2;
129b19713cbSBarry Smith     o_j = old_j2;
130b19713cbSBarry Smith     o_i = old_i2;
131b19713cbSBarry Smith   } else {
132b19713cbSBarry Smith     o_a = old_a;
133b19713cbSBarry Smith     o_j = old_j;
134b19713cbSBarry Smith     o_i = old_i;
13586bacbd2SBarry Smith   }
13686bacbd2SBarry Smith 
13786bacbd2SBarry Smith   /* ------------------------------------------------------------
13886bacbd2SBarry Smith      Call Saad's ilutp() routine to generate the factorization
13986bacbd2SBarry Smith      ------------------------------------------------------------
14086bacbd2SBarry Smith   */
14186bacbd2SBarry Smith 
142b0a32e0cSBarry Smith   ierr = PetscMalloc(2*n*sizeof(int),&iperm);CHKERRQ(ierr);
143b0a32e0cSBarry Smith   ierr = PetscMalloc(2*n*sizeof(int),&jw);CHKERRQ(ierr);
14487828ca2SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscScalar),&w);CHKERRQ(ierr);
14586bacbd2SBarry Smith 
14687828ca2SBarry Smith   SPARSEKIT2ilutp(&n,o_a,o_j,o_i,&lfill,(PetscReal)info->dt,&permtol,&n,new_a,new_j,new_i,&jmax,w,jw,iperm,&ierr);
14786bacbd2SBarry Smith   if (ierr) {
14886bacbd2SBarry Smith     switch (ierr) {
14929bbc08cSBarry Smith       case -3: SETERRQ2(1,"ilutp(), matrix U overflows, need larger info->fill current fill %g space allocated %d",info->fill,jmax);
15029bbc08cSBarry Smith       case -2: SETERRQ2(1,"ilutp(), matrix L overflows, need larger info->fill current fill %g space allocated %d",info->fill,jmax);
15129bbc08cSBarry Smith       case -5: SETERRQ(1,"ilutp(), zero row encountered");
15229bbc08cSBarry Smith       case -1: SETERRQ(1,"ilutp(), input matrix may be wrong");
15329bbc08cSBarry Smith       case -4: SETERRQ1(1,"ilutp(), illegal info->fill value %d",jmax);
15429bbc08cSBarry Smith       default: SETERRQ1(1,"ilutp(), zero pivot detected on row %d",ierr);
15586bacbd2SBarry Smith     }
15686bacbd2SBarry Smith   }
15786bacbd2SBarry Smith 
15886bacbd2SBarry Smith   ierr = PetscFree(w);CHKERRQ(ierr);
15986bacbd2SBarry Smith   ierr = PetscFree(jw);CHKERRQ(ierr);
16086bacbd2SBarry Smith 
16186bacbd2SBarry Smith   /* ------------------------------------------------------------
16286bacbd2SBarry Smith      Saad's routine gives the result in Modified Sparse Row (msr)
16386bacbd2SBarry Smith      Convert to Compressed Sparse Row format (csr)
16486bacbd2SBarry Smith      ------------------------------------------------------------
16586bacbd2SBarry Smith   */
16686bacbd2SBarry Smith 
16787828ca2SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscScalar),&wk);CHKERRQ(ierr);
168b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&iwk);CHKERRQ(ierr);
16986bacbd2SBarry Smith 
1705bd2ddc7SBarry Smith   SPARSEKIT2msrcsr(&n,new_a,new_j,new_a,new_j,new_i,wk,iwk);
17186bacbd2SBarry Smith 
17286bacbd2SBarry Smith   ierr = PetscFree(iwk);CHKERRQ(ierr);
17386bacbd2SBarry Smith   ierr = PetscFree(wk);CHKERRQ(ierr);
17486bacbd2SBarry Smith 
17586bacbd2SBarry Smith   if (reorder) {
17686bacbd2SBarry Smith     ierr = PetscFree(old_a2);CHKERRQ(ierr);
17786bacbd2SBarry Smith     ierr = PetscFree(old_j2);CHKERRQ(ierr);
17886bacbd2SBarry Smith     ierr = PetscFree(old_i2);CHKERRQ(ierr);
179313ae024SBarry Smith   } else {
180b19713cbSBarry Smith     /* fix permutation of old_j that the factorization introduced */
181f170005cSBarry Smith     for (i=old_i[0]; i<old_i[n]; i++) {
182b19713cbSBarry Smith       old_j[i-1] = iperm[old_j[i-1]-1];
183b19713cbSBarry Smith     }
184b19713cbSBarry Smith   }
185b19713cbSBarry Smith 
186b801d0f9SBarry Smith   /* get rid of the shift to indices starting at 1 */
187b19713cbSBarry Smith   if (ishift) {
18886bacbd2SBarry Smith     for (i=0; i<n+1; i++) {
189b19713cbSBarry Smith       old_i[i]--;
190b19713cbSBarry Smith     }
191b19713cbSBarry Smith     for (i=old_i[0];i<old_i[n];i++) {
192b19713cbSBarry Smith       old_j[i]--;
193b19713cbSBarry Smith     }
19486bacbd2SBarry Smith   }
19586bacbd2SBarry Smith 
196b19713cbSBarry Smith   /* Make the factored matrix 0-based */
197b19713cbSBarry Smith   if (ishift) {
19886bacbd2SBarry Smith     for (i=0; i<n+1; i++) {
199b19713cbSBarry Smith       new_i[i]--;
200b19713cbSBarry Smith     }
201b19713cbSBarry Smith     for (i=new_i[0];i<new_i[n];i++) {
202b19713cbSBarry Smith       new_j[i]--;
203b19713cbSBarry Smith     }
20486bacbd2SBarry Smith   }
20586bacbd2SBarry Smith 
20686bacbd2SBarry Smith   /*-- due to the pivoting, we need to reorder iscol to correctly --*/
20786bacbd2SBarry Smith   /*-- permute the right-hand-side and solution vectors           --*/
2084c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
2094c49b128SBarry Smith   ierr = ISInvertPermutation(isrow,PETSC_DECIDE,&isirow);CHKERRQ(ierr);
210c0c2c81eSBarry Smith   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
21186bacbd2SBarry Smith   for(i=0; i<n; i++) {
21286bacbd2SBarry Smith     ordcol[i] = ic[iperm[i]-1];
21386bacbd2SBarry Smith   };
21486bacbd2SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
21507d2056aSBarry Smith   ierr = ISDestroy(isicol);CHKERRQ(ierr);
21686bacbd2SBarry Smith 
217c0c2c81eSBarry Smith   ierr = PetscFree(iperm);CHKERRQ(ierr);
218c0c2c81eSBarry Smith 
219329f5518SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,ordcol,&iscolf);CHKERRQ(ierr);
22007d2056aSBarry Smith   ierr = PetscFree(ordcol);CHKERRQ(ierr);
22186bacbd2SBarry Smith 
22286bacbd2SBarry Smith   /*----- put together the new matrix -----*/
22386bacbd2SBarry Smith 
22486bacbd2SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr);
22586bacbd2SBarry Smith   (*fact)->factor    = FACTOR_LU;
22686bacbd2SBarry Smith   (*fact)->assembled = PETSC_TRUE;
22786bacbd2SBarry Smith 
22886bacbd2SBarry Smith   b = (Mat_SeqAIJ*)(*fact)->data;
22986bacbd2SBarry Smith   ierr = PetscFree(b->imax);CHKERRQ(ierr);
23086bacbd2SBarry Smith   b->sorted        = PETSC_FALSE;
23107d2056aSBarry Smith   b->singlemalloc  = PETSC_FALSE;
232b19713cbSBarry Smith   /* the next line frees the default space generated by the MatCreate() */
23386bacbd2SBarry Smith   ierr             = PetscFree(b->a);CHKERRQ(ierr);
23486bacbd2SBarry Smith   ierr             = PetscFree(b->ilen);CHKERRQ(ierr);
23586bacbd2SBarry Smith   b->a             = new_a;
23686bacbd2SBarry Smith   b->j             = new_j;
23786bacbd2SBarry Smith   b->i             = new_i;
23886bacbd2SBarry Smith   b->ilen          = 0;
23986bacbd2SBarry Smith   b->imax          = 0;
2401f9e874aSBarry Smith   /*  I am not sure why these are the inverses of the row and column permutations; but the other way is NO GOOD */
241313ae024SBarry Smith   b->row           = isirow;
24286bacbd2SBarry Smith   b->col           = iscolf;
24387828ca2SBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
24486bacbd2SBarry Smith   b->maxnz = b->nz = new_i[n];
24586bacbd2SBarry Smith   b->indexshift    = a->indexshift;
24686bacbd2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
24786bacbd2SBarry Smith   (*fact)->info.factor_mallocs = 0;
24886bacbd2SBarry Smith 
24986bacbd2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
25086bacbd2SBarry Smith 
25186bacbd2SBarry Smith   /* check out for identical nodes. If found, use inode functions */
2523a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(*fact,PETSC_FALSE);CHKERRQ(ierr);
25386bacbd2SBarry Smith 
254431e710aSBarry Smith   af = ((double)b->nz)/((double)a->nz) + .001;
255b0a32e0cSBarry Smith   PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:Fill ratio:given %g needed %g\n",info->fill,af);
256b0a32e0cSBarry Smith   PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
257b0a32e0cSBarry Smith   PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
258b0a32e0cSBarry Smith   PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:for best performance.\n");
259431e710aSBarry Smith 
26086bacbd2SBarry Smith   PetscFunctionReturn(0);
26186bacbd2SBarry Smith }
26286bacbd2SBarry Smith 
263289bc588SBarry Smith /*
264289bc588SBarry Smith     Factorization code for AIJ format.
265289bc588SBarry Smith */
2664a2ae208SSatish Balay #undef __FUNCT__
267b9617806SBarry Smith #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ"
2689e046878SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatLUInfo *info,Mat *B)
269289bc588SBarry Smith {
270416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b;
271289bc588SBarry Smith   IS         isicol;
272273d9f13SBarry Smith   int        *r,*ic,ierr,i,n = A->m,*ai = a->i,*aj = a->j;
273416022c9SBarry Smith   int        *ainew,*ajnew,jmax,*fill,*ajtmp,nz,shift = a->indexshift;
27491bf9adeSBarry Smith   int        *idnew,idx,row,m,fm,nnz,nzi,realloc = 0,nzbd,*im;
2759e046878SBarry Smith   PetscReal  f;
276289bc588SBarry Smith 
2773a40ed3dSBarry Smith   PetscFunctionBegin;
27829bbc08cSBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
2794c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
280ac121b3dSBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
281ac121b3dSBarry Smith   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
282289bc588SBarry Smith 
283289bc588SBarry Smith   /* get new row pointers */
284b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&ainew);CHKERRQ(ierr);
285dbb450caSBarry Smith   ainew[0] = -shift;
286289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
287*d3d32019SBarry Smith   f = info->fill;
288dbb450caSBarry Smith   jmax  = (int)(f*ai[n]+(!shift));
289b0a32e0cSBarry Smith   ierr = PetscMalloc((jmax)*sizeof(int),&ajnew);CHKERRQ(ierr);
290289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
291b0a32e0cSBarry Smith   ierr = PetscMalloc((2*n+1)*sizeof(int),&fill);CHKERRQ(ierr);
2922fb3ed76SBarry Smith   im   = fill + n + 1;
293289bc588SBarry Smith   /* idnew is location of diagonal in factor */
294b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&idnew);CHKERRQ(ierr);
295dbb450caSBarry Smith   idnew[0] = -shift;
296289bc588SBarry Smith 
297289bc588SBarry Smith   for (i=0; i<n; i++) {
298289bc588SBarry Smith     /* first copy previous fill into linked list */
299289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
30029bbc08cSBarry Smith     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix");
301dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
302289bc588SBarry Smith     fill[n] = n;
303289bc588SBarry Smith     while (nz--) {
304289bc588SBarry Smith       fm  = n;
305dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
306289bc588SBarry Smith       do {
307289bc588SBarry Smith         m  = fm;
308289bc588SBarry Smith         fm = fill[m];
309289bc588SBarry Smith       } while (fm < idx);
310289bc588SBarry Smith       fill[m]   = idx;
311289bc588SBarry Smith       fill[idx] = fm;
312289bc588SBarry Smith     }
313289bc588SBarry Smith     row = fill[n];
314289bc588SBarry Smith     while (row < i) {
315dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
3162fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
3172fb3ed76SBarry Smith       nz    = im[row] - nzbd;
318289bc588SBarry Smith       fm    = row;
3192fb3ed76SBarry Smith       while (nz-- > 0) {
320dbb450caSBarry Smith         idx = *ajtmp++ + shift;
3212fb3ed76SBarry Smith         nzbd++;
3222fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
323289bc588SBarry Smith         do {
324289bc588SBarry Smith           m  = fm;
325289bc588SBarry Smith           fm = fill[m];
326289bc588SBarry Smith         } while (fm < idx);
327289bc588SBarry Smith         if (fm != idx) {
328289bc588SBarry Smith           fill[m]   = idx;
329289bc588SBarry Smith           fill[idx] = fm;
330289bc588SBarry Smith           fm        = idx;
331289bc588SBarry Smith           nnz++;
332289bc588SBarry Smith         }
333289bc588SBarry Smith       }
334289bc588SBarry Smith       row = fill[row];
335289bc588SBarry Smith     }
336289bc588SBarry Smith     /* copy new filled row into permanent storage */
337289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
338496e697eSBarry Smith     if (ainew[i+1] > jmax) {
339ecf371e4SBarry Smith 
340ecf371e4SBarry Smith       /* estimate how much additional space we will need */
341ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
342ecf371e4SBarry Smith       /* just double the memory each time */
343ecf371e4SBarry Smith       int maxadd = jmax;
344ecf371e4SBarry Smith       /* maxadd = (int)((f*(ai[n]+(!shift))*(n-i+5))/n); */
345bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
3462fb3ed76SBarry Smith       jmax += maxadd;
347ecf371e4SBarry Smith 
348ecf371e4SBarry Smith       /* allocate a longer ajnew */
349b0a32e0cSBarry Smith       ierr = PetscMalloc(jmax*sizeof(int),&ajtmp);CHKERRQ(ierr);
350549d3d68SSatish Balay       ierr  = PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));CHKERRQ(ierr);
351606d414cSSatish Balay       ierr  = PetscFree(ajnew);CHKERRQ(ierr);
352289bc588SBarry Smith       ajnew = ajtmp;
3532fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
354289bc588SBarry Smith     }
355dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
356289bc588SBarry Smith     fm    = fill[n];
357289bc588SBarry Smith     nzi   = 0;
3582fb3ed76SBarry Smith     im[i] = nnz;
359289bc588SBarry Smith     while (nnz--) {
360289bc588SBarry Smith       if (fm < i) nzi++;
361dbb450caSBarry Smith       *ajtmp++ = fm - shift;
362289bc588SBarry Smith       fm       = fill[fm];
363289bc588SBarry Smith     }
364289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
365289bc588SBarry Smith   }
366464e8d44SSatish Balay   if (ai[n] != 0) {
367329f5518SBarry Smith     PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
368b0a32e0cSBarry Smith     PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
369b0a32e0cSBarry Smith     PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
370b0a32e0cSBarry Smith     PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
371b0a32e0cSBarry Smith     PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
37205bf559cSSatish Balay   } else {
373b0a32e0cSBarry Smith     PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
37405bf559cSSatish Balay   }
3752fb3ed76SBarry Smith 
376898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
377898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
3781987afe7SBarry Smith 
379606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
380289bc588SBarry Smith 
381289bc588SBarry Smith   /* put together the new matrix */
382b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B);CHKERRQ(ierr);
383b0a32e0cSBarry Smith   PetscLogObjectParent(*B,isicol);
384416022c9SBarry Smith   b = (Mat_SeqAIJ*)(*B)->data;
385606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
3867c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
387e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
388606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
389606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
39087828ca2SBarry Smith   ierr          = PetscMalloc((ainew[n]+shift+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr);
391416022c9SBarry Smith   b->j          = ajnew;
392416022c9SBarry Smith   b->i          = ainew;
393416022c9SBarry Smith   b->diag       = idnew;
394416022c9SBarry Smith   b->ilen       = 0;
395416022c9SBarry Smith   b->imax       = 0;
396416022c9SBarry Smith   b->row        = isrow;
397416022c9SBarry Smith   b->col        = iscol;
398085256b3SBarry Smith   b->lu_damping   = info->damping;
39987828ca2SBarry Smith   b->lu_zeropivot = info->zeropivot;
400c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
401c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
40282bf6240SBarry Smith   b->icol       = isicol;
40387828ca2SBarry Smith   ierr          = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
404416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
4057fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
40687828ca2SBarry Smith   PetscLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(PetscScalar)));
407416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
4087fd98bd6SLois Curfman McInnes 
409329f5518SBarry Smith   (*B)->factor                 =  FACTOR_LU;
410ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
411ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
4123a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(*B,PETSC_FALSE);CHKERRQ(ierr);
4137dda7e10SBarry Smith   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */
414703deb49SSatish Balay 
415e93ccc4dSBarry Smith   if (ai[n] != 0) {
416329f5518SBarry Smith     (*B)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
417eea2913aSSatish Balay   } else {
418eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
419eea2913aSSatish Balay   }
4203a40ed3dSBarry Smith   PetscFunctionReturn(0);
421289bc588SBarry Smith }
4220a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
4233a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
42441c01911SSatish Balay 
4254a2ae208SSatish Balay #undef __FUNCT__
4264a2ae208SSatish Balay #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ"
427416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
428289bc588SBarry Smith {
429416022c9SBarry Smith   Mat          C = *B;
430416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ *)C->data;
43182bf6240SBarry Smith   IS           isrow = b->row,isicol = b->icol;
432273d9f13SBarry Smith   int          *r,*ic,ierr,i,j,n = A->m,*ai = b->i,*aj = b->j;
4331987afe7SBarry Smith   int          *ajtmpold,*ajtmp,nz,row,*ics,shift = a->indexshift;
434085256b3SBarry Smith   int          *diag_offset = b->diag,diag;
435085256b3SBarry Smith   int          *pj,ndamp = 0;
43687828ca2SBarry Smith   PetscScalar  *rtmp,*v,*pc,multiplier,*pv,*rtmps;
437*d3d32019SBarry Smith   PetscReal    damping = b->lu_damping, zeropivot = b->lu_zeropivot,rs;
438*d3d32019SBarry Smith   PetscTruth   damp = PETSC_FALSE;
439289bc588SBarry Smith 
4403a40ed3dSBarry Smith   PetscFunctionBegin;
44178b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r);CHKERRQ(ierr);
44278b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
44387828ca2SBarry Smith   ierr  = PetscMalloc((n+1)*sizeof(PetscScalar),&rtmp);CHKERRQ(ierr);
44487828ca2SBarry Smith   ierr  = PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
4450cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
446289bc588SBarry Smith 
447085256b3SBarry Smith   do {
448289bc588SBarry Smith     for (i=0; i<n; i++) {
449289bc588SBarry Smith       nz    = ai[i+1] - ai[i];
450dbb450caSBarry Smith       ajtmp = aj + ai[i] + shift;
45148e94559SBarry Smith       for  (j=0; j<nz; j++) rtmps[ajtmp[j]] = 0.0;
452289bc588SBarry Smith 
453289bc588SBarry Smith       /* load in initial (unfactored row) */
454416022c9SBarry Smith       nz       = a->i[r[i]+1] - a->i[r[i]];
455416022c9SBarry Smith       ajtmpold = a->j + a->i[r[i]] + shift;
456416022c9SBarry Smith       v        = a->a + a->i[r[i]] + shift;
457085256b3SBarry Smith       for (j=0; j<nz; j++) {
458085256b3SBarry Smith         rtmp[ics[ajtmpold[j]]] = v[j];
459*d3d32019SBarry Smith         if (damp && ajtmpold[j] == r[i]) { /* damp the diagonal of the matrix */
460085256b3SBarry Smith           rtmp[ics[ajtmpold[j]]] += damping;
461085256b3SBarry Smith         }
462085256b3SBarry Smith       }
463289bc588SBarry Smith 
464dbb450caSBarry Smith       row = *ajtmp++ + shift;
465f2582111SSatish Balay       while  (row < i) {
4668c37ef55SBarry Smith         pc = rtmp + row;
4678c37ef55SBarry Smith         if (*pc != 0.0) {
4681987afe7SBarry Smith           pv         = b->a + diag_offset[row] + shift;
4691987afe7SBarry Smith           pj         = b->j + diag_offset[row] + (!shift);
47035aab85fSBarry Smith           multiplier = *pc / *pv++;
4718c37ef55SBarry Smith           *pc        = multiplier;
472f2582111SSatish Balay           nz         = ai[row+1] - diag_offset[row] - 1;
473f2582111SSatish Balay           for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
474b0a32e0cSBarry Smith           PetscLogFlops(2*nz);
475289bc588SBarry Smith         }
476dbb450caSBarry Smith         row = *ajtmp++ + shift;
477289bc588SBarry Smith       }
478416022c9SBarry Smith       /* finished row so stick it into b->a */
479416022c9SBarry Smith       pv   = b->a + ai[i] + shift;
480416022c9SBarry Smith       pj   = b->j + ai[i] + shift;
4818c37ef55SBarry Smith       nz   = ai[i+1] - ai[i];
48235aab85fSBarry Smith       diag = diag_offset[i] - ai[i];
483*d3d32019SBarry Smith       /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */
484*d3d32019SBarry Smith       rs   = 0.0;
485*d3d32019SBarry Smith       for (j=0; j<nz; j++) {
486*d3d32019SBarry Smith         pv[j] = rtmps[pj[j]];
487*d3d32019SBarry Smith         if (j != diag) rs += PetscAbsScalar(pv[j]);
488*d3d32019SBarry Smith       }
489*d3d32019SBarry Smith       if (PetscAbsScalar(pv[diag]) < zeropivot*rs) {
490*d3d32019SBarry Smith         if (damping) {
491*d3d32019SBarry Smith           if (damp) damping *= 2.0;
492085256b3SBarry Smith           damp = PETSC_TRUE;
493085256b3SBarry Smith           ndamp++;
494085256b3SBarry Smith           break;
495085256b3SBarry Smith         } else {
49687828ca2SBarry Smith           SETERRQ3(PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot row %d value %g tolerance %g",i,PetscAbsScalar(pv[diag]),zeropivot);
497d708749eSBarry Smith         }
49835aab85fSBarry Smith       }
49935aab85fSBarry Smith     }
500085256b3SBarry Smith   } while (damp);
5010f11f9aeSSatish Balay 
50235aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
50335aab85fSBarry Smith   for (i=0; i<n; i++) {
50435aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
50535aab85fSBarry Smith   }
50635aab85fSBarry Smith 
507606d414cSSatish Balay   ierr = PetscFree(rtmp);CHKERRQ(ierr);
50878b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
50978b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
510416022c9SBarry Smith   C->factor = FACTOR_LU;
5117dda7e10SBarry Smith   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */
512c456f294SBarry Smith   C->assembled = PETSC_TRUE;
513b0a32e0cSBarry Smith   PetscLogFlops(C->n);
514*d3d32019SBarry Smith   if (ndamp) {
515b0a32e0cSBarry Smith     PetscLogInfo(0,"MatLUFactorNumerical_SeqAIJ: number of damping tries %d damping value %g\n",ndamp,damping);
516085256b3SBarry Smith   }
5173a40ed3dSBarry Smith   PetscFunctionReturn(0);
518289bc588SBarry Smith }
5190a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5204a2ae208SSatish Balay #undef __FUNCT__
5214a2ae208SSatish Balay #define __FUNCT__ "MatLUFactor_SeqAIJ"
5229e046878SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,MatLUInfo *info)
523da3a660dSBarry Smith {
524273d9f13SBarry Smith   int ierr;
525416022c9SBarry Smith   Mat C;
526416022c9SBarry Smith 
5273a40ed3dSBarry Smith   PetscFunctionBegin;
5289e046878SBarry Smith   ierr = MatLUFactorSymbolic(A,row,col,info,&C);CHKERRQ(ierr);
529f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C);CHKERRQ(ierr);
530273d9f13SBarry Smith   ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
531440f4c53SSatish Balay   PetscLogObjectParent(A,((Mat_SeqAIJ*)(A->data))->icol);
5323a40ed3dSBarry Smith   PetscFunctionReturn(0);
533da3a660dSBarry Smith }
5340a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5354a2ae208SSatish Balay #undef __FUNCT__
5364a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqAIJ"
537416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx)
5388c37ef55SBarry Smith {
539416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
540416022c9SBarry Smith   IS           iscol = a->col,isrow = a->row;
541273d9f13SBarry Smith   int          *r,*c,ierr,i, n = A->m,*vi,*ai = a->i,*aj = a->j;
5423b2fbd54SBarry Smith   int          nz,shift = a->indexshift,*rout,*cout;
54387828ca2SBarry Smith   PetscScalar  *x,*b,*tmp,*tmps,*aa = a->a,sum,*v;
5448c37ef55SBarry Smith 
5453a40ed3dSBarry Smith   PetscFunctionBegin;
5463a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
54791bf9adeSBarry Smith 
548e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
549e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
550416022c9SBarry Smith   tmp  = a->solve_work;
5518c37ef55SBarry Smith 
5523b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
5533b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
5548c37ef55SBarry Smith 
5558c37ef55SBarry Smith   /* forward solve the lower triangular */
5568c37ef55SBarry Smith   tmp[0] = b[*r++];
5570cf60383SBarry Smith   tmps   = tmp + shift;
5588c37ef55SBarry Smith   for (i=1; i<n; i++) {
559dbb450caSBarry Smith     v   = aa + ai[i] + shift;
560dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
561416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
5628c37ef55SBarry Smith     sum = b[*r++];
5630cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
5648c37ef55SBarry Smith     tmp[i] = sum;
5658c37ef55SBarry Smith   }
5668c37ef55SBarry Smith 
5678c37ef55SBarry Smith   /* backward solve the upper triangular */
5688c37ef55SBarry Smith   for (i=n-1; i>=0; 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;
5728c37ef55SBarry Smith     sum = tmp[i];
5730cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
574416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
5758c37ef55SBarry Smith   }
5768c37ef55SBarry Smith 
5773b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
5783b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
579cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
580cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
581b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - A->n);
5823a40ed3dSBarry Smith   PetscFunctionReturn(0);
5838c37ef55SBarry Smith }
584026e39d0SSatish Balay 
585930ae53cSBarry Smith /* ----------------------------------------------------------- */
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering"
588930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx)
589930ae53cSBarry Smith {
590930ae53cSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
591273d9f13SBarry Smith   int          n = A->m,*ai = a->i,*aj = a->j,*adiag = a->diag,ierr;
592362ced78SSatish Balay   PetscScalar  *x,*b,*aa = a->a;
593aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
594d85afc42SSatish Balay   int          adiag_i,i,*vi,nz,ai_i;
595362ced78SSatish Balay   PetscScalar  *v,sum;
596d85afc42SSatish Balay #endif
597930ae53cSBarry Smith 
5983a40ed3dSBarry Smith   PetscFunctionBegin;
5993a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
600930ae53cSBarry Smith   if (a->indexshift) {
6013a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
6023a40ed3dSBarry Smith      PetscFunctionReturn(0);
603930ae53cSBarry Smith   }
604930ae53cSBarry Smith 
605e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
606e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
607930ae53cSBarry Smith 
608aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
6091c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
6101c4feecaSSatish Balay #else
611930ae53cSBarry Smith   /* forward solve the lower triangular */
612930ae53cSBarry Smith   x[0] = b[0];
613930ae53cSBarry Smith   for (i=1; i<n; i++) {
614930ae53cSBarry Smith     ai_i = ai[i];
615930ae53cSBarry Smith     v    = aa + ai_i;
616930ae53cSBarry Smith     vi   = aj + ai_i;
617930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
618930ae53cSBarry Smith     sum  = b[i];
619930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
620930ae53cSBarry Smith     x[i] = sum;
621930ae53cSBarry Smith   }
622930ae53cSBarry Smith 
623930ae53cSBarry Smith   /* backward solve the upper triangular */
624930ae53cSBarry Smith   for (i=n-1; i>=0; i--){
625930ae53cSBarry Smith     adiag_i = adiag[i];
626d708749eSBarry Smith     v       = aa + adiag_i + 1;
627d708749eSBarry Smith     vi      = aj + adiag_i + 1;
628930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
629930ae53cSBarry Smith     sum     = x[i];
630930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
631930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
632930ae53cSBarry Smith   }
6331c4feecaSSatish Balay #endif
634b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - A->n);
635cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
636cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
6373a40ed3dSBarry Smith   PetscFunctionReturn(0);
638930ae53cSBarry Smith }
639930ae53cSBarry Smith 
6404a2ae208SSatish Balay #undef __FUNCT__
6414a2ae208SSatish Balay #define __FUNCT__ "MatSolveAdd_SeqAIJ"
642416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx)
643da3a660dSBarry Smith {
644416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
645416022c9SBarry Smith   IS           iscol = a->col,isrow = a->row;
646273d9f13SBarry Smith   int          *r,*c,ierr,i, n = A->m,*vi,*ai = a->i,*aj = a->j;
6473b2fbd54SBarry Smith   int          nz,shift = a->indexshift,*rout,*cout;
64887828ca2SBarry Smith   PetscScalar  *x,*b,*tmp,*aa = a->a,sum,*v;
649da3a660dSBarry Smith 
6503a40ed3dSBarry Smith   PetscFunctionBegin;
65178b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);}
652da3a660dSBarry Smith 
653e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
654e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
655416022c9SBarry Smith   tmp  = a->solve_work;
656da3a660dSBarry Smith 
6573b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
6583b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
659da3a660dSBarry Smith 
660da3a660dSBarry Smith   /* forward solve the lower triangular */
661da3a660dSBarry Smith   tmp[0] = b[*r++];
662da3a660dSBarry Smith   for (i=1; i<n; i++) {
663dbb450caSBarry Smith     v   = aa + ai[i] + shift;
664dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
665416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
666da3a660dSBarry Smith     sum = b[*r++];
667dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
668da3a660dSBarry Smith     tmp[i] = sum;
669da3a660dSBarry Smith   }
670da3a660dSBarry Smith 
671da3a660dSBarry Smith   /* backward solve the upper triangular */
672da3a660dSBarry Smith   for (i=n-1; i>=0; i--){
673416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
674416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
675416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
676da3a660dSBarry Smith     sum = tmp[i];
677dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
678416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
679da3a660dSBarry Smith     x[*c--] += tmp[i];
680da3a660dSBarry Smith   }
681da3a660dSBarry Smith 
6823b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
6833b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
684cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
685cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
686b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
687898183ecSLois Curfman McInnes 
6883a40ed3dSBarry Smith   PetscFunctionReturn(0);
689da3a660dSBarry Smith }
690da3a660dSBarry Smith /* -------------------------------------------------------------------*/
6914a2ae208SSatish Balay #undef __FUNCT__
6924a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqAIJ"
6937c922b88SBarry Smith int MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx)
694da3a660dSBarry Smith {
695416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
6962235e667SBarry Smith   IS           iscol = a->col,isrow = a->row;
697273d9f13SBarry Smith   int          *r,*c,ierr,i,n = A->m,*vi,*ai = a->i,*aj = a->j;
698f1af5d2fSBarry Smith   int          nz,shift = a->indexshift,*rout,*cout,*diag = a->diag;
69987828ca2SBarry Smith   PetscScalar  *x,*b,*tmp,*aa = a->a,*v,s1;
700da3a660dSBarry Smith 
7013a40ed3dSBarry Smith   PetscFunctionBegin;
702e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
703e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
704416022c9SBarry Smith   tmp  = a->solve_work;
705da3a660dSBarry Smith 
7062235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7072235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
708da3a660dSBarry Smith 
709da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
7102235e667SBarry Smith   for (i=0; i<n; i++) tmp[i] = b[c[i]];
711da3a660dSBarry Smith 
712da3a660dSBarry Smith   /* forward solve the U^T */
713da3a660dSBarry Smith   for (i=0; i<n; i++) {
714f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
715f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
716f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
717c38d4ed2SBarry Smith     s1  = tmp[i];
718ef66eb69SBarry Smith     s1 *= (*v++);  /* multiply by inverse of diagonal entry */
719da3a660dSBarry Smith     while (nz--) {
720f1af5d2fSBarry Smith       tmp[*vi++ + shift] -= (*v++)*s1;
721da3a660dSBarry Smith     }
722c38d4ed2SBarry Smith     tmp[i] = s1;
723da3a660dSBarry Smith   }
724da3a660dSBarry Smith 
725da3a660dSBarry Smith   /* backward solve the L^T */
726da3a660dSBarry Smith   for (i=n-1; i>=0; i--){
727f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
728f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
729f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
730f1af5d2fSBarry Smith     s1  = tmp[i];
731da3a660dSBarry Smith     while (nz--) {
732f1af5d2fSBarry Smith       tmp[*vi-- + shift] -= (*v--)*s1;
733da3a660dSBarry Smith     }
734da3a660dSBarry Smith   }
735da3a660dSBarry Smith 
736da3a660dSBarry Smith   /* copy tmp into x according to permutation */
737da3a660dSBarry Smith   for (i=0; i<n; i++) x[r[i]] = tmp[i];
738da3a660dSBarry Smith 
7392235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
7402235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
741cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
742cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
743da3a660dSBarry Smith 
744b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz-A->n);
7453a40ed3dSBarry Smith   PetscFunctionReturn(0);
746da3a660dSBarry Smith }
747da3a660dSBarry Smith 
7484a2ae208SSatish Balay #undef __FUNCT__
7494a2ae208SSatish Balay #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ"
7507c922b88SBarry Smith int MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx)
751da3a660dSBarry Smith {
752416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
7532235e667SBarry Smith   IS           iscol = a->col,isrow = a->row;
754273d9f13SBarry Smith   int          *r,*c,ierr,i,n = A->m,*vi,*ai = a->i,*aj = a->j;
755f1af5d2fSBarry Smith   int          nz,shift = a->indexshift,*rout,*cout,*diag = a->diag;
75687828ca2SBarry Smith   PetscScalar  *x,*b,*tmp,*aa = a->a,*v;
7576abc6512SBarry Smith 
7583a40ed3dSBarry Smith   PetscFunctionBegin;
7596abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
7606abc6512SBarry Smith 
761e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
762e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
763416022c9SBarry Smith   tmp = a->solve_work;
7646abc6512SBarry Smith 
7652235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7662235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
7676abc6512SBarry Smith 
7686abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
7692235e667SBarry Smith   for (i=0; i<n; i++) tmp[i] = b[c[i]];
7706abc6512SBarry Smith 
7716abc6512SBarry Smith   /* forward solve the U^T */
7726abc6512SBarry Smith   for (i=0; i<n; i++) {
773f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
774f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
775f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
7766abc6512SBarry Smith     tmp[i] *= *v++;
7776abc6512SBarry Smith     while (nz--) {
778dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
7796abc6512SBarry Smith     }
7806abc6512SBarry Smith   }
7816abc6512SBarry Smith 
7826abc6512SBarry Smith   /* backward solve the L^T */
7836abc6512SBarry Smith   for (i=n-1; i>=0; i--){
784f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
785f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
786f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
7876abc6512SBarry Smith     while (nz--) {
788dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
7896abc6512SBarry Smith     }
7906abc6512SBarry Smith   }
7916abc6512SBarry Smith 
7926abc6512SBarry Smith   /* copy tmp into x according to permutation */
7936abc6512SBarry Smith   for (i=0; i<n; i++) x[r[i]] += tmp[i];
7946abc6512SBarry Smith 
7952235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
7962235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
797cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
798cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
7996abc6512SBarry Smith 
800b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
8013a40ed3dSBarry Smith   PetscFunctionReturn(0);
802da3a660dSBarry Smith }
8039e25ed09SBarry Smith /* ----------------------------------------------------------------*/
804ca44d042SBarry Smith EXTERN int MatMissingDiagonal_SeqAIJ(Mat);
80508480c60SBarry Smith 
8064a2ae208SSatish Balay #undef __FUNCT__
8074a2ae208SSatish Balay #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ"
8086cf997b0SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatILUInfo *info,Mat *fact)
8099e25ed09SBarry Smith {
810416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b;
8119e25ed09SBarry Smith   IS         isicol;
812273d9f13SBarry Smith   int        *r,*ic,ierr,prow,n = A->m,*ai = a->i,*aj = a->j;
81356beaf04SBarry Smith   int        *ainew,*ajnew,jmax,*fill,*xi,nz,*im,*ajfill,*flev;
814335d9088SBarry Smith   int        *dloc,idx,row,m,fm,nzf,nzi,len, realloc = 0,dcount = 0;
8156cf997b0SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift,levels,diagonal_fill;
81677c4ece6SBarry Smith   PetscTruth col_identity,row_identity;
817329f5518SBarry Smith   PetscReal  f;
8189e25ed09SBarry Smith 
8193a40ed3dSBarry Smith   PetscFunctionBegin;
8206cf997b0SBarry Smith   f             = info->fill;
8210cd17293SBarry Smith   levels        = (int)info->levels;
8220cd17293SBarry Smith   diagonal_fill = (int)info->diagonal_fill;
8234c49b128SBarry Smith   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
82482bf6240SBarry Smith 
82508480c60SBarry Smith   /* special case that simply copies fill pattern */
826be0abb6dSBarry Smith   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
827be0abb6dSBarry Smith   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
82886bacbd2SBarry Smith   if (!levels && row_identity && col_identity) {
8292e8a6d31SBarry Smith     ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact);CHKERRQ(ierr);
83008480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
83108480c60SBarry Smith     b               = (Mat_SeqAIJ*)(*fact)->data;
83208480c60SBarry Smith     if (!b->diag) {
8337c922b88SBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
83408480c60SBarry Smith     }
8357c922b88SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
83608480c60SBarry Smith     b->row              = isrow;
83708480c60SBarry Smith     b->col              = iscol;
83882bf6240SBarry Smith     b->icol             = isicol;
839a2e34c3dSBarry Smith     b->lu_damping       = info->damping;
84087828ca2SBarry Smith     b->lu_zeropivot     = info->zeropivot;
84187828ca2SBarry Smith     ierr                = PetscMalloc(((*fact)->m+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
842f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
843c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
844c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
8453a40ed3dSBarry Smith     PetscFunctionReturn(0);
84608480c60SBarry Smith   }
84708480c60SBarry Smith 
848898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
849898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
8509e25ed09SBarry Smith 
8519e25ed09SBarry Smith   /* get new row pointers */
852b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&ainew);CHKERRQ(ierr);
853dbb450caSBarry Smith   ainew[0] = -shift;
8549e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
855dbb450caSBarry Smith   jmax = (int)(f*(ai[n]+!shift));
856b0a32e0cSBarry Smith   ierr = PetscMalloc((jmax)*sizeof(int),&ajnew);CHKERRQ(ierr);
8579e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
858b0a32e0cSBarry Smith   ierr = PetscMalloc((jmax)*sizeof(int),&ajfill);CHKERRQ(ierr);
8599e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
860b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&fill);CHKERRQ(ierr);
86156beaf04SBarry Smith   /* im is level for each filled value */
862b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&im);CHKERRQ(ierr);
86356beaf04SBarry Smith   /* dloc is location of diagonal in factor */
864b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&dloc);CHKERRQ(ierr);
86556beaf04SBarry Smith   dloc[0]  = 0;
86656beaf04SBarry Smith   for (prow=0; prow<n; prow++) {
8676cf997b0SBarry Smith 
8686cf997b0SBarry Smith     /* copy current row into linked list */
86956beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
87029bbc08cSBarry Smith     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix");
871dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
8729e25ed09SBarry Smith     fill[n]    = n;
873435faa5fSBarry Smith     fill[prow] = -1; /* marker to indicate if diagonal exists */
8749e25ed09SBarry Smith     while (nz--) {
8759e25ed09SBarry Smith       fm  = n;
876dbb450caSBarry Smith       idx = ic[*xi++ + shift];
8779e25ed09SBarry Smith       do {
8789e25ed09SBarry Smith         m  = fm;
8799e25ed09SBarry Smith         fm = fill[m];
8809e25ed09SBarry Smith       } while (fm < idx);
8819e25ed09SBarry Smith       fill[m]   = idx;
8829e25ed09SBarry Smith       fill[idx] = fm;
88356beaf04SBarry Smith       im[idx]   = 0;
8849e25ed09SBarry Smith     }
8856cf997b0SBarry Smith 
8866cf997b0SBarry Smith     /* make sure diagonal entry is included */
887435faa5fSBarry Smith     if (diagonal_fill && fill[prow] == -1) {
8886cf997b0SBarry Smith       fm = n;
889435faa5fSBarry Smith       while (fill[fm] < prow) fm = fill[fm];
890435faa5fSBarry Smith       fill[prow] = fill[fm]; /* insert diagonal into linked list */
891435faa5fSBarry Smith       fill[fm]   = prow;
8926cf997b0SBarry Smith       im[prow]   = 0;
8936cf997b0SBarry Smith       nzf++;
894335d9088SBarry Smith       dcount++;
8956cf997b0SBarry Smith     }
8966cf997b0SBarry Smith 
89756beaf04SBarry Smith     nzi = 0;
8989e25ed09SBarry Smith     row = fill[n];
89956beaf04SBarry Smith     while (row < prow) {
90056beaf04SBarry Smith       incrlev = im[row] + 1;
90156beaf04SBarry Smith       nz      = dloc[row];
9026cf997b0SBarry Smith       xi      = ajnew  + ainew[row] + shift + nz + 1;
903dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
904416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
90556beaf04SBarry Smith       fm      = row;
90656beaf04SBarry Smith       while (nnz-- > 0) {
907dbb450caSBarry Smith         idx = *xi++ + shift;
90856beaf04SBarry Smith         if (*flev + incrlev > levels) {
90956beaf04SBarry Smith           flev++;
91056beaf04SBarry Smith           continue;
91156beaf04SBarry Smith         }
91256beaf04SBarry Smith         do {
9139e25ed09SBarry Smith           m  = fm;
9149e25ed09SBarry Smith           fm = fill[m];
91556beaf04SBarry Smith         } while (fm < idx);
9169e25ed09SBarry Smith         if (fm != idx) {
91756beaf04SBarry Smith           im[idx]   = *flev + incrlev;
9189e25ed09SBarry Smith           fill[m]   = idx;
9199e25ed09SBarry Smith           fill[idx] = fm;
9209e25ed09SBarry Smith           fm        = idx;
92156beaf04SBarry Smith           nzf++;
922ecf371e4SBarry Smith         } else {
92356beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
9249e25ed09SBarry Smith         }
92556beaf04SBarry Smith         flev++;
9269e25ed09SBarry Smith       }
9279e25ed09SBarry Smith       row = fill[row];
92856beaf04SBarry Smith       nzi++;
9299e25ed09SBarry Smith     }
9309e25ed09SBarry Smith     /* copy new filled row into permanent storage */
93156beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
932d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
933ecf371e4SBarry Smith 
934ecf371e4SBarry Smith       /* estimate how much additional space we will need */
935ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
936ecf371e4SBarry Smith       /* just double the memory each time */
937ecf371e4SBarry Smith       /*  maxadd = (int)((f*(ai[n]+!shift)*(n-prow+5))/n); */
938ecf371e4SBarry Smith       int maxadd = jmax;
93956beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
940bbb6d6a8SBarry Smith       jmax += maxadd;
941ecf371e4SBarry Smith 
942ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
943b0a32e0cSBarry Smith       ierr   = PetscMalloc(jmax*sizeof(int),&xi);CHKERRQ(ierr);
944549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
945606d414cSSatish Balay       ierr   = PetscFree(ajnew);CHKERRQ(ierr);
94656beaf04SBarry Smith       ajnew  = xi;
947b0a32e0cSBarry Smith       ierr   = PetscMalloc(jmax*sizeof(int),&xi);CHKERRQ(ierr);
948549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
949606d414cSSatish Balay       ierr   = PetscFree(ajfill);CHKERRQ(ierr);
95056beaf04SBarry Smith       ajfill = xi;
951ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
9529e25ed09SBarry Smith     }
953dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
954dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
95556beaf04SBarry Smith     dloc[prow]  = nzi;
9569e25ed09SBarry Smith     fm          = fill[n];
95756beaf04SBarry Smith     while (nzf--) {
958dbb450caSBarry Smith       *xi++   = fm - shift;
95956beaf04SBarry Smith       *flev++ = im[fm];
9609e25ed09SBarry Smith       fm      = fill[fm];
9619e25ed09SBarry Smith     }
9626cf997b0SBarry Smith     /* make sure row has diagonal entry */
9636cf997b0SBarry Smith     if (ajnew[ainew[prow]+shift+dloc[prow]]+shift != prow) {
96429bbc08cSBarry Smith       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %d has missing diagonal in factored matrix\n\
9656cf997b0SBarry Smith     try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow);
9666cf997b0SBarry Smith     }
9679e25ed09SBarry Smith   }
968606d414cSSatish Balay   ierr = PetscFree(ajfill);CHKERRQ(ierr);
969898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
970898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
971606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
972606d414cSSatish Balay   ierr = PetscFree(im);CHKERRQ(ierr);
9739e25ed09SBarry Smith 
974f64065bbSBarry Smith   {
975329f5518SBarry Smith     PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]);
976b0a32e0cSBarry Smith     PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
977c0d6ac57SBarry Smith     PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -[sub_]pc_ilu_fill %g or use \n",af);
978c0d6ac57SBarry Smith     PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill([sub]pc,%g);\n",af);
979b0a32e0cSBarry Smith     PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
980335d9088SBarry Smith     if (diagonal_fill) {
981b1bcba4aSBarry Smith       PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replaced %d missing diagonals",dcount);
982335d9088SBarry Smith     }
983f64065bbSBarry Smith   }
984bbb6d6a8SBarry Smith 
9859e25ed09SBarry Smith   /* put together the new matrix */
986b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr);
987b0a32e0cSBarry Smith   PetscLogObjectParent(*fact,isicol);
988416022c9SBarry Smith   b = (Mat_SeqAIJ*)(*fact)->data;
989606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
9907c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
99187828ca2SBarry Smith   len = (ainew[n] + shift)*sizeof(PetscScalar);
9929e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
993606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
994606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
995b0a32e0cSBarry Smith   ierr = PetscMalloc(len+1,&b->a);CHKERRQ(ierr);
996416022c9SBarry Smith   b->j          = ajnew;
997416022c9SBarry Smith   b->i          = ainew;
99856beaf04SBarry Smith   for (i=0; i<n; i++) dloc[i] += ainew[i];
999416022c9SBarry Smith   b->diag       = dloc;
1000416022c9SBarry Smith   b->ilen       = 0;
1001416022c9SBarry Smith   b->imax       = 0;
1002416022c9SBarry Smith   b->row        = isrow;
1003416022c9SBarry Smith   b->col        = iscol;
1004c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1005c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
100682bf6240SBarry Smith   b->icol       = isicol;
100787828ca2SBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
1008416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
100956beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
101087828ca2SBarry Smith   PetscLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(PetscScalar)));
1011416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
1012a2e34c3dSBarry Smith   b->lu_damping   = info->damping;
101387828ca2SBarry Smith   b->lu_zeropivot = info->zeropivot;
10149e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
10153a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(*fact,PETSC_FALSE);CHKERRQ(ierr);
10163a7fca6bSBarry Smith   (*fact)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */
1017ae068f58SLois Curfman McInnes 
1018ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
1019ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
1020329f5518SBarry Smith   (*fact)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[prow]);
1021329f5518SBarry Smith   (*fact)->factor                 =  FACTOR_LU;
10223a40ed3dSBarry Smith   PetscFunctionReturn(0);
10239e25ed09SBarry Smith }
1024d5d45c9bSBarry Smith 
1025d5d45c9bSBarry Smith 
1026d5d45c9bSBarry Smith 
1027d5d45c9bSBarry Smith 
1028