1289bc588SBarry Smith 270f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h" 31c4feecaSSatish Balay #include "src/inline/dot.h" 4ed480e8bSBarry Smith #include "src/inline/spops.h" 53b2fbd54SBarry Smith 64a2ae208SSatish Balay #undef __FUNCT__ 74a2ae208SSatish Balay #define __FUNCT__ "MatOrdering_Flow_SeqAIJ" 8dfbe8321SBarry Smith PetscErrorCode MatOrdering_Flow_SeqAIJ(Mat mat,const MatOrderingType type,IS *irow,IS *icol) 93b2fbd54SBarry Smith { 103a40ed3dSBarry Smith PetscFunctionBegin; 113a40ed3dSBarry Smith 1229bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"Code not written"); 13aa482453SBarry Smith #if !defined(PETSC_USE_DEBUG) 14d64ed03dSBarry Smith PetscFunctionReturn(0); 15d64ed03dSBarry Smith #endif 163b2fbd54SBarry Smith } 173b2fbd54SBarry Smith 1886bacbd2SBarry Smith 19dfbe8321SBarry Smith EXTERN PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat); 20dfbe8321SBarry Smith EXTERN PetscErrorCode Mat_AIJ_CheckInode(Mat,PetscTruth); 2186bacbd2SBarry Smith 22dfbe8321SBarry Smith EXTERN PetscErrorCode SPARSEKIT2dperm(int*,PetscScalar*,int*,int*,PetscScalar*,int*,int*,int*,int*,int*); 23dfbe8321SBarry Smith EXTERN PetscErrorCode SPARSEKIT2ilutp(int*,PetscScalar*,int*,int*,int*,PetscReal,PetscReal*,int*,PetscScalar*,int*,int*,int*,PetscScalar*,int*,int*,int*); 24dfbe8321SBarry Smith EXTERN PetscErrorCode SPARSEKIT2msrcsr(int*,PetscScalar*,int*,PetscScalar*,int*,int*,PetscScalar*,int*); 2586bacbd2SBarry Smith 264a2ae208SSatish Balay #undef __FUNCT__ 274a2ae208SSatish Balay #define __FUNCT__ "MatILUDTFactor_SeqAIJ" 2886bacbd2SBarry Smith /* ------------------------------------------------------------ 2986bacbd2SBarry Smith 3086bacbd2SBarry Smith This interface was contribed by Tony Caola 3186bacbd2SBarry Smith 3286bacbd2SBarry Smith This routine is an interface to the pivoting drop-tolerance 335bd2ddc7SBarry Smith ILU routine written by Yousef Saad (saad@cs.umn.edu) as part of 345bd2ddc7SBarry Smith SPARSEKIT2. 355bd2ddc7SBarry Smith 365bd2ddc7SBarry Smith The SPARSEKIT2 routines used here are covered by the GNU 375bd2ddc7SBarry Smith copyright; see the file gnu in this directory. 3886bacbd2SBarry Smith 3986bacbd2SBarry Smith Thanks to Prof. Saad, Dr. Hysom, and Dr. Smith for their 4086bacbd2SBarry Smith help in getting this routine ironed out. 4186bacbd2SBarry Smith 425bd2ddc7SBarry Smith The major drawback to this routine is that if info->fill is 435bd2ddc7SBarry Smith not large enough it fails rather than allocating more space; 445bd2ddc7SBarry Smith this can be fixed by hacking/improving the f2c version of 455bd2ddc7SBarry Smith Yousef Saad's code. 4686bacbd2SBarry Smith 4786bacbd2SBarry Smith ------------------------------------------------------------ 4886bacbd2SBarry Smith */ 49dfbe8321SBarry Smith PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,MatFactorInfo *info,IS isrow,IS iscol,Mat *fact) 5086bacbd2SBarry Smith { 5160ec86bdSBarry Smith #if defined(PETSC_AVOID_GNUCOPYRIGHT_CODE) 5260ec86bdSBarry Smith PetscFunctionBegin; 5360ec86bdSBarry Smith SETERRQ(1,"This distribution does not include GNU Copyright code\n\ 5460ec86bdSBarry Smith You can obtain the drop tolerance routines by installing PETSc from\n\ 5560ec86bdSBarry Smith www.mcs.anl.gov/petsc\n"); 5660ec86bdSBarry Smith #else 5786bacbd2SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 5807d2056aSBarry Smith IS iscolf,isicol,isirow; 5986bacbd2SBarry Smith PetscTruth reorder; 60*6849ba73SBarry Smith PetscErrorCode ierr; 61*6849ba73SBarry Smith int *c,*r,*ic,i,n = A->m,sierr; 62329f5518SBarry Smith int *old_i = a->i,*old_j = a->j,*new_i,*old_i2 = 0,*old_j2 = 0,*new_j; 63313ae024SBarry Smith int *ordcol,*iwk,*iperm,*jw; 64b19713cbSBarry Smith int jmax,lfill,job,*o_i,*o_j; 6587828ca2SBarry Smith PetscScalar *old_a = a->a,*w,*new_a,*old_a2 = 0,*wk,*o_a; 66329f5518SBarry Smith PetscReal permtol,af; 6786bacbd2SBarry Smith 6886bacbd2SBarry Smith PetscFunctionBegin; 6986bacbd2SBarry Smith 7086bacbd2SBarry Smith if (info->dt == PETSC_DEFAULT) info->dt = .005; 7186bacbd2SBarry Smith if (info->dtcount == PETSC_DEFAULT) info->dtcount = (int)(1.5*a->rmax); 7286bacbd2SBarry Smith if (info->dtcol == PETSC_DEFAULT) info->dtcol = .01; 7333259db3SBarry Smith if (info->fill == PETSC_DEFAULT) info->fill = ((double)(n*(info->dtcount+1)))/a->nz; 746faa4630SBarry Smith lfill = (int)(info->dtcount/2.0); 756faa4630SBarry Smith jmax = (int)(info->fill*a->nz); 7686bacbd2SBarry Smith permtol = info->dtcol; 7786bacbd2SBarry Smith 7886bacbd2SBarry Smith 7986bacbd2SBarry Smith /* ------------------------------------------------------------ 8086bacbd2SBarry Smith If reorder=.TRUE., then the original matrix has to be 8186bacbd2SBarry Smith reordered to reflect the user selected ordering scheme, and 8286bacbd2SBarry Smith then de-reordered so it is in it's original format. 8386bacbd2SBarry Smith Because Saad's dperm() is NOT in place, we have to copy 8486bacbd2SBarry Smith the original matrix and allocate more storage. . . 8586bacbd2SBarry Smith ------------------------------------------------------------ 8686bacbd2SBarry Smith */ 8786bacbd2SBarry Smith 8886bacbd2SBarry Smith /* set reorder to true if either isrow or iscol is not identity */ 8986bacbd2SBarry Smith ierr = ISIdentity(isrow,&reorder);CHKERRQ(ierr); 9086bacbd2SBarry Smith if (reorder) {ierr = ISIdentity(iscol,&reorder);CHKERRQ(ierr);} 9186bacbd2SBarry Smith reorder = PetscNot(reorder); 9286bacbd2SBarry Smith 9386bacbd2SBarry Smith 9486bacbd2SBarry Smith /* storage for ilu factor */ 95b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&new_i);CHKERRQ(ierr); 96b0a32e0cSBarry Smith ierr = PetscMalloc(jmax*sizeof(int),&new_j);CHKERRQ(ierr); 9787828ca2SBarry Smith ierr = PetscMalloc(jmax*sizeof(PetscScalar),&new_a);CHKERRQ(ierr); 98b0a32e0cSBarry Smith ierr = PetscMalloc(n*sizeof(int),&ordcol);CHKERRQ(ierr); 9986bacbd2SBarry Smith 10086bacbd2SBarry Smith /* ------------------------------------------------------------ 10186bacbd2SBarry Smith Make sure that everything is Fortran formatted (1-Based) 10286bacbd2SBarry Smith ------------------------------------------------------------ 10386bacbd2SBarry Smith */ 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 }; 110010a20caSHong Zhang 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 } 119b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&old_i2);CHKERRQ(ierr); 120b0a32e0cSBarry Smith ierr = PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(int),&old_j2);CHKERRQ(ierr); 12187828ca2SBarry Smith ierr = PetscMalloc((old_i[n]-old_i[0]+1)*sizeof(PetscScalar),&old_a2);CHKERRQ(ierr); 1225bd2ddc7SBarry Smith job = 3; SPARSEKIT2dperm(&n,old_a,old_j,old_i,old_a2,old_j2,old_i2,r,c,&job); 123c0c2c81eSBarry Smith for (i=0;i<n;i++) { 124c0c2c81eSBarry Smith r[i] = r[i]-1; 125c0c2c81eSBarry Smith c[i] = c[i]-1; 126c0c2c81eSBarry Smith } 127c0c2c81eSBarry Smith ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr); 128c0c2c81eSBarry Smith ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 129b19713cbSBarry Smith o_a = old_a2; 130b19713cbSBarry Smith o_j = old_j2; 131b19713cbSBarry Smith o_i = old_i2; 132b19713cbSBarry Smith } else { 133b19713cbSBarry Smith o_a = old_a; 134b19713cbSBarry Smith o_j = old_j; 135b19713cbSBarry Smith o_i = old_i; 13686bacbd2SBarry Smith } 13786bacbd2SBarry Smith 13886bacbd2SBarry Smith /* ------------------------------------------------------------ 13986bacbd2SBarry Smith Call Saad's ilutp() routine to generate the factorization 14086bacbd2SBarry Smith ------------------------------------------------------------ 14186bacbd2SBarry Smith */ 14286bacbd2SBarry Smith 143b0a32e0cSBarry Smith ierr = PetscMalloc(2*n*sizeof(int),&iperm);CHKERRQ(ierr); 144b0a32e0cSBarry Smith ierr = PetscMalloc(2*n*sizeof(int),&jw);CHKERRQ(ierr); 14587828ca2SBarry Smith ierr = PetscMalloc(n*sizeof(PetscScalar),&w);CHKERRQ(ierr); 14686bacbd2SBarry Smith 147*6849ba73SBarry Smith SPARSEKIT2ilutp(&n,o_a,o_j,o_i,&lfill,(PetscReal)info->dt,&permtol,&n,new_a,new_j,new_i,&jmax,w,jw,iperm,&sierr); 148*6849ba73SBarry Smith if (sierr) { 149*6849ba73SBarry Smith switch (sierr) { 15029bbc08cSBarry Smith case -3: SETERRQ2(1,"ilutp(), matrix U overflows, need larger info->fill current fill %g space allocated %d",info->fill,jmax); 15129bbc08cSBarry Smith case -2: SETERRQ2(1,"ilutp(), matrix L overflows, need larger info->fill current fill %g space allocated %d",info->fill,jmax); 15229bbc08cSBarry Smith case -5: SETERRQ(1,"ilutp(), zero row encountered"); 15329bbc08cSBarry Smith case -1: SETERRQ(1,"ilutp(), input matrix may be wrong"); 15429bbc08cSBarry Smith case -4: SETERRQ1(1,"ilutp(), illegal info->fill value %d",jmax); 155*6849ba73SBarry Smith default: SETERRQ1(1,"ilutp(), zero pivot detected on row %d",sierr); 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 16887828ca2SBarry Smith ierr = PetscMalloc(n*sizeof(PetscScalar),&wk);CHKERRQ(ierr); 169b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&iwk);CHKERRQ(ierr); 17086bacbd2SBarry Smith 1715bd2ddc7SBarry Smith SPARSEKIT2msrcsr(&n,new_a,new_j,new_a,new_j,new_i,wk,iwk); 17286bacbd2SBarry Smith 17386bacbd2SBarry Smith ierr = PetscFree(iwk);CHKERRQ(ierr); 17486bacbd2SBarry Smith ierr = PetscFree(wk);CHKERRQ(ierr); 17586bacbd2SBarry Smith 17686bacbd2SBarry Smith if (reorder) { 17786bacbd2SBarry Smith ierr = PetscFree(old_a2);CHKERRQ(ierr); 17886bacbd2SBarry Smith ierr = PetscFree(old_j2);CHKERRQ(ierr); 17986bacbd2SBarry Smith ierr = PetscFree(old_i2);CHKERRQ(ierr); 180313ae024SBarry Smith } else { 181b19713cbSBarry Smith /* fix permutation of old_j that the factorization introduced */ 182f170005cSBarry Smith for (i=old_i[0]; i<old_i[n]; i++) { 183b19713cbSBarry Smith old_j[i-1] = iperm[old_j[i-1]-1]; 184b19713cbSBarry Smith } 185b19713cbSBarry Smith } 186b19713cbSBarry Smith 187b801d0f9SBarry Smith /* get rid of the shift to indices starting at 1 */ 18886bacbd2SBarry Smith for (i=0; i<n+1; i++) { 189b19713cbSBarry Smith old_i[i]--; 190b19713cbSBarry Smith } 191b19713cbSBarry Smith for (i=old_i[0];i<old_i[n];i++) { 192b19713cbSBarry Smith old_j[i]--; 193b19713cbSBarry Smith } 19486bacbd2SBarry Smith 195b19713cbSBarry Smith /* Make the factored matrix 0-based */ 19686bacbd2SBarry Smith for (i=0; i<n+1; i++) { 197b19713cbSBarry Smith new_i[i]--; 198b19713cbSBarry Smith } 199b19713cbSBarry Smith for (i=new_i[0];i<new_i[n];i++) { 200b19713cbSBarry Smith new_j[i]--; 201b19713cbSBarry Smith } 20286bacbd2SBarry Smith 20386bacbd2SBarry Smith /*-- due to the pivoting, we need to reorder iscol to correctly --*/ 20486bacbd2SBarry Smith /*-- permute the right-hand-side and solution vectors --*/ 2054c49b128SBarry Smith ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 2064c49b128SBarry Smith ierr = ISInvertPermutation(isrow,PETSC_DECIDE,&isirow);CHKERRQ(ierr); 207c0c2c81eSBarry Smith ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 20886bacbd2SBarry Smith for(i=0; i<n; i++) { 20986bacbd2SBarry Smith ordcol[i] = ic[iperm[i]-1]; 21086bacbd2SBarry Smith }; 21186bacbd2SBarry Smith ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 21207d2056aSBarry Smith ierr = ISDestroy(isicol);CHKERRQ(ierr); 21386bacbd2SBarry Smith 214c0c2c81eSBarry Smith ierr = PetscFree(iperm);CHKERRQ(ierr); 215c0c2c81eSBarry Smith 216329f5518SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,n,ordcol,&iscolf);CHKERRQ(ierr); 21707d2056aSBarry Smith ierr = PetscFree(ordcol);CHKERRQ(ierr); 21886bacbd2SBarry Smith 21986bacbd2SBarry Smith /*----- put together the new matrix -----*/ 22086bacbd2SBarry Smith 221f204ca49SKris Buschelman ierr = MatCreate(A->comm,n,n,n,n,fact);CHKERRQ(ierr); 222f204ca49SKris Buschelman ierr = MatSetType(*fact,A->type_name);CHKERRQ(ierr); 223f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(*fact,0,PETSC_NULL);CHKERRQ(ierr); 22486bacbd2SBarry Smith (*fact)->factor = FACTOR_LU; 22586bacbd2SBarry Smith (*fact)->assembled = PETSC_TRUE; 22686bacbd2SBarry Smith 22786bacbd2SBarry Smith b = (Mat_SeqAIJ*)(*fact)->data; 22886bacbd2SBarry Smith ierr = PetscFree(b->imax);CHKERRQ(ierr); 22986bacbd2SBarry Smith b->sorted = PETSC_FALSE; 23007d2056aSBarry Smith b->singlemalloc = PETSC_FALSE; 231b19713cbSBarry Smith /* the next line frees the default space generated by the MatCreate() */ 23286bacbd2SBarry Smith ierr = PetscFree(b->a);CHKERRQ(ierr); 23386bacbd2SBarry Smith ierr = PetscFree(b->ilen);CHKERRQ(ierr); 23486bacbd2SBarry Smith b->a = new_a; 23586bacbd2SBarry Smith b->j = new_j; 23686bacbd2SBarry Smith b->i = new_i; 23786bacbd2SBarry Smith b->ilen = 0; 23886bacbd2SBarry Smith b->imax = 0; 2391f9e874aSBarry Smith /* I am not sure why these are the inverses of the row and column permutations; but the other way is NO GOOD */ 240313ae024SBarry Smith b->row = isirow; 24186bacbd2SBarry Smith b->col = iscolf; 24287828ca2SBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 24386bacbd2SBarry Smith b->maxnz = b->nz = new_i[n]; 24486bacbd2SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr); 24586bacbd2SBarry Smith (*fact)->info.factor_mallocs = 0; 24686bacbd2SBarry Smith 24786bacbd2SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 24886bacbd2SBarry Smith 24986bacbd2SBarry Smith /* check out for identical nodes. If found, use inode functions */ 2503a7fca6bSBarry Smith ierr = Mat_AIJ_CheckInode(*fact,PETSC_FALSE);CHKERRQ(ierr); 25186bacbd2SBarry Smith 252431e710aSBarry Smith af = ((double)b->nz)/((double)a->nz) + .001; 253b0a32e0cSBarry Smith PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:Fill ratio:given %g needed %g\n",info->fill,af); 254b0a32e0cSBarry Smith PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af); 255b0a32e0cSBarry Smith PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:PCILUSetFill(pc,%g);\n",af); 256b0a32e0cSBarry Smith PetscLogInfo(A,"MatILUDTFactor_SeqAIJ:for best performance.\n"); 257431e710aSBarry Smith 25886bacbd2SBarry Smith PetscFunctionReturn(0); 25960ec86bdSBarry Smith #endif 26086bacbd2SBarry Smith } 26186bacbd2SBarry Smith 262289bc588SBarry Smith /* 263289bc588SBarry Smith Factorization code for AIJ format. 264289bc588SBarry Smith */ 2654a2ae208SSatish Balay #undef __FUNCT__ 266b9617806SBarry Smith #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ" 267dfbe8321SBarry Smith PetscErrorCode MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatFactorInfo *info,Mat *B) 268289bc588SBarry Smith { 269416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 270289bc588SBarry Smith IS isicol; 271*6849ba73SBarry Smith PetscErrorCode ierr; 272*6849ba73SBarry Smith int *r,*ic,i,n = A->m,*ai = a->i,*aj = a->j; 273010a20caSHong Zhang int *ainew,*ajnew,jmax,*fill,*ajtmp,nz; 27491bf9adeSBarry Smith int *idnew,idx,row,m,fm,nnz,nzi,realloc = 0,nzbd,*im; 2759e046878SBarry Smith PetscReal f; 276289bc588SBarry Smith 2773a40ed3dSBarry Smith PetscFunctionBegin; 27829bbc08cSBarry Smith if (A->M != A->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 2794c49b128SBarry Smith ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 280ac121b3dSBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 281ac121b3dSBarry Smith ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 282289bc588SBarry Smith 283289bc588SBarry Smith /* get new row pointers */ 284b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&ainew);CHKERRQ(ierr); 285010a20caSHong Zhang ainew[0] = 0; 286289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 287d3d32019SBarry Smith f = info->fill; 288010a20caSHong Zhang jmax = (int)(f*ai[n]+1); 289b0a32e0cSBarry Smith ierr = PetscMalloc((jmax)*sizeof(int),&ajnew);CHKERRQ(ierr); 290289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 291b0a32e0cSBarry Smith ierr = PetscMalloc((2*n+1)*sizeof(int),&fill);CHKERRQ(ierr); 2922fb3ed76SBarry Smith im = fill + n + 1; 293289bc588SBarry Smith /* idnew is location of diagonal in factor */ 294b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&idnew);CHKERRQ(ierr); 295010a20caSHong Zhang idnew[0] = 0; 296289bc588SBarry Smith 297289bc588SBarry Smith for (i=0; i<n; i++) { 298289bc588SBarry Smith /* first copy previous fill into linked list */ 299289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 30029bbc08cSBarry Smith if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix"); 301010a20caSHong Zhang ajtmp = aj + ai[r[i]]; 302289bc588SBarry Smith fill[n] = n; 303289bc588SBarry Smith while (nz--) { 304289bc588SBarry Smith fm = n; 305010a20caSHong Zhang idx = ic[*ajtmp++]; 306289bc588SBarry Smith do { 307289bc588SBarry Smith m = fm; 308289bc588SBarry Smith fm = fill[m]; 309289bc588SBarry Smith } while (fm < idx); 310289bc588SBarry Smith fill[m] = idx; 311289bc588SBarry Smith fill[idx] = fm; 312289bc588SBarry Smith } 313289bc588SBarry Smith row = fill[n]; 314289bc588SBarry Smith while (row < i) { 315010a20caSHong Zhang ajtmp = ajnew + idnew[row] + 1; 3162fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 3172fb3ed76SBarry Smith nz = im[row] - nzbd; 318289bc588SBarry Smith fm = row; 3192fb3ed76SBarry Smith while (nz-- > 0) { 320010a20caSHong Zhang idx = *ajtmp++ ; 3212fb3ed76SBarry Smith nzbd++; 3222fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 323289bc588SBarry Smith do { 324289bc588SBarry Smith m = fm; 325289bc588SBarry Smith fm = fill[m]; 326289bc588SBarry Smith } while (fm < idx); 327289bc588SBarry Smith if (fm != idx) { 328289bc588SBarry Smith fill[m] = idx; 329289bc588SBarry Smith fill[idx] = fm; 330289bc588SBarry Smith fm = idx; 331289bc588SBarry Smith nnz++; 332289bc588SBarry Smith } 333289bc588SBarry Smith } 334289bc588SBarry Smith row = fill[row]; 335289bc588SBarry Smith } 336289bc588SBarry Smith /* copy new filled row into permanent storage */ 337289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 338496e697eSBarry Smith if (ainew[i+1] > jmax) { 339ecf371e4SBarry Smith 340ecf371e4SBarry Smith /* estimate how much additional space we will need */ 341ecf371e4SBarry Smith /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */ 342ecf371e4SBarry Smith /* just double the memory each time */ 343ecf371e4SBarry Smith int maxadd = jmax; 344ecf371e4SBarry Smith /* maxadd = (int)((f*(ai[n]+(!shift))*(n-i+5))/n); */ 345bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 3462fb3ed76SBarry Smith jmax += maxadd; 347ecf371e4SBarry Smith 348ecf371e4SBarry Smith /* allocate a longer ajnew */ 349b0a32e0cSBarry Smith ierr = PetscMalloc(jmax*sizeof(int),&ajtmp);CHKERRQ(ierr); 350010a20caSHong Zhang ierr = PetscMemcpy(ajtmp,ajnew,(ainew[i])*sizeof(int));CHKERRQ(ierr); 351606d414cSSatish Balay ierr = PetscFree(ajnew);CHKERRQ(ierr); 352289bc588SBarry Smith ajnew = ajtmp; 3532fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 354289bc588SBarry Smith } 355010a20caSHong Zhang ajtmp = ajnew + ainew[i]; 356289bc588SBarry Smith fm = fill[n]; 357289bc588SBarry Smith nzi = 0; 3582fb3ed76SBarry Smith im[i] = nnz; 359289bc588SBarry Smith while (nnz--) { 360289bc588SBarry Smith if (fm < i) nzi++; 361010a20caSHong Zhang *ajtmp++ = fm ; 362289bc588SBarry Smith fm = fill[fm]; 363289bc588SBarry Smith } 364289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 365289bc588SBarry Smith } 366464e8d44SSatish Balay if (ai[n] != 0) { 367329f5518SBarry Smith PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]); 368b0a32e0cSBarry Smith PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af); 369b0a32e0cSBarry Smith PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af); 370b0a32e0cSBarry Smith PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af); 371b0a32e0cSBarry Smith PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n"); 37205bf559cSSatish Balay } else { 373b0a32e0cSBarry Smith PetscLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n"); 37405bf559cSSatish Balay } 3752fb3ed76SBarry Smith 376898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 377898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3781987afe7SBarry Smith 379606d414cSSatish Balay ierr = PetscFree(fill);CHKERRQ(ierr); 380289bc588SBarry Smith 381289bc588SBarry Smith /* put together the new matrix */ 382f204ca49SKris Buschelman ierr = MatCreate(A->comm,n,n,n,n,B);CHKERRQ(ierr); 383f204ca49SKris Buschelman ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr); 384f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(*B,0,PETSC_NULL);CHKERRQ(ierr); 385b0a32e0cSBarry Smith PetscLogObjectParent(*B,isicol); 386416022c9SBarry Smith b = (Mat_SeqAIJ*)(*B)->data; 387606d414cSSatish Balay ierr = PetscFree(b->imax);CHKERRQ(ierr); 3887c922b88SBarry Smith b->singlemalloc = PETSC_FALSE; 389e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 390606d414cSSatish Balay ierr = PetscFree(b->a);CHKERRQ(ierr); 391606d414cSSatish Balay ierr = PetscFree(b->ilen);CHKERRQ(ierr); 392010a20caSHong Zhang ierr = PetscMalloc((ainew[n]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 393416022c9SBarry Smith b->j = ajnew; 394416022c9SBarry Smith b->i = ainew; 395416022c9SBarry Smith b->diag = idnew; 396416022c9SBarry Smith b->ilen = 0; 397416022c9SBarry Smith b->imax = 0; 398416022c9SBarry Smith b->row = isrow; 399416022c9SBarry Smith b->col = iscol; 400085256b3SBarry Smith b->lu_damping = info->damping; 40187828ca2SBarry Smith b->lu_zeropivot = info->zeropivot; 4022cea7109SBarry Smith b->lu_shift = info->shift; 4032cea7109SBarry Smith b->lu_shift_fraction = info->shift_fraction; 404c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 405c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 40682bf6240SBarry Smith b->icol = isicol; 40787828ca2SBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 408416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 4097fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 410010a20caSHong Zhang PetscLogObjectMemory(*B,(ainew[n]-n)*(sizeof(int)+sizeof(PetscScalar))); 411010a20caSHong Zhang b->maxnz = b->nz = ainew[n] ; 4127fd98bd6SLois Curfman McInnes 413329f5518SBarry Smith (*B)->factor = FACTOR_LU; 414ae068f58SLois Curfman McInnes (*B)->info.factor_mallocs = realloc; 415ae068f58SLois Curfman McInnes (*B)->info.fill_ratio_given = f; 4163a7fca6bSBarry Smith ierr = Mat_AIJ_CheckInode(*B,PETSC_FALSE);CHKERRQ(ierr); 4177dda7e10SBarry Smith (*B)->ops->lufactornumeric = A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */ 418703deb49SSatish Balay 419e93ccc4dSBarry Smith if (ai[n] != 0) { 420329f5518SBarry Smith (*B)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[n]); 421eea2913aSSatish Balay } else { 422eea2913aSSatish Balay (*B)->info.fill_ratio_needed = 0.0; 423eea2913aSSatish Balay } 4243a40ed3dSBarry Smith PetscFunctionReturn(0); 425289bc588SBarry Smith } 4260a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 427dfbe8321SBarry Smith EXTERN PetscErrorCode Mat_AIJ_CheckInode(Mat,PetscTruth); 42841c01911SSatish Balay 4294a2ae208SSatish Balay #undef __FUNCT__ 4304a2ae208SSatish Balay #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ" 431dfbe8321SBarry Smith PetscErrorCode MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 432289bc588SBarry Smith { 433416022c9SBarry Smith Mat C = *B; 434416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ *)C->data; 43582bf6240SBarry Smith IS isrow = b->row,isicol = b->icol; 436*6849ba73SBarry Smith PetscErrorCode ierr; 437*6849ba73SBarry Smith int *r,*ic,i,j,n = A->m,*ai = b->i,*aj = b->j; 438010a20caSHong Zhang int *ajtmpold,*ajtmp,nz,row,*ics; 4390cf777b8SBarry Smith int *diag_offset = b->diag,diag,*pj,ndamp = 0, nshift=0; 44087828ca2SBarry Smith PetscScalar *rtmp,*v,*pc,multiplier,*pv,*rtmps; 4410cf777b8SBarry Smith PetscReal damping = b->lu_damping, zeropivot = b->lu_zeropivot,rs,d; 4420cf777b8SBarry Smith PetscReal row_shift,shift_fraction,shift_amount,shift_lo=0., shift_hi=1., shift_top=0.; 4430cf777b8SBarry Smith PetscTruth damp,lushift; 444289bc588SBarry Smith 4453a40ed3dSBarry Smith PetscFunctionBegin; 44678b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 44778b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 44887828ca2SBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&rtmp);CHKERRQ(ierr); 44987828ca2SBarry Smith ierr = PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 450010a20caSHong Zhang rtmps = rtmp; ics = ic; 451289bc588SBarry Smith 4526cc28720Svictorle if (!a->diag) { 4530cf777b8SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 4540cf777b8SBarry Smith } 4550cf777b8SBarry Smith 4560cf777b8SBarry Smith if (b->lu_shift) { /* set max shift */ 4570cf777b8SBarry Smith int *aai = a->i,*ddiag = a->diag; 4580cf777b8SBarry Smith shift_top = 0; 4596cc28720Svictorle for (i=0; i<n; i++) { 460010a20caSHong Zhang d = PetscAbsScalar((a->a)[ddiag[i]]); 4616cc28720Svictorle /* calculate amt of shift needed for this row */ 4626c037d1bSvictorle if (d<=0) { 4633aeef889SHong Zhang row_shift = 0; 4643aeef889SHong Zhang } else { 4653aeef889SHong Zhang row_shift = -2*d; 4663aeef889SHong Zhang } 467010a20caSHong Zhang v = a->a+aai[i]; 4688a2e2d9cSvictorle for (j=0; j<aai[i+1]-aai[i]; j++) 4696cc28720Svictorle row_shift += PetscAbsScalar(v[j]); 4706cc28720Svictorle if (row_shift>shift_top) shift_top = row_shift; 4716cc28720Svictorle } 4726cc28720Svictorle } 4736cc28720Svictorle 4746cc28720Svictorle shift_fraction = 0; shift_amount = 0; 475085256b3SBarry Smith do { 4768a5eb818SBarry Smith damp = PETSC_FALSE; 4776cc28720Svictorle lushift = PETSC_FALSE; 478289bc588SBarry Smith for (i=0; i<n; i++) { 479289bc588SBarry Smith nz = ai[i+1] - ai[i]; 480010a20caSHong Zhang ajtmp = aj + ai[i]; 48148e94559SBarry Smith for (j=0; j<nz; j++) rtmps[ajtmp[j]] = 0.0; 482289bc588SBarry Smith 483289bc588SBarry Smith /* load in initial (unfactored row) */ 484416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 485010a20caSHong Zhang ajtmpold = a->j + a->i[r[i]]; 486010a20caSHong Zhang v = a->a + a->i[r[i]]; 487085256b3SBarry Smith for (j=0; j<nz; j++) { 488085256b3SBarry Smith rtmp[ics[ajtmpold[j]]] = v[j]; 489085256b3SBarry Smith } 490e2dd4fc4Svictorle rtmp[ics[r[i]]] += damping + shift_amount; /* damp the diagonal of the matrix */ 491289bc588SBarry Smith 492010a20caSHong Zhang row = *ajtmp++ ; 493f2582111SSatish Balay while (row < i) { 4948c37ef55SBarry Smith pc = rtmp + row; 4958c37ef55SBarry Smith if (*pc != 0.0) { 496010a20caSHong Zhang pv = b->a + diag_offset[row]; 497010a20caSHong Zhang pj = b->j + diag_offset[row] + 1; 49835aab85fSBarry Smith multiplier = *pc / *pv++; 4998c37ef55SBarry Smith *pc = multiplier; 500f2582111SSatish Balay nz = ai[row+1] - diag_offset[row] - 1; 501f2582111SSatish Balay for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 502b0a32e0cSBarry Smith PetscLogFlops(2*nz); 503289bc588SBarry Smith } 504010a20caSHong Zhang row = *ajtmp++; 505289bc588SBarry Smith } 506416022c9SBarry Smith /* finished row so stick it into b->a */ 507010a20caSHong Zhang pv = b->a + ai[i] ; 508010a20caSHong Zhang pj = b->j + ai[i] ; 5098c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 51035aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 511d3d32019SBarry Smith /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */ 512d3d32019SBarry Smith rs = 0.0; 513d3d32019SBarry Smith for (j=0; j<nz; j++) { 514d3d32019SBarry Smith pv[j] = rtmps[pj[j]]; 515d3d32019SBarry Smith if (j != diag) rs += PetscAbsScalar(pv[j]); 516d3d32019SBarry Smith } 5176cc28720Svictorle #define MAX_NSHIFT 5 518b2da817dSvictorle if (PetscRealPart(pv[diag]) <= zeropivot*rs && b->lu_shift) { 5196cc28720Svictorle if (nshift>MAX_NSHIFT) { 5200cf777b8SBarry Smith SETERRQ(1,"Unable to determine shift to enforce positive definite preconditioner"); 5216cc28720Svictorle } else if (nshift==MAX_NSHIFT) { 5226cc28720Svictorle shift_fraction = shift_hi; 5236c037d1bSvictorle lushift = PETSC_FALSE; 5246cc28720Svictorle } else { 5256cc28720Svictorle shift_lo = shift_fraction; shift_fraction = (shift_hi+shift_lo)/2.; 5266c037d1bSvictorle lushift = PETSC_TRUE; 5276cc28720Svictorle } 5286cc28720Svictorle shift_amount = shift_fraction * shift_top; 5290cf777b8SBarry Smith nshift++; 5300cf777b8SBarry Smith break; 5316cc28720Svictorle } 5329e29f15eSvictorle if (PetscAbsScalar(pv[diag]) <= zeropivot*rs) { 533d3d32019SBarry Smith if (damping) { 5348a5eb818SBarry Smith if (ndamp) damping *= 2.0; 535085256b3SBarry Smith damp = PETSC_TRUE; 536085256b3SBarry Smith ndamp++; 537085256b3SBarry Smith break; 538085256b3SBarry Smith } else { 53963bb2aa6SBarry Smith SETERRQ4(PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot row %d value %g tolerance %g * rs %g",i,PetscAbsScalar(pv[diag]),zeropivot,rs); 540d708749eSBarry Smith } 54135aab85fSBarry Smith } 54235aab85fSBarry Smith } 5436cc28720Svictorle if (!lushift && b->lu_shift && shift_fraction>0 && nshift<MAX_NSHIFT) { 5446cc28720Svictorle /* 5456c037d1bSvictorle * if no shift in this attempt & shifting & started shifting & can refine, 5466cc28720Svictorle * then try lower shift 5476cc28720Svictorle */ 5480cf777b8SBarry Smith shift_hi = shift_fraction; 5490cf777b8SBarry Smith shift_fraction = (shift_hi+shift_lo)/2.; 5506cc28720Svictorle shift_amount = shift_fraction * shift_top; 5510cf777b8SBarry Smith lushift = PETSC_TRUE; 5520cf777b8SBarry Smith nshift++; 5536cc28720Svictorle } 5546cc28720Svictorle } while (damp || lushift); 5550f11f9aeSSatish Balay 55635aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 55735aab85fSBarry Smith for (i=0; i<n; i++) { 558010a20caSHong Zhang b->a[diag_offset[i]] = 1.0/b->a[diag_offset[i]]; 55935aab85fSBarry Smith } 56035aab85fSBarry Smith 561606d414cSSatish Balay ierr = PetscFree(rtmp);CHKERRQ(ierr); 56278b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 56378b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 564416022c9SBarry Smith C->factor = FACTOR_LU; 5657dda7e10SBarry Smith (*B)->ops->lufactornumeric = A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */ 566c456f294SBarry Smith C->assembled = PETSC_TRUE; 567b0a32e0cSBarry Smith PetscLogFlops(C->n); 568d3d32019SBarry Smith if (ndamp) { 569b0a32e0cSBarry Smith PetscLogInfo(0,"MatLUFactorNumerical_SeqAIJ: number of damping tries %d damping value %g\n",ndamp,damping); 570085256b3SBarry Smith } 5716cc28720Svictorle if (nshift) { 5726cc28720Svictorle b->lu_shift_fraction = shift_fraction; 573fb10cecfSBarry Smith PetscLogInfo(0,"MatLUFactorNumerical_SeqAIJ: diagonal shifted up by %e fraction top_value %e number shifts %d\n",shift_fraction,shift_top,nshift); 5746cc28720Svictorle } 5753a40ed3dSBarry Smith PetscFunctionReturn(0); 576289bc588SBarry Smith } 5770889b5dcSvictorle 5780889b5dcSvictorle #undef __FUNCT__ 5790889b5dcSvictorle #define __FUNCT__ "MatUsePETSc_SeqAIJ" 580dfbe8321SBarry Smith PetscErrorCode MatUsePETSc_SeqAIJ(Mat A) 5810889b5dcSvictorle { 5820889b5dcSvictorle PetscFunctionBegin; 5830889b5dcSvictorle A->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJ; 5840889b5dcSvictorle A->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 5850889b5dcSvictorle PetscFunctionReturn(0); 5860889b5dcSvictorle } 5870889b5dcSvictorle 5880889b5dcSvictorle 5890a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 5904a2ae208SSatish Balay #undef __FUNCT__ 5914a2ae208SSatish Balay #define __FUNCT__ "MatLUFactor_SeqAIJ" 592dfbe8321SBarry Smith PetscErrorCode MatLUFactor_SeqAIJ(Mat A,IS row,IS col,MatFactorInfo *info) 593da3a660dSBarry Smith { 594dfbe8321SBarry Smith PetscErrorCode ierr; 595416022c9SBarry Smith Mat C; 596416022c9SBarry Smith 5973a40ed3dSBarry Smith PetscFunctionBegin; 5989e046878SBarry Smith ierr = MatLUFactorSymbolic(A,row,col,info,&C);CHKERRQ(ierr); 599f2582111SSatish Balay ierr = MatLUFactorNumeric(A,&C);CHKERRQ(ierr); 600273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 601440f4c53SSatish Balay PetscLogObjectParent(A,((Mat_SeqAIJ*)(A->data))->icol); 6023a40ed3dSBarry Smith PetscFunctionReturn(0); 603da3a660dSBarry Smith } 6040a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 6054a2ae208SSatish Balay #undef __FUNCT__ 6064a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqAIJ" 607dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx) 6088c37ef55SBarry Smith { 609416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 610416022c9SBarry Smith IS iscol = a->col,isrow = a->row; 611*6849ba73SBarry Smith PetscErrorCode ierr; 612*6849ba73SBarry Smith int *r,*c,i, n = A->m,*vi,*ai = a->i,*aj = a->j; 613010a20caSHong Zhang int nz,*rout,*cout; 61487828ca2SBarry Smith PetscScalar *x,*b,*tmp,*tmps,*aa = a->a,sum,*v; 6158c37ef55SBarry Smith 6163a40ed3dSBarry Smith PetscFunctionBegin; 6173a40ed3dSBarry Smith if (!n) PetscFunctionReturn(0); 61891bf9adeSBarry Smith 6191ebc52fbSHong Zhang ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 6201ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 621416022c9SBarry Smith tmp = a->solve_work; 6228c37ef55SBarry Smith 6233b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 6243b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 6258c37ef55SBarry Smith 6268c37ef55SBarry Smith /* forward solve the lower triangular */ 6278c37ef55SBarry Smith tmp[0] = b[*r++]; 628010a20caSHong Zhang tmps = tmp; 6298c37ef55SBarry Smith for (i=1; i<n; i++) { 630010a20caSHong Zhang v = aa + ai[i] ; 631010a20caSHong Zhang vi = aj + ai[i] ; 63253e7425aSBarry Smith nz = a->diag[i] - ai[i]; 63353e7425aSBarry Smith sum = b[*r++]; 634ed480e8bSBarry Smith SPARSEDENSEMDOT(sum,tmps,v,vi,nz); 6358c37ef55SBarry Smith tmp[i] = sum; 6368c37ef55SBarry Smith } 6378c37ef55SBarry Smith 6388c37ef55SBarry Smith /* backward solve the upper triangular */ 6398c37ef55SBarry Smith for (i=n-1; i>=0; i--){ 640010a20caSHong Zhang v = aa + a->diag[i] + 1; 641010a20caSHong Zhang vi = aj + a->diag[i] + 1; 642416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 6438c37ef55SBarry Smith sum = tmp[i]; 644ed480e8bSBarry Smith SPARSEDENSEMDOT(sum,tmps,v,vi,nz); 645010a20caSHong Zhang x[*c--] = tmp[i] = sum*aa[a->diag[i]]; 6468c37ef55SBarry Smith } 6478c37ef55SBarry Smith 6483b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 6493b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 6501ebc52fbSHong Zhang ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 6511ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 652b0a32e0cSBarry Smith PetscLogFlops(2*a->nz - A->n); 6533a40ed3dSBarry Smith PetscFunctionReturn(0); 6548c37ef55SBarry Smith } 655026e39d0SSatish Balay 656930ae53cSBarry Smith /* ----------------------------------------------------------- */ 6574a2ae208SSatish Balay #undef __FUNCT__ 6584a2ae208SSatish Balay #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering" 659dfbe8321SBarry Smith PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx) 660930ae53cSBarry Smith { 661930ae53cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 662*6849ba73SBarry Smith PetscErrorCode ierr; 663*6849ba73SBarry Smith int n = A->m,*ai = a->i,*aj = a->j,*adiag = a->diag; 664362ced78SSatish Balay PetscScalar *x,*b,*aa = a->a; 665aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 666d85afc42SSatish Balay int adiag_i,i,*vi,nz,ai_i; 667362ced78SSatish Balay PetscScalar *v,sum; 668d85afc42SSatish Balay #endif 669930ae53cSBarry Smith 6703a40ed3dSBarry Smith PetscFunctionBegin; 6713a40ed3dSBarry Smith if (!n) PetscFunctionReturn(0); 672930ae53cSBarry Smith 6731ebc52fbSHong Zhang ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 6741ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 675930ae53cSBarry Smith 676aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 6771c4feecaSSatish Balay fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 6781c4feecaSSatish Balay #else 679930ae53cSBarry Smith /* forward solve the lower triangular */ 680930ae53cSBarry Smith x[0] = b[0]; 681930ae53cSBarry Smith for (i=1; i<n; i++) { 682930ae53cSBarry Smith ai_i = ai[i]; 683930ae53cSBarry Smith v = aa + ai_i; 684930ae53cSBarry Smith vi = aj + ai_i; 685930ae53cSBarry Smith nz = adiag[i] - ai_i; 686930ae53cSBarry Smith sum = b[i]; 687930ae53cSBarry Smith while (nz--) sum -= *v++ * x[*vi++]; 688930ae53cSBarry Smith x[i] = sum; 689930ae53cSBarry Smith } 690930ae53cSBarry Smith 691930ae53cSBarry Smith /* backward solve the upper triangular */ 692930ae53cSBarry Smith for (i=n-1; i>=0; i--){ 693930ae53cSBarry Smith adiag_i = adiag[i]; 694d708749eSBarry Smith v = aa + adiag_i + 1; 695d708749eSBarry Smith vi = aj + adiag_i + 1; 696930ae53cSBarry Smith nz = ai[i+1] - adiag_i - 1; 697930ae53cSBarry Smith sum = x[i]; 698930ae53cSBarry Smith while (nz--) sum -= *v++ * x[*vi++]; 699930ae53cSBarry Smith x[i] = sum*aa[adiag_i]; 700930ae53cSBarry Smith } 7011c4feecaSSatish Balay #endif 702b0a32e0cSBarry Smith PetscLogFlops(2*a->nz - A->n); 7031ebc52fbSHong Zhang ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 7041ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 7053a40ed3dSBarry Smith PetscFunctionReturn(0); 706930ae53cSBarry Smith } 707930ae53cSBarry Smith 7084a2ae208SSatish Balay #undef __FUNCT__ 7094a2ae208SSatish Balay #define __FUNCT__ "MatSolveAdd_SeqAIJ" 710dfbe8321SBarry Smith PetscErrorCode MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx) 711da3a660dSBarry Smith { 712416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 713416022c9SBarry Smith IS iscol = a->col,isrow = a->row; 714*6849ba73SBarry Smith PetscErrorCode ierr; 715*6849ba73SBarry Smith int *r,*c,i, n = A->m,*vi,*ai = a->i,*aj = a->j; 716010a20caSHong Zhang int nz,*rout,*cout; 71787828ca2SBarry Smith PetscScalar *x,*b,*tmp,*aa = a->a,sum,*v; 718da3a660dSBarry Smith 7193a40ed3dSBarry Smith PetscFunctionBegin; 72078b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);} 721da3a660dSBarry Smith 7221ebc52fbSHong Zhang ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 7231ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 724416022c9SBarry Smith tmp = a->solve_work; 725da3a660dSBarry Smith 7263b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 7273b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 728da3a660dSBarry Smith 729da3a660dSBarry Smith /* forward solve the lower triangular */ 730da3a660dSBarry Smith tmp[0] = b[*r++]; 731da3a660dSBarry Smith for (i=1; i<n; i++) { 732010a20caSHong Zhang v = aa + ai[i] ; 733010a20caSHong Zhang vi = aj + ai[i] ; 734416022c9SBarry Smith nz = a->diag[i] - ai[i]; 735da3a660dSBarry Smith sum = b[*r++]; 736010a20caSHong Zhang while (nz--) sum -= *v++ * tmp[*vi++ ]; 737da3a660dSBarry Smith tmp[i] = sum; 738da3a660dSBarry Smith } 739da3a660dSBarry Smith 740da3a660dSBarry Smith /* backward solve the upper triangular */ 741da3a660dSBarry Smith for (i=n-1; i>=0; i--){ 742010a20caSHong Zhang v = aa + a->diag[i] + 1; 743010a20caSHong Zhang vi = aj + a->diag[i] + 1; 744416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 745da3a660dSBarry Smith sum = tmp[i]; 746010a20caSHong Zhang while (nz--) sum -= *v++ * tmp[*vi++ ]; 747010a20caSHong Zhang tmp[i] = sum*aa[a->diag[i]]; 748da3a660dSBarry Smith x[*c--] += tmp[i]; 749da3a660dSBarry Smith } 750da3a660dSBarry Smith 7513b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 7523b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 7531ebc52fbSHong Zhang ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 7541ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 755b0a32e0cSBarry Smith PetscLogFlops(2*a->nz); 756898183ecSLois Curfman McInnes 7573a40ed3dSBarry Smith PetscFunctionReturn(0); 758da3a660dSBarry Smith } 759da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 7604a2ae208SSatish Balay #undef __FUNCT__ 7614a2ae208SSatish Balay #define __FUNCT__ "MatSolveTranspose_SeqAIJ" 762dfbe8321SBarry Smith PetscErrorCode MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx) 763da3a660dSBarry Smith { 764416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 7652235e667SBarry Smith IS iscol = a->col,isrow = a->row; 766*6849ba73SBarry Smith PetscErrorCode ierr; 767*6849ba73SBarry Smith int *r,*c,i,n = A->m,*vi,*ai = a->i,*aj = a->j; 768010a20caSHong Zhang int nz,*rout,*cout,*diag = a->diag; 76987828ca2SBarry Smith PetscScalar *x,*b,*tmp,*aa = a->a,*v,s1; 770da3a660dSBarry Smith 7713a40ed3dSBarry Smith PetscFunctionBegin; 7721ebc52fbSHong Zhang ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 7731ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 774416022c9SBarry Smith tmp = a->solve_work; 775da3a660dSBarry Smith 7762235e667SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 7772235e667SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 778da3a660dSBarry Smith 779da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 7802235e667SBarry Smith for (i=0; i<n; i++) tmp[i] = b[c[i]]; 781da3a660dSBarry Smith 782da3a660dSBarry Smith /* forward solve the U^T */ 783da3a660dSBarry Smith for (i=0; i<n; i++) { 784010a20caSHong Zhang v = aa + diag[i] ; 785010a20caSHong Zhang vi = aj + diag[i] + 1; 786f1af5d2fSBarry Smith nz = ai[i+1] - diag[i] - 1; 787c38d4ed2SBarry Smith s1 = tmp[i]; 788ef66eb69SBarry Smith s1 *= (*v++); /* multiply by inverse of diagonal entry */ 789da3a660dSBarry Smith while (nz--) { 790010a20caSHong Zhang tmp[*vi++ ] -= (*v++)*s1; 791da3a660dSBarry Smith } 792c38d4ed2SBarry Smith tmp[i] = s1; 793da3a660dSBarry Smith } 794da3a660dSBarry Smith 795da3a660dSBarry Smith /* backward solve the L^T */ 796da3a660dSBarry Smith for (i=n-1; i>=0; i--){ 797010a20caSHong Zhang v = aa + diag[i] - 1 ; 798010a20caSHong Zhang vi = aj + diag[i] - 1 ; 799f1af5d2fSBarry Smith nz = diag[i] - ai[i]; 800f1af5d2fSBarry Smith s1 = tmp[i]; 801da3a660dSBarry Smith while (nz--) { 802010a20caSHong Zhang tmp[*vi-- ] -= (*v--)*s1; 803da3a660dSBarry Smith } 804da3a660dSBarry Smith } 805da3a660dSBarry Smith 806da3a660dSBarry Smith /* copy tmp into x according to permutation */ 807da3a660dSBarry Smith for (i=0; i<n; i++) x[r[i]] = tmp[i]; 808da3a660dSBarry Smith 8092235e667SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 8102235e667SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 8111ebc52fbSHong Zhang ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 8121ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 813da3a660dSBarry Smith 814b0a32e0cSBarry Smith PetscLogFlops(2*a->nz-A->n); 8153a40ed3dSBarry Smith PetscFunctionReturn(0); 816da3a660dSBarry Smith } 817da3a660dSBarry Smith 8184a2ae208SSatish Balay #undef __FUNCT__ 8194a2ae208SSatish Balay #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ" 820dfbe8321SBarry Smith PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx) 821da3a660dSBarry Smith { 822416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8232235e667SBarry Smith IS iscol = a->col,isrow = a->row; 824*6849ba73SBarry Smith PetscErrorCode ierr; 825*6849ba73SBarry Smith int *r,*c,i,n = A->m,*vi,*ai = a->i,*aj = a->j; 826010a20caSHong Zhang int nz,*rout,*cout,*diag = a->diag; 82787828ca2SBarry Smith PetscScalar *x,*b,*tmp,*aa = a->a,*v; 8286abc6512SBarry Smith 8293a40ed3dSBarry Smith PetscFunctionBegin; 8306abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 8316abc6512SBarry Smith 8321ebc52fbSHong Zhang ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 8331ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 834416022c9SBarry Smith tmp = a->solve_work; 8356abc6512SBarry Smith 8362235e667SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 8372235e667SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 8386abc6512SBarry Smith 8396abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 8402235e667SBarry Smith for (i=0; i<n; i++) tmp[i] = b[c[i]]; 8416abc6512SBarry Smith 8426abc6512SBarry Smith /* forward solve the U^T */ 8436abc6512SBarry Smith for (i=0; i<n; i++) { 844010a20caSHong Zhang v = aa + diag[i] ; 845010a20caSHong Zhang vi = aj + diag[i] + 1; 846f1af5d2fSBarry Smith nz = ai[i+1] - diag[i] - 1; 8476abc6512SBarry Smith tmp[i] *= *v++; 8486abc6512SBarry Smith while (nz--) { 849010a20caSHong Zhang tmp[*vi++ ] -= (*v++)*tmp[i]; 8506abc6512SBarry Smith } 8516abc6512SBarry Smith } 8526abc6512SBarry Smith 8536abc6512SBarry Smith /* backward solve the L^T */ 8546abc6512SBarry Smith for (i=n-1; i>=0; i--){ 855010a20caSHong Zhang v = aa + diag[i] - 1 ; 856010a20caSHong Zhang vi = aj + diag[i] - 1 ; 857f1af5d2fSBarry Smith nz = diag[i] - ai[i]; 8586abc6512SBarry Smith while (nz--) { 859010a20caSHong Zhang tmp[*vi-- ] -= (*v--)*tmp[i]; 8606abc6512SBarry Smith } 8616abc6512SBarry Smith } 8626abc6512SBarry Smith 8636abc6512SBarry Smith /* copy tmp into x according to permutation */ 8646abc6512SBarry Smith for (i=0; i<n; i++) x[r[i]] += tmp[i]; 8656abc6512SBarry Smith 8662235e667SBarry Smith ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 8672235e667SBarry Smith ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 8681ebc52fbSHong Zhang ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 8691ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 8706abc6512SBarry Smith 871b0a32e0cSBarry Smith PetscLogFlops(2*a->nz); 8723a40ed3dSBarry Smith PetscFunctionReturn(0); 873da3a660dSBarry Smith } 8749e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 875dfbe8321SBarry Smith EXTERN PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat); 87608480c60SBarry Smith 8774a2ae208SSatish Balay #undef __FUNCT__ 8784a2ae208SSatish Balay #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ" 879dfbe8321SBarry Smith PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatFactorInfo *info,Mat *fact) 8809e25ed09SBarry Smith { 881416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 8829e25ed09SBarry Smith IS isicol; 883*6849ba73SBarry Smith PetscErrorCode ierr; 884*6849ba73SBarry Smith int *r,*ic,prow,n = A->m,*ai = a->i,*aj = a->j; 88556beaf04SBarry Smith int *ainew,*ajnew,jmax,*fill,*xi,nz,*im,*ajfill,*flev; 886335d9088SBarry Smith int *dloc,idx,row,m,fm,nzf,nzi,len, realloc = 0,dcount = 0; 887010a20caSHong Zhang int incrlev,nnz,i,levels,diagonal_fill; 88877c4ece6SBarry Smith PetscTruth col_identity,row_identity; 889329f5518SBarry Smith PetscReal f; 8909e25ed09SBarry Smith 8913a40ed3dSBarry Smith PetscFunctionBegin; 8926cf997b0SBarry Smith f = info->fill; 8930cd17293SBarry Smith levels = (int)info->levels; 8940cd17293SBarry Smith diagonal_fill = (int)info->diagonal_fill; 8954c49b128SBarry Smith ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 89682bf6240SBarry Smith 89708480c60SBarry Smith /* special case that simply copies fill pattern */ 898be0abb6dSBarry Smith ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 899be0abb6dSBarry Smith ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 90086bacbd2SBarry Smith if (!levels && row_identity && col_identity) { 9012e8a6d31SBarry Smith ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact);CHKERRQ(ierr); 90208480c60SBarry Smith (*fact)->factor = FACTOR_LU; 90308480c60SBarry Smith b = (Mat_SeqAIJ*)(*fact)->data; 90408480c60SBarry Smith if (!b->diag) { 9057c922b88SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(*fact);CHKERRQ(ierr); 90608480c60SBarry Smith } 9077c922b88SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(*fact);CHKERRQ(ierr); 90808480c60SBarry Smith b->row = isrow; 90908480c60SBarry Smith b->col = iscol; 91082bf6240SBarry Smith b->icol = isicol; 911a2e34c3dSBarry Smith b->lu_damping = info->damping; 91287828ca2SBarry Smith b->lu_zeropivot = info->zeropivot; 9132cea7109SBarry Smith b->lu_shift = info->shift; 9142cea7109SBarry Smith b->lu_shift_fraction= info->shift_fraction; 91587828ca2SBarry Smith ierr = PetscMalloc(((*fact)->m+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 916f830108cSBarry Smith (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 917c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 918c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 9193a40ed3dSBarry Smith PetscFunctionReturn(0); 92008480c60SBarry Smith } 92108480c60SBarry Smith 922898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 923898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 9249e25ed09SBarry Smith 9259e25ed09SBarry Smith /* get new row pointers */ 926b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&ainew);CHKERRQ(ierr); 927010a20caSHong Zhang ainew[0] = 0; 9289e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 929010a20caSHong Zhang jmax = (int)(f*(ai[n]+1)); 930b0a32e0cSBarry Smith ierr = PetscMalloc((jmax)*sizeof(int),&ajnew);CHKERRQ(ierr); 9319e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 932b0a32e0cSBarry Smith ierr = PetscMalloc((jmax)*sizeof(int),&ajfill);CHKERRQ(ierr); 9339e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 934b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&fill);CHKERRQ(ierr); 93556beaf04SBarry Smith /* im is level for each filled value */ 936b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&im);CHKERRQ(ierr); 93756beaf04SBarry Smith /* dloc is location of diagonal in factor */ 938b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&dloc);CHKERRQ(ierr); 93956beaf04SBarry Smith dloc[0] = 0; 94056beaf04SBarry Smith for (prow=0; prow<n; prow++) { 9416cf997b0SBarry Smith 9426cf997b0SBarry Smith /* copy current row into linked list */ 94356beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 94429bbc08cSBarry Smith if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix"); 945010a20caSHong Zhang xi = aj + ai[r[prow]] ; 9469e25ed09SBarry Smith fill[n] = n; 947435faa5fSBarry Smith fill[prow] = -1; /* marker to indicate if diagonal exists */ 9489e25ed09SBarry Smith while (nz--) { 9499e25ed09SBarry Smith fm = n; 950010a20caSHong Zhang idx = ic[*xi++ ]; 9519e25ed09SBarry Smith do { 9529e25ed09SBarry Smith m = fm; 9539e25ed09SBarry Smith fm = fill[m]; 9549e25ed09SBarry Smith } while (fm < idx); 9559e25ed09SBarry Smith fill[m] = idx; 9569e25ed09SBarry Smith fill[idx] = fm; 95756beaf04SBarry Smith im[idx] = 0; 9589e25ed09SBarry Smith } 9596cf997b0SBarry Smith 9606cf997b0SBarry Smith /* make sure diagonal entry is included */ 961435faa5fSBarry Smith if (diagonal_fill && fill[prow] == -1) { 9626cf997b0SBarry Smith fm = n; 963435faa5fSBarry Smith while (fill[fm] < prow) fm = fill[fm]; 964435faa5fSBarry Smith fill[prow] = fill[fm]; /* insert diagonal into linked list */ 965435faa5fSBarry Smith fill[fm] = prow; 9666cf997b0SBarry Smith im[prow] = 0; 9676cf997b0SBarry Smith nzf++; 968335d9088SBarry Smith dcount++; 9696cf997b0SBarry Smith } 9706cf997b0SBarry Smith 97156beaf04SBarry Smith nzi = 0; 9729e25ed09SBarry Smith row = fill[n]; 97356beaf04SBarry Smith while (row < prow) { 97456beaf04SBarry Smith incrlev = im[row] + 1; 97556beaf04SBarry Smith nz = dloc[row]; 976010a20caSHong Zhang xi = ajnew + ainew[row] + nz + 1; 977010a20caSHong Zhang flev = ajfill + ainew[row] + nz + 1; 978416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 97956beaf04SBarry Smith fm = row; 98056beaf04SBarry Smith while (nnz-- > 0) { 981010a20caSHong Zhang idx = *xi++ ; 98256beaf04SBarry Smith if (*flev + incrlev > levels) { 98356beaf04SBarry Smith flev++; 98456beaf04SBarry Smith continue; 98556beaf04SBarry Smith } 98656beaf04SBarry Smith do { 9879e25ed09SBarry Smith m = fm; 9889e25ed09SBarry Smith fm = fill[m]; 98956beaf04SBarry Smith } while (fm < idx); 9909e25ed09SBarry Smith if (fm != idx) { 99156beaf04SBarry Smith im[idx] = *flev + incrlev; 9929e25ed09SBarry Smith fill[m] = idx; 9939e25ed09SBarry Smith fill[idx] = fm; 9949e25ed09SBarry Smith fm = idx; 99556beaf04SBarry Smith nzf++; 996ecf371e4SBarry Smith } else { 99756beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 9989e25ed09SBarry Smith } 99956beaf04SBarry Smith flev++; 10009e25ed09SBarry Smith } 10019e25ed09SBarry Smith row = fill[row]; 100256beaf04SBarry Smith nzi++; 10039e25ed09SBarry Smith } 10049e25ed09SBarry Smith /* copy new filled row into permanent storage */ 100556beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 1006010a20caSHong Zhang if (ainew[prow+1] > jmax) { 1007ecf371e4SBarry Smith 1008ecf371e4SBarry Smith /* estimate how much additional space we will need */ 1009ecf371e4SBarry Smith /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */ 1010ecf371e4SBarry Smith /* just double the memory each time */ 1011ecf371e4SBarry Smith /* maxadd = (int)((f*(ai[n]+!shift)*(n-prow+5))/n); */ 1012ecf371e4SBarry Smith int maxadd = jmax; 101356beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 1014bbb6d6a8SBarry Smith jmax += maxadd; 1015ecf371e4SBarry Smith 1016ecf371e4SBarry Smith /* allocate a longer ajnew and ajfill */ 1017b0a32e0cSBarry Smith ierr = PetscMalloc(jmax*sizeof(int),&xi);CHKERRQ(ierr); 1018010a20caSHong Zhang ierr = PetscMemcpy(xi,ajnew,(ainew[prow])*sizeof(int));CHKERRQ(ierr); 1019606d414cSSatish Balay ierr = PetscFree(ajnew);CHKERRQ(ierr); 102056beaf04SBarry Smith ajnew = xi; 1021b0a32e0cSBarry Smith ierr = PetscMalloc(jmax*sizeof(int),&xi);CHKERRQ(ierr); 1022010a20caSHong Zhang ierr = PetscMemcpy(xi,ajfill,(ainew[prow])*sizeof(int));CHKERRQ(ierr); 1023606d414cSSatish Balay ierr = PetscFree(ajfill);CHKERRQ(ierr); 102456beaf04SBarry Smith ajfill = xi; 1025ecf371e4SBarry Smith realloc++; /* count how many times we realloc */ 10269e25ed09SBarry Smith } 1027010a20caSHong Zhang xi = ajnew + ainew[prow] ; 1028010a20caSHong Zhang flev = ajfill + ainew[prow] ; 102956beaf04SBarry Smith dloc[prow] = nzi; 10309e25ed09SBarry Smith fm = fill[n]; 103156beaf04SBarry Smith while (nzf--) { 1032010a20caSHong Zhang *xi++ = fm ; 103356beaf04SBarry Smith *flev++ = im[fm]; 10349e25ed09SBarry Smith fm = fill[fm]; 10359e25ed09SBarry Smith } 10366cf997b0SBarry Smith /* make sure row has diagonal entry */ 1037010a20caSHong Zhang if (ajnew[ainew[prow]+dloc[prow]] != prow) { 103829bbc08cSBarry Smith SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %d has missing diagonal in factored matrix\n\ 10396cf997b0SBarry Smith try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow); 10406cf997b0SBarry Smith } 10419e25ed09SBarry Smith } 1042606d414cSSatish Balay ierr = PetscFree(ajfill);CHKERRQ(ierr); 1043898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1044898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1045606d414cSSatish Balay ierr = PetscFree(fill);CHKERRQ(ierr); 1046606d414cSSatish Balay ierr = PetscFree(im);CHKERRQ(ierr); 10479e25ed09SBarry Smith 1048f64065bbSBarry Smith { 1049329f5518SBarry Smith PetscReal af = ((PetscReal)ainew[n])/((PetscReal)ai[n]); 1050b0a32e0cSBarry Smith PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af); 1051c0d6ac57SBarry Smith PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -[sub_]pc_ilu_fill %g or use \n",af); 1052c0d6ac57SBarry Smith PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill([sub]pc,%g);\n",af); 1053b0a32e0cSBarry Smith PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n"); 1054335d9088SBarry Smith if (diagonal_fill) { 1055b1bcba4aSBarry Smith PetscLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replaced %d missing diagonals",dcount); 1056335d9088SBarry Smith } 1057f64065bbSBarry Smith } 1058bbb6d6a8SBarry Smith 10599e25ed09SBarry Smith /* put together the new matrix */ 1060f204ca49SKris Buschelman ierr = MatCreate(A->comm,n,n,n,n,fact);CHKERRQ(ierr); 1061f204ca49SKris Buschelman ierr = MatSetType(*fact,A->type_name);CHKERRQ(ierr); 1062f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(*fact,0,PETSC_NULL);CHKERRQ(ierr); 1063b0a32e0cSBarry Smith PetscLogObjectParent(*fact,isicol); 1064416022c9SBarry Smith b = (Mat_SeqAIJ*)(*fact)->data; 1065606d414cSSatish Balay ierr = PetscFree(b->imax);CHKERRQ(ierr); 10667c922b88SBarry Smith b->singlemalloc = PETSC_FALSE; 1067010a20caSHong Zhang len = (ainew[n] )*sizeof(PetscScalar); 10689e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 1069606d414cSSatish Balay ierr = PetscFree(b->a);CHKERRQ(ierr); 1070606d414cSSatish Balay ierr = PetscFree(b->ilen);CHKERRQ(ierr); 1071b0a32e0cSBarry Smith ierr = PetscMalloc(len+1,&b->a);CHKERRQ(ierr); 1072416022c9SBarry Smith b->j = ajnew; 1073416022c9SBarry Smith b->i = ainew; 107456beaf04SBarry Smith for (i=0; i<n; i++) dloc[i] += ainew[i]; 1075416022c9SBarry Smith b->diag = dloc; 1076416022c9SBarry Smith b->ilen = 0; 1077416022c9SBarry Smith b->imax = 0; 1078416022c9SBarry Smith b->row = isrow; 1079416022c9SBarry Smith b->col = iscol; 1080c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1081c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 108282bf6240SBarry Smith b->icol = isicol; 108387828ca2SBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1084416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 108556beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 1086010a20caSHong Zhang PetscLogObjectMemory(*fact,(ainew[n]-n) * (sizeof(int)+sizeof(PetscScalar))); 1087010a20caSHong Zhang b->maxnz = b->nz = ainew[n] ; 1088a2e34c3dSBarry Smith b->lu_damping = info->damping; 10892cea7109SBarry Smith b->lu_shift = info->shift; 10902cea7109SBarry Smith b->lu_shift_fraction = info->shift_fraction; 109187828ca2SBarry Smith b->lu_zeropivot = info->zeropivot; 10929e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 10933a7fca6bSBarry Smith ierr = Mat_AIJ_CheckInode(*fact,PETSC_FALSE);CHKERRQ(ierr); 10943a7fca6bSBarry Smith (*fact)->ops->lufactornumeric = A->ops->lufactornumeric; /* Use Inode variant ONLY if A has inodes */ 1095ae068f58SLois Curfman McInnes 1096ae068f58SLois Curfman McInnes (*fact)->info.factor_mallocs = realloc; 1097ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_given = f; 1098329f5518SBarry Smith (*fact)->info.fill_ratio_needed = ((PetscReal)ainew[n])/((PetscReal)ai[prow]); 10993a40ed3dSBarry Smith PetscFunctionReturn(0); 11009e25ed09SBarry Smith } 1101d5d45c9bSBarry Smith 1102a6175056SHong Zhang #include "src/mat/impls/sbaij/seq/sbaij.h" 1103a6175056SHong Zhang #undef __FUNCT__ 1104a6175056SHong Zhang #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ" 1105dfbe8321SBarry Smith PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat A,Mat *fact) 1106a6175056SHong Zhang { 11070968510aSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1108dfbe8321SBarry Smith PetscErrorCode ierr; 1109d5d45c9bSBarry Smith 1110a6175056SHong Zhang PetscFunctionBegin; 11110968510aSHong Zhang if (!a->sbaijMat){ 11120968510aSHong Zhang ierr = MatConvert(A,MATSEQSBAIJ,&a->sbaijMat);CHKERRQ(ierr); 1113a6175056SHong Zhang } 111403aac1b8SHong Zhang 1115b45a75daSHong Zhang ierr = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering(a->sbaijMat,fact);CHKERRQ(ierr); 11160968510aSHong Zhang ierr = MatDestroy(a->sbaijMat);CHKERRQ(ierr); 1117064503c5SHong Zhang a->sbaijMat = PETSC_NULL; 1118653a6975SHong Zhang 1119a6175056SHong Zhang PetscFunctionReturn(0); 1120a6175056SHong Zhang } 1121a6175056SHong Zhang 1122a6175056SHong Zhang #undef __FUNCT__ 1123a6175056SHong Zhang #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ" 1124dfbe8321SBarry Smith PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat A,IS perm,MatFactorInfo *info,Mat *fact) 1125a6175056SHong Zhang { 11260968510aSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1127dfbe8321SBarry Smith PetscErrorCode ierr; 1128653a6975SHong Zhang PetscTruth perm_identity; 1129a6175056SHong Zhang 1130a6175056SHong Zhang PetscFunctionBegin; 1131653a6975SHong Zhang ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 1132653a6975SHong Zhang if (!perm_identity){ 1133653a6975SHong Zhang SETERRQ(1,"Non-identity permutation is not supported yet"); 1134653a6975SHong Zhang } 11350968510aSHong Zhang if (!a->sbaijMat){ 11360968510aSHong Zhang ierr = MatConvert(A,MATSEQSBAIJ,&a->sbaijMat);CHKERRQ(ierr); 11370968510aSHong Zhang } 1138a6175056SHong Zhang 1139b00f7748SHong Zhang ierr = MatICCFactorSymbolic(a->sbaijMat,perm,info,fact);CHKERRQ(ierr); 1140653a6975SHong Zhang (*fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 1141653a6975SHong Zhang 1142a6175056SHong Zhang PetscFunctionReturn(0); 1143a6175056SHong Zhang } 1144d5d45c9bSBarry Smith 1145f76d2b81SHong Zhang #undef __FUNCT__ 1146f76d2b81SHong Zhang #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqAIJ" 1147dfbe8321SBarry Smith PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat A,IS perm,MatFactorInfo *info,Mat *fact) 1148f76d2b81SHong Zhang { 1149f76d2b81SHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1150dfbe8321SBarry Smith PetscErrorCode ierr; 1151f76d2b81SHong Zhang PetscTruth perm_identity; 1152f76d2b81SHong Zhang 1153f76d2b81SHong Zhang PetscFunctionBegin; 1154f76d2b81SHong Zhang ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 1155f76d2b81SHong Zhang if (!perm_identity){ 1156f76d2b81SHong Zhang SETERRQ(1,"Non-identity permutation is not supported yet"); 1157f76d2b81SHong Zhang } 1158f76d2b81SHong Zhang if (!a->sbaijMat){ 1159f76d2b81SHong Zhang ierr = MatConvert(A,MATSEQSBAIJ,&a->sbaijMat);CHKERRQ(ierr); 1160f76d2b81SHong Zhang } 1161f76d2b81SHong Zhang 1162f76d2b81SHong Zhang ierr = MatCholeskyFactorSymbolic(a->sbaijMat,perm,info,fact);CHKERRQ(ierr); 1163f76d2b81SHong Zhang (*fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 1164f76d2b81SHong Zhang 1165f76d2b81SHong Zhang PetscFunctionReturn(0); 1166f76d2b81SHong Zhang } 1167