1*5bd2ddc7SBarry Smith /*$Id: aijfact.c,v 1.136 1999/12/16 23:35:48 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 23*5bd2ddc7SBarry Smith extern int SPARSEKIT2dperm(int*,Scalar*,int*,int*,Scalar*,int*,int*,int*,int*,int*); 24*5bd2ddc7SBarry Smith extern int SPARSEKIT2ilutp(int*,Scalar*,int*,int*,int*,double*,double*,int*,Scalar*,int*,int*,int*,Scalar*,int*,int*,int*); 25*5bd2ddc7SBarry 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 34*5bd2ddc7SBarry Smith ILU routine written by Yousef Saad (saad@cs.umn.edu) as part of 35*5bd2ddc7SBarry Smith SPARSEKIT2. 36*5bd2ddc7SBarry Smith 37*5bd2ddc7SBarry Smith The SPARSEKIT2 routines used here are covered by the GNU 38*5bd2ddc7SBarry 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 43*5bd2ddc7SBarry Smith The major drawback to this routine is that if info->fill is 44*5bd2ddc7SBarry Smith not large enough it fails rather than allocating more space; 45*5bd2ddc7SBarry Smith this can be fixed by hacking/improving the f2c version of 46*5bd2ddc7SBarry Smith Yousef Saad's code. 4786bacbd2SBarry Smith 48*5bd2ddc7SBarry Smith ishift = 0, for indices start at 1 49*5bd2ddc7SBarry Smith ishift = 1, for indices starting at 0 5086bacbd2SBarry Smith ------------------------------------------------------------ 5186bacbd2SBarry Smith */ 5286bacbd2SBarry Smith 5386bacbd2SBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,MatILUInfo *info,IS isrow,IS iscol,Mat *fact) 5486bacbd2SBarry Smith { 5586bacbd2SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 5607d2056aSBarry Smith IS iscolf, isicol, isirow; 5786bacbd2SBarry Smith PetscTruth reorder; 58*5bd2ddc7SBarry 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; 61*5bd2ddc7SBarry Smith int ishift = !a->indexshift; 62b19713cbSBarry Smith int jmax,lfill,job,*o_i,*o_j; 63*5bd2ddc7SBarry Smith Scalar *old_a = a->a, *w, *new_a, *old_a2, *wk,*o_a; 64*5bd2ddc7SBarry Smith double permtol; 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; 7186bacbd2SBarry Smith if (info->fill == PETSC_DEFAULT) info->fill = (n*info->dtcount)/a->nz; 7286bacbd2SBarry Smith lfill = info->dtcount/2; 7386bacbd2SBarry Smith jmax = 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); 122*5bd2ddc7SBarry 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 147*5bd2ddc7SBarry 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 171*5bd2ddc7SBarry 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 25586bacbd2SBarry Smith PetscFunctionReturn(0); 25686bacbd2SBarry Smith } 25786bacbd2SBarry Smith 258289bc588SBarry Smith /* 259289bc588SBarry Smith Factorization code for AIJ format. 260289bc588SBarry Smith */ 2615615d1e5SSatish Balay #undef __FUNC__ 2625615d1e5SSatish Balay #define __FUNC__ "MatLUFactorSymbolic_SeqAIJ" 263416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 264289bc588SBarry Smith { 265416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 266289bc588SBarry Smith IS isicol; 267416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 268416022c9SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 26991bf9adeSBarry Smith int *idnew, idx, row,m,fm, nnz, nzi, realloc = 0,nzbd,*im; 270289bc588SBarry Smith 2713a40ed3dSBarry Smith PetscFunctionBegin; 272d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(isrow,IS_COOKIE); 273d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(iscol,IS_COOKIE); 274f1af5d2fSBarry Smith if (A->M != A->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 2753b2fbd54SBarry Smith 27678b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol);CHKERRQ(ierr); 277ac121b3dSBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 278ac121b3dSBarry Smith ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 279289bc588SBarry Smith 280289bc588SBarry Smith /* get new row pointers */ 2810452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) );CHKPTRQ(ainew); 282dbb450caSBarry Smith ainew[0] = -shift; 283289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 284dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 2850452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajnew); 286289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 2870452661fSBarry Smith fill = (int *) PetscMalloc( (2*n+1)*sizeof(int));CHKPTRQ(fill); 2882fb3ed76SBarry Smith im = fill + n + 1; 289289bc588SBarry Smith /* idnew is location of diagonal in factor */ 2900452661fSBarry Smith idnew = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(idnew); 291dbb450caSBarry Smith idnew[0] = -shift; 292289bc588SBarry Smith 293289bc588SBarry Smith for ( i=0; i<n; i++ ) { 294289bc588SBarry Smith /* first copy previous fill into linked list */ 295289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 2966b96ef82SSatish Balay if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix"); 297dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 298289bc588SBarry Smith fill[n] = n; 299289bc588SBarry Smith while (nz--) { 300289bc588SBarry Smith fm = n; 301dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 302289bc588SBarry Smith do { 303289bc588SBarry Smith m = fm; 304289bc588SBarry Smith fm = fill[m]; 305289bc588SBarry Smith } while (fm < idx); 306289bc588SBarry Smith fill[m] = idx; 307289bc588SBarry Smith fill[idx] = fm; 308289bc588SBarry Smith } 309289bc588SBarry Smith row = fill[n]; 310289bc588SBarry Smith while ( row < i ) { 311dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 3122fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 3132fb3ed76SBarry Smith nz = im[row] - nzbd; 314289bc588SBarry Smith fm = row; 3152fb3ed76SBarry Smith while (nz-- > 0) { 316dbb450caSBarry Smith idx = *ajtmp++ + shift; 3172fb3ed76SBarry Smith nzbd++; 3182fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 319289bc588SBarry Smith do { 320289bc588SBarry Smith m = fm; 321289bc588SBarry Smith fm = fill[m]; 322289bc588SBarry Smith } while (fm < idx); 323289bc588SBarry Smith if (fm != idx) { 324289bc588SBarry Smith fill[m] = idx; 325289bc588SBarry Smith fill[idx] = fm; 326289bc588SBarry Smith fm = idx; 327289bc588SBarry Smith nnz++; 328289bc588SBarry Smith } 329289bc588SBarry Smith } 330289bc588SBarry Smith row = fill[row]; 331289bc588SBarry Smith } 332289bc588SBarry Smith /* copy new filled row into permanent storage */ 333289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 334496e697eSBarry Smith if (ainew[i+1] > jmax) { 335ecf371e4SBarry Smith 336ecf371e4SBarry Smith /* estimate how much additional space we will need */ 337ecf371e4SBarry Smith /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */ 338ecf371e4SBarry Smith /* just double the memory each time */ 339ecf371e4SBarry Smith int maxadd = jmax; 340ecf371e4SBarry Smith /* maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); */ 341bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 3422fb3ed76SBarry Smith jmax += maxadd; 343ecf371e4SBarry Smith 344ecf371e4SBarry Smith /* allocate a longer ajnew */ 3450452661fSBarry Smith ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 346549d3d68SSatish Balay ierr = PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));CHKERRQ(ierr); 347606d414cSSatish Balay ierr = PetscFree(ajnew);CHKERRQ(ierr); 348289bc588SBarry Smith ajnew = ajtmp; 3492fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 350289bc588SBarry Smith } 351dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 352289bc588SBarry Smith fm = fill[n]; 353289bc588SBarry Smith nzi = 0; 3542fb3ed76SBarry Smith im[i] = nnz; 355289bc588SBarry Smith while (nnz--) { 356289bc588SBarry Smith if (fm < i) nzi++; 357dbb450caSBarry Smith *ajtmp++ = fm - shift; 358289bc588SBarry Smith fm = fill[fm]; 359289bc588SBarry Smith } 360289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 361289bc588SBarry Smith } 362464e8d44SSatish Balay if (ai[n] != 0) { 363464e8d44SSatish Balay double af = ((double)ainew[n])/((double)ai[n]); 364c38d4ed2SBarry Smith PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af); 365981c4779SBarry Smith PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af); 366981c4779SBarry Smith PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af); 367981c4779SBarry Smith PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n"); 36805bf559cSSatish Balay } else { 369981c4779SBarry Smith PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n"); 37005bf559cSSatish Balay } 3712fb3ed76SBarry Smith 372898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 373898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3741987afe7SBarry Smith 375606d414cSSatish Balay ierr = PetscFree(fill);CHKERRQ(ierr); 376289bc588SBarry Smith 377289bc588SBarry Smith /* put together the new matrix */ 378b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B);CHKERRQ(ierr); 3791987afe7SBarry Smith PLogObjectParent(*B,isicol); 380416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 381606d414cSSatish Balay ierr = PetscFree(b->imax);CHKERRQ(ierr); 3827c922b88SBarry Smith b->singlemalloc = PETSC_FALSE; 383e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 384606d414cSSatish Balay ierr = PetscFree(b->a);CHKERRQ(ierr); 385606d414cSSatish Balay ierr = PetscFree(b->ilen);CHKERRQ(ierr); 38691bf9adeSBarry Smith b->a = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a); 387416022c9SBarry Smith b->j = ajnew; 388416022c9SBarry Smith b->i = ainew; 389416022c9SBarry Smith b->diag = idnew; 390416022c9SBarry Smith b->ilen = 0; 391416022c9SBarry Smith b->imax = 0; 392416022c9SBarry Smith b->row = isrow; 393416022c9SBarry Smith b->col = iscol; 394c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 395c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 39682bf6240SBarry Smith b->icol = isicol; 39791bf9adeSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 398416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 3997fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 400416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 401416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 4027fd98bd6SLois Curfman McInnes 403e93ccc4dSBarry Smith (*B)->factor = FACTOR_LU;; 404ae068f58SLois Curfman McInnes (*B)->info.factor_mallocs = realloc; 405ae068f58SLois Curfman McInnes (*B)->info.fill_ratio_given = f; 406703deb49SSatish Balay (*B)->ops->lufactornumeric = A->ops->lufactornumeric; /* Use Inode variant if A has inodes */ 407703deb49SSatish Balay 408e93ccc4dSBarry Smith if (ai[n] != 0) { 409e93ccc4dSBarry Smith (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[n]); 410eea2913aSSatish Balay } else { 411eea2913aSSatish Balay (*B)->info.fill_ratio_needed = 0.0; 412eea2913aSSatish Balay } 4133a40ed3dSBarry Smith PetscFunctionReturn(0); 414289bc588SBarry Smith } 4150a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 416184914b5SBarry Smith extern int Mat_AIJ_CheckInode(Mat); 41741c01911SSatish Balay 4185615d1e5SSatish Balay #undef __FUNC__ 4195615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ" 420416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 421289bc588SBarry Smith { 422416022c9SBarry Smith Mat C = *B; 423416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 42482bf6240SBarry Smith IS isrow = b->row, isicol = b->icol; 425416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 4261987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 427f2582111SSatish Balay int *diag_offset = b->diag,diag,k; 42835aab85fSBarry Smith int preserve_row_sums = (int) a->ilu_preserve_row_sums; 4293a40ed3dSBarry Smith register int *pj; 4308ecf7165SBarry Smith Scalar *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0; 43135aab85fSBarry Smith double ssum; 43235aab85fSBarry Smith register Scalar *pv, *rtmps,*u_values; 433289bc588SBarry Smith 4343a40ed3dSBarry Smith PetscFunctionBegin; 43582bf6240SBarry Smith 43678b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 43778b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 4380452661fSBarry Smith rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) );CHKPTRQ(rtmp); 439549d3d68SSatish Balay ierr = PetscMemzero(rtmp,(n+1)*sizeof(Scalar));CHKERRQ(ierr); 4400cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 441289bc588SBarry Smith 4426cf997b0SBarry Smith /* precalculate row sums */ 44335aab85fSBarry Smith if (preserve_row_sums) { 44435aab85fSBarry Smith rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) );CHKPTRQ(rowsums); 44535aab85fSBarry Smith for ( i=0; i<n; i++ ) { 44635aab85fSBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 44735aab85fSBarry Smith v = a->a + a->i[r[i]] + shift; 44835aab85fSBarry Smith sum = 0.0; 44935aab85fSBarry Smith for ( j=0; j<nz; j++ ) sum += v[j]; 45035aab85fSBarry Smith rowsums[i] = sum; 45135aab85fSBarry Smith } 45235aab85fSBarry Smith } 45335aab85fSBarry Smith 454289bc588SBarry Smith for ( i=0; i<n; i++ ) { 455289bc588SBarry Smith nz = ai[i+1] - ai[i]; 456dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 45748e94559SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 458289bc588SBarry Smith 459289bc588SBarry Smith /* load in initial (unfactored row) */ 460416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 461416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 462416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 4630cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 464289bc588SBarry Smith 465dbb450caSBarry Smith row = *ajtmp++ + shift; 466f2582111SSatish Balay while (row < i ) { 4678c37ef55SBarry Smith pc = rtmp + row; 4688c37ef55SBarry Smith if (*pc != 0.0) { 4691987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 4701987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 47135aab85fSBarry Smith multiplier = *pc / *pv++; 4728c37ef55SBarry Smith *pc = multiplier; 473f2582111SSatish Balay nz = ai[row+1] - diag_offset[row] - 1; 474f2582111SSatish Balay for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 475f2582111SSatish Balay PLogFlops(2*nz); 476289bc588SBarry Smith } 477dbb450caSBarry Smith row = *ajtmp++ + shift; 478289bc588SBarry Smith } 479416022c9SBarry Smith /* finished row so stick it into b->a */ 480416022c9SBarry Smith pv = b->a + ai[i] + shift; 481416022c9SBarry Smith pj = b->j + ai[i] + shift; 4828c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 483416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 48435aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 48535aab85fSBarry Smith /* 48635aab85fSBarry Smith Possibly adjust diagonal entry on current row to force 48735aab85fSBarry Smith LU matrix to have same row sum as initial matrix. 48835aab85fSBarry Smith */ 489d708749eSBarry Smith if (pv[diag] == 0.0) { 4906cf997b0SBarry Smith SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot row %d",i); 491d708749eSBarry Smith } 49235aab85fSBarry Smith if (preserve_row_sums) { 49335aab85fSBarry Smith pj = b->j + ai[i] + shift; 49435aab85fSBarry Smith sum = rowsums[i]; 49535aab85fSBarry Smith for ( j=0; j<diag; j++ ) { 49635aab85fSBarry Smith u_values = b->a + diag_offset[pj[j]] + shift; 49735aab85fSBarry Smith nz = ai[pj[j]+1] - diag_offset[pj[j]]; 49835aab85fSBarry Smith inner_sum = 0.0; 49935aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 50035aab85fSBarry Smith inner_sum += u_values[k]; 50135aab85fSBarry Smith } 50235aab85fSBarry Smith sum -= pv[j]*inner_sum; 50335aab85fSBarry Smith 50435aab85fSBarry Smith } 50535aab85fSBarry Smith nz = ai[i+1] - diag_offset[i] - 1; 50635aab85fSBarry Smith u_values = b->a + diag_offset[i] + 1 + shift; 50735aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 50835aab85fSBarry Smith sum -= u_values[k]; 50935aab85fSBarry Smith } 51035aab85fSBarry Smith ssum = PetscAbsScalar(sum/pv[diag]); 51135aab85fSBarry Smith if (ssum < 1000. && ssum > .001) pv[diag] = sum; 51235aab85fSBarry Smith } 51335aab85fSBarry Smith /* check pivot entry for current row */ 5148c37ef55SBarry Smith } 5150f11f9aeSSatish Balay 51635aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 51735aab85fSBarry Smith for ( i=0; i<n; i++ ) { 51835aab85fSBarry Smith b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 51935aab85fSBarry Smith } 52035aab85fSBarry Smith 521606d414cSSatish Balay if (preserve_row_sums) {ierr = PetscFree(rowsums);CHKERRQ(ierr);} 522606d414cSSatish Balay ierr = PetscFree(rtmp);CHKERRQ(ierr); 52378b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 52478b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 525416022c9SBarry Smith C->factor = FACTOR_LU; 52641c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C);CHKERRQ(ierr); 527c456f294SBarry Smith C->assembled = PETSC_TRUE; 528416022c9SBarry Smith PLogFlops(b->n); 5293a40ed3dSBarry Smith PetscFunctionReturn(0); 530289bc588SBarry Smith } 5310a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 5325615d1e5SSatish Balay #undef __FUNC__ 5335615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ" 534416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 535da3a660dSBarry Smith { 536416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 53786bacbd2SBarry Smith int ierr,refct; 538416022c9SBarry Smith Mat C; 539f830108cSBarry Smith PetscOps *Abops; 5400a6ffc59SBarry Smith MatOps Aops; 541416022c9SBarry Smith 5423a40ed3dSBarry Smith PetscFunctionBegin; 543f2582111SSatish Balay ierr = MatLUFactorSymbolic(A,row,col,f,&C);CHKERRQ(ierr); 544f2582111SSatish Balay ierr = MatLUFactorNumeric(A,&C);CHKERRQ(ierr); 545da3a660dSBarry Smith 546da3a660dSBarry Smith /* free all the data structures from mat */ 547606d414cSSatish Balay ierr = PetscFree(mat->a);CHKERRQ(ierr); 548606d414cSSatish Balay if (!mat->singlemalloc) { 549606d414cSSatish Balay ierr = PetscFree(mat->i);CHKERRQ(ierr); 550606d414cSSatish Balay ierr = PetscFree(mat->j);CHKERRQ(ierr); 551606d414cSSatish Balay } 552606d414cSSatish Balay if (mat->diag) {ierr = PetscFree(mat->diag);CHKERRQ(ierr);} 553606d414cSSatish Balay if (mat->ilen) {ierr = PetscFree(mat->ilen);CHKERRQ(ierr);} 554606d414cSSatish Balay if (mat->imax) {ierr = PetscFree(mat->imax);CHKERRQ(ierr);} 555606d414cSSatish Balay if (mat->solve_work) {ierr = PetscFree(mat->solve_work);CHKERRQ(ierr);} 556606d414cSSatish Balay if (mat->inode.size) {ierr = PetscFree(mat->inode.size);CHKERRQ(ierr);} 5570f0aacb9SBarry Smith if (mat->icol) {ierr = ISDestroy(mat->icol);CHKERRQ(ierr);} 558606d414cSSatish Balay ierr = PetscFree(mat);CHKERRQ(ierr); 559da3a660dSBarry Smith 56017642b18SBarry Smith ierr = MapDestroy(A->rmap);CHKERRQ(ierr); 56117642b18SBarry Smith ierr = MapDestroy(A->cmap);CHKERRQ(ierr); 56217642b18SBarry Smith 563f830108cSBarry Smith /* 564f830108cSBarry Smith This is horrible, horrible code. We need to keep the 565f830108cSBarry Smith A pointers for the bops and ops but copy everything 566f830108cSBarry Smith else from C. 567f830108cSBarry Smith */ 568f830108cSBarry Smith Abops = A->bops; 569f830108cSBarry Smith Aops = A->ops; 57086bacbd2SBarry Smith refct = A->refct; 571549d3d68SSatish Balay ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr); 572451c4af8SSatish Balay mat = (Mat_SeqAIJ *) A->data; 573451c4af8SSatish Balay PLogObjectParent(A,mat->icol); 574451c4af8SSatish Balay 575f830108cSBarry Smith A->bops = Abops; 576f830108cSBarry Smith A->ops = Aops; 577bef8e0ddSBarry Smith A->qlist = 0; 57886bacbd2SBarry Smith A->refct = refct; 579c38d4ed2SBarry Smith /* copy over the type_name and name */ 580c38d4ed2SBarry Smith ierr = PetscStrallocpy(C->type_name,&A->type_name);CHKERRQ(ierr); 581c38d4ed2SBarry Smith ierr = PetscStrallocpy(C->name,&A->name);CHKERRQ(ierr); 582f830108cSBarry Smith 5830452661fSBarry Smith PetscHeaderDestroy(C); 584c38d4ed2SBarry Smith 5853a40ed3dSBarry Smith PetscFunctionReturn(0); 586da3a660dSBarry Smith } 5870a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 5885615d1e5SSatish Balay #undef __FUNC__ 5895615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ" 590416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 5918c37ef55SBarry Smith { 592416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 593416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 594416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 5953b2fbd54SBarry Smith int nz,shift = a->indexshift,*rout,*cout; 596416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 5978c37ef55SBarry Smith 5983a40ed3dSBarry Smith PetscFunctionBegin; 5993a40ed3dSBarry Smith if (!n) PetscFunctionReturn(0); 60091bf9adeSBarry Smith 601e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 602e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 603416022c9SBarry Smith tmp = a->solve_work; 6048c37ef55SBarry Smith 6053b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 6063b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 6078c37ef55SBarry Smith 6088c37ef55SBarry Smith /* forward solve the lower triangular */ 6098c37ef55SBarry Smith tmp[0] = b[*r++]; 6100cf60383SBarry Smith tmps = tmp + shift; 6118c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 612dbb450caSBarry Smith v = aa + ai[i] + shift; 613dbb450caSBarry Smith vi = aj + ai[i] + shift; 614416022c9SBarry Smith nz = a->diag[i] - ai[i]; 6158c37ef55SBarry Smith sum = b[*r++]; 6160cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 6178c37ef55SBarry Smith tmp[i] = sum; 6188c37ef55SBarry Smith } 6198c37ef55SBarry Smith 6208c37ef55SBarry Smith /* backward solve the upper triangular */ 6218c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 622416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 623416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 624416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 6258c37ef55SBarry Smith sum = tmp[i]; 6260cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 627416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 6288c37ef55SBarry Smith } 6298c37ef55SBarry Smith 6303b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 6313b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 632cb5b572fSBarry Smith ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 633cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 634416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 6353a40ed3dSBarry Smith PetscFunctionReturn(0); 6368c37ef55SBarry Smith } 637026e39d0SSatish Balay 638930ae53cSBarry Smith /* ----------------------------------------------------------- */ 639930ae53cSBarry Smith #undef __FUNC__ 640930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering" 641930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx) 642930ae53cSBarry Smith { 643930ae53cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 644d85afc42SSatish Balay int n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr; 645d85afc42SSatish Balay Scalar *x,*b, *aa = a->a, sum; 646aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 647d85afc42SSatish Balay int adiag_i,i,*vi,nz,ai_i; 648d85afc42SSatish Balay Scalar *v; 649d85afc42SSatish Balay #endif 650930ae53cSBarry Smith 6513a40ed3dSBarry Smith PetscFunctionBegin; 6523a40ed3dSBarry Smith if (!n) PetscFunctionReturn(0); 653930ae53cSBarry Smith if (a->indexshift) { 6543a40ed3dSBarry Smith ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr); 6553a40ed3dSBarry Smith PetscFunctionReturn(0); 656930ae53cSBarry Smith } 657930ae53cSBarry Smith 658e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 659e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 660930ae53cSBarry Smith 661aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 6621c4feecaSSatish Balay fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 6631c4feecaSSatish Balay #else 664930ae53cSBarry Smith /* forward solve the lower triangular */ 665930ae53cSBarry Smith x[0] = b[0]; 666930ae53cSBarry Smith for ( i=1; i<n; i++ ) { 667930ae53cSBarry Smith ai_i = ai[i]; 668930ae53cSBarry Smith v = aa + ai_i; 669930ae53cSBarry Smith vi = aj + ai_i; 670930ae53cSBarry Smith nz = adiag[i] - ai_i; 671930ae53cSBarry Smith sum = b[i]; 672930ae53cSBarry Smith while (nz--) sum -= *v++ * x[*vi++]; 673930ae53cSBarry Smith x[i] = sum; 674930ae53cSBarry Smith } 675930ae53cSBarry Smith 676930ae53cSBarry Smith /* backward solve the upper triangular */ 677930ae53cSBarry Smith for ( i=n-1; i>=0; i-- ){ 678930ae53cSBarry Smith adiag_i = adiag[i]; 679d708749eSBarry Smith v = aa + adiag_i + 1; 680d708749eSBarry Smith vi = aj + adiag_i + 1; 681930ae53cSBarry Smith nz = ai[i+1] - adiag_i - 1; 682930ae53cSBarry Smith sum = x[i]; 683930ae53cSBarry Smith while (nz--) sum -= *v++ * x[*vi++]; 684930ae53cSBarry Smith x[i] = sum*aa[adiag_i]; 685930ae53cSBarry Smith } 6861c4feecaSSatish Balay #endif 687930ae53cSBarry Smith PLogFlops(2*a->nz - a->n); 688cb5b572fSBarry Smith ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 689cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 6903a40ed3dSBarry Smith PetscFunctionReturn(0); 691930ae53cSBarry Smith } 692930ae53cSBarry Smith 6935615d1e5SSatish Balay #undef __FUNC__ 6945615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ" 695416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 696da3a660dSBarry Smith { 697416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 698416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 699416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 7003b2fbd54SBarry Smith int nz, shift = a->indexshift,*rout,*cout; 701416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 702da3a660dSBarry Smith 7033a40ed3dSBarry Smith PetscFunctionBegin; 70478b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);} 705da3a660dSBarry Smith 706e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 707e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 708416022c9SBarry Smith tmp = a->solve_work; 709da3a660dSBarry Smith 7103b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 7113b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 712da3a660dSBarry Smith 713da3a660dSBarry Smith /* forward solve the lower triangular */ 714da3a660dSBarry Smith tmp[0] = b[*r++]; 715da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 716dbb450caSBarry Smith v = aa + ai[i] + shift; 717dbb450caSBarry Smith vi = aj + ai[i] + shift; 718416022c9SBarry Smith nz = a->diag[i] - ai[i]; 719da3a660dSBarry Smith sum = b[*r++]; 720dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 721da3a660dSBarry Smith tmp[i] = sum; 722da3a660dSBarry Smith } 723da3a660dSBarry Smith 724da3a660dSBarry Smith /* backward solve the upper triangular */ 725da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 726416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 727416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 728416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 729da3a660dSBarry Smith sum = tmp[i]; 730dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 731416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 732da3a660dSBarry Smith x[*c--] += tmp[i]; 733da3a660dSBarry Smith } 734da3a660dSBarry Smith 7353b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 7363b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 737cb5b572fSBarry Smith ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 738cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 739416022c9SBarry Smith PLogFlops(2*a->nz); 740898183ecSLois Curfman McInnes 7413a40ed3dSBarry Smith PetscFunctionReturn(0); 742da3a660dSBarry Smith } 743da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 7445615d1e5SSatish Balay #undef __FUNC__ 7457c922b88SBarry Smith #define __FUNC__ "MatSolveTranspose_SeqAIJ" 7467c922b88SBarry Smith int MatSolveTranspose_SeqAIJ(Mat A,Vec bb, Vec xx) 747da3a660dSBarry Smith { 748416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 7492235e667SBarry Smith IS iscol = a->col, isrow = a->row; 750416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 751f1af5d2fSBarry Smith int nz,shift = a->indexshift,*rout,*cout, *diag = a->diag; 752f1af5d2fSBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v, s1; 753da3a660dSBarry Smith 7543a40ed3dSBarry Smith PetscFunctionBegin; 755e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 756e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 757416022c9SBarry Smith tmp = a->solve_work; 758da3a660dSBarry Smith 7592235e667SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 7602235e667SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 761da3a660dSBarry Smith 762da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 7632235e667SBarry Smith for ( i=0; i<n; i++ ) tmp[i] = b[c[i]]; 764da3a660dSBarry Smith 765da3a660dSBarry Smith /* forward solve the U^T */ 766da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 767f1af5d2fSBarry Smith v = aa + diag[i] + shift; 768f1af5d2fSBarry Smith vi = aj + diag[i] + (!shift); 769f1af5d2fSBarry Smith nz = ai[i+1] - diag[i] - 1; 770c38d4ed2SBarry Smith s1 = tmp[i]; 771c38d4ed2SBarry Smith s1 *= *(v++); /* multiply by inverse of diagonal entry */ 772da3a660dSBarry Smith while (nz--) { 773f1af5d2fSBarry Smith tmp[*vi++ + shift] -= (*v++)*s1; 774da3a660dSBarry Smith } 775c38d4ed2SBarry Smith tmp[i] = s1; 776da3a660dSBarry Smith } 777da3a660dSBarry Smith 778da3a660dSBarry Smith /* backward solve the L^T */ 779da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 780f1af5d2fSBarry Smith v = aa + diag[i] - 1 + shift; 781f1af5d2fSBarry Smith vi = aj + diag[i] - 1 + shift; 782f1af5d2fSBarry Smith nz = diag[i] - ai[i]; 783f1af5d2fSBarry Smith s1 = tmp[i]; 784da3a660dSBarry Smith while (nz--) { 785f1af5d2fSBarry Smith tmp[*vi-- + shift] -= (*v--)*s1; 786da3a660dSBarry Smith } 787da3a660dSBarry Smith } 788da3a660dSBarry Smith 789da3a660dSBarry Smith /* copy tmp into x according to permutation */ 790da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 791da3a660dSBarry Smith 7922235e667SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 7932235e667SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 794cb5b572fSBarry Smith ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 795cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 796da3a660dSBarry Smith 797416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 7983a40ed3dSBarry Smith PetscFunctionReturn(0); 799da3a660dSBarry Smith } 800da3a660dSBarry Smith 8015615d1e5SSatish Balay #undef __FUNC__ 8027c922b88SBarry Smith #define __FUNC__ "MatSolveTransposeAdd_SeqAIJ" 8037c922b88SBarry Smith int MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 804da3a660dSBarry Smith { 805416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 8062235e667SBarry Smith IS iscol = a->col, isrow = a->row; 807416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 808f1af5d2fSBarry Smith int nz,shift = a->indexshift, *rout, *cout, *diag = a->diag; 809416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 8106abc6512SBarry Smith 8113a40ed3dSBarry Smith PetscFunctionBegin; 8126abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 8136abc6512SBarry Smith 814e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 815e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 816416022c9SBarry Smith tmp = a->solve_work; 8176abc6512SBarry Smith 8182235e667SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 8192235e667SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 8206abc6512SBarry Smith 8216abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 8222235e667SBarry Smith for ( i=0; i<n; i++ ) tmp[i] = b[c[i]]; 8236abc6512SBarry Smith 8246abc6512SBarry Smith /* forward solve the U^T */ 8256abc6512SBarry Smith for ( i=0; i<n; i++ ) { 826f1af5d2fSBarry Smith v = aa + diag[i] + shift; 827f1af5d2fSBarry Smith vi = aj + diag[i] + (!shift); 828f1af5d2fSBarry Smith nz = ai[i+1] - diag[i] - 1; 8296abc6512SBarry Smith tmp[i] *= *v++; 8306abc6512SBarry Smith while (nz--) { 831dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 8326abc6512SBarry Smith } 8336abc6512SBarry Smith } 8346abc6512SBarry Smith 8356abc6512SBarry Smith /* backward solve the L^T */ 8366abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 837f1af5d2fSBarry Smith v = aa + diag[i] - 1 + shift; 838f1af5d2fSBarry Smith vi = aj + diag[i] - 1 + shift; 839f1af5d2fSBarry Smith nz = diag[i] - ai[i]; 8406abc6512SBarry Smith while (nz--) { 841dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 8426abc6512SBarry Smith } 8436abc6512SBarry Smith } 8446abc6512SBarry Smith 8456abc6512SBarry Smith /* copy tmp into x according to permutation */ 8466abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 8476abc6512SBarry Smith 8482235e667SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 8492235e667SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 850cb5b572fSBarry Smith ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 851cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 8526abc6512SBarry Smith 853416022c9SBarry Smith PLogFlops(2*a->nz); 8543a40ed3dSBarry Smith PetscFunctionReturn(0); 855da3a660dSBarry Smith } 8569e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 8577c922b88SBarry Smith extern int MatMissingDiagonal_SeqAIJ(Mat); 85808480c60SBarry Smith 8595615d1e5SSatish Balay #undef __FUNC__ 8605615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ" 8616cf997b0SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatILUInfo *info,Mat *fact) 8629e25ed09SBarry Smith { 863416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 8649e25ed09SBarry Smith IS isicol; 865416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 86656beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 867335d9088SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0, dcount = 0; 8686cf997b0SBarry Smith int incrlev,nnz,i,shift = a->indexshift,levels,diagonal_fill; 86977c4ece6SBarry Smith PetscTruth col_identity, row_identity; 8706cf997b0SBarry Smith double f; 8719e25ed09SBarry Smith 8723a40ed3dSBarry Smith PetscFunctionBegin; 8736cf997b0SBarry Smith if (info) { 8746cf997b0SBarry Smith f = info->fill; 8750cd17293SBarry Smith levels = (int) info->levels; 8760cd17293SBarry Smith diagonal_fill = (int) info->diagonal_fill; 8776cf997b0SBarry Smith } else { 8786cf997b0SBarry Smith f = 1.0; 8796cf997b0SBarry Smith levels = 0; 8806cf997b0SBarry Smith diagonal_fill = 0; 8816cf997b0SBarry Smith } 88282bf6240SBarry Smith ierr = ISInvertPermutation(iscol,&isicol);CHKERRQ(ierr); 88382bf6240SBarry Smith 88408480c60SBarry Smith /* special case that simply copies fill pattern */ 885be0abb6dSBarry Smith ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 886be0abb6dSBarry Smith ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 88786bacbd2SBarry Smith if (!levels && row_identity && col_identity) { 8882e8a6d31SBarry Smith ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact);CHKERRQ(ierr); 88908480c60SBarry Smith (*fact)->factor = FACTOR_LU; 89008480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 89108480c60SBarry Smith if (!b->diag) { 8927c922b88SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr); 89308480c60SBarry Smith } 8947c922b88SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(*fact);CHKERRQ(ierr); 89508480c60SBarry Smith b->row = isrow; 89608480c60SBarry Smith b->col = iscol; 89782bf6240SBarry Smith b->icol = isicol; 8980452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 899f830108cSBarry Smith (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 900c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 901c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 9023a40ed3dSBarry Smith PetscFunctionReturn(0); 90308480c60SBarry Smith } 90408480c60SBarry Smith 905898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 906898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 9079e25ed09SBarry Smith 9089e25ed09SBarry Smith /* get new row pointers */ 9090452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) );CHKPTRQ(ainew); 910dbb450caSBarry Smith ainew[0] = -shift; 9119e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 912dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 9130452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajnew); 9149e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 9150452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) );CHKPTRQ(ajfill); 9169e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 9170452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(fill); 91856beaf04SBarry Smith /* im is level for each filled value */ 9190452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(im); 92056beaf04SBarry Smith /* dloc is location of diagonal in factor */ 9210452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int));CHKPTRQ(dloc); 92256beaf04SBarry Smith dloc[0] = 0; 92356beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 9246cf997b0SBarry Smith 9256cf997b0SBarry Smith /* copy current row into linked list */ 92656beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 927385f7a74SSatish Balay if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix"); 928dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 9299e25ed09SBarry Smith fill[n] = n; 930435faa5fSBarry Smith fill[prow] = -1; /* marker to indicate if diagonal exists */ 9319e25ed09SBarry Smith while (nz--) { 9329e25ed09SBarry Smith fm = n; 933dbb450caSBarry Smith idx = ic[*xi++ + shift]; 9349e25ed09SBarry Smith do { 9359e25ed09SBarry Smith m = fm; 9369e25ed09SBarry Smith fm = fill[m]; 9379e25ed09SBarry Smith } while (fm < idx); 9389e25ed09SBarry Smith fill[m] = idx; 9399e25ed09SBarry Smith fill[idx] = fm; 94056beaf04SBarry Smith im[idx] = 0; 9419e25ed09SBarry Smith } 9426cf997b0SBarry Smith 9436cf997b0SBarry Smith /* make sure diagonal entry is included */ 944435faa5fSBarry Smith if (diagonal_fill && fill[prow] == -1) { 9456cf997b0SBarry Smith fm = n; 946435faa5fSBarry Smith while (fill[fm] < prow) fm = fill[fm]; 947435faa5fSBarry Smith fill[prow] = fill[fm]; /* insert diagonal into linked list */ 948435faa5fSBarry Smith fill[fm] = prow; 9496cf997b0SBarry Smith im[prow] = 0; 9506cf997b0SBarry Smith nzf++; 951335d9088SBarry Smith dcount++; 9526cf997b0SBarry Smith } 9536cf997b0SBarry Smith 95456beaf04SBarry Smith nzi = 0; 9559e25ed09SBarry Smith row = fill[n]; 95656beaf04SBarry Smith while ( row < prow ) { 95756beaf04SBarry Smith incrlev = im[row] + 1; 95856beaf04SBarry Smith nz = dloc[row]; 9596cf997b0SBarry Smith xi = ajnew + ainew[row] + shift + nz + 1; 960dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 961416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 96256beaf04SBarry Smith fm = row; 96356beaf04SBarry Smith while (nnz-- > 0) { 964dbb450caSBarry Smith idx = *xi++ + shift; 96556beaf04SBarry Smith if (*flev + incrlev > levels) { 96656beaf04SBarry Smith flev++; 96756beaf04SBarry Smith continue; 96856beaf04SBarry Smith } 96956beaf04SBarry Smith do { 9709e25ed09SBarry Smith m = fm; 9719e25ed09SBarry Smith fm = fill[m]; 97256beaf04SBarry Smith } while (fm < idx); 9739e25ed09SBarry Smith if (fm != idx) { 97456beaf04SBarry Smith im[idx] = *flev + incrlev; 9759e25ed09SBarry Smith fill[m] = idx; 9769e25ed09SBarry Smith fill[idx] = fm; 9779e25ed09SBarry Smith fm = idx; 97856beaf04SBarry Smith nzf++; 979ecf371e4SBarry Smith } else { 98056beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 9819e25ed09SBarry Smith } 98256beaf04SBarry Smith flev++; 9839e25ed09SBarry Smith } 9849e25ed09SBarry Smith row = fill[row]; 98556beaf04SBarry Smith nzi++; 9869e25ed09SBarry Smith } 9879e25ed09SBarry Smith /* copy new filled row into permanent storage */ 98856beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 989d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 990ecf371e4SBarry Smith 991ecf371e4SBarry Smith /* estimate how much additional space we will need */ 992ecf371e4SBarry Smith /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */ 993ecf371e4SBarry Smith /* just double the memory each time */ 994ecf371e4SBarry Smith /* maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */ 995ecf371e4SBarry Smith int maxadd = jmax; 99656beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 997bbb6d6a8SBarry Smith jmax += maxadd; 998ecf371e4SBarry Smith 999ecf371e4SBarry Smith /* allocate a longer ajnew and ajfill */ 10000452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 1001549d3d68SSatish Balay ierr = PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr); 1002606d414cSSatish Balay ierr = PetscFree(ajnew);CHKERRQ(ierr); 100356beaf04SBarry Smith ajnew = xi; 10040452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 1005549d3d68SSatish Balay ierr = PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));CHKERRQ(ierr); 1006606d414cSSatish Balay ierr = PetscFree(ajfill);CHKERRQ(ierr); 100756beaf04SBarry Smith ajfill = xi; 1008ecf371e4SBarry Smith realloc++; /* count how many times we realloc */ 10099e25ed09SBarry Smith } 1010dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 1011dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 101256beaf04SBarry Smith dloc[prow] = nzi; 10139e25ed09SBarry Smith fm = fill[n]; 101456beaf04SBarry Smith while (nzf--) { 1015dbb450caSBarry Smith *xi++ = fm - shift; 101656beaf04SBarry Smith *flev++ = im[fm]; 10179e25ed09SBarry Smith fm = fill[fm]; 10189e25ed09SBarry Smith } 10196cf997b0SBarry Smith /* make sure row has diagonal entry */ 10206cf997b0SBarry Smith if (ajnew[ainew[prow]+shift+dloc[prow]]+shift != prow) { 10216cf997b0SBarry Smith SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,1,"Row %d has missing diagonal in factored matrix\n\ 10226cf997b0SBarry Smith try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow); 10236cf997b0SBarry Smith } 10249e25ed09SBarry Smith } 1025606d414cSSatish Balay ierr = PetscFree(ajfill); CHKERRQ(ierr); 1026898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1027898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1028606d414cSSatish Balay ierr = PetscFree(fill);CHKERRQ(ierr); 1029606d414cSSatish Balay ierr = PetscFree(im);CHKERRQ(ierr); 10309e25ed09SBarry Smith 1031f64065bbSBarry Smith { 1032464e8d44SSatish Balay double af = ((double)ainew[n])/((double)ai[n]); 1033c38d4ed2SBarry Smith PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af); 1034981c4779SBarry Smith PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af); 1035981c4779SBarry Smith PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af); 1036981c4779SBarry Smith PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n"); 1037335d9088SBarry Smith if (diagonal_fill) { 1038335d9088SBarry Smith PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replace %d missing diagonals",dcount); 1039335d9088SBarry Smith } 1040f64065bbSBarry Smith } 1041bbb6d6a8SBarry Smith 10429e25ed09SBarry Smith /* put together the new matrix */ 1043b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact);CHKERRQ(ierr); 1044fa6007c9SSatish Balay PLogObjectParent(*fact,isicol); 1045416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 1046606d414cSSatish Balay ierr = PetscFree(b->imax);CHKERRQ(ierr); 10477c922b88SBarry Smith b->singlemalloc = PETSC_FALSE; 1048dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 10499e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 1050606d414cSSatish Balay ierr = PetscFree(b->a);CHKERRQ(ierr); 1051606d414cSSatish Balay ierr = PetscFree(b->ilen);CHKERRQ(ierr); 10526b96ef82SSatish Balay b->a = (Scalar *) PetscMalloc( len+1 );CHKPTRQ(b->a); 1053416022c9SBarry Smith b->j = ajnew; 1054416022c9SBarry Smith b->i = ainew; 105556beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 1056416022c9SBarry Smith b->diag = dloc; 1057416022c9SBarry Smith b->ilen = 0; 1058416022c9SBarry Smith b->imax = 0; 1059416022c9SBarry Smith b->row = isrow; 1060416022c9SBarry Smith b->col = iscol; 1061c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1062c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 106382bf6240SBarry Smith b->icol = isicol; 106482bf6240SBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 1065416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 106656beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 1067dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 1068416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 10699e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 1070ae068f58SLois Curfman McInnes 1071ae068f58SLois Curfman McInnes (*fact)->info.factor_mallocs = realloc; 1072ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_given = f; 1073ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]); 1074e93ccc4dSBarry Smith (*fact)->factor = FACTOR_LU;; 1075ae068f58SLois Curfman McInnes 10763a40ed3dSBarry Smith PetscFunctionReturn(0); 10779e25ed09SBarry Smith } 1078d5d45c9bSBarry Smith 1079d5d45c9bSBarry Smith 1080d5d45c9bSBarry Smith 1081d5d45c9bSBarry Smith 1082