xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 431e710a740bc1d59ba55ceacd5493df3b074061)
1*431e710aSBarry Smith /*$Id: aijfact.c,v 1.139 1999/12/18 00:44:57 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__
891e9ee9fSBarry Smith #define __FUNC__ "MatOrdering_Flow_SeqAIJ"
991e9ee9fSBarry Smith int MatOrdering_Flow_SeqAIJ(Mat mat,MatOrderingType type,IS *irow,IS *icol)
103b2fbd54SBarry Smith {
113a40ed3dSBarry Smith   PetscFunctionBegin;
123a40ed3dSBarry Smith 
13e3372554SBarry Smith   SETERRQ(PETSC_ERR_SUP,0,"Code not written");
14aa482453SBarry Smith #if !defined(PETSC_USE_DEBUG)
15d64ed03dSBarry Smith   PetscFunctionReturn(0);
16d64ed03dSBarry Smith #endif
173b2fbd54SBarry Smith }
183b2fbd54SBarry Smith 
1986bacbd2SBarry Smith 
2086bacbd2SBarry Smith extern int MatMarkDiagonal_SeqAIJ(Mat);
2186bacbd2SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
2286bacbd2SBarry Smith 
235bd2ddc7SBarry Smith extern int SPARSEKIT2dperm(int*,Scalar*,int*,int*,Scalar*,int*,int*,int*,int*,int*);
245bd2ddc7SBarry Smith extern int SPARSEKIT2ilutp(int*,Scalar*,int*,int*,int*,double*,double*,int*,Scalar*,int*,int*,int*,Scalar*,int*,int*,int*);
255bd2ddc7SBarry Smith extern int SPARSEKIT2msrcsr(int*,Scalar*,int*,Scalar*,int*,int*,Scalar*,int*);
2686bacbd2SBarry Smith 
2786bacbd2SBarry Smith #undef __FUNC__
2886bacbd2SBarry Smith #define __FUNC__ "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;
585bd2ddc7SBarry Smith   int        *c,*r,*ic,ierr, i, n = a->m;
5986bacbd2SBarry Smith   int        *old_i = a->i, *old_j = a->j, *new_i, *old_i2, *old_j2,*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;
635bd2ddc7SBarry Smith   Scalar     *old_a = a->a, *w, *new_a, *old_a2, *wk,*o_a;
64*431e710aSBarry Smith   double     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;
716faa4630SBarry Smith   if (info->fill == PETSC_DEFAULT)    info->fill    = ((double)(n*info->dtcount))/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) {
15086bacbd2SBarry Smith       case -3: SETERRQ1(1,1,"ilutp(), matrix U overflows, need larger info->fill value %d",jmax);
15186bacbd2SBarry Smith       case -2: SETERRQ1(1,1,"ilutp(), matrix L overflows, need larger info->fill value %d",jmax);
15286bacbd2SBarry Smith       case -5: SETERRQ(1,1,"ilutp(), zero row encountered");
15386bacbd2SBarry Smith       case -1: SETERRQ(1,1,"ilutp(), input matrix may be wrong");
15486bacbd2SBarry Smith       case -4: SETERRQ1(1,1,"ilutp(), illegal info->fill value %d",jmax);
15586bacbd2SBarry Smith       default: SETERRQ1(1,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 */
182b19713cbSBarry 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           --*/
209a3a5ab83SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
210a3a5ab83SBarry Smith   ierr = ISInvertPermutation(isrow,&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 
22086bacbd2SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF, n, ordcol, &iscolf);
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 
255*431e710aSBarry Smith   af = ((double)b->nz)/((double)a->nz) + .001;
256*431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:Fill ratio:given %g needed %g\n",info->fill,af);
257*431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
258*431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
259*431e710aSBarry Smith   PLogInfo(A,"MatILUDTFactor_SeqAIJ:for best performance.\n");
260*431e710aSBarry Smith 
26186bacbd2SBarry Smith   PetscFunctionReturn(0);
26286bacbd2SBarry Smith }
26386bacbd2SBarry Smith 
264289bc588SBarry Smith /*
265289bc588SBarry Smith     Factorization code for AIJ format.
266289bc588SBarry Smith */
2675615d1e5SSatish Balay #undef __FUNC__
2685615d1e5SSatish Balay #define __FUNC__ "MatLUFactorSymbolic_SeqAIJ"
269416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
270289bc588SBarry Smith {
271416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
272289bc588SBarry Smith   IS         isicol;
273416022c9SBarry 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;
276289bc588SBarry Smith 
2773a40ed3dSBarry Smith   PetscFunctionBegin;
278d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
279d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
280f1af5d2fSBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square");
2813b2fbd54SBarry Smith 
28278b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol);CHKERRQ(ierr);
283ac121b3dSBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
284ac121b3dSBarry Smith   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
285289bc588SBarry Smith 
286289bc588SBarry Smith   /* get new row pointers */
2870452661fSBarry Smith   ainew    = (int *) PetscMalloc( (n+1)*sizeof(int) );CHKPTRQ(ainew);
288dbb450caSBarry Smith   ainew[0] = -shift;
289289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
290dbb450caSBarry Smith   jmax  = (int) (f*ai[n]+(!shift));
2910452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajnew);
292289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
2930452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int));CHKPTRQ(fill);
2942fb3ed76SBarry Smith   im   = fill + n + 1;
295289bc588SBarry Smith   /* idnew is location of diagonal in factor */
2960452661fSBarry Smith   idnew    = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(idnew);
297dbb450caSBarry Smith   idnew[0] = -shift;
298289bc588SBarry Smith 
299289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
300289bc588SBarry Smith     /* first copy previous fill into linked list */
301289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
3026b96ef82SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
303dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
304289bc588SBarry Smith     fill[n] = n;
305289bc588SBarry Smith     while (nz--) {
306289bc588SBarry Smith       fm  = n;
307dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
308289bc588SBarry Smith       do {
309289bc588SBarry Smith         m  = fm;
310289bc588SBarry Smith         fm = fill[m];
311289bc588SBarry Smith       } while (fm < idx);
312289bc588SBarry Smith       fill[m]   = idx;
313289bc588SBarry Smith       fill[idx] = fm;
314289bc588SBarry Smith     }
315289bc588SBarry Smith     row = fill[n];
316289bc588SBarry Smith     while ( row < i ) {
317dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
3182fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
3192fb3ed76SBarry Smith       nz    = im[row] - nzbd;
320289bc588SBarry Smith       fm    = row;
3212fb3ed76SBarry Smith       while (nz-- > 0) {
322dbb450caSBarry Smith         idx = *ajtmp++ + shift;
3232fb3ed76SBarry Smith         nzbd++;
3242fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
325289bc588SBarry Smith         do {
326289bc588SBarry Smith           m  = fm;
327289bc588SBarry Smith           fm = fill[m];
328289bc588SBarry Smith         } while (fm < idx);
329289bc588SBarry Smith         if (fm != idx) {
330289bc588SBarry Smith           fill[m]   = idx;
331289bc588SBarry Smith           fill[idx] = fm;
332289bc588SBarry Smith           fm        = idx;
333289bc588SBarry Smith           nnz++;
334289bc588SBarry Smith         }
335289bc588SBarry Smith       }
336289bc588SBarry Smith       row = fill[row];
337289bc588SBarry Smith     }
338289bc588SBarry Smith     /* copy new filled row into permanent storage */
339289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
340496e697eSBarry Smith     if (ainew[i+1] > jmax) {
341ecf371e4SBarry Smith 
342ecf371e4SBarry Smith       /* estimate how much additional space we will need */
343ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
344ecf371e4SBarry Smith       /* just double the memory each time */
345ecf371e4SBarry Smith       int maxadd = jmax;
346ecf371e4SBarry Smith       /* maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); */
347bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
3482fb3ed76SBarry Smith       jmax += maxadd;
349ecf371e4SBarry Smith 
350ecf371e4SBarry Smith       /* allocate a longer ajnew */
3510452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
352549d3d68SSatish Balay       ierr  = PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));CHKERRQ(ierr);
353606d414cSSatish Balay       ierr  = PetscFree(ajnew);CHKERRQ(ierr);
354289bc588SBarry Smith       ajnew = ajtmp;
3552fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
356289bc588SBarry Smith     }
357dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
358289bc588SBarry Smith     fm    = fill[n];
359289bc588SBarry Smith     nzi   = 0;
3602fb3ed76SBarry Smith     im[i] = nnz;
361289bc588SBarry Smith     while (nnz--) {
362289bc588SBarry Smith       if (fm < i) nzi++;
363dbb450caSBarry Smith       *ajtmp++ = fm - shift;
364289bc588SBarry Smith       fm       = fill[fm];
365289bc588SBarry Smith     }
366289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
367289bc588SBarry Smith   }
368464e8d44SSatish Balay   if (ai[n] != 0) {
369464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
370c38d4ed2SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
371981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
372981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
373981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
37405bf559cSSatish Balay   } else {
375981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
37605bf559cSSatish Balay   }
3772fb3ed76SBarry Smith 
378898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
379898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
3801987afe7SBarry Smith 
381606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
382289bc588SBarry Smith 
383289bc588SBarry Smith   /* put together the new matrix */
384b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B);CHKERRQ(ierr);
3851987afe7SBarry Smith   PLogObjectParent(*B,isicol);
386416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
387606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
3887c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
389e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
390606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
391606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
39291bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
393416022c9SBarry Smith   b->j          = ajnew;
394416022c9SBarry Smith   b->i          = ainew;
395416022c9SBarry Smith   b->diag       = idnew;
396416022c9SBarry Smith   b->ilen       = 0;
397416022c9SBarry Smith   b->imax       = 0;
398416022c9SBarry Smith   b->row        = isrow;
399416022c9SBarry Smith   b->col        = iscol;
400c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
401c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
40282bf6240SBarry Smith   b->icol       = isicol;
40391bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
404416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
4057fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
406416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
407416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
4087fd98bd6SLois Curfman McInnes 
409e93ccc4dSBarry Smith   (*B)->factor                 =  FACTOR_LU;;
410ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
411ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
412703deb49SSatish Balay   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant if A has inodes */
413703deb49SSatish Balay 
414e93ccc4dSBarry Smith   if (ai[n] != 0) {
415e93ccc4dSBarry Smith     (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[n]);
416eea2913aSSatish Balay   } else {
417eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
418eea2913aSSatish Balay   }
4193a40ed3dSBarry Smith   PetscFunctionReturn(0);
420289bc588SBarry Smith }
4210a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
422184914b5SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
42341c01911SSatish Balay 
4245615d1e5SSatish Balay #undef __FUNC__
4255615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
426416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
427289bc588SBarry Smith {
428416022c9SBarry Smith   Mat        C = *B;
429416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
43082bf6240SBarry Smith   IS         isrow = b->row, isicol = b->icol;
431416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
4321987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
433f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
43435aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
4353a40ed3dSBarry Smith   register   int    *pj;
4368ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
43735aab85fSBarry Smith   double     ssum;
43835aab85fSBarry Smith   register   Scalar *pv, *rtmps,*u_values;
439289bc588SBarry Smith 
4403a40ed3dSBarry Smith   PetscFunctionBegin;
44182bf6240SBarry Smith 
44278b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r);CHKERRQ(ierr);
44378b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
4440452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) );CHKPTRQ(rtmp);
445549d3d68SSatish Balay   ierr  = PetscMemzero(rtmp,(n+1)*sizeof(Scalar));CHKERRQ(ierr);
4460cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
447289bc588SBarry Smith 
4486cf997b0SBarry Smith   /* precalculate row sums */
44935aab85fSBarry Smith   if (preserve_row_sums) {
45035aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) );CHKPTRQ(rowsums);
45135aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
45235aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
45335aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
45435aab85fSBarry Smith       sum = 0.0;
45535aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
45635aab85fSBarry Smith       rowsums[i] = sum;
45735aab85fSBarry Smith     }
45835aab85fSBarry Smith   }
45935aab85fSBarry Smith 
460289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
461289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
462dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
46348e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
464289bc588SBarry Smith 
465289bc588SBarry Smith     /* load in initial (unfactored row) */
466416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
467416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
468416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
4690cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
470289bc588SBarry Smith 
471dbb450caSBarry Smith     row = *ajtmp++ + shift;
472f2582111SSatish Balay     while  (row < i ) {
4738c37ef55SBarry Smith       pc = rtmp + row;
4748c37ef55SBarry Smith       if (*pc != 0.0) {
4751987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
4761987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
47735aab85fSBarry Smith         multiplier = *pc / *pv++;
4788c37ef55SBarry Smith         *pc        = multiplier;
479f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
480f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
481f2582111SSatish Balay         PLogFlops(2*nz);
482289bc588SBarry Smith       }
483dbb450caSBarry Smith       row = *ajtmp++ + shift;
484289bc588SBarry Smith     }
485416022c9SBarry Smith     /* finished row so stick it into b->a */
486416022c9SBarry Smith     pv = b->a + ai[i] + shift;
487416022c9SBarry Smith     pj = b->j + ai[i] + shift;
4888c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
489416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
49035aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
49135aab85fSBarry Smith     /*
49235aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
49335aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
49435aab85fSBarry Smith     */
495d708749eSBarry Smith     if (pv[diag] == 0.0) {
4966cf997b0SBarry Smith       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot row %d",i);
497d708749eSBarry Smith     }
49835aab85fSBarry Smith     if (preserve_row_sums) {
49935aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
50035aab85fSBarry Smith       sum = rowsums[i];
50135aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
50235aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
50335aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
50435aab85fSBarry Smith         inner_sum = 0.0;
50535aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
50635aab85fSBarry Smith           inner_sum += u_values[k];
50735aab85fSBarry Smith         }
50835aab85fSBarry Smith         sum -= pv[j]*inner_sum;
50935aab85fSBarry Smith 
51035aab85fSBarry Smith       }
51135aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
51235aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
51335aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
51435aab85fSBarry Smith         sum -= u_values[k];
51535aab85fSBarry Smith       }
51635aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
51735aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
51835aab85fSBarry Smith     }
51935aab85fSBarry Smith     /* check pivot entry for current row */
5208c37ef55SBarry Smith   }
5210f11f9aeSSatish Balay 
52235aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
52335aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
52435aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
52535aab85fSBarry Smith   }
52635aab85fSBarry Smith 
527606d414cSSatish Balay   if (preserve_row_sums) {ierr = PetscFree(rowsums);CHKERRQ(ierr);}
528606d414cSSatish Balay   ierr = PetscFree(rtmp);CHKERRQ(ierr);
52978b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
53078b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
531416022c9SBarry Smith   C->factor = FACTOR_LU;
53241c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C);CHKERRQ(ierr);
533c456f294SBarry Smith   C->assembled = PETSC_TRUE;
534416022c9SBarry Smith   PLogFlops(b->n);
5353a40ed3dSBarry Smith   PetscFunctionReturn(0);
536289bc588SBarry Smith }
5370a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5385615d1e5SSatish Balay #undef __FUNC__
5395615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
540416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
541da3a660dSBarry Smith {
542416022c9SBarry Smith   Mat_SeqAIJ     *mat = (Mat_SeqAIJ *) A->data;
54386bacbd2SBarry Smith   int            ierr,refct;
544416022c9SBarry Smith   Mat            C;
545f830108cSBarry Smith   PetscOps       *Abops;
5460a6ffc59SBarry Smith   MatOps         Aops;
547416022c9SBarry Smith 
5483a40ed3dSBarry Smith   PetscFunctionBegin;
549f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C);CHKERRQ(ierr);
550f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C);CHKERRQ(ierr);
551da3a660dSBarry Smith 
552da3a660dSBarry Smith   /* free all the data structures from mat */
553606d414cSSatish Balay   ierr = PetscFree(mat->a);CHKERRQ(ierr);
554606d414cSSatish Balay   if (!mat->singlemalloc) {
555606d414cSSatish Balay     ierr = PetscFree(mat->i);CHKERRQ(ierr);
556606d414cSSatish Balay     ierr = PetscFree(mat->j);CHKERRQ(ierr);
557606d414cSSatish Balay   }
558606d414cSSatish Balay   if (mat->diag) {ierr = PetscFree(mat->diag);CHKERRQ(ierr);}
559606d414cSSatish Balay   if (mat->ilen) {ierr = PetscFree(mat->ilen);CHKERRQ(ierr);}
560606d414cSSatish Balay   if (mat->imax) {ierr = PetscFree(mat->imax);CHKERRQ(ierr);}
561606d414cSSatish Balay   if (mat->solve_work) {ierr = PetscFree(mat->solve_work);CHKERRQ(ierr);}
562606d414cSSatish Balay   if (mat->inode.size) {ierr = PetscFree(mat->inode.size);CHKERRQ(ierr);}
5630f0aacb9SBarry Smith   if (mat->icol) {ierr = ISDestroy(mat->icol);CHKERRQ(ierr);}
564606d414cSSatish Balay   ierr = PetscFree(mat);CHKERRQ(ierr);
565da3a660dSBarry Smith 
56617642b18SBarry Smith   ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
56717642b18SBarry Smith   ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
56817642b18SBarry Smith 
569f830108cSBarry Smith   /*
570f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
571f830108cSBarry Smith     A pointers for the bops and ops but copy everything
572f830108cSBarry Smith     else from C.
573f830108cSBarry Smith   */
574f830108cSBarry Smith   Abops = A->bops;
575f830108cSBarry Smith   Aops  = A->ops;
57686bacbd2SBarry Smith   refct = A->refct;
577549d3d68SSatish Balay   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
578451c4af8SSatish Balay   mat   = (Mat_SeqAIJ *) A->data;
579451c4af8SSatish Balay   PLogObjectParent(A,mat->icol);
580451c4af8SSatish Balay 
581f830108cSBarry Smith   A->bops  = Abops;
582f830108cSBarry Smith   A->ops   = Aops;
583bef8e0ddSBarry Smith   A->qlist = 0;
58486bacbd2SBarry Smith   A->refct = refct;
585c38d4ed2SBarry Smith   /* copy over the type_name and name */
586c38d4ed2SBarry Smith   ierr     = PetscStrallocpy(C->type_name,&A->type_name);CHKERRQ(ierr);
587c38d4ed2SBarry Smith   ierr     = PetscStrallocpy(C->name,&A->name);CHKERRQ(ierr);
588f830108cSBarry Smith 
5890452661fSBarry Smith   PetscHeaderDestroy(C);
590c38d4ed2SBarry Smith 
5913a40ed3dSBarry Smith   PetscFunctionReturn(0);
592da3a660dSBarry Smith }
5930a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5945615d1e5SSatish Balay #undef __FUNC__
5955615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
596416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
5978c37ef55SBarry Smith {
598416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
599416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
600416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
6013b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
602416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
6038c37ef55SBarry Smith 
6043a40ed3dSBarry Smith   PetscFunctionBegin;
6053a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
60691bf9adeSBarry Smith 
607e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
608e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
609416022c9SBarry Smith   tmp  = a->solve_work;
6108c37ef55SBarry Smith 
6113b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
6123b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
6138c37ef55SBarry Smith 
6148c37ef55SBarry Smith   /* forward solve the lower triangular */
6158c37ef55SBarry Smith   tmp[0] = b[*r++];
6160cf60383SBarry Smith   tmps   = tmp + shift;
6178c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
618dbb450caSBarry Smith     v   = aa + ai[i] + shift;
619dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
620416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
6218c37ef55SBarry Smith     sum = b[*r++];
6220cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
6238c37ef55SBarry Smith     tmp[i] = sum;
6248c37ef55SBarry Smith   }
6258c37ef55SBarry Smith 
6268c37ef55SBarry Smith   /* backward solve the upper triangular */
6278c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
628416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
629416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
630416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
6318c37ef55SBarry Smith     sum = tmp[i];
6320cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
633416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
6348c37ef55SBarry Smith   }
6358c37ef55SBarry Smith 
6363b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
6373b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
638cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
639cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
640416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
6413a40ed3dSBarry Smith   PetscFunctionReturn(0);
6428c37ef55SBarry Smith }
643026e39d0SSatish Balay 
644930ae53cSBarry Smith /* ----------------------------------------------------------- */
645930ae53cSBarry Smith #undef __FUNC__
646930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering"
647930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx)
648930ae53cSBarry Smith {
649930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
650d85afc42SSatish Balay   int        n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr;
651d85afc42SSatish Balay   Scalar     *x,*b, *aa = a->a, sum;
652aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
653d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
654d85afc42SSatish Balay   Scalar     *v;
655d85afc42SSatish Balay #endif
656930ae53cSBarry Smith 
6573a40ed3dSBarry Smith   PetscFunctionBegin;
6583a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
659930ae53cSBarry Smith   if (a->indexshift) {
6603a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
6613a40ed3dSBarry Smith      PetscFunctionReturn(0);
662930ae53cSBarry Smith   }
663930ae53cSBarry Smith 
664e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
665e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
666930ae53cSBarry Smith 
667aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
6681c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
6691c4feecaSSatish Balay #else
670930ae53cSBarry Smith   /* forward solve the lower triangular */
671930ae53cSBarry Smith   x[0] = b[0];
672930ae53cSBarry Smith   for ( i=1; i<n; i++ ) {
673930ae53cSBarry Smith     ai_i = ai[i];
674930ae53cSBarry Smith     v    = aa + ai_i;
675930ae53cSBarry Smith     vi   = aj + ai_i;
676930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
677930ae53cSBarry Smith     sum  = b[i];
678930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
679930ae53cSBarry Smith     x[i] = sum;
680930ae53cSBarry Smith   }
681930ae53cSBarry Smith 
682930ae53cSBarry Smith   /* backward solve the upper triangular */
683930ae53cSBarry Smith   for ( i=n-1; i>=0; i-- ){
684930ae53cSBarry Smith     adiag_i = adiag[i];
685d708749eSBarry Smith     v       = aa + adiag_i + 1;
686d708749eSBarry Smith     vi      = aj + adiag_i + 1;
687930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
688930ae53cSBarry Smith     sum     = x[i];
689930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
690930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
691930ae53cSBarry Smith   }
6921c4feecaSSatish Balay #endif
693930ae53cSBarry Smith   PLogFlops(2*a->nz - a->n);
694cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
695cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
6963a40ed3dSBarry Smith   PetscFunctionReturn(0);
697930ae53cSBarry Smith }
698930ae53cSBarry Smith 
6995615d1e5SSatish Balay #undef __FUNC__
7005615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
701416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
702da3a660dSBarry Smith {
703416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
704416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
705416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
7063b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
707416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
708da3a660dSBarry Smith 
7093a40ed3dSBarry Smith   PetscFunctionBegin;
71078b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);}
711da3a660dSBarry Smith 
712e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
713e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
714416022c9SBarry Smith   tmp  = a->solve_work;
715da3a660dSBarry Smith 
7163b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7173b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
718da3a660dSBarry Smith 
719da3a660dSBarry Smith   /* forward solve the lower triangular */
720da3a660dSBarry Smith   tmp[0] = b[*r++];
721da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
722dbb450caSBarry Smith     v   = aa + ai[i] + shift;
723dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
724416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
725da3a660dSBarry Smith     sum = b[*r++];
726dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
727da3a660dSBarry Smith     tmp[i] = sum;
728da3a660dSBarry Smith   }
729da3a660dSBarry Smith 
730da3a660dSBarry Smith   /* backward solve the upper triangular */
731da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
732416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
733416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
734416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
735da3a660dSBarry Smith     sum = tmp[i];
736dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
737416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
738da3a660dSBarry Smith     x[*c--] += tmp[i];
739da3a660dSBarry Smith   }
740da3a660dSBarry Smith 
7413b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
7423b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
743cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
744cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
745416022c9SBarry Smith   PLogFlops(2*a->nz);
746898183ecSLois Curfman McInnes 
7473a40ed3dSBarry Smith   PetscFunctionReturn(0);
748da3a660dSBarry Smith }
749da3a660dSBarry Smith /* -------------------------------------------------------------------*/
7505615d1e5SSatish Balay #undef __FUNC__
7517c922b88SBarry Smith #define __FUNC__ "MatSolveTranspose_SeqAIJ"
7527c922b88SBarry Smith int MatSolveTranspose_SeqAIJ(Mat A,Vec bb, Vec xx)
753da3a660dSBarry Smith {
754416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7552235e667SBarry Smith   IS         iscol = a->col, isrow = a->row;
756416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
757f1af5d2fSBarry Smith   int        nz,shift = a->indexshift,*rout,*cout, *diag = a->diag;
758f1af5d2fSBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v, s1;
759da3a660dSBarry Smith 
7603a40ed3dSBarry Smith   PetscFunctionBegin;
761e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
762e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
763416022c9SBarry Smith   tmp  = a->solve_work;
764da3a660dSBarry Smith 
7652235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7662235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
767da3a660dSBarry Smith 
768da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
7692235e667SBarry Smith   for ( i=0; i<n; i++ ) tmp[i] = b[c[i]];
770da3a660dSBarry Smith 
771da3a660dSBarry Smith   /* forward solve the U^T */
772da3a660dSBarry 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;
776c38d4ed2SBarry Smith     s1  = tmp[i];
777c38d4ed2SBarry Smith     s1 *= *(v++);  /* multiply by inverse of diagonal entry */
778da3a660dSBarry Smith     while (nz--) {
779f1af5d2fSBarry Smith       tmp[*vi++ + shift] -= (*v++)*s1;
780da3a660dSBarry Smith     }
781c38d4ed2SBarry Smith     tmp[i] = s1;
782da3a660dSBarry Smith   }
783da3a660dSBarry Smith 
784da3a660dSBarry Smith   /* backward solve the L^T */
785da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
786f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
787f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
788f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
789f1af5d2fSBarry Smith     s1  = tmp[i];
790da3a660dSBarry Smith     while (nz--) {
791f1af5d2fSBarry Smith       tmp[*vi-- + shift] -= (*v--)*s1;
792da3a660dSBarry Smith     }
793da3a660dSBarry Smith   }
794da3a660dSBarry Smith 
795da3a660dSBarry Smith   /* copy tmp into x according to permutation */
796da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
797da3a660dSBarry Smith 
7982235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
7992235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
800cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
801cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
802da3a660dSBarry Smith 
803416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
8043a40ed3dSBarry Smith   PetscFunctionReturn(0);
805da3a660dSBarry Smith }
806da3a660dSBarry Smith 
8075615d1e5SSatish Balay #undef __FUNC__
8087c922b88SBarry Smith #define __FUNC__ "MatSolveTransposeAdd_SeqAIJ"
8097c922b88SBarry Smith int MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
810da3a660dSBarry Smith {
811416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
8122235e667SBarry Smith   IS         iscol = a->col, isrow = a->row;
813416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
814f1af5d2fSBarry Smith   int        nz,shift = a->indexshift, *rout, *cout, *diag = a->diag;
815416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
8166abc6512SBarry Smith 
8173a40ed3dSBarry Smith   PetscFunctionBegin;
8186abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
8196abc6512SBarry Smith 
820e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
821e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
822416022c9SBarry Smith   tmp = a->solve_work;
8236abc6512SBarry Smith 
8242235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
8252235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
8266abc6512SBarry Smith 
8276abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
8282235e667SBarry Smith   for ( i=0; i<n; i++ ) tmp[i] = b[c[i]];
8296abc6512SBarry Smith 
8306abc6512SBarry Smith   /* forward solve the U^T */
8316abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
832f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
833f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
834f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
8356abc6512SBarry Smith     tmp[i] *= *v++;
8366abc6512SBarry Smith     while (nz--) {
837dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
8386abc6512SBarry Smith     }
8396abc6512SBarry Smith   }
8406abc6512SBarry Smith 
8416abc6512SBarry Smith   /* backward solve the L^T */
8426abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
843f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
844f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
845f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
8466abc6512SBarry Smith     while (nz--) {
847dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
8486abc6512SBarry Smith     }
8496abc6512SBarry Smith   }
8506abc6512SBarry Smith 
8516abc6512SBarry Smith   /* copy tmp into x according to permutation */
8526abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
8536abc6512SBarry Smith 
8542235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
8552235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
856cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
857cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8586abc6512SBarry Smith 
859416022c9SBarry Smith   PLogFlops(2*a->nz);
8603a40ed3dSBarry Smith   PetscFunctionReturn(0);
861da3a660dSBarry Smith }
8629e25ed09SBarry Smith /* ----------------------------------------------------------------*/
8637c922b88SBarry Smith extern int MatMissingDiagonal_SeqAIJ(Mat);
86408480c60SBarry Smith 
8655615d1e5SSatish Balay #undef __FUNC__
8665615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
8676cf997b0SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatILUInfo *info,Mat *fact)
8689e25ed09SBarry Smith {
869416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
8709e25ed09SBarry Smith   IS         isicol;
871416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
87256beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
873335d9088SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0, dcount = 0;
8746cf997b0SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift,levels,diagonal_fill;
87577c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
8766cf997b0SBarry Smith   double     f;
8779e25ed09SBarry Smith 
8783a40ed3dSBarry Smith   PetscFunctionBegin;
8796cf997b0SBarry Smith   if (info) {
8806cf997b0SBarry Smith     f             = info->fill;
8810cd17293SBarry Smith     levels        = (int) info->levels;
8820cd17293SBarry Smith     diagonal_fill = (int) info->diagonal_fill;
8836cf997b0SBarry Smith   } else {
8846cf997b0SBarry Smith     f             = 1.0;
8856cf997b0SBarry Smith     levels        = 0;
8866cf997b0SBarry Smith     diagonal_fill = 0;
8876cf997b0SBarry Smith   }
88882bf6240SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol);CHKERRQ(ierr);
88982bf6240SBarry Smith 
89008480c60SBarry Smith   /* special case that simply copies fill pattern */
891be0abb6dSBarry Smith   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
892be0abb6dSBarry Smith   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
89386bacbd2SBarry Smith   if (!levels && row_identity && col_identity) {
8942e8a6d31SBarry Smith     ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact);CHKERRQ(ierr);
89508480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
89608480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
89708480c60SBarry Smith     if (!b->diag) {
8987c922b88SBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
89908480c60SBarry Smith     }
9007c922b88SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
90108480c60SBarry Smith     b->row              = isrow;
90208480c60SBarry Smith     b->col              = iscol;
90382bf6240SBarry Smith     b->icol             = isicol;
9040452661fSBarry Smith     b->solve_work       = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
905f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
906c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
907c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
9083a40ed3dSBarry Smith     PetscFunctionReturn(0);
90908480c60SBarry Smith   }
91008480c60SBarry Smith 
911898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
912898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
9139e25ed09SBarry Smith 
9149e25ed09SBarry Smith   /* get new row pointers */
9150452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) );CHKPTRQ(ainew);
916dbb450caSBarry Smith   ainew[0] = -shift;
9179e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
918dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
9190452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajnew);
9209e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
9210452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajfill);
9229e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
9230452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(fill);
92456beaf04SBarry Smith   /* im is level for each filled value */
9250452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(im);
92656beaf04SBarry Smith   /* dloc is location of diagonal in factor */
9270452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(dloc);
92856beaf04SBarry Smith   dloc[0]  = 0;
92956beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
9306cf997b0SBarry Smith 
9316cf997b0SBarry Smith     /* copy current row into linked list */
93256beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
933385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
934dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
9359e25ed09SBarry Smith     fill[n]    = n;
936435faa5fSBarry Smith     fill[prow] = -1; /* marker to indicate if diagonal exists */
9379e25ed09SBarry Smith     while (nz--) {
9389e25ed09SBarry Smith       fm  = n;
939dbb450caSBarry Smith       idx = ic[*xi++ + shift];
9409e25ed09SBarry Smith       do {
9419e25ed09SBarry Smith         m  = fm;
9429e25ed09SBarry Smith         fm = fill[m];
9439e25ed09SBarry Smith       } while (fm < idx);
9449e25ed09SBarry Smith       fill[m]   = idx;
9459e25ed09SBarry Smith       fill[idx] = fm;
94656beaf04SBarry Smith       im[idx]   = 0;
9479e25ed09SBarry Smith     }
9486cf997b0SBarry Smith 
9496cf997b0SBarry Smith     /* make sure diagonal entry is included */
950435faa5fSBarry Smith     if (diagonal_fill && fill[prow] == -1) {
9516cf997b0SBarry Smith       fm = n;
952435faa5fSBarry Smith       while (fill[fm] < prow) fm = fill[fm];
953435faa5fSBarry Smith       fill[prow] = fill[fm]; /* insert diagonal into linked list */
954435faa5fSBarry Smith       fill[fm]   = prow;
9556cf997b0SBarry Smith       im[prow]   = 0;
9566cf997b0SBarry Smith       nzf++;
957335d9088SBarry Smith       dcount++;
9586cf997b0SBarry Smith     }
9596cf997b0SBarry Smith 
96056beaf04SBarry Smith     nzi = 0;
9619e25ed09SBarry Smith     row = fill[n];
96256beaf04SBarry Smith     while ( row < prow ) {
96356beaf04SBarry Smith       incrlev = im[row] + 1;
96456beaf04SBarry Smith       nz      = dloc[row];
9656cf997b0SBarry Smith       xi      = ajnew  + ainew[row] + shift + nz + 1;
966dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
967416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
96856beaf04SBarry Smith       fm      = row;
96956beaf04SBarry Smith       while (nnz-- > 0) {
970dbb450caSBarry Smith         idx = *xi++ + shift;
97156beaf04SBarry Smith         if (*flev + incrlev > levels) {
97256beaf04SBarry Smith           flev++;
97356beaf04SBarry Smith           continue;
97456beaf04SBarry Smith         }
97556beaf04SBarry Smith         do {
9769e25ed09SBarry Smith           m  = fm;
9779e25ed09SBarry Smith           fm = fill[m];
97856beaf04SBarry Smith         } while (fm < idx);
9799e25ed09SBarry Smith         if (fm != idx) {
98056beaf04SBarry Smith           im[idx]   = *flev + incrlev;
9819e25ed09SBarry Smith           fill[m]   = idx;
9829e25ed09SBarry Smith           fill[idx] = fm;
9839e25ed09SBarry Smith           fm        = idx;
98456beaf04SBarry Smith           nzf++;
985ecf371e4SBarry Smith         } else {
98656beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
9879e25ed09SBarry Smith         }
98856beaf04SBarry Smith         flev++;
9899e25ed09SBarry Smith       }
9909e25ed09SBarry Smith       row = fill[row];
99156beaf04SBarry Smith       nzi++;
9929e25ed09SBarry Smith     }
9939e25ed09SBarry Smith     /* copy new filled row into permanent storage */
99456beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
995d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
996ecf371e4SBarry Smith 
997ecf371e4SBarry Smith       /* estimate how much additional space we will need */
998ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
999ecf371e4SBarry Smith       /* just double the memory each time */
1000ecf371e4SBarry Smith       /*  maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */
1001ecf371e4SBarry Smith       int maxadd = jmax;
100256beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
1003bbb6d6a8SBarry Smith       jmax += maxadd;
1004ecf371e4SBarry Smith 
1005ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
10060452661fSBarry Smith       xi     = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
1007549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
1008606d414cSSatish Balay       ierr = PetscFree(ajnew);CHKERRQ(ierr);
100956beaf04SBarry Smith       ajnew  = xi;
10100452661fSBarry Smith       xi     = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
1011549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
1012606d414cSSatish Balay       ierr = PetscFree(ajfill);CHKERRQ(ierr);
101356beaf04SBarry Smith       ajfill = xi;
1014ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
10159e25ed09SBarry Smith     }
1016dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
1017dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
101856beaf04SBarry Smith     dloc[prow]  = nzi;
10199e25ed09SBarry Smith     fm          = fill[n];
102056beaf04SBarry Smith     while (nzf--) {
1021dbb450caSBarry Smith       *xi++   = fm - shift;
102256beaf04SBarry Smith       *flev++ = im[fm];
10239e25ed09SBarry Smith       fm      = fill[fm];
10249e25ed09SBarry Smith     }
10256cf997b0SBarry Smith     /* make sure row has diagonal entry */
10266cf997b0SBarry Smith     if (ajnew[ainew[prow]+shift+dloc[prow]]+shift != prow) {
10276cf997b0SBarry Smith       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,1,"Row %d has missing diagonal in factored matrix\n\
10286cf997b0SBarry Smith     try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow);
10296cf997b0SBarry Smith     }
10309e25ed09SBarry Smith   }
1031606d414cSSatish Balay   ierr = PetscFree(ajfill); CHKERRQ(ierr);
1032898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
1033898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
1034606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
1035606d414cSSatish Balay   ierr = PetscFree(im);CHKERRQ(ierr);
10369e25ed09SBarry Smith 
1037f64065bbSBarry Smith   {
1038464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
1039c38d4ed2SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
1040981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
1041981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
1042981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
1043335d9088SBarry Smith     if (diagonal_fill) {
1044335d9088SBarry Smith       PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replace %d missing diagonals",dcount);
1045335d9088SBarry Smith     }
1046f64065bbSBarry Smith   }
1047bbb6d6a8SBarry Smith 
10489e25ed09SBarry Smith   /* put together the new matrix */
1049b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr);
1050fa6007c9SSatish Balay   PLogObjectParent(*fact,isicol);
1051416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
1052606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
10537c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
1054dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
10559e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
1056606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
1057606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
10586b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 );CHKPTRQ(b->a);
1059416022c9SBarry Smith   b->j          = ajnew;
1060416022c9SBarry Smith   b->i          = ainew;
106156beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
1062416022c9SBarry Smith   b->diag       = dloc;
1063416022c9SBarry Smith   b->ilen       = 0;
1064416022c9SBarry Smith   b->imax       = 0;
1065416022c9SBarry Smith   b->row        = isrow;
1066416022c9SBarry Smith   b->col        = iscol;
1067c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1068c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
106982bf6240SBarry Smith   b->icol       = isicol;
107082bf6240SBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
1071416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
107256beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
1073dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
1074416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
10759e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
1076ae068f58SLois Curfman McInnes 
1077ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
1078ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
1079ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
1080e93ccc4dSBarry Smith   (*fact)->factor                 =  FACTOR_LU;;
1081ae068f58SLois Curfman McInnes 
10823a40ed3dSBarry Smith   PetscFunctionReturn(0);
10839e25ed09SBarry Smith }
1084d5d45c9bSBarry Smith 
1085d5d45c9bSBarry Smith 
1086d5d45c9bSBarry Smith 
1087d5d45c9bSBarry Smith 
1088