xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 313ae0245cff3ba7c286a22eaeb55dfedd91614e)
1*313ae024SBarry Smith /*$Id: aijfact.c,v 1.132 1999/12/16 23:11:29 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 
2386bacbd2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && defined(PETSC_HAVE_SAADILUDT)
2486bacbd2SBarry Smith 
2586bacbd2SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
2686bacbd2SBarry Smith #define dperm_  DPERM
2786bacbd2SBarry Smith #define ilutp_  ILUTP
2886bacbd2SBarry Smith #define ilu0_   ILU0
2986bacbd2SBarry Smith #define msrcsr_ MSRCSR
3086bacbd2SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
3186bacbd2SBarry Smith #define dperm_  dperm
3286bacbd2SBarry Smith #define ilutp_  ilutp
3386bacbd2SBarry Smith #define ilu0_   ilu0
3486bacbd2SBarry Smith #define msrcsr_ msrcsr
3586bacbd2SBarry Smith #endif
3686bacbd2SBarry Smith 
3786bacbd2SBarry Smith EXTERN_C_BEGIN
3886bacbd2SBarry Smith extern void dperm_(int*,double*,int*,int*,double*,int*,int*,int*,int*,int*);
3986bacbd2SBarry Smith extern void ilutp_(int*,double*,int*,int*,int*,double*,double*,int*,double*,int*,int*,int*,double*,int*,int*,int*);
4086bacbd2SBarry Smith extern void msrcsr_(int*,double*,int*,double*,int*,int*,double*,int*);
4186bacbd2SBarry Smith extern void ilu0_(int*,double*,int*,int*,double*,int*,int*,int*,int *);
4286bacbd2SBarry Smith EXTERN_C_END
4386bacbd2SBarry Smith 
4486bacbd2SBarry Smith #undef __FUNC__
4586bacbd2SBarry Smith #define __FUNC__ "MatILUDTFactor_SeqAIJ"
4686bacbd2SBarry Smith   /* ------------------------------------------------------------
4786bacbd2SBarry Smith 
4886bacbd2SBarry Smith           This interface was contribed by Tony Caola
4986bacbd2SBarry Smith 
5086bacbd2SBarry Smith      This routine is an interface to the pivoting drop-tolerance
5186bacbd2SBarry Smith      ILU routine written by Yousef Saad (saad@cs.umn.edu).  It
5286bacbd2SBarry Smith      was inspired by the non-pivoting iludt written by David
5386bacbd2SBarry Smith      Hysom (hysom@cs.odu.edu).
5486bacbd2SBarry Smith 
5586bacbd2SBarry Smith      Thanks to Prof. Saad, Dr. Hysom, and Dr. Smith for their
5686bacbd2SBarry Smith      help in getting this routine ironed out.
5786bacbd2SBarry Smith 
5886bacbd2SBarry Smith      As of right now, there are a couple of things that could
5986bacbd2SBarry Smith      be, uh, better.
6086bacbd2SBarry Smith 
6186bacbd2SBarry Smith      1 - Since Saad's routine is Fortran based, memory cannot be
6286bacbd2SBarry Smith      malloc'd.  I was trying to get the expected fill from the
6386bacbd2SBarry Smith      preconditioner and use this number as the multiplier in
6486bacbd2SBarry Smith      the equation for jmax, below, but couldn't figure it out.
6586bacbd2SBarry Smith      Anyway, perhaps a better solution is to run SPARSKIT through
6686bacbd2SBarry Smith      f2c and incorporate mallocs(), but I want to graduate so I'll
6786bacbd2SBarry Smith      just rebuild Petsc. . .
6886bacbd2SBarry Smith 
6986bacbd2SBarry Smith      shift = 1, ishift = 0, for indices start at 1
7086bacbd2SBarry Smith      shift = 0, ishift = 1, for indices starting at 0
7186bacbd2SBarry Smith      ------------------------------------------------------------
7286bacbd2SBarry Smith   */
7386bacbd2SBarry Smith 
7486bacbd2SBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,MatILUInfo *info,IS isrow,IS iscol,Mat *fact)
7586bacbd2SBarry Smith {
7686bacbd2SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
7786bacbd2SBarry Smith   IS         iscolf, isrowf, isicol, isirow;
7886bacbd2SBarry Smith   PetscTruth reorder;
7986bacbd2SBarry Smith   int        *c,*r,*ic,*ir, ierr, i, n = a->m;
8086bacbd2SBarry Smith   int        *old_i = a->i, *old_j = a->j, *new_i, *old_i2, *old_j2,*new_j;
81*313ae024SBarry Smith   int        *ordcol, *iwk,*iperm, *jw;
8286bacbd2SBarry Smith   int        ishift = !a->indexshift,shift = -a->indexshift;
83b19713cbSBarry Smith   int        jmax,lfill,job,*o_i,*o_j;
84b19713cbSBarry Smith   Scalar     *old_a = a->a, *w, *new_a, *old_a2, *wk,permtol=0.0,*o_a;
8586bacbd2SBarry Smith 
8686bacbd2SBarry Smith   PetscFunctionBegin;
8786bacbd2SBarry Smith 
8886bacbd2SBarry Smith   if (info->dt == PETSC_DEFAULT)      info->dt      = .005;
8986bacbd2SBarry Smith   if (info->dtcount == PETSC_DEFAULT) info->dtcount = (int) (1.5*a->rmax);
9086bacbd2SBarry Smith   if (info->dtcol == PETSC_DEFAULT)   info->dtcol   = .01;
9186bacbd2SBarry Smith   if (info->fill == PETSC_DEFAULT)    info->fill    = (n*info->dtcount)/a->nz;
9286bacbd2SBarry Smith   lfill   = info->dtcount/2;
9386bacbd2SBarry Smith   jmax    = info->fill*a->nz;
9486bacbd2SBarry Smith   permtol = info->dtcol;
9586bacbd2SBarry Smith 
9686bacbd2SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
9786bacbd2SBarry Smith   ierr = ISInvertPermutation(isrow,&isirow); CHKERRQ(ierr);
9886bacbd2SBarry Smith 
9986bacbd2SBarry Smith 
10086bacbd2SBarry Smith   /* ------------------------------------------------------------
10186bacbd2SBarry Smith      If reorder=.TRUE., then the original matrix has to be
10286bacbd2SBarry Smith      reordered to reflect the user selected ordering scheme, and
10386bacbd2SBarry Smith      then de-reordered so it is in it's original format.
10486bacbd2SBarry Smith      Because Saad's dperm() is NOT in place, we have to copy
10586bacbd2SBarry Smith      the original matrix and allocate more storage. . .
10686bacbd2SBarry Smith      ------------------------------------------------------------
10786bacbd2SBarry Smith   */
10886bacbd2SBarry Smith 
10986bacbd2SBarry Smith   /* set reorder to true if either isrow or iscol is not identity */
11086bacbd2SBarry Smith   ierr = ISIdentity(isrow,&reorder);CHKERRQ(ierr);
11186bacbd2SBarry Smith   if (reorder) {ierr = ISIdentity(iscol,&reorder);CHKERRQ(ierr);}
11286bacbd2SBarry Smith   reorder = PetscNot(reorder);
11386bacbd2SBarry Smith 
11486bacbd2SBarry Smith 
11586bacbd2SBarry Smith   /* storage for ilu factor */
11686bacbd2SBarry Smith   new_i = (int *)    PetscMalloc((n+1)*sizeof(int));   CHKPTRQ(new_i);
11786bacbd2SBarry Smith   new_j = (int *)    PetscMalloc(jmax*sizeof(int));    CHKPTRQ(new_j);
11886bacbd2SBarry Smith   new_a = (Scalar *) PetscMalloc(jmax*sizeof(Scalar)); CHKPTRQ(new_a);
11986bacbd2SBarry Smith 
12086bacbd2SBarry Smith   ordcol = (int *) PetscMalloc(n*sizeof(int)); CHKPTRQ(ordcol);
12186bacbd2SBarry Smith 
12286bacbd2SBarry Smith   /* ------------------------------------------------------------
12386bacbd2SBarry Smith      Make sure that everything is Fortran formatted (1-Based)
12486bacbd2SBarry Smith      ------------------------------------------------------------
12586bacbd2SBarry Smith   */
126b19713cbSBarry Smith   if (ishift) {
127b19713cbSBarry Smith     for (i=old_i[0];i<old_i[n];i++) {
128b19713cbSBarry Smith       old_j[i]++;
12986bacbd2SBarry Smith     }
130b19713cbSBarry Smith     for(i=0;i<n+1;i++) {
131b19713cbSBarry Smith       old_i[i]++;
132b19713cbSBarry Smith     };
13386bacbd2SBarry Smith   };
13486bacbd2SBarry Smith 
13586bacbd2SBarry Smith   if (reorder) {
136c0c2c81eSBarry Smith     ierr = ISGetIndices(iscol,&c);           CHKERRQ(ierr);
137c0c2c81eSBarry Smith     ierr = ISGetIndices(isrow,&r);           CHKERRQ(ierr);
138c0c2c81eSBarry Smith     for(i=0;i<n;i++) {
139c0c2c81eSBarry Smith       r[i]  = r[i]+1;
140c0c2c81eSBarry Smith       c[i]  = c[i]+1;
141c0c2c81eSBarry Smith     }
142*313ae024SBarry Smith     old_i2 = (int *) PetscMalloc((n+1)*sizeof(int)); CHKPTRQ(old_i2);
143*313ae024SBarry Smith     old_j2 = (int *) PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(int)); CHKPTRQ(old_j2);
144*313ae024SBarry Smith     old_a2 = (Scalar *) PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(Scalar));CHKPTRQ(old_a2);
145*313ae024SBarry Smith     job = 3; dperm_(&n,old_a,old_j,old_i,old_a2,old_j2,old_i2,r,c,&job);
146c0c2c81eSBarry Smith     for (i=0;i<n;i++) {
147c0c2c81eSBarry Smith       r[i]  = r[i]-1;
148c0c2c81eSBarry Smith       c[i]  = c[i]-1;
149c0c2c81eSBarry Smith     }
150c0c2c81eSBarry Smith     ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
151c0c2c81eSBarry Smith     ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
152b19713cbSBarry Smith     o_a = old_a2;
153b19713cbSBarry Smith     o_j = old_j2;
154b19713cbSBarry Smith     o_i = old_i2;
155b19713cbSBarry Smith   } else {
156b19713cbSBarry Smith     o_a = old_a;
157b19713cbSBarry Smith     o_j = old_j;
158b19713cbSBarry Smith     o_i = old_i;
15986bacbd2SBarry Smith   }
16086bacbd2SBarry Smith 
16186bacbd2SBarry Smith   /* ------------------------------------------------------------
16286bacbd2SBarry Smith      Call Saad's ilutp() routine to generate the factorization
16386bacbd2SBarry Smith      ------------------------------------------------------------
16486bacbd2SBarry Smith   */
16586bacbd2SBarry Smith 
16686bacbd2SBarry Smith   iperm   = (int *)    PetscMalloc(2*n*sizeof(int)); CHKPTRQ(iperm);
16786bacbd2SBarry Smith   jw      = (int *)    PetscMalloc(2*n*sizeof(int)); CHKPTRQ(jw);
16886bacbd2SBarry Smith   w       = (Scalar *) PetscMalloc(n*sizeof(Scalar)); CHKPTRQ(w);
16986bacbd2SBarry Smith 
170b19713cbSBarry Smith   ilutp_(&n,o_a,o_j,o_i,&lfill,&info->dt,&permtol,&n,new_a,new_j,new_i,&jmax,w,jw,iperm,&ierr);
17186bacbd2SBarry Smith   if (ierr) {
17286bacbd2SBarry Smith     switch (ierr) {
17386bacbd2SBarry Smith       case -3: SETERRQ1(1,1,"ilutp(), matrix U overflows, need larger info->fill value %d",jmax);
17486bacbd2SBarry Smith                break;
17586bacbd2SBarry Smith       case -2: SETERRQ1(1,1,"ilutp(), matrix L overflows, need larger info->fill value %d",jmax);
17686bacbd2SBarry Smith                break;
17786bacbd2SBarry Smith       case -5: SETERRQ(1,1,"ilutp(), zero row encountered");
17886bacbd2SBarry Smith                break;
17986bacbd2SBarry Smith       case -1: SETERRQ(1,1,"ilutp(), input matrix may be wrong");
18086bacbd2SBarry Smith                break;
18186bacbd2SBarry Smith       case -4: SETERRQ1(1,1,"ilutp(), illegal info->fill value %d",jmax);
18286bacbd2SBarry Smith                break;
18386bacbd2SBarry Smith       default: SETERRQ1(1,1,"ilutp(), zero pivot detected on row %d",ierr);
18486bacbd2SBarry Smith     }
18586bacbd2SBarry Smith   }
18686bacbd2SBarry Smith 
18786bacbd2SBarry Smith   ierr = PetscFree(w);CHKERRQ(ierr);
18886bacbd2SBarry Smith   ierr = PetscFree(jw);CHKERRQ(ierr);
18986bacbd2SBarry Smith 
19086bacbd2SBarry Smith   /* ------------------------------------------------------------
19186bacbd2SBarry Smith      Saad's routine gives the result in Modified Sparse Row (msr)
19286bacbd2SBarry Smith      Convert to Compressed Sparse Row format (csr)
19386bacbd2SBarry Smith      ------------------------------------------------------------
19486bacbd2SBarry Smith   */
19586bacbd2SBarry Smith 
19686bacbd2SBarry Smith   wk  = (Scalar *)    PetscMalloc(n*sizeof(Scalar)); CHKPTRQ(wk);
19786bacbd2SBarry Smith   iwk = (int *) PetscMalloc((n+1)*sizeof(int)); CHKPTRQ(iwk);
19886bacbd2SBarry Smith 
19986bacbd2SBarry Smith   msrcsr_(&n,new_a,new_j,new_a,new_j,new_i,wk,iwk);
20086bacbd2SBarry Smith 
20186bacbd2SBarry Smith   ierr = PetscFree(iwk);CHKERRQ(ierr);
20286bacbd2SBarry Smith   ierr = PetscFree(wk);CHKERRQ(ierr);
20386bacbd2SBarry Smith 
20486bacbd2SBarry Smith   if (reorder) {
20586bacbd2SBarry Smith     ierr = PetscFree(old_a2);CHKERRQ(ierr);
20686bacbd2SBarry Smith     ierr = PetscFree(old_j2);CHKERRQ(ierr);
20786bacbd2SBarry Smith     ierr = PetscFree(old_i2);CHKERRQ(ierr);
208*313ae024SBarry Smith   } else {
209b19713cbSBarry Smith     /* fix permutation of old_j that the factorization introduced */
210b19713cbSBarry Smith     for (i=old_i[0]; i<=old_i[n]; i++) {
211b19713cbSBarry Smith       old_j[i-1] = iperm[old_j[i-1]-1];
212b19713cbSBarry Smith     }
213b19713cbSBarry Smith   }
214b19713cbSBarry Smith 
215b801d0f9SBarry Smith   /* get rid of the shift to indices starting at 1 */
216b19713cbSBarry Smith   if (ishift) {
21786bacbd2SBarry Smith     for (i=0; i<n+1; i++) {
218b19713cbSBarry Smith       old_i[i]--;
219b19713cbSBarry Smith     }
220b19713cbSBarry Smith     for (i=old_i[0];i<old_i[n];i++) {
221b19713cbSBarry Smith       old_j[i]--;
222b19713cbSBarry Smith     }
22386bacbd2SBarry Smith   }
22486bacbd2SBarry Smith 
225b19713cbSBarry Smith   /* Make the factored matrix 0-based */
226b19713cbSBarry Smith   if (ishift) {
22786bacbd2SBarry Smith     for (i=0; i<n+1; i++) {
228b19713cbSBarry Smith       new_i[i]--;
229b19713cbSBarry Smith     }
230b19713cbSBarry Smith     for (i=new_i[0];i<new_i[n];i++) {
231b19713cbSBarry Smith       new_j[i]--;
232b19713cbSBarry Smith     }
23386bacbd2SBarry Smith   }
23486bacbd2SBarry Smith 
23586bacbd2SBarry Smith   /*-- due to the pivoting, we need to reorder iscol to correctly --*/
23686bacbd2SBarry Smith   /*-- permute the right-hand-side and solution vectors           --*/
237c0c2c81eSBarry Smith   ierr = ISGetIndices(isicol,&ic);          CHKERRQ(ierr);
23886bacbd2SBarry Smith   for(i=0; i<n; i++) {
23986bacbd2SBarry Smith     ordcol[i] = ic[iperm[i]-1];
24086bacbd2SBarry Smith   };
24186bacbd2SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
24286bacbd2SBarry Smith 
243c0c2c81eSBarry Smith   ierr = PetscFree(iperm);CHKERRQ(ierr);
244c0c2c81eSBarry Smith 
24586bacbd2SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF, n, ordcol, &iscolf);
24686bacbd2SBarry Smith 
24786bacbd2SBarry Smith   /*----- put together the new matrix -----*/
24886bacbd2SBarry Smith 
24986bacbd2SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
25086bacbd2SBarry Smith   (*fact)->factor    = FACTOR_LU;
25186bacbd2SBarry Smith   (*fact)->assembled = PETSC_TRUE;
25286bacbd2SBarry Smith 
25386bacbd2SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
25486bacbd2SBarry Smith   ierr = PetscFree(b->imax);CHKERRQ(ierr);
25586bacbd2SBarry Smith   b->sorted        = PETSC_FALSE;
25686bacbd2SBarry Smith   b->singlemalloc  = PETSC_TRUE;
257b19713cbSBarry Smith   /* the next line frees the default space generated by the MatCreate() */
25886bacbd2SBarry Smith   ierr             = PetscFree(b->a);CHKERRQ(ierr);
25986bacbd2SBarry Smith   ierr             = PetscFree(b->ilen);CHKERRQ(ierr);
26086bacbd2SBarry Smith   b->a             = new_a;
26186bacbd2SBarry Smith   b->j             = new_j;
26286bacbd2SBarry Smith   b->i             = new_i;
26386bacbd2SBarry Smith   b->ilen          = 0;
26486bacbd2SBarry Smith   b->imax          = 0;
265*313ae024SBarry Smith   b->row           = isirow;
266*313ae024SBarry Smith   ierr             = PetscObjectReference((PetscObject)isirow);CHKERRQ(ierr);
26786bacbd2SBarry Smith   b->col           = iscolf;
26886bacbd2SBarry Smith   b->solve_work    =  (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
26986bacbd2SBarry Smith   b->maxnz = b->nz = new_i[n];
27086bacbd2SBarry Smith   b->indexshift    = a->indexshift;
27186bacbd2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
27286bacbd2SBarry Smith   (*fact)->info.factor_mallocs = 0;
27386bacbd2SBarry Smith 
27486bacbd2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
27586bacbd2SBarry Smith 
27686bacbd2SBarry Smith   /* check out for identical nodes. If found, use inode functions */
27786bacbd2SBarry Smith   ierr = Mat_AIJ_CheckInode(*fact);CHKERRQ(ierr);
27886bacbd2SBarry Smith 
27986bacbd2SBarry Smith   PetscFunctionReturn(0);
28086bacbd2SBarry Smith }
28186bacbd2SBarry Smith #else
28286bacbd2SBarry Smith #undef __FUNC__
28386bacbd2SBarry Smith #define __FUNC__ "MatILUDTFactor_SeqAIJ"
284c3b2863dSBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,MatILUInfo *info,IS isrow,IS iscol,Mat *fact)
28586bacbd2SBarry Smith {
28686bacbd2SBarry Smith   PetscFunctionBegin;
28786bacbd2SBarry Smith   SETERRQ(1,1,"You must install Saad's ILUDT to use this");
28886bacbd2SBarry Smith }
28986bacbd2SBarry Smith #endif
29086bacbd2SBarry Smith 
291289bc588SBarry Smith /*
292289bc588SBarry Smith     Factorization code for AIJ format.
293289bc588SBarry Smith */
2945615d1e5SSatish Balay #undef __FUNC__
2955615d1e5SSatish Balay #define __FUNC__ "MatLUFactorSymbolic_SeqAIJ"
296416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
297289bc588SBarry Smith {
298416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
299289bc588SBarry Smith   IS         isicol;
300416022c9SBarry Smith   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
301416022c9SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
30291bf9adeSBarry Smith   int        *idnew, idx, row,m,fm, nnz, nzi, realloc = 0,nzbd,*im;
303289bc588SBarry Smith 
3043a40ed3dSBarry Smith   PetscFunctionBegin;
305d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
306d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
307f1af5d2fSBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square");
3083b2fbd54SBarry Smith 
30978b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol);CHKERRQ(ierr);
310ac121b3dSBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
311ac121b3dSBarry Smith   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
312289bc588SBarry Smith 
313289bc588SBarry Smith   /* get new row pointers */
3140452661fSBarry Smith   ainew    = (int *) PetscMalloc( (n+1)*sizeof(int) );CHKPTRQ(ainew);
315dbb450caSBarry Smith   ainew[0] = -shift;
316289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
317dbb450caSBarry Smith   jmax  = (int) (f*ai[n]+(!shift));
3180452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajnew);
319289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
3200452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int));CHKPTRQ(fill);
3212fb3ed76SBarry Smith   im   = fill + n + 1;
322289bc588SBarry Smith   /* idnew is location of diagonal in factor */
3230452661fSBarry Smith   idnew    = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(idnew);
324dbb450caSBarry Smith   idnew[0] = -shift;
325289bc588SBarry Smith 
326289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
327289bc588SBarry Smith     /* first copy previous fill into linked list */
328289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
3296b96ef82SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
330dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
331289bc588SBarry Smith     fill[n] = n;
332289bc588SBarry Smith     while (nz--) {
333289bc588SBarry Smith       fm  = n;
334dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
335289bc588SBarry Smith       do {
336289bc588SBarry Smith         m  = fm;
337289bc588SBarry Smith         fm = fill[m];
338289bc588SBarry Smith       } while (fm < idx);
339289bc588SBarry Smith       fill[m]   = idx;
340289bc588SBarry Smith       fill[idx] = fm;
341289bc588SBarry Smith     }
342289bc588SBarry Smith     row = fill[n];
343289bc588SBarry Smith     while ( row < i ) {
344dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
3452fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
3462fb3ed76SBarry Smith       nz    = im[row] - nzbd;
347289bc588SBarry Smith       fm    = row;
3482fb3ed76SBarry Smith       while (nz-- > 0) {
349dbb450caSBarry Smith         idx = *ajtmp++ + shift;
3502fb3ed76SBarry Smith         nzbd++;
3512fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
352289bc588SBarry Smith         do {
353289bc588SBarry Smith           m  = fm;
354289bc588SBarry Smith           fm = fill[m];
355289bc588SBarry Smith         } while (fm < idx);
356289bc588SBarry Smith         if (fm != idx) {
357289bc588SBarry Smith           fill[m]   = idx;
358289bc588SBarry Smith           fill[idx] = fm;
359289bc588SBarry Smith           fm        = idx;
360289bc588SBarry Smith           nnz++;
361289bc588SBarry Smith         }
362289bc588SBarry Smith       }
363289bc588SBarry Smith       row = fill[row];
364289bc588SBarry Smith     }
365289bc588SBarry Smith     /* copy new filled row into permanent storage */
366289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
367496e697eSBarry Smith     if (ainew[i+1] > jmax) {
368ecf371e4SBarry Smith 
369ecf371e4SBarry Smith       /* estimate how much additional space we will need */
370ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
371ecf371e4SBarry Smith       /* just double the memory each time */
372ecf371e4SBarry Smith       int maxadd = jmax;
373ecf371e4SBarry Smith       /* maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); */
374bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
3752fb3ed76SBarry Smith       jmax += maxadd;
376ecf371e4SBarry Smith 
377ecf371e4SBarry Smith       /* allocate a longer ajnew */
3780452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
379549d3d68SSatish Balay       ierr  = PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));CHKERRQ(ierr);
380606d414cSSatish Balay       ierr  = PetscFree(ajnew);CHKERRQ(ierr);
381289bc588SBarry Smith       ajnew = ajtmp;
3822fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
383289bc588SBarry Smith     }
384dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
385289bc588SBarry Smith     fm    = fill[n];
386289bc588SBarry Smith     nzi   = 0;
3872fb3ed76SBarry Smith     im[i] = nnz;
388289bc588SBarry Smith     while (nnz--) {
389289bc588SBarry Smith       if (fm < i) nzi++;
390dbb450caSBarry Smith       *ajtmp++ = fm - shift;
391289bc588SBarry Smith       fm       = fill[fm];
392289bc588SBarry Smith     }
393289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
394289bc588SBarry Smith   }
395464e8d44SSatish Balay   if (ai[n] != 0) {
396464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
397c38d4ed2SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
398981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
399981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
400981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
40105bf559cSSatish Balay   } else {
402981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
40305bf559cSSatish Balay   }
4042fb3ed76SBarry Smith 
405898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
406898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
4071987afe7SBarry Smith 
408606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
409289bc588SBarry Smith 
410289bc588SBarry Smith   /* put together the new matrix */
411b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B);CHKERRQ(ierr);
4121987afe7SBarry Smith   PLogObjectParent(*B,isicol);
413416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
414606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
4157c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
416e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
417606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
418606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
41991bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
420416022c9SBarry Smith   b->j          = ajnew;
421416022c9SBarry Smith   b->i          = ainew;
422416022c9SBarry Smith   b->diag       = idnew;
423416022c9SBarry Smith   b->ilen       = 0;
424416022c9SBarry Smith   b->imax       = 0;
425416022c9SBarry Smith   b->row        = isrow;
426416022c9SBarry Smith   b->col        = iscol;
427c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
428c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
42982bf6240SBarry Smith   b->icol       = isicol;
43091bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
431416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
4327fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
433416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
434416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
4357fd98bd6SLois Curfman McInnes 
436e93ccc4dSBarry Smith   (*B)->factor                 =  FACTOR_LU;;
437ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
438ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
439703deb49SSatish Balay   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant if A has inodes */
440703deb49SSatish Balay 
441e93ccc4dSBarry Smith   if (ai[n] != 0) {
442e93ccc4dSBarry Smith     (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[n]);
443eea2913aSSatish Balay   } else {
444eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
445eea2913aSSatish Balay   }
4463a40ed3dSBarry Smith   PetscFunctionReturn(0);
447289bc588SBarry Smith }
4480a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
449184914b5SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
45041c01911SSatish Balay 
4515615d1e5SSatish Balay #undef __FUNC__
4525615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
453416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
454289bc588SBarry Smith {
455416022c9SBarry Smith   Mat        C = *B;
456416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
45782bf6240SBarry Smith   IS         isrow = b->row, isicol = b->icol;
458416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
4591987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
460f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
46135aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
4623a40ed3dSBarry Smith   register   int    *pj;
4638ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
46435aab85fSBarry Smith   double     ssum;
46535aab85fSBarry Smith   register   Scalar *pv, *rtmps,*u_values;
466289bc588SBarry Smith 
4673a40ed3dSBarry Smith   PetscFunctionBegin;
46882bf6240SBarry Smith 
46978b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r);CHKERRQ(ierr);
47078b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
4710452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) );CHKPTRQ(rtmp);
472549d3d68SSatish Balay   ierr  = PetscMemzero(rtmp,(n+1)*sizeof(Scalar));CHKERRQ(ierr);
4730cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
474289bc588SBarry Smith 
4756cf997b0SBarry Smith   /* precalculate row sums */
47635aab85fSBarry Smith   if (preserve_row_sums) {
47735aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) );CHKPTRQ(rowsums);
47835aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
47935aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
48035aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
48135aab85fSBarry Smith       sum = 0.0;
48235aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
48335aab85fSBarry Smith       rowsums[i] = sum;
48435aab85fSBarry Smith     }
48535aab85fSBarry Smith   }
48635aab85fSBarry Smith 
487289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
488289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
489dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
49048e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
491289bc588SBarry Smith 
492289bc588SBarry Smith     /* load in initial (unfactored row) */
493416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
494416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
495416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
4960cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
497289bc588SBarry Smith 
498dbb450caSBarry Smith     row = *ajtmp++ + shift;
499f2582111SSatish Balay     while  (row < i ) {
5008c37ef55SBarry Smith       pc = rtmp + row;
5018c37ef55SBarry Smith       if (*pc != 0.0) {
5021987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
5031987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
50435aab85fSBarry Smith         multiplier = *pc / *pv++;
5058c37ef55SBarry Smith         *pc        = multiplier;
506f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
507f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
508f2582111SSatish Balay         PLogFlops(2*nz);
509289bc588SBarry Smith       }
510dbb450caSBarry Smith       row = *ajtmp++ + shift;
511289bc588SBarry Smith     }
512416022c9SBarry Smith     /* finished row so stick it into b->a */
513416022c9SBarry Smith     pv = b->a + ai[i] + shift;
514416022c9SBarry Smith     pj = b->j + ai[i] + shift;
5158c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
516416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
51735aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
51835aab85fSBarry Smith     /*
51935aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
52035aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
52135aab85fSBarry Smith     */
522d708749eSBarry Smith     if (pv[diag] == 0.0) {
5236cf997b0SBarry Smith       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot row %d",i);
524d708749eSBarry Smith     }
52535aab85fSBarry Smith     if (preserve_row_sums) {
52635aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
52735aab85fSBarry Smith       sum = rowsums[i];
52835aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
52935aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
53035aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
53135aab85fSBarry Smith         inner_sum = 0.0;
53235aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
53335aab85fSBarry Smith           inner_sum += u_values[k];
53435aab85fSBarry Smith         }
53535aab85fSBarry Smith         sum -= pv[j]*inner_sum;
53635aab85fSBarry Smith 
53735aab85fSBarry Smith       }
53835aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
53935aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
54035aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
54135aab85fSBarry Smith         sum -= u_values[k];
54235aab85fSBarry Smith       }
54335aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
54435aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
54535aab85fSBarry Smith     }
54635aab85fSBarry Smith     /* check pivot entry for current row */
5478c37ef55SBarry Smith   }
5480f11f9aeSSatish Balay 
54935aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
55035aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
55135aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
55235aab85fSBarry Smith   }
55335aab85fSBarry Smith 
554606d414cSSatish Balay   if (preserve_row_sums) {ierr = PetscFree(rowsums);CHKERRQ(ierr);}
555606d414cSSatish Balay   ierr = PetscFree(rtmp);CHKERRQ(ierr);
55678b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
55778b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
558416022c9SBarry Smith   C->factor = FACTOR_LU;
55941c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C);CHKERRQ(ierr);
560c456f294SBarry Smith   C->assembled = PETSC_TRUE;
561416022c9SBarry Smith   PLogFlops(b->n);
5623a40ed3dSBarry Smith   PetscFunctionReturn(0);
563289bc588SBarry Smith }
5640a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
5655615d1e5SSatish Balay #undef __FUNC__
5665615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
567416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
568da3a660dSBarry Smith {
569416022c9SBarry Smith   Mat_SeqAIJ     *mat = (Mat_SeqAIJ *) A->data;
57086bacbd2SBarry Smith   int            ierr,refct;
571416022c9SBarry Smith   Mat            C;
572f830108cSBarry Smith   PetscOps       *Abops;
5730a6ffc59SBarry Smith   MatOps         Aops;
574416022c9SBarry Smith 
5753a40ed3dSBarry Smith   PetscFunctionBegin;
576f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C);CHKERRQ(ierr);
577f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C);CHKERRQ(ierr);
578da3a660dSBarry Smith 
579da3a660dSBarry Smith   /* free all the data structures from mat */
580606d414cSSatish Balay   ierr = PetscFree(mat->a);CHKERRQ(ierr);
581606d414cSSatish Balay   if (!mat->singlemalloc) {
582606d414cSSatish Balay     ierr = PetscFree(mat->i);CHKERRQ(ierr);
583606d414cSSatish Balay     ierr = PetscFree(mat->j);CHKERRQ(ierr);
584606d414cSSatish Balay   }
585606d414cSSatish Balay   if (mat->diag) {ierr = PetscFree(mat->diag);CHKERRQ(ierr);}
586606d414cSSatish Balay   if (mat->ilen) {ierr = PetscFree(mat->ilen);CHKERRQ(ierr);}
587606d414cSSatish Balay   if (mat->imax) {ierr = PetscFree(mat->imax);CHKERRQ(ierr);}
588606d414cSSatish Balay   if (mat->solve_work) {ierr = PetscFree(mat->solve_work);CHKERRQ(ierr);}
589606d414cSSatish Balay   if (mat->inode.size) {ierr = PetscFree(mat->inode.size);CHKERRQ(ierr);}
5900f0aacb9SBarry Smith   if (mat->icol) {ierr = ISDestroy(mat->icol);CHKERRQ(ierr);}
591606d414cSSatish Balay   ierr = PetscFree(mat);CHKERRQ(ierr);
592da3a660dSBarry Smith 
59317642b18SBarry Smith   ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
59417642b18SBarry Smith   ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
59517642b18SBarry Smith 
596f830108cSBarry Smith   /*
597f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
598f830108cSBarry Smith     A pointers for the bops and ops but copy everything
599f830108cSBarry Smith     else from C.
600f830108cSBarry Smith   */
601f830108cSBarry Smith   Abops = A->bops;
602f830108cSBarry Smith   Aops  = A->ops;
60386bacbd2SBarry Smith   refct = A->refct;
604549d3d68SSatish Balay   ierr  = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
605451c4af8SSatish Balay   mat   = (Mat_SeqAIJ *) A->data;
606451c4af8SSatish Balay   PLogObjectParent(A,mat->icol);
607451c4af8SSatish Balay 
608f830108cSBarry Smith   A->bops  = Abops;
609f830108cSBarry Smith   A->ops   = Aops;
610bef8e0ddSBarry Smith   A->qlist = 0;
61186bacbd2SBarry Smith   A->refct = refct;
612c38d4ed2SBarry Smith   /* copy over the type_name and name */
613c38d4ed2SBarry Smith   ierr     = PetscStrallocpy(C->type_name,&A->type_name);CHKERRQ(ierr);
614c38d4ed2SBarry Smith   ierr     = PetscStrallocpy(C->name,&A->name);CHKERRQ(ierr);
615f830108cSBarry Smith 
6160452661fSBarry Smith   PetscHeaderDestroy(C);
617c38d4ed2SBarry Smith 
6183a40ed3dSBarry Smith   PetscFunctionReturn(0);
619da3a660dSBarry Smith }
6200a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
6215615d1e5SSatish Balay #undef __FUNC__
6225615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
623416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
6248c37ef55SBarry Smith {
625416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
626416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
627416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
6283b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
629416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
6308c37ef55SBarry Smith 
6313a40ed3dSBarry Smith   PetscFunctionBegin;
6323a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
63391bf9adeSBarry Smith 
634e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
635e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
636416022c9SBarry Smith   tmp  = a->solve_work;
6378c37ef55SBarry Smith 
6383b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
6393b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
6408c37ef55SBarry Smith 
6418c37ef55SBarry Smith   /* forward solve the lower triangular */
6428c37ef55SBarry Smith   tmp[0] = b[*r++];
6430cf60383SBarry Smith   tmps   = tmp + shift;
6448c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
645dbb450caSBarry Smith     v   = aa + ai[i] + shift;
646dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
647416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
6488c37ef55SBarry Smith     sum = b[*r++];
6490cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
6508c37ef55SBarry Smith     tmp[i] = sum;
6518c37ef55SBarry Smith   }
6528c37ef55SBarry Smith 
6538c37ef55SBarry Smith   /* backward solve the upper triangular */
6548c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
655416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
656416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
657416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
6588c37ef55SBarry Smith     sum = tmp[i];
6590cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
660416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
6618c37ef55SBarry Smith   }
6628c37ef55SBarry Smith 
6633b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
6643b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
665cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
666cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
667416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
6683a40ed3dSBarry Smith   PetscFunctionReturn(0);
6698c37ef55SBarry Smith }
670026e39d0SSatish Balay 
671930ae53cSBarry Smith /* ----------------------------------------------------------- */
672930ae53cSBarry Smith #undef __FUNC__
673930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering"
674930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx)
675930ae53cSBarry Smith {
676930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
677d85afc42SSatish Balay   int        n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr;
678d85afc42SSatish Balay   Scalar     *x,*b, *aa = a->a, sum;
679aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
680d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
681d85afc42SSatish Balay   Scalar     *v;
682d85afc42SSatish Balay #endif
683930ae53cSBarry Smith 
6843a40ed3dSBarry Smith   PetscFunctionBegin;
6853a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
686930ae53cSBarry Smith   if (a->indexshift) {
6873a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
6883a40ed3dSBarry Smith      PetscFunctionReturn(0);
689930ae53cSBarry Smith   }
690930ae53cSBarry Smith 
691e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
692e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
693930ae53cSBarry Smith 
694aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
6951c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
6961c4feecaSSatish Balay #else
697930ae53cSBarry Smith   /* forward solve the lower triangular */
698930ae53cSBarry Smith   x[0] = b[0];
699930ae53cSBarry Smith   for ( i=1; i<n; i++ ) {
700930ae53cSBarry Smith     ai_i = ai[i];
701930ae53cSBarry Smith     v    = aa + ai_i;
702930ae53cSBarry Smith     vi   = aj + ai_i;
703930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
704930ae53cSBarry Smith     sum  = b[i];
705930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
706930ae53cSBarry Smith     x[i] = sum;
707930ae53cSBarry Smith   }
708930ae53cSBarry Smith 
709930ae53cSBarry Smith   /* backward solve the upper triangular */
710930ae53cSBarry Smith   for ( i=n-1; i>=0; i-- ){
711930ae53cSBarry Smith     adiag_i = adiag[i];
712d708749eSBarry Smith     v       = aa + adiag_i + 1;
713d708749eSBarry Smith     vi      = aj + adiag_i + 1;
714930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
715930ae53cSBarry Smith     sum     = x[i];
716930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
717930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
718930ae53cSBarry Smith   }
7191c4feecaSSatish Balay #endif
720930ae53cSBarry Smith   PLogFlops(2*a->nz - a->n);
721cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
722cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
7233a40ed3dSBarry Smith   PetscFunctionReturn(0);
724930ae53cSBarry Smith }
725930ae53cSBarry Smith 
7265615d1e5SSatish Balay #undef __FUNC__
7275615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
728416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
729da3a660dSBarry Smith {
730416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
731416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
732416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
7333b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
734416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
735da3a660dSBarry Smith 
7363a40ed3dSBarry Smith   PetscFunctionBegin;
73778b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);}
738da3a660dSBarry Smith 
739e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
740e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
741416022c9SBarry Smith   tmp  = a->solve_work;
742da3a660dSBarry Smith 
7433b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7443b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
745da3a660dSBarry Smith 
746da3a660dSBarry Smith   /* forward solve the lower triangular */
747da3a660dSBarry Smith   tmp[0] = b[*r++];
748da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
749dbb450caSBarry Smith     v   = aa + ai[i] + shift;
750dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
751416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
752da3a660dSBarry Smith     sum = b[*r++];
753dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
754da3a660dSBarry Smith     tmp[i] = sum;
755da3a660dSBarry Smith   }
756da3a660dSBarry Smith 
757da3a660dSBarry Smith   /* backward solve the upper triangular */
758da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
759416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
760416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
761416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
762da3a660dSBarry Smith     sum = tmp[i];
763dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
764416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
765da3a660dSBarry Smith     x[*c--] += tmp[i];
766da3a660dSBarry Smith   }
767da3a660dSBarry Smith 
7683b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
7693b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
770cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
771cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
772416022c9SBarry Smith   PLogFlops(2*a->nz);
773898183ecSLois Curfman McInnes 
7743a40ed3dSBarry Smith   PetscFunctionReturn(0);
775da3a660dSBarry Smith }
776da3a660dSBarry Smith /* -------------------------------------------------------------------*/
7775615d1e5SSatish Balay #undef __FUNC__
7787c922b88SBarry Smith #define __FUNC__ "MatSolveTranspose_SeqAIJ"
7797c922b88SBarry Smith int MatSolveTranspose_SeqAIJ(Mat A,Vec bb, Vec xx)
780da3a660dSBarry Smith {
781416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7822235e667SBarry Smith   IS         iscol = a->col, isrow = a->row;
783416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
784f1af5d2fSBarry Smith   int        nz,shift = a->indexshift,*rout,*cout, *diag = a->diag;
785f1af5d2fSBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v, s1;
786da3a660dSBarry Smith 
7873a40ed3dSBarry Smith   PetscFunctionBegin;
788e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
789e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
790416022c9SBarry Smith   tmp  = a->solve_work;
791da3a660dSBarry Smith 
7922235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
7932235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
794da3a660dSBarry Smith 
795da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
7962235e667SBarry Smith   for ( i=0; i<n; i++ ) tmp[i] = b[c[i]];
797da3a660dSBarry Smith 
798da3a660dSBarry Smith   /* forward solve the U^T */
799da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
800f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
801f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
802f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
803c38d4ed2SBarry Smith     s1  = tmp[i];
804c38d4ed2SBarry Smith     s1 *= *(v++);  /* multiply by inverse of diagonal entry */
805da3a660dSBarry Smith     while (nz--) {
806f1af5d2fSBarry Smith       tmp[*vi++ + shift] -= (*v++)*s1;
807da3a660dSBarry Smith     }
808c38d4ed2SBarry Smith     tmp[i] = s1;
809da3a660dSBarry Smith   }
810da3a660dSBarry Smith 
811da3a660dSBarry Smith   /* backward solve the L^T */
812da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
813f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
814f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
815f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
816f1af5d2fSBarry Smith     s1  = tmp[i];
817da3a660dSBarry Smith     while (nz--) {
818f1af5d2fSBarry Smith       tmp[*vi-- + shift] -= (*v--)*s1;
819da3a660dSBarry Smith     }
820da3a660dSBarry Smith   }
821da3a660dSBarry Smith 
822da3a660dSBarry Smith   /* copy tmp into x according to permutation */
823da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
824da3a660dSBarry Smith 
8252235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
8262235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
827cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
828cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
829da3a660dSBarry Smith 
830416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
8313a40ed3dSBarry Smith   PetscFunctionReturn(0);
832da3a660dSBarry Smith }
833da3a660dSBarry Smith 
8345615d1e5SSatish Balay #undef __FUNC__
8357c922b88SBarry Smith #define __FUNC__ "MatSolveTransposeAdd_SeqAIJ"
8367c922b88SBarry Smith int MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
837da3a660dSBarry Smith {
838416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
8392235e667SBarry Smith   IS         iscol = a->col, isrow = a->row;
840416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
841f1af5d2fSBarry Smith   int        nz,shift = a->indexshift, *rout, *cout, *diag = a->diag;
842416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
8436abc6512SBarry Smith 
8443a40ed3dSBarry Smith   PetscFunctionBegin;
8456abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
8466abc6512SBarry Smith 
847e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
848e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
849416022c9SBarry Smith   tmp = a->solve_work;
8506abc6512SBarry Smith 
8512235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
8522235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
8536abc6512SBarry Smith 
8546abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
8552235e667SBarry Smith   for ( i=0; i<n; i++ ) tmp[i] = b[c[i]];
8566abc6512SBarry Smith 
8576abc6512SBarry Smith   /* forward solve the U^T */
8586abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
859f1af5d2fSBarry Smith     v   = aa + diag[i] + shift;
860f1af5d2fSBarry Smith     vi  = aj + diag[i] + (!shift);
861f1af5d2fSBarry Smith     nz  = ai[i+1] - diag[i] - 1;
8626abc6512SBarry Smith     tmp[i] *= *v++;
8636abc6512SBarry Smith     while (nz--) {
864dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
8656abc6512SBarry Smith     }
8666abc6512SBarry Smith   }
8676abc6512SBarry Smith 
8686abc6512SBarry Smith   /* backward solve the L^T */
8696abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
870f1af5d2fSBarry Smith     v   = aa + diag[i] - 1 + shift;
871f1af5d2fSBarry Smith     vi  = aj + diag[i] - 1 + shift;
872f1af5d2fSBarry Smith     nz  = diag[i] - ai[i];
8736abc6512SBarry Smith     while (nz--) {
874dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
8756abc6512SBarry Smith     }
8766abc6512SBarry Smith   }
8776abc6512SBarry Smith 
8786abc6512SBarry Smith   /* copy tmp into x according to permutation */
8796abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
8806abc6512SBarry Smith 
8812235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
8822235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
883cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
884cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8856abc6512SBarry Smith 
886416022c9SBarry Smith   PLogFlops(2*a->nz);
8873a40ed3dSBarry Smith   PetscFunctionReturn(0);
888da3a660dSBarry Smith }
8899e25ed09SBarry Smith /* ----------------------------------------------------------------*/
8907c922b88SBarry Smith extern int MatMissingDiagonal_SeqAIJ(Mat);
89108480c60SBarry Smith 
8925615d1e5SSatish Balay #undef __FUNC__
8935615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
8946cf997b0SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatILUInfo *info,Mat *fact)
8959e25ed09SBarry Smith {
896416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
8979e25ed09SBarry Smith   IS         isicol;
898416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
89956beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
900335d9088SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0, dcount = 0;
9016cf997b0SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift,levels,diagonal_fill;
90277c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
9036cf997b0SBarry Smith   double     f;
9049e25ed09SBarry Smith 
9053a40ed3dSBarry Smith   PetscFunctionBegin;
9066cf997b0SBarry Smith   if (info) {
9076cf997b0SBarry Smith     f             = info->fill;
9080cd17293SBarry Smith     levels        = (int) info->levels;
9090cd17293SBarry Smith     diagonal_fill = (int) info->diagonal_fill;
9106cf997b0SBarry Smith   } else {
9116cf997b0SBarry Smith     f             = 1.0;
9126cf997b0SBarry Smith     levels        = 0;
9136cf997b0SBarry Smith     diagonal_fill = 0;
9146cf997b0SBarry Smith   }
91582bf6240SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol);CHKERRQ(ierr);
91682bf6240SBarry Smith 
91708480c60SBarry Smith   /* special case that simply copies fill pattern */
918be0abb6dSBarry Smith   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
919be0abb6dSBarry Smith   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
92086bacbd2SBarry Smith   if (!levels && row_identity && col_identity) {
9212e8a6d31SBarry Smith     ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact);CHKERRQ(ierr);
92208480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
92308480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
92408480c60SBarry Smith     if (!b->diag) {
9257c922b88SBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
92608480c60SBarry Smith     }
9277c922b88SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(*fact);CHKERRQ(ierr);
92808480c60SBarry Smith     b->row              = isrow;
92908480c60SBarry Smith     b->col              = iscol;
93082bf6240SBarry Smith     b->icol             = isicol;
9310452661fSBarry Smith     b->solve_work       = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
932f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
933c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
934c38d4ed2SBarry Smith     ierr                = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
9353a40ed3dSBarry Smith     PetscFunctionReturn(0);
93608480c60SBarry Smith   }
93708480c60SBarry Smith 
938898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
939898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
9409e25ed09SBarry Smith 
9419e25ed09SBarry Smith   /* get new row pointers */
9420452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) );CHKPTRQ(ainew);
943dbb450caSBarry Smith   ainew[0] = -shift;
9449e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
945dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
9460452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajnew);
9479e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
9480452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajfill);
9499e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
9500452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(fill);
95156beaf04SBarry Smith   /* im is level for each filled value */
9520452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(im);
95356beaf04SBarry Smith   /* dloc is location of diagonal in factor */
9540452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(dloc);
95556beaf04SBarry Smith   dloc[0]  = 0;
95656beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
9576cf997b0SBarry Smith 
9586cf997b0SBarry Smith     /* copy current row into linked list */
95956beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
960385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
961dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
9629e25ed09SBarry Smith     fill[n]    = n;
963435faa5fSBarry Smith     fill[prow] = -1; /* marker to indicate if diagonal exists */
9649e25ed09SBarry Smith     while (nz--) {
9659e25ed09SBarry Smith       fm  = n;
966dbb450caSBarry Smith       idx = ic[*xi++ + shift];
9679e25ed09SBarry Smith       do {
9689e25ed09SBarry Smith         m  = fm;
9699e25ed09SBarry Smith         fm = fill[m];
9709e25ed09SBarry Smith       } while (fm < idx);
9719e25ed09SBarry Smith       fill[m]   = idx;
9729e25ed09SBarry Smith       fill[idx] = fm;
97356beaf04SBarry Smith       im[idx]   = 0;
9749e25ed09SBarry Smith     }
9756cf997b0SBarry Smith 
9766cf997b0SBarry Smith     /* make sure diagonal entry is included */
977435faa5fSBarry Smith     if (diagonal_fill && fill[prow] == -1) {
9786cf997b0SBarry Smith       fm = n;
979435faa5fSBarry Smith       while (fill[fm] < prow) fm = fill[fm];
980435faa5fSBarry Smith       fill[prow] = fill[fm]; /* insert diagonal into linked list */
981435faa5fSBarry Smith       fill[fm]   = prow;
9826cf997b0SBarry Smith       im[prow]   = 0;
9836cf997b0SBarry Smith       nzf++;
984335d9088SBarry Smith       dcount++;
9856cf997b0SBarry Smith     }
9866cf997b0SBarry Smith 
98756beaf04SBarry Smith     nzi = 0;
9889e25ed09SBarry Smith     row = fill[n];
98956beaf04SBarry Smith     while ( row < prow ) {
99056beaf04SBarry Smith       incrlev = im[row] + 1;
99156beaf04SBarry Smith       nz      = dloc[row];
9926cf997b0SBarry Smith       xi      = ajnew  + ainew[row] + shift + nz + 1;
993dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
994416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
99556beaf04SBarry Smith       fm      = row;
99656beaf04SBarry Smith       while (nnz-- > 0) {
997dbb450caSBarry Smith         idx = *xi++ + shift;
99856beaf04SBarry Smith         if (*flev + incrlev > levels) {
99956beaf04SBarry Smith           flev++;
100056beaf04SBarry Smith           continue;
100156beaf04SBarry Smith         }
100256beaf04SBarry Smith         do {
10039e25ed09SBarry Smith           m  = fm;
10049e25ed09SBarry Smith           fm = fill[m];
100556beaf04SBarry Smith         } while (fm < idx);
10069e25ed09SBarry Smith         if (fm != idx) {
100756beaf04SBarry Smith           im[idx]   = *flev + incrlev;
10089e25ed09SBarry Smith           fill[m]   = idx;
10099e25ed09SBarry Smith           fill[idx] = fm;
10109e25ed09SBarry Smith           fm        = idx;
101156beaf04SBarry Smith           nzf++;
1012ecf371e4SBarry Smith         } else {
101356beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
10149e25ed09SBarry Smith         }
101556beaf04SBarry Smith         flev++;
10169e25ed09SBarry Smith       }
10179e25ed09SBarry Smith       row = fill[row];
101856beaf04SBarry Smith       nzi++;
10199e25ed09SBarry Smith     }
10209e25ed09SBarry Smith     /* copy new filled row into permanent storage */
102156beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
1022d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
1023ecf371e4SBarry Smith 
1024ecf371e4SBarry Smith       /* estimate how much additional space we will need */
1025ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
1026ecf371e4SBarry Smith       /* just double the memory each time */
1027ecf371e4SBarry Smith       /*  maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */
1028ecf371e4SBarry Smith       int maxadd = jmax;
102956beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
1030bbb6d6a8SBarry Smith       jmax += maxadd;
1031ecf371e4SBarry Smith 
1032ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
10330452661fSBarry Smith       xi     = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
1034549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
1035606d414cSSatish Balay       ierr = PetscFree(ajnew);CHKERRQ(ierr);
103656beaf04SBarry Smith       ajnew  = xi;
10370452661fSBarry Smith       xi     = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
1038549d3d68SSatish Balay       ierr   = PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr);
1039606d414cSSatish Balay       ierr = PetscFree(ajfill);CHKERRQ(ierr);
104056beaf04SBarry Smith       ajfill = xi;
1041ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
10429e25ed09SBarry Smith     }
1043dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
1044dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
104556beaf04SBarry Smith     dloc[prow]  = nzi;
10469e25ed09SBarry Smith     fm          = fill[n];
104756beaf04SBarry Smith     while (nzf--) {
1048dbb450caSBarry Smith       *xi++   = fm - shift;
104956beaf04SBarry Smith       *flev++ = im[fm];
10509e25ed09SBarry Smith       fm      = fill[fm];
10519e25ed09SBarry Smith     }
10526cf997b0SBarry Smith     /* make sure row has diagonal entry */
10536cf997b0SBarry Smith     if (ajnew[ainew[prow]+shift+dloc[prow]]+shift != prow) {
10546cf997b0SBarry Smith       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,1,"Row %d has missing diagonal in factored matrix\n\
10556cf997b0SBarry Smith     try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow);
10566cf997b0SBarry Smith     }
10579e25ed09SBarry Smith   }
1058606d414cSSatish Balay   ierr = PetscFree(ajfill); CHKERRQ(ierr);
1059898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
1060898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
1061606d414cSSatish Balay   ierr = PetscFree(fill);CHKERRQ(ierr);
1062606d414cSSatish Balay   ierr = PetscFree(im);CHKERRQ(ierr);
10639e25ed09SBarry Smith 
1064f64065bbSBarry Smith   {
1065464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
1066c38d4ed2SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af);
1067981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
1068981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
1069981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
1070335d9088SBarry Smith     if (diagonal_fill) {
1071335d9088SBarry Smith       PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replace %d missing diagonals",dcount);
1072335d9088SBarry Smith     }
1073f64065bbSBarry Smith   }
1074bbb6d6a8SBarry Smith 
10759e25ed09SBarry Smith   /* put together the new matrix */
1076b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr);
1077fa6007c9SSatish Balay   PLogObjectParent(*fact,isicol);
1078416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
1079606d414cSSatish Balay   ierr = PetscFree(b->imax);CHKERRQ(ierr);
10807c922b88SBarry Smith   b->singlemalloc = PETSC_FALSE;
1081dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
10829e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
1083606d414cSSatish Balay   ierr = PetscFree(b->a);CHKERRQ(ierr);
1084606d414cSSatish Balay   ierr = PetscFree(b->ilen);CHKERRQ(ierr);
10856b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 );CHKPTRQ(b->a);
1086416022c9SBarry Smith   b->j          = ajnew;
1087416022c9SBarry Smith   b->i          = ainew;
108856beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
1089416022c9SBarry Smith   b->diag       = dloc;
1090416022c9SBarry Smith   b->ilen       = 0;
1091416022c9SBarry Smith   b->imax       = 0;
1092416022c9SBarry Smith   b->row        = isrow;
1093416022c9SBarry Smith   b->col        = iscol;
1094c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1095c38d4ed2SBarry Smith   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
109682bf6240SBarry Smith   b->icol       = isicol;
109782bf6240SBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
1098416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
109956beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
1100dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
1101416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
11029e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
1103ae068f58SLois Curfman McInnes 
1104ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
1105ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
1106ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
1107e93ccc4dSBarry Smith   (*fact)->factor                 =  FACTOR_LU;;
1108ae068f58SLois Curfman McInnes 
11093a40ed3dSBarry Smith   PetscFunctionReturn(0);
11109e25ed09SBarry Smith }
1111d5d45c9bSBarry Smith 
1112d5d45c9bSBarry Smith 
1113d5d45c9bSBarry Smith 
1114d5d45c9bSBarry Smith 
1115